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

95 lines
6.4 KiB
TypeScript
Raw 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 { act, 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'
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 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 }
async function open() {
render(<SummaryQA summaryId="s" content="Passage sélectionnable" />)
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' }))
}
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('attend la capacité avant de présenter une case native cochée', async () => {
let resolve!: (value: unknown) => void
get.mockImplementationOnce(() => new Promise(r => { resolve = r }))
await open()
expect(screen.queryByRole('checkbox')).not.toBeInTheDocument()
expect(screen.queryByRole('form')).not.toBeInTheDocument()
expect(screen.queryByText(unavailable)).not.toBeInTheDocument()
await act(async () => resolve(conversation))
const checkbox = await screen.findByRole('checkbox', { name: label })
expect(checkbox.tagName).toBe('INPUT'); expect(checkbox).toBeChecked()
})
it('envoie false pour un modèle incompatible mais permet la question classique', async () => {
get.mockResolvedValue({ ...conversation, web_search_supported: false })
await open()
const checkbox = await screen.findByRole('checkbox', { name: unavailable })
expect(checkbox).toBeDisabled(); expect(checkbox).not.toBeChecked()
await userEvent.type(screen.getByRole('textbox'), 'Pourquoi ?'); await send()
expect(ask.mock.calls[0][1].use_web_search).toBe(false)
})
it('conserve choix fermé et après erreur, rejoue le même UUID puis change UUID si option modifiée', async () => {
ask.mockRejectedValue(new Error('network'))
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' }))
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)
await send(); await screen.findByRole('alert'); expect(ask.mock.calls[1][1]).toEqual(first)
await userEvent.click(screen.getByRole('checkbox')); await send(); await screen.findByRole('alert')
expect(ask.mock.calls[2][1]).toEqual({ ...first, request_id: expect.any(String), use_web_search: true })
expect(ask.mock.calls[2][1].request_id).not.toBe(first.request_id)
})
it('désactive pendant envoi et réinitialise après réponse', async () => {
let resolve!: (value: unknown) => void
ask.mockImplementationOnce(() => new Promise(r => { resolve = r }))
await open(); await userEvent.click(await screen.findByRole('checkbox'))
await userEvent.type(screen.getByRole('textbox'), 'Pourquoi ?'); await send()
expect(screen.getByRole('checkbox')).toBeDisabled()
await act(async () => resolve({ ...ask.mock.calls[0][1], ...answered }))
expect(screen.getByRole('checkbox')).toBeChecked(); expect(screen.getByRole('checkbox')).toBeEnabled()
expect(screen.getByRole('textbox')).toHaveValue('')
})
it('réinitialise aussi après polling de sa propre demande', async () => {
ask.mockImplementationOnce(async (_id, body) => ({ ...body, ...answered, status: 'pending' }))
await open(); await userEvent.click(await screen.findByRole('checkbox'))
await userEvent.type(screen.getByRole('textbox'), 'Pourquoi ?'); await send()
expect(screen.getByRole('checkbox')).toBeDisabled()
get.mockResolvedValue({ ...conversation, messages: [{ ...ask.mock.calls[0][1], ...answered }] })
await waitFor(() => expect(screen.getByRole('textbox')).toHaveValue(''), { timeout: 4500 })
expect(screen.getByRole('checkbox')).toBeChecked()
})
it('affiche seulement citations DTO HTTPS avec titre ou hostname et nouvel onglet annoncé', async () => {
get.mockResolvedValue({ ...conversation, messages: [{ ...answered, passage: 'Extrait', question: 'Question', web_search_performed: true, answer: '[Faux lien](https://model.invalid) https://raw.invalid', citations: [
{ title: 'Source fiable', url: 'https://example.com/article' }, { title: '', url: 'https://fallback.example/path' },
{ title: 'HTTP', url: 'http://unsafe.example' }, { title: 'Script', url: 'javascript:alert(1)' }, { title: 'Invalide', url: 'not a url' },
] }] })
await open()
const link = await screen.findByRole('link', { name: 'Source fiable (nouvel onglet)' })
expect(link).toHaveAttribute('href', 'https://example.com/article')
expect(link).toHaveAttribute('target', '_blank'); expect(link).toHaveAttribute('rel', 'noopener noreferrer')
expect(screen.getByRole('link', { name: 'fallback.example (nouvel onglet)' })).toBeInTheDocument()
expect(screen.getAllByRole('link')).toHaveLength(2)
})
it.each([undefined, [], [{ title: 'Interdit', url: 'http://unsafe.example' }]])('aucun panneau vide ni lien pour citations absentes ou invalides %s', async citations => {
get.mockResolvedValue({ ...conversation, messages: [{ ...answered, passage: 'Extrait', question: 'Question', citations, web_search_performed: true }] })
await open(); await screen.findByRole('textbox')
expect(screen.queryByRole('link')).not.toBeInTheDocument()
expect(screen.queryByRole('list', { name: 'Sources de la réponse' })).not.toBeInTheDocument()
})