feat(qa): add synthesis conversations with source retention (#52)

This commit is contained in:
2026-11-25 19:46:06 +01:00
parent 90dd2a65d2
commit 287811184f
49 changed files with 2636 additions and 47 deletions

View File

@ -0,0 +1,118 @@
import { cleanup, fireEvent, render, screen, waitFor } 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'
import { ApiError } from '../src/api/client'
const { get, ask } = vi.hoisted(() => ({ get: vi.fn(), ask: vi.fn() }))
vi.mock('@/api/summaryQA', () => ({ summaryQAApi: { get, ask } }))
const conversation = { id: 'c', summary_id: 's', mode: 'stateless', uses_source_articles: false, provider_session_supported: false, source_expires_at: '2026-01-01T00:00:00Z', sources_expired: true, source_count: 0, messages: [] }
function selectPassage() {
const range = document.createRange()
range.selectNodeContents(screen.getByText('Un passage de synthèse.'))
window.getSelection()!.removeAllRanges()
window.getSelection()!.addRange(range)
fireEvent(document, new Event('selectionchange'))
}
beforeEach(() => { get.mockResolvedValue(conversation); ask.mockResolvedValue({ id: 't', request_id: 'r', passage: 'Un passage de synthèse.', question: 'Pourquoi ?', answer: 'Une réponse.', status: 'answered', created_at: '', replayed: false }) })
afterEach(() => { cleanup(); window.getSelection()?.removeAllRanges() })
it('préserve les événements natifs et le focus ; seule une action explicite ouvre la question', async () => {
render(<SummaryQA summaryId="s" content="Un passage de synthèse." />)
const text = screen.getByText('Un passage de synthèse.')
for (const type of ['contextmenu', 'selectstart', 'touchstart', 'copy']) expect(fireEvent(text, new Event(type, { bubbles: true, cancelable: true }))).toBe(true)
selectPassage()
expect(get).not.toHaveBeenCalled()
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
const question = await screen.findByRole('textbox', { name: 'Votre question' })
expect(question).toHaveFocus()
expect(await screen.findByText(/Les sources ont expiré/)).toBeInTheDocument()
expect(screen.getByText(/sans articles sources ni session fournisseur réutilisée/)).toBeInTheDocument()
await userEvent.type(question, 'Pourquoi ?')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
expect(await screen.findByText('Une réponse.')).toBeInTheDocument()
expect(ask).toHaveBeenCalledWith('s', expect.objectContaining({ passage: 'Un passage de synthèse.', question: 'Pourquoi ?', request_id: expect.stringMatching(/^[a-f0-9-]{36}$/) }))
})
it('reprend la conversation et efface les données temporaires au changement de synthèse', async () => {
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." />)
await userEvent.click(screen.getByRole('button', { name: 'Questions sur cette synthèse' }))
expect(await screen.findByText('Ancienne réponse')).toBeInTheDocument()
selectPassage()
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
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.getByRole('button', { name: 'Interroger ce passage' })).toBeDisabled()
})
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.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/)
expect(screen.getByRole('textbox')).toHaveValue('Pourquoi ?')
})
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.type(await screen.findByRole('textbox'), 'Pourquoi ?')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
await screen.findByRole('alert')
const first = ask.mock.calls.at(-1)![1]
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
await waitFor(() => expect(ask.mock.calls.at(-1)![1]).toEqual(first))
})
it('bloque un second envoi pendant la réponse et annonce sa réception', async () => {
let finish!: (value: unknown) => void
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.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()
expect(screen.getByRole('textbox')).toBeDisabled()
expect(ask).toHaveBeenCalledTimes(1)
finish({ id: 't', request_id: 'r', passage: 'Un passage de synthèse.', question: 'Pourquoi ?', answer: 'Réponse terminale', status: 'answered' })
expect(await screen.findByRole('log')).toHaveTextContent('Réponse terminale')
})
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." />)
await userEvent.click(screen.getByRole('button', { name: 'Questions sur cette synthèse' }))
expect(await screen.findByRole('alert')).not.toHaveTextContent('PRIVATE_PROVIDER_SECRET')
await userEvent.click(screen.getByRole('button', { name: 'Actualiser la conversation' }))
expect(await screen.findByRole('textbox', { name: 'Votre question' })).toBeInTheDocument()
})
it('refuse les passages trop longs et les questions vides avant appel', async () => {
const content = 'a'.repeat(8001)
render(<SummaryQA summaryId="s" content={content} />)
const range = document.createRange()
range.selectNodeContents(screen.getByText(content))
window.getSelection()!.removeAllRanges()
window.getSelection()!.addRange(range)
fireEvent(document, new Event('selectionchange'))
await userEvent.click(screen.getByRole('button', { name: 'Interroger ce passage' }))
await screen.findByRole('textbox')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
expect(screen.getByRole('textbox')).toHaveAttribute('aria-invalid', 'true')
expect(screen.getByRole('alert')).toHaveTextContent('8 000')
expect(ask).not.toHaveBeenCalled()
})
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.type(await screen.findByRole('textbox'), 'Pourquoi ?')
await userEvent.click(screen.getByRole('button', { name: 'Envoyer la question' }))
const alert = await screen.findByRole('alert')
expect(alert).toHaveTextContent('Trop de questions en peu de temps.')
expect(alert).toHaveTextContent(seconds === undefined ? 'Réessayez dans quelques instants.' : 'Réessayez dans 45 secondes.')
expect(screen.getByRole('textbox')).toHaveValue('Pourquoi ?')
})