Files
TradarrV2/frontend/tests/summary-qa-ticket64.test.tsx

139 lines
9.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { SummaryQA } from '../src/components/SummaryQA'
const { get, ask } = vi.hoisted(() => ({ get: vi.fn(), ask: vi.fn() }))
vi.mock('@/api/summaryQA', () => ({ summaryQAApi: { get, ask } }))
const launcher = 'Ouvrir le chat de cette synthèse'
const attach = 'Ajouter le passage sélectionné à la question'
const conversation = { id: 'c', web_search_supported: true, messages: [] }
const answered = { id: 't', status: 'answered', answer: 'Réponse complète', citations: [], web_search_performed: false }
function select(value = 'Premier passage.') {
const range = document.createRange(); range.selectNodeContents(screen.getByText(value))
window.getSelection()!.removeAllRanges(); window.getSelection()!.addRange(range)
fireEvent(document, new Event('selectionchange'))
}
function view() { return render(<SummaryQA summaryId="s" content={'Premier passage.\n\nSecond passage.'} />) }
async function open() { await userEvent.click(screen.getByRole('button', { name: launcher })); await screen.findByRole('textbox') }
async function send() { await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' })) }
beforeEach(() => {
vi.spyOn(window, 'scrollTo').mockImplementation(() => {})
get.mockResolvedValue(conversation)
ask.mockImplementation(async (_id, body) => ({ ...body, ...answered }))
})
afterEach(() => { cleanup(); window.getSelection()?.removeAllRanges(); vi.restoreAllMocks() })
it('ouvre sans sélection ni envoi automatique et transmet passage vide pour la synthèse entière', async () => {
view()
const button = screen.getByRole('button', { name: launcher })
expect(button).toHaveAttribute('title', launcher)
expect(button).not.toHaveClass('fixed')
expect(get).not.toHaveBeenCalled()
await open()
expect(ask).not.toHaveBeenCalled()
expect(screen.getByRole('textbox')).toHaveValue('')
expect(screen.queryByText('Passage joint')).not.toBeInTheDocument()
await userEvent.type(screen.getByRole('textbox'), 'Question globale ?'); await send()
expect(ask).toHaveBeenCalledWith('s', expect.objectContaining({ passage: '', question: 'Question globale ?', use_web_search: true }))
expect(await screen.findByText('Réponse complète')).toBeInTheDocument()
})
it('attache seulement via +, conserve puis remplace explicitement et retire le passage', async () => {
view(); select(); await open()
expect(screen.queryByText('Passage joint')).not.toBeInTheDocument()
expect(screen.getByRole('button', { name: attach }).querySelector('svg')).not.toBeNull()
await userEvent.click(screen.getByRole('button', { name: attach }))
expect(screen.getByLabelText('Passage joint')).toHaveTextContent('Premier passage.')
expect(screen.getByLabelText('Passage joint')).toHaveClass('line-clamp-3')
await userEvent.type(screen.getByRole('textbox'), 'Brouillon')
await userEvent.click(screen.getByRole('checkbox'))
await userEvent.keyboard('{Escape}'); select('Second passage.'); await open()
expect(screen.getByRole('textbox')).toHaveValue('Brouillon')
expect(screen.getByRole('checkbox')).not.toBeChecked()
expect(screen.getByLabelText('Passage joint')).toHaveTextContent('Premier passage.')
await userEvent.click(screen.getByRole('button', { name: attach }))
expect(screen.getByLabelText('Passage joint')).toHaveTextContent('Second passage.')
await userEvent.click(screen.getByRole('button', { name: 'Retirer le passage joint' }))
expect(screen.queryByLabelText('Passage joint')).not.toBeInTheDocument()
await send()
expect(ask.mock.calls[0][1]).toMatchObject({ passage: '', use_web_search: false })
})
it('isole nouveau summaryId, historique serveur, brouillon et pièce jointe', async () => {
const current = view(); select(); await open()
await userEvent.click(screen.getByRole('button', { name: attach }))
await userEvent.type(screen.getByRole('textbox'), 'Ancien brouillon')
get.mockResolvedValueOnce({ ...conversation, id: 'other', messages: [{ ...answered, passage: '', question: 'Question autre synthèse', request_id: 'other' }] })
current.rerender(<SummaryQA summaryId="other" content="Autre synthèse" />)
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
await open()
expect(get).toHaveBeenLastCalledWith('other')
expect(screen.getByRole('textbox')).toHaveValue('')
expect(screen.queryByLabelText('Passage joint')).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: attach })).not.toBeInTheDocument()
expect(screen.getByText('Question autre synthèse')).toBeInTheDocument()
expect(screen.getByRole('checkbox')).toBeChecked()
})
it('bulles auteur accessibles à droite/gauche et citations sous assistant y compris états non terminés', async () => {
get.mockResolvedValue({ ...conversation, messages: [
{ ...answered, id: 'a', passage: 'Extrait', question: 'Pourquoi', request_id: 'a', web_search_performed: true, citations: [{ title: 'Source', url: 'https://example.test/source' }] },
{ ...answered, id: 'p', question: 'En cours', request_id: 'p', passage: '', status: 'pending' },
{ ...answered, id: 'f', question: 'Erreur', request_id: 'f', passage: '', status: 'failed' },
] })
view(); await open()
for (const user of screen.getAllByRole('article', { name: 'Vous' })) expect(user).toHaveClass('self-end')
const assistants = screen.getAllByRole('article', { name: 'Assistant' })
for (const assistant of assistants) expect(assistant).toHaveClass('self-start')
expect(within(assistants[0]).getByRole('link', { name: 'Source (nouvel onglet)' })).toBeInTheDocument()
expect(within(assistants[1]).getByRole('status')).toHaveTextContent('Réponse en cours')
expect(assistants[2]).toHaveTextContent('La réponse a échoué')
})
it('change UUID pour question et passage, réutilise seulement la demande inchangée après transport', async () => {
ask.mockRejectedValue(new Error('network'))
view(); select(); await open()
await userEvent.type(screen.getByRole('textbox'), 'Question'); await send(); await screen.findByRole('alert')
const first = ask.mock.calls[0][1]
await send(); await screen.findByRole('alert')
expect(ask.mock.calls[1][1]).toEqual(first)
await userEvent.click(screen.getByRole('button', { name: attach })); await send(); await screen.findByRole('alert')
expect(ask.mock.calls[2][1].passage).toBe('Premier passage.')
expect(ask.mock.calls[2][1].request_id).not.toBe(first.request_id)
const attached = ask.mock.calls[2][1]
await userEvent.type(screen.getByRole('textbox'), ' différente'); await send(); await screen.findByRole('alert')
expect(ask.mock.calls[3][1].request_id).not.toBe(attached.request_id)
})
it('efface le brouillon envoyé et sa pièce jointe après succès direct', async () => {
view(); select(); await open(); await userEvent.click(screen.getByRole('button', { name: attach }))
await userEvent.click(screen.getByRole('checkbox'))
await userEvent.type(screen.getByRole('textbox'), 'Question'); await send()
await screen.findByText('Réponse complète')
expect(screen.getByRole('textbox')).toHaveValue('')
expect(screen.queryByLabelText('Passage joint')).not.toBeInTheDocument()
expect(screen.getByRole('checkbox')).toBeChecked()
})
it('le polling ne supprime jamais le nouveau brouillon rédigé après envoi pending', async () => {
ask.mockImplementationOnce(async (_id, body) => ({ ...body, ...answered, status: 'pending' }))
view(); select(); await open(); await userEvent.click(screen.getByRole('button', { name: attach }))
await userEvent.type(screen.getByRole('textbox'), 'Question envoyée'); await send()
await userEvent.clear(screen.getByRole('textbox')); await userEvent.type(screen.getByRole('textbox'), 'Nouveau brouillon')
get.mockResolvedValue({ ...conversation, messages: [{ ...ask.mock.calls[0][1], ...answered }] })
await waitFor(() => expect(screen.getByText('Réponse complète')).toBeInTheDocument(), { timeout: 4500 })
expect(screen.getByRole('textbox')).toHaveValue('Nouveau brouillon')
expect(screen.getByLabelText('Passage joint')).toHaveTextContent('Premier passage.')
})
it('le launcher est portalé dans body sans déplacer le lecteur de synthèse', async () => {
const result = render(<SummaryQA summaryId="s" content="Contenu" />)
expect(screen.getByRole('button', { name: launcher }).parentElement?.parentElement).toBe(document.body)
expect(within(result.container).getByText('Contenu')).toBeInTheDocument()
expect(within(result.container).queryByRole('button', { name: launcher })).not.toBeInTheDocument()
await open()
})
it('valide question vide/plus de 4000 caractères et accepte une question d’un caractère sans passage', async () => {
view(); await open(); await send()
expect(screen.getByRole('alert')).toHaveTextContent('1 à 4 000')
expect(ask).not.toHaveBeenCalled()
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'a'.repeat(4001) } })
await send(); expect(ask).not.toHaveBeenCalled()
fireEvent.change(screen.getByRole('textbox'), { target: { value: '?' } })
await send()
expect(ask.mock.calls[0][1]).toMatchObject({ question: '?', passage: '' })
})