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,28 @@
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 () => {
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 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)
expect(await summaryQAApi.ask('s', question)).toEqual(turn)
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/summaries/s/conversation', { headers: { 'Content-Type': 'application/json', Authorization: 'Bearer user-token' } })
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/summaries/s/conversation/messages', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer user-token' }, body: JSON.stringify(question) })
})
it.each([
['45', 45], ['0', 0], [null, undefined], ['', undefined], ['-1', undefined], ['1.5', undefined],
['Infinity', undefined], ['999999999999999999999999', undefined], ['PRIVATE_HEADER', undefined],
['Wed, 25 Nov 2026 12:00:30 GMT', 30], ['Wed, 25 Nov 2026 11:59:00 GMT', 0],
])('capture Retry-After %s comme délai sûr %s', async (header, seconds) => {
vi.spyOn(Date, 'now').mockReturnValue(Date.parse('2026-11-25T12:00:00Z'))
try {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: 'qa_rate_limited' }), {
status: 429, headers: header === null ? {} : { 'Retry-After': header },
})))
await expect(summaryQAApi.get('s')).rejects.toMatchObject({ message: 'qa_rate_limited', status: 429, retryAfterSeconds: seconds })
} finally { vi.restoreAllMocks() }
})