Files
TradarrV2/frontend/tests/admin-article-limits.test.tsx

270 lines
15 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 { cleanup, render, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { MemoryRouter } from 'react-router-dom'
import { AdminSettings } from '../src/pages/admin/AdminSettings'
import { Sources } from '../src/pages/admin/Sources'
const { listSettings, getDefaultPrompt, updateSettings, listSources, updateSource } = vi.hoisted(() => ({
listSettings: vi.fn(),
getDefaultPrompt: vi.fn(),
updateSettings: vi.fn(),
listSources: vi.fn(),
updateSource: vi.fn(),
}))
vi.mock('@/api/admin', () => ({
adminApi: { listSettings, getDefaultPrompt, updateSettings, listSources, updateSource },
}))
describe('plafonds d’articles administrateur', () => {
afterEach(cleanup)
beforeEach(() => {
listSettings.mockResolvedValue([{ key: 'summary_max_articles', value: '700' }])
getDefaultPrompt.mockResolvedValue({ prompt: 'system' })
updateSettings.mockResolvedValue(undefined)
listSources.mockResolvedValue([{ id: 'source-1', name: 'Bloomberg', type: 'rss', source_key: 'bloomberg', supported: true, operational: true, enabled: true, article_limit: 240 }])
updateSource.mockResolvedValue(undefined)
})
it('borne le plafond global de 1 à 500 sans écrêter une valeur historique', async () => {
render(<Sources />)
const input = await screen.findByLabelText('Articles max par résumé')
expect(input).toHaveValue(700)
expect(input).toHaveAttribute('min', '1')
expect(input).toHaveAttribute('max', '500')
})
it('sauvegarde le plafond global via les paramètres admin', async () => {
const user = userEvent.setup()
render(<Sources />)
const input = await screen.findByLabelText('Articles max par résumé')
await user.clear(input)
await user.type(input, '50')
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
await waitFor(() => expect(updateSettings).toHaveBeenCalledWith([{ key: 'summary_max_articles', value: '50' }]))
})
it('borne chaque plafond source de 1 à 200 et conserve une ancienne valeur hors plage', async () => {
render(<Sources />)
const input = await screen.findByLabelText('Plafond Bloomberg')
expect(input).toHaveValue(240)
expect(input).toHaveAttribute('min', '1')
expect(input).toHaveAttribute('max', '200')
})
it('sauvegarde le plafond de la source via l’API admin', async () => {
const user = userEvent.setup()
render(<Sources />)
const input = await screen.findByLabelText('Plafond Bloomberg')
await user.clear(input)
await user.type(input, '20')
await user.click(screen.getByRole('button', { name: 'Enregistrer le plafond Bloomberg' }))
await waitFor(() => expect(updateSource).toHaveBeenCalledWith('source-1', true, 20))
})
it('remplace l’éditeur en doublon dans Paramètres par un lien et garde le filtre séparé', async () => {
listSettings.mockResolvedValue([{ key: 'summary_max_articles', value: '700' }, { key: 'filter_batch_size', value: '10' }])
render(<MemoryRouter><AdminSettings /></MemoryRouter>)
expect(await screen.findByRole('link', { name: 'Sources et plafonds' })).toHaveAttribute('href', '/admin/sources')
expect(screen.queryByLabelText('Articles max par résumé')).not.toBeInTheDocument()
expect(screen.getByLabelText('Taille des batches (filtre)')).toHaveValue(10)
})
it('bloque une valeur historique globale jusqu’à une correction explicite sans substitution', async () => {
const user = userEvent.setup()
render(<Sources />)
const input = await screen.findByLabelText('Articles max par résumé')
const save = screen.getByRole('button', { name: 'Enregistrer', exact: true })
expect(input).toHaveValue(700)
expect(save).toBeDisabled()
await user.click(save)
expect(updateSettings).not.toHaveBeenCalled()
for (const value of ['0', '501', '2.5']) {
await user.clear(input)
await user.type(input, value)
expect(save).toBeDisabled()
expect(input).toHaveValue(Number(value))
}
await user.clear(input)
expect(save).toBeDisabled()
await user.type(input, '500')
expect(save).toBeEnabled()
})
it('initialise uniquement les valeurs absentes à 50 et 20', async () => {
listSettings.mockResolvedValue([])
listSources.mockResolvedValue([{ id: 'source-1', name: 'Bloomberg', type: 'rss', source_key: 'bloomberg', supported: true, operational: true, enabled: true }])
render(<Sources />)
expect(await screen.findByLabelText('Articles max par résumé')).toHaveValue(50)
expect(screen.getByLabelText('Plafond Bloomberg')).toHaveValue(20)
})
it('conserve le plafond historique pendant la désactivation et la réactivation', async () => {
const user = userEvent.setup()
render(<Sources />)
const input = await screen.findByLabelText('Plafond Bloomberg')
await user.click(screen.getByRole('button', { name: 'Désactiver Bloomberg' }))
await waitFor(() => expect(input).toBeDisabled())
expect(input).toHaveValue(240)
expect(updateSource).toHaveBeenLastCalledWith('source-1', false)
await user.click(screen.getByRole('button', { name: 'Activer Bloomberg' }))
await waitFor(() => expect(input).toBeEnabled())
expect(input).toHaveValue(240)
expect(updateSource).toHaveBeenLastCalledWith('source-1', true)
})
it('conserve les saisies après échec et annonce la réussite uniquement après sauvegarde', async () => {
updateSettings.mockRejectedValueOnce(new Error('network'))
updateSource.mockRejectedValueOnce(new Error('network'))
const user = userEvent.setup()
render(<Sources />)
const global = await screen.findByLabelText('Articles max par résumé')
await user.clear(global)
await user.type(global, '50')
await user.click(screen.getByRole('button', { name: 'Enregistrer', exact: true }))
expect(await screen.findByRole('alert')).toHaveTextContent('Votre saisie est conservée.')
expect(global).toHaveValue(50)
expect(screen.queryByRole('status')).not.toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Enregistrer', exact: true }))
expect(await screen.findByRole('status')).toHaveTextContent('Plafond global enregistré.')
const source = screen.getByLabelText('Plafond Bloomberg')
await user.clear(source)
await user.type(source, '20')
await user.click(screen.getByRole('button', { name: 'Enregistrer le plafond Bloomberg' }))
expect(await screen.findByRole('alert')).toHaveTextContent('Impossible d’enregistrer le plafond.')
expect(source).toHaveValue(20)
})
it('rejette les plafonds source hors bornes et fractionnaires sans les écrêter', async () => {
const user = userEvent.setup()
render(<Sources />)
const input = await screen.findByLabelText('Plafond Bloomberg')
const save = screen.getByRole('button', { name: 'Enregistrer le plafond Bloomberg' })
for (const value of ['0', '201', '1.5']) {
await user.clear(input)
await user.type(input, value)
expect(input).toHaveValue(Number(value))
expect(save).toBeDisabled()
}
expect(updateSource).not.toHaveBeenCalled()
})
it('ne rend activables que Bloomberg et Investing.com; conserve les autres sources sans commandes', async () => {
listSources.mockResolvedValue([
{ id: 'y', name: 'Yahoo Finance', type: 'stocktwits', source_key: 'yahoo_finance', supported: false, operational: false, enabled: false, article_limit: 83 },
{ id: 'b', name: 'Bloomberg', type: 'bloomberg', source_key: 'bloomberg', supported: true, operational: false, enabled: false, article_limit: 77 },
{ id: 'i', name: 'Investing.com', type: 'rss', source_key: 'investing_com', supported: true, operational: true, enabled: true, article_limit: 20 },
{ id: 'c', name: 'CNBC Business', type: 'cnbc_business', source_key: 'cnbc_business', supported: false, operational: false, enabled: false, article_limit: 20 },
{ id: 'w', name: 'Watcher.Guru', type: 'watcherguru', source_key: 'watcher_guru', supported: false, operational: false, enabled: false, article_limit: 64 },
{ id: 's', name: 'StockTwits', type: 'stocktwits', source_key: null, supported: false, operational: false, enabled: false, article_limit: 31 },
{ id: 'r', name: 'Reuters', type: 'reuters', source_key: null, supported: false, operational: false, enabled: false, article_limit: 42 },
])
render(<Sources />)
expect(await screen.findByRole('heading', { name: 'Sources retirées' })).toBeInTheDocument()
for (const name of ['Bloomberg', 'Investing.com']) {
expect(screen.getByRole('button', { name: new RegExp(`^(?:Activer|Désactiver) ${name}$`) })).toBeInTheDocument()
expect(screen.getByLabelText(`Plafond ${name}`)).toBeInTheDocument()
}
const historical = screen.getByRole('region', { name: 'Sources retirées' })
for (const name of ['Yahoo Finance', 'CNBC Business', 'StockTwits', 'Watcher.Guru', 'Reuters']) {
expect(within(historical).getByText(name)).toBeInTheDocument()
expect(screen.queryByLabelText(`Plafond ${name}`)).not.toBeInTheDocument()
}
expect(within(historical).getByText('Exclue des synthèses · Plafond historique : 64')).toBeInTheDocument()
expect(historical.querySelectorAll('button,input,form,a')).toHaveLength(0)
expect(screen.getByLabelText('Plafond Bloomberg')).toHaveValue(77)
expect(screen.getByLabelText('Plafond Investing.com')).toHaveValue(20)
})
it('garde les anciennes sources repliées et en lecture seule même si enabled est historique', async () => {
listSources.mockResolvedValue([
{ id: 'old', name: 'Reuters', type: 'reuters', source_key: null, supported: false, operational: false, enabled: true, article_limit: 42 },
])
const user = userEvent.setup()
render(<Sources />)
const region = await screen.findByRole('region', { name: 'Sources retirées' })
const disclosure = region.querySelector('details')!
expect(disclosure).not.toHaveAttribute('open')
await user.click(within(region).getByText('Consulter les sources retirées (1)'))
expect(disclosure).toHaveAttribute('open')
expect(within(region).getByText('Reuters')).toBeVisible()
expect(within(region).queryByRole('button')).not.toBeInTheDocument()
expect(within(region).queryByRole('spinbutton')).not.toBeInTheDocument()
expect(updateSource).not.toHaveBeenCalled()
})
it('annonce les réglages des prochaines synthèses sans annoncer de génération lancée', async () => {
listSources.mockResolvedValue([{ id: 'y', name: 'Stocktwits', type: 'stocktwits', source_key: 'bloomberg', supported: true, operational: true, enabled: true, article_limit: 83 }])
const user = userEvent.setup()
render(<Sources />)
const input = await screen.findByLabelText('Plafond Bloomberg')
expect(screen.queryByText('Stocktwits')).not.toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Désactiver Bloomberg' }))
expect(await screen.findByRole('status')).toHaveTextContent('Bloomberg désactivée pour les prochaines synthèses. Aucune génération n’a été lancée.')
expect(input).toHaveValue(83)
expect(input).toBeDisabled()
await user.click(screen.getByRole('button', { name: 'Activer Bloomberg' }))
await waitFor(() => expect(input).toBeEnabled())
await user.click(screen.getByRole('button', { name: 'Enregistrer le plafond Bloomberg' }))
expect(await screen.findByRole('status')).toHaveTextContent('Plafond Bloomberg enregistré pour les prochaines synthèses.')
expect(updateSource).toHaveBeenLastCalledWith('y', true, 83)
})
it('permet de recharger après un échec sans afficher de commandes pour des sources supposées', async () => {
listSources.mockRejectedValueOnce(new Error('network'))
const user = userEvent.setup()
render(<Sources />)
expect(await screen.findByRole('alert')).toHaveTextContent('Impossible de charger')
expect(screen.queryByRole('spinbutton')).not.toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Réessayer' }))
expect(await screen.findByLabelText('Plafond Bloomberg')).toHaveValue(240)
})
it('sauvegarde le plafond Investing.com et refuse toute commande Watcher Guru même avec un ancien DTO activable', async () => {
listSources.mockResolvedValue([
{ id: 'investing-new', name: 'Investing.com', type: 'rss', source_key: 'investing_com', supported: true, operational: true, enabled: true, article_limit: 37 },
{ id: 'watcher-old', name: 'Watcher.Guru', type: 'watcherguru', source_key: 'watcher_guru', supported: true, operational: true, enabled: true, article_limit: 64 },
])
const user = userEvent.setup()
render(<Sources />)
const cap = await screen.findByLabelText('Plafond Investing.com')
expect(cap).toHaveValue(37)
expect(screen.queryByRole('button', { name: /^(Activer|Désactiver) Watcher/ })).not.toBeInTheDocument()
const historical = screen.getByRole('region', { name: 'Sources retirées' })
expect(within(historical).getByText('Watcher.Guru')).toBeInTheDocument()
expect(within(historical).queryByRole('spinbutton')).not.toBeInTheDocument()
expect(within(historical).queryByRole('button')).not.toBeInTheDocument()
await user.clear(cap)
await user.type(cap, '42')
await user.click(screen.getByRole('button', { name: 'Enregistrer le plafond Investing.com' }))
await waitFor(() => expect(updateSource).toHaveBeenCalledWith('investing-new', true, 42))
expect(updateSource).toHaveBeenCalledTimes(1)
expect(await screen.findByRole('status')).toHaveTextContent('Plafond Investing.com enregistré pour les prochaines synthèses.')
expect(screen.getByText(/Seules Bloomberg et Investing.com/)).toBeInTheDocument()
})
})
describe('ticket #42 — sources RSS et sources retirées', () => {
afterEach(cleanup)
it('affiche Investing.com depuis la clé canonique backend et conserve Yahoo Finance et CNBC Business en lecture seule', async () => {
listSettings.mockResolvedValue([])
listSources.mockResolvedValue([
{ id: 'b', source_key: 'bloomberg', name: 'Bloomberg', type: 'rss', supported: true, operational: true, enabled: true, article_limit: 20 },
{ id: 'i', source_key: 'investing_com', name: 'Legacy RSS label', type: 'rss', supported: true, operational: false, enabled: false, article_limit: 40 },
{ id: 'y', source_key: 'yahoo_finance', name: 'legacy', type: 'stocktwits', supported: true, operational: true, enabled: true, article_limit: 20 },
{ id: 'c', source_key: 'cnbc_business', name: 'legacy', type: 'rss', supported: true, operational: true, enabled: true, article_limit: 20 },
])
render(<Sources />)
expect(await screen.findByRole('heading', { name: 'Investing.com' })).toBeInTheDocument()
expect(screen.getByLabelText('Plafond Investing.com')).toHaveValue(40)
expect(screen.getByRole('button', { name: 'Activer Investing.com' })).toBeEnabled()
expect(screen.queryByRole('button', { name: /^(Activer|Désactiver) Yahoo Finance$/ })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: /^(Activer|Désactiver) CNBC Business$/ })).not.toBeInTheDocument()
const retired = screen.getByRole('region', { name: 'Sources retirées' })
expect(within(retired).getByText('Yahoo Finance')).toBeInTheDocument()
expect(within(retired).getByText('CNBC Business')).toBeInTheDocument()
expect(screen.queryByRole('heading', { name: 'Legacy RSS label' })).not.toBeInTheDocument()
})
})