feat(ui): turn synthesis Q&A into persistent chat (#64)

This commit is contained in:
2026-11-26 14:56:13 +01:00
parent 0a19d1c617
commit f75de489cc
9 changed files with 458 additions and 131 deletions

View File

@ -1,6 +1,7 @@
import { act, cleanup, render, screen, waitFor } from '@testing-library/react'
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { summaryQAApi } from '../src/api/summaryQA'
import { Dashboard } from '../src/pages/Dashboard'
const {
@ -113,6 +114,74 @@ describe('Dashboard — génération de synthèses éphémères', () => {
createReport.mockResolvedValue({ id: 'report-1' })
})
it('restaure indépendamment question, passage joint et Web après A → B → A → B', async () => {
const a = { ...previousSummary, id: 'summary-a', content: 'Passage synthèse A.' }
const b = { ...previousSummary, id: 'summary-b', content: 'Passage synthèse B.' }
listSummaries.mockResolvedValue([generatedSummary, a, b])
const getChat = vi.spyOn(summaryQAApi, 'get').mockImplementation(async id => ({
id: `chat-${id}`, summary_id: id, mode: 'stateless', web_search_supported: true,
uses_source_articles: false, provider_session_supported: false, source_expires_at: '', sources_expired: true, source_count: 0,
messages: [{ id: `turn-${id}`, request_id: `request-${id}`, passage: '', question: `Historique ${id}`, answer: `Réponse ${id}`, status: 'answered', created_at: '', replayed: false, use_web_search: false, web_search_performed: false, citations: [] }],
}))
vi.spyOn(window, 'scrollTo').mockImplementation(() => {})
const select = (content: string) => {
const range = document.createRange(); range.selectNodeContents(screen.getByText(content, { exact: true }))
window.getSelection()!.removeAllRanges(); window.getSelection()!.addRange(range)
fireEvent(document, new Event('selectionchange'))
}
const open = async () => {
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
return screen.findByRole('textbox')
}
try {
renderDashboard(); await screen.findByText(generatedSummary.content)
await userEvent.click(screen.getByRole('button', { name: /Passage synthèse A/ }))
select(a.content)
const questionA = await open()
expect(questionA).toHaveValue('')
await userEvent.type(questionA, 'Brouillon A')
await userEvent.click(screen.getByRole('button', { name: 'Ajouter le passage sélectionné à la question' }))
await userEvent.click(screen.getByRole('checkbox'))
await userEvent.keyboard('{Escape}')
await userEvent.click(screen.getByRole('button', { name: /Passage synthèse B/ }))
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
const questionB = await open()
expect(questionB).toHaveValue('')
expect(screen.queryByLabelText('Passage joint')).not.toBeInTheDocument()
expect(screen.getByRole('checkbox')).toBeChecked()
expect(screen.getByText('Historique summary-b')).toBeInTheDocument()
expect(screen.queryByText('Historique summary-a')).not.toBeInTheDocument()
await userEvent.type(questionB, 'Brouillon B')
await userEvent.keyboard('{Escape}')
select(b.content); await open()
await userEvent.click(screen.getByRole('button', { name: 'Ajouter le passage sélectionné à la question' }))
await userEvent.keyboard('{Escape}')
await userEvent.click(screen.getByRole('button', { name: /Passage synthèse A/ }))
expect(await open()).toHaveValue('Brouillon A')
expect(screen.getByLabelText('Passage joint')).toHaveTextContent(a.content)
expect(screen.getByRole('checkbox')).not.toBeChecked()
expect(screen.getByText('Historique summary-a')).toBeInTheDocument()
expect(screen.queryByText('Historique summary-b')).not.toBeInTheDocument()
await userEvent.keyboard('{Escape}')
await userEvent.click(screen.getByRole('button', { name: /Passage synthèse B/ }))
expect(await open()).toHaveValue('Brouillon B')
expect(screen.getByLabelText('Passage joint')).toHaveTextContent(b.content)
expect(screen.getByRole('checkbox')).toBeChecked()
expect(getChat.mock.calls.map(([id]) => id)).toEqual(['summary-a', 'summary-b', 'summary-a', 'summary-b'])
} finally { window.getSelection()?.removeAllRanges(); vi.restoreAllMocks() }
})
it('place le launcher chat dans le header directement au-dessus de Générer', async () => {
renderDashboard()
const launcher = await screen.findByRole('button', { name: 'Ouvrir le chat de cette synthèse' })
const generate = screen.getByRole('button', { name: 'Générer une synthèse' })
expect(launcher.parentElement?.nextElementSibling).toBe(generate)
expect(launcher.parentElement?.parentElement).toBe(generate.parentElement)
expect(generate.parentElement).toHaveClass('flex-col')
expect(launcher).not.toHaveClass('fixed')
expect(launcher.querySelector('svg')).toBeInTheDocument()
})
it.each(['ranking_model_unavailable', 'ranking_model_incompatible'])('guide vers les préférences pour %s', async (failure_code) => {
generationStatus.mockResolvedValueOnce({ id: 'failed', status: 'failed', failure_code })
renderDashboard()
@ -163,7 +232,8 @@ describe('Dashboard — génération de synthèses éphémères', () => {
expect(screen.getByText('2 articles parmi les plus anciens ont été retirés pour respecter le plafond du prompt.')).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: /Ancienne synthèse/ }))
expect(screen.getAllByText('2 articles parmi les plus anciens ont été retirés pour respecter le plafond du prompt.')).toHaveLength(2)
if (count) expect(screen.getAllByText(expected)).toHaveLength(1)
// The newest summary is now selectable in history as well as shown in the latest-result notice.
if (count) expect(screen.getAllByText(expected)).toHaveLength(2)
})
it('affiche le compteur du résultat récent lié à la synthèse sans historique disponible', async () => {

View File

@ -1,11 +1,11 @@
import { afterEach, expect, it, vi } from 'vitest'
import { summaryQAApi } from '../src/api/summaryQA'
afterEach(() => { vi.unstubAllGlobals(); localStorage.clear() })
it('utilise les nouvelles routes owner-scoped et le DTO Question exact de #53', async () => {
it.each(['', 'Passage'])('transmet passage %s et le DTO Web sans altération via les routes owner-scoped', async passage => {
localStorage.setItem('token', 'user-token')
const conversation = { id: 'c', summary_id: 's', mode: 'stateless', uses_source_articles: false, provider_session_supported: false, source_expires_at: '2026-12-01T00:00:00Z', sources_expired: false, source_count: 2, messages: [] }
const question = { request_id: '00000000-0000-0000-0000-000000005401', passage: 'Passage', question: 'Pourquoi ?' }
const turn = { ...question, id: 't', answer: 'Réponse', status: 'answered', created_at: '2026-11-25T00:00:00Z', replayed: false }
const conversation = { id: 'c', summary_id: 's', web_search_supported: true, mode: 'stateless', uses_source_articles: false, provider_session_supported: false, source_expires_at: '2026-12-01T00:00:00Z', sources_expired: false, source_count: 2, messages: [] }
const question = { request_id: '00000000-0000-0000-0000-000000005401', passage, question: 'Pourquoi ?', use_web_search: true }
const turn = { ...question, id: 't', web_search_performed: true, citations: [{ title: 'Source', url: 'https://example.test/source' }], answer: 'Réponse', status: 'answered', created_at: '2026-11-25T00:00:00Z', replayed: false }
const fetchMock = vi.fn().mockResolvedValueOnce(new Response(JSON.stringify({ data: conversation }))).mockResolvedValueOnce(new Response(JSON.stringify({ data: turn })))
vi.stubGlobal('fetch', fetchMock)
expect(await summaryQAApi.get('s')).toEqual(conversation)

View File

@ -22,11 +22,12 @@ it('préserve les événements natifs et le focus ; seule une action explicite o
selectPassage()
expect(get).not.toHaveBeenCalled()
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
const question = await screen.findByRole('textbox', { name: 'Votre question' })
expect(screen.getByRole('heading', { name: 'Questions sur cette synthèse' })).toHaveFocus()
expect(screen.queryAllByText(/Les sources ont expiré|Expiration des sources|sans articles sources ni session fournisseur réutilisée|Sélectionnez un passage normalement|Posez une question sur le passage sélectionné/)).toHaveLength(0)
expect(screen.queryByRole('button', { name: /Actualiser|Développer|Réduire/ })).not.toBeInTheDocument()
await userEvent.click(screen.getByRole('button', { name: 'Ajouter le passage sélectionné à la question' }))
await userEvent.type(question, 'Pourquoi ?')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
expect(await screen.findByText('Une réponse.')).toBeInTheDocument()
@ -36,19 +37,19 @@ it('reprend la conversation et efface les données temporaires au changement de
get.mockResolvedValueOnce({ ...conversation, messages: [{ id: 'old', passage: 'Ancien extrait', question: 'Ancienne question', answer: 'Ancienne réponse', status: 'answered' }] })
const view = render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
selectPassage()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
expect(await screen.findByText('Ancienne réponse')).toBeInTheDocument()
await userEvent.type(screen.getByRole('textbox'), 'Brouillon')
view.rerender(<SummaryQA summaryId="other" content="Autre synthèse" />)
expect(screen.queryByText('Ancienne réponse')).not.toBeInTheDocument()
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Interroger ce passage' })).not.toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' })).toBeInTheDocument()
})
it('explique le modèle indisponible en conservant la question', async () => {
ask.mockRejectedValueOnce(new ApiError('qa_model_unavailable', 409))
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
selectPassage()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
await userEvent.type(await screen.findByRole('textbox'), 'Pourquoi ?')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
expect(await screen.findByRole('alert')).toHaveTextContent(/modèle.*indisponible/)
@ -58,7 +59,7 @@ it('réutilise le request_id après une erreur réseau', async () => {
ask.mockRejectedValueOnce(new Error('network'))
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
selectPassage()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
await userEvent.type(await screen.findByRole('textbox'), 'Pourquoi ?')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
await screen.findByRole('alert')
@ -71,7 +72,7 @@ it('bloque un second envoi pendant la réponse et annonce sa réception', async
ask.mockImplementationOnce(() => new Promise(resolve => { finish = resolve }))
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
selectPassage()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
await userEvent.type(await screen.findByRole('textbox'), 'Pourquoi ?')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
expect(screen.getByRole('button', { name: 'Réponse en cours…' })).toBeDisabled()
@ -84,12 +85,12 @@ it('reprend un chargement échoué sans afficher de détails techniques', async
get.mockRejectedValueOnce(new ApiError('PRIVATE_PROVIDER_SECRET', 503))
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
selectPassage()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
expect(await screen.findByRole('alert')).not.toHaveTextContent('PRIVATE_PROVIDER_SECRET')
await waitFor(() => expect(get).toHaveBeenCalledTimes(2), { timeout: 4500 })
expect(await screen.findByRole('textbox', { name: 'Votre question' })).toBeInTheDocument()
})
it('ne propose pas l’action pour un passage hors limite', () => {
it('permet le chat sans joindre un passage hors limite', async () => {
const content = 'a'.repeat(8001)
render(<SummaryQA summaryId="s" content={content} />)
const range = document.createRange()
@ -97,14 +98,16 @@ it('ne propose pas l’action pour un passage hors limite', () => {
window.getSelection()!.removeAllRanges()
window.getSelection()!.addRange(range)
fireEvent(document, new Event('selectionchange'))
expect(screen.queryByRole('button', { name: 'Interroger ce passage' })).not.toBeInTheDocument()
expect(get).not.toHaveBeenCalled()
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
await screen.findByRole('textbox')
expect(screen.queryByRole('button', { name: 'Ajouter le passage sélectionné à la question' })).not.toBeInTheDocument()
})
it.each([45, undefined])('explique le quota Q&A avec délai %s et conserve la question', async seconds => {
ask.mockRejectedValueOnce(Object.assign(new ApiError('qa_rate_limited', 429), { retryAfterSeconds: seconds }))
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
selectPassage()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
await userEvent.type(await screen.findByRole('textbox'), 'Pourquoi ?')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
const alert = await screen.findByRole('alert')
@ -112,13 +115,11 @@ it.each([45, undefined])('explique le quota Q&A avec délai %s et conserve la qu
expect(alert).toHaveTextContent(seconds === undefined ? 'Réessayez dans quelques instants.' : 'Réessayez dans 45 secondes.')
expect(screen.getByRole('textbox')).toHaveValue('Pourquoi ?')
})
it('affiche seulement le bouton fixe après sélection valide et la mémorise au scroll/collapse', () => {
it('garde le launcher dans le flux et mémorise une sélection au scroll/collapse sans ouvrir', () => {
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
expect(screen.queryByRole('button', { name: 'Interroger ce passage' })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Questions sur cette synthèse' })).not.toBeInTheDocument()
const action = screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' })
expect(action).not.toHaveClass('fixed')
selectPassage()
const action = screen.getByRole('button', { name: 'Interroger ce passage' })
expect(action).toHaveClass('fixed')
window.getSelection()?.removeAllRanges()
fireEvent(document, new Event('selectionchange'))
fireEvent.scroll(window)
@ -128,7 +129,7 @@ it('affiche seulement le bouton fixe après sélection valide et la mémorise au
it('ouvre une modale accessible, conserve le brouillon et restaure le focus après Échap', async () => {
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
selectPassage()
const action = screen.getByRole('button', { name: 'Interroger ce passage' })
const action = screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' })
await userEvent.click(action)
expect(await screen.findByRole('dialog', { name: 'Questions sur cette synthèse' })).toBeInTheDocument()
await userEvent.type(await screen.findByRole('textbox'), 'Brouillon conservé')
@ -149,8 +150,9 @@ it('croppe visuellement le passage mais transmet son intégralité', async () =>
window.getSelection()!.addRange(range)
fireEvent(document, new Event('selectionchange'))
const fullPassage = window.getSelection()!.toString()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
const preview = await screen.findByLabelText('Passage sélectionné')
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
await userEvent.click(screen.getByRole('button', { name: 'Ajouter le passage sélectionné à la question' }))
const preview = await screen.findByLabelText('Passage joint')
expect(preview).toHaveClass('line-clamp-3')
expect(preview.textContent).toBe(fullPassage)
expect(preview).not.toHaveAttribute('aria-hidden', 'true')
@ -164,12 +166,12 @@ it('garde la requête et son request_id lors de fermeture puis reprise après er
ask.mockImplementationOnce(() => new Promise((_, reject) => { fail = reject }))
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
selectPassage()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
await userEvent.type(await screen.findByRole('textbox'), 'Pourquoi ?')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
const first = ask.mock.calls.at(-1)![1]
await userEvent.keyboard('{Escape}')
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
expect(screen.getByRole('button', { name: 'Réponse en cours…' })).toBeDisabled()
expect(ask).toHaveBeenCalledTimes(1)
fail(new Error('network'))
@ -182,7 +184,7 @@ it('reprend après conflit, erreur de lecture et réponse pending sans perdre le
ask.mockRejectedValueOnce(new ApiError('qa_question_in_progress', 409))
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
selectPassage()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
await userEvent.type(await screen.findByRole('textbox'), 'Mon brouillon')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
expect(await screen.findByRole('alert')).not.toHaveTextContent(/actualis/i)

View File

@ -4,7 +4,7 @@ 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 label = 'Utiliser la recherche Web si nécessaire pour répondre, en transmettant au fournisseur de recherche les termes et éléments de contexte utiles.'
const label = 'Utiliser la recherche Web si nécessaire pour répondre ; la synthèse complète, les échanges précédents, votre question et tout passage joint seront transmis au fournisseur du modèle sélectionné.'
const unavailable = 'La recherche Web n’est pas disponible avec le modèle de cette synthèse.'
const conversation = { id: 'c', web_search_supported: true, messages: [] }
const answered = { id: 't', status: 'answered', answer: 'Réponse', citations: [], web_search_performed: false }
@ -13,7 +13,7 @@ async function open() {
const range = document.createRange(); range.selectNodeContents(screen.getByText('Passage sélectionnable'))
window.getSelection()!.removeAllRanges(); window.getSelection()!.addRange(range)
fireEvent(document, new Event('selectionchange'))
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
}
async function send() { await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' })) }
beforeEach(() => {
@ -46,7 +46,7 @@ it('conserve choix fermé et après erreur, rejoue le même UUID puis change UUI
await open(); await userEvent.click(await screen.findByRole('checkbox', { name: label }))
await userEvent.type(screen.getByRole('textbox'), 'Pourquoi ?')
await userEvent.keyboard('{Escape}')
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await userEvent.click(screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }))
expect(screen.getByRole('checkbox')).not.toBeChecked()
await send(); await screen.findByRole('alert')
const first = ask.mock.calls[0][1]; expect(first.use_web_search).toBe(false)

View File

@ -0,0 +1,141 @@
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 rendu dans le host header sans déplacer le lecteur de synthèse', async () => {
const host = document.createElement('div'); document.body.append(host)
try {
const result = render(<SummaryQA summaryId="s" content="Contenu" launcherHost={host} />)
expect(within(host).getByRole('button', { name: launcher })).toBeInTheDocument()
expect(within(result.container).getByText('Contenu')).toBeInTheDocument()
expect(within(result.container).queryByRole('button', { name: launcher })).not.toBeInTheDocument()
await open()
} finally { host.remove() }
})
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: '' })
})