29 lines
2.5 KiB
TypeScript
29 lines
2.5 KiB
TypeScript
import { afterEach, expect, it, vi } from 'vitest'
|
|
import { summaryQAApi } from '../src/api/summaryQA'
|
|
afterEach(() => { vi.unstubAllGlobals(); localStorage.clear() })
|
|
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', 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)
|
|
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() }
|
|
})
|