|
|
|
|
@ -0,0 +1,116 @@
|
|
|
|
|
import { cleanup, render, screen, within } from '@testing-library/react'
|
|
|
|
|
import userEvent from '@testing-library/user-event'
|
|
|
|
|
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
|
|
|
|
|
import { ApiError } from '../src/api/client'
|
|
|
|
|
import { Preferences } from '../src/pages/Preferences'
|
|
|
|
|
import { GenerationDetails } from '../src/components/GenerationDetails'
|
|
|
|
|
import type { GenerationOutcome } from '../src/api/summaries'
|
|
|
|
|
|
|
|
|
|
const { get, update, listSummaryModels } = vi.hoisted(() => ({ get: vi.fn(), update: vi.fn(), listSummaryModels: vi.fn() }))
|
|
|
|
|
vi.mock('@/api/preferences', () => ({ preferencesApi: { get, update, listSummaryModels } }))
|
|
|
|
|
vi.mock('@/components/Appearance', () => ({ Appearance: () => null }))
|
|
|
|
|
const profile = { context_window_tokens: 128000, input_limit_tokens: 128000, output_reserve_tokens: 4096, framing_tokens: 1024, token_count_method: 'utf8_bytes_upper_bound_v1' }
|
|
|
|
|
const preferences = { language: 'fr', ranking_mode: 'title_content', summary_model_id: 'a', summary_prompt_token_budget: 250000 }
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.resetAllMocks()
|
|
|
|
|
get.mockResolvedValue(preferences)
|
|
|
|
|
listSummaryModels.mockResolvedValue([{ id: 'a', provider: 'openai', model: 'gpt-4o-mini', prompt_capacity: profile }, { id: 'b', provider: 'ollama', model: 'local', prompt_capacity: null }])
|
|
|
|
|
update.mockImplementation(async value => value)
|
|
|
|
|
})
|
|
|
|
|
afterEach(cleanup)
|
|
|
|
|
|
|
|
|
|
it('charge le budget, décrit sa portée et la capacité effective du modèle sans réduire la préférence', async () => {
|
|
|
|
|
render(<Preferences />)
|
|
|
|
|
const budget = await screen.findByRole('spinbutton', { name: 'Budget maximal du prompt de synthèse' })
|
|
|
|
|
expect(budget).toHaveValue(250000)
|
|
|
|
|
expect(budget).toHaveAccessibleDescription(/instructions, watchlist, contexte et articles/)
|
|
|
|
|
expect(screen.getByText(/ne règle pas la longueur de sortie/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/Budget effectif : 123\s?904 tokens/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/Réserve de sortie : 4\s?096 tokens/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/taille estimée conservative/i)).toBeInTheDocument()
|
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
|
|
|
expect(update).toHaveBeenCalledWith(preferences)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('utilise 250000 en absence de champ historique et sauvegarde une valeur personnalisée', async () => {
|
|
|
|
|
get.mockResolvedValueOnce({ language: 'fr', ranking_mode: 'title', summary_model_id: 'a' })
|
|
|
|
|
render(<Preferences />)
|
|
|
|
|
const budget = await screen.findByRole('spinbutton', { name: 'Budget maximal du prompt de synthèse' })
|
|
|
|
|
expect(budget).toHaveValue(250000)
|
|
|
|
|
await userEvent.clear(budget)
|
|
|
|
|
await userEvent.type(budget, '80000')
|
|
|
|
|
expect(screen.getByText(/Budget effectif : 80\s?000 tokens/)).toBeInTheDocument()
|
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
|
|
|
expect(update).toHaveBeenCalledWith(expect.objectContaining({ summary_prompt_token_budget: 80000 }))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it.each(['', '0', '-1', '1.5', '2000001'])('refuse un budget invalide %s avec une erreur accessible sans requête', async value => {
|
|
|
|
|
render(<Preferences />)
|
|
|
|
|
const budget = await screen.findByRole('spinbutton', { name: 'Budget maximal du prompt de synthèse' })
|
|
|
|
|
await userEvent.clear(budget)
|
|
|
|
|
if (value) await userEvent.type(budget, value)
|
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
|
|
|
expect(screen.getByRole('alert')).toHaveTextContent(/entier entre 1 et 2\s?000\s?000 tokens/)
|
|
|
|
|
expect(budget).toHaveAttribute('aria-invalid', 'true')
|
|
|
|
|
expect(budget).toHaveAccessibleDescription(/entier entre 1 et 2\s?000\s?000 tokens/)
|
|
|
|
|
expect(update).not.toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it.each([1, 2000000])('accepte la borne %s', async value => {
|
|
|
|
|
render(<Preferences />)
|
|
|
|
|
const budget = await screen.findByRole('spinbutton', { name: 'Budget maximal du prompt de synthèse' })
|
|
|
|
|
await userEvent.clear(budget)
|
|
|
|
|
await userEvent.type(budget, String(value))
|
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
|
|
|
expect(update).toHaveBeenCalledWith(expect.objectContaining({ summary_prompt_token_budget: value }))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('explique une capacité inconnue sans inférence locale et conserve le budget en cas de rejet serveur', async () => {
|
|
|
|
|
render(<Preferences />)
|
|
|
|
|
await userEvent.selectOptions(await screen.findByLabelText('Modèle de synthèse'), 'b')
|
|
|
|
|
expect(screen.getByText(/Capacité du modèle inconnue/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/ne pourra pas être générée/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.queryByText(/Budget effectif :/)).not.toBeInTheDocument()
|
|
|
|
|
update.mockRejectedValueOnce(new ApiError('invalid_prompt_budget', 400))
|
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
|
|
|
expect(screen.getByRole('alert')).toHaveTextContent(/entier entre 1 et 2\s?000\s?000 tokens/)
|
|
|
|
|
expect(screen.getByRole('spinbutton')).toHaveValue(250000)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const outcome = {
|
|
|
|
|
id: 'g', status: 'partial', language: 'fr', ranking_mode: 'title_content', total_candidates: 6,
|
|
|
|
|
content_fallbacks: 1, laya_selected: 2, selected: 2, global_limit: 50,
|
|
|
|
|
content_complete: 3, content_incomplete: 2, content_unavailable: 1, dates_available: 4,
|
|
|
|
|
failure_code: 'content_incomplete',
|
|
|
|
|
prompt_budget: { requested_tokens: 250000, effective_tokens: 123904, prompt_tokens: 42000, output_reserve_tokens: 4096, token_count_method: 'utf8_bytes_upper_bound_v1' },
|
|
|
|
|
source_stats: [{ source_id: 'y', source_type: 'Yahoo Finance', status: 'ok', candidates: 6, selected: 2, laya_selected: 2, article_limit: 10, content_fallbacks: 1, content_complete: 3, content_incomplete: 2, content_unavailable: 1, dates_available: 4 }],
|
|
|
|
|
} as GenerationOutcome
|
|
|
|
|
|
|
|
|
|
it('présente uniquement les compteurs agrégés et les budgets, avec les limites de la couverture partielle', () => {
|
|
|
|
|
const value = { ...outcome, title: 'PRIVATE_TITLE', url: 'https://private.invalid', content: 'PRIVATE_ARTICLE', prompt: 'PRIVATE_PROMPT' }
|
|
|
|
|
const { container } = render(<GenerationDetails outcome={value} />)
|
|
|
|
|
expect(screen.getByText(/Contenus complets : 3/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/Contenus partiels : 2/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/Contenus absents : 1/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/Dates disponibles : 4/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/Seuls les contenus complets/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/Taille estimée conservative : 42\s?000 tokens/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/Budget demandé : 250\s?000 tokens/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.getByText(/Budget effectif : 123\s?904 tokens/)).toBeInTheDocument()
|
|
|
|
|
expect(within(screen.getByRole('table')).getByRole('columnheader', { name: 'Contenus partiels' })).toBeInTheDocument()
|
|
|
|
|
expect(container.innerHTML).not.toMatch(/PRIVATE_|private.invalid|utf8_bytes/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('distingue le contenu non évalué en mode titre des corps absents', () => {
|
|
|
|
|
render(<GenerationDetails outcome={{ ...outcome, ranking_mode: 'title', content_complete: 0, content_incomplete: 0, content_unavailable: 0, failure_code: undefined }} />)
|
|
|
|
|
expect(screen.getByText(/Contenu non évalué en mode titre uniquement/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.queryByText(/Contenus absents : 0/)).not.toBeInTheDocument()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('ne transforme pas les compteurs historiques absents en faux zéros', () => {
|
|
|
|
|
const { content_complete: _complete, content_incomplete: _partial, content_unavailable: _absent, dates_available: _dates, prompt_budget: _budget, ...historic } = outcome
|
|
|
|
|
void [_complete, _partial, _absent, _dates, _budget]
|
|
|
|
|
render(<GenerationDetails outcome={historic} />)
|
|
|
|
|
expect(screen.getByText(/Complétude non renseignée pour cette génération/)).toBeInTheDocument()
|
|
|
|
|
expect(screen.queryByText(/Contenus complets : 0/)).not.toBeInTheDocument()
|
|
|
|
|
})
|