Files
TradarrV2/frontend/tests/admin-source-feeds-ticket41.test.tsx

73 lines
4.9 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, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { Sources } from '../src/pages/admin/Sources'
const latest = 'https://feed.quanwenrss.com/feed/bloomberg-latest.xml'
const economics = 'https://feed.quanwenrss.com/feed/bloomberg-economics.xml'
const investing = 'https://feed.quanwenrss.com/feed/investing-news.xml'
const fetchMock = vi.fn()
let sources: object[]
beforeEach(() => {
sources = [
{ id: 'b', source_key: 'bloomberg', name: 'Bloomberg', supported: true, operational: true, enabled: true, article_limit: 20, feed_urls: [latest] },
{ id: 'i', source_key: 'investing_com', name: 'Investing.com', supported: true, operational: false, enabled: false, article_limit: 40, feed_urls: [investing] },
]
vi.stubGlobal('fetch', fetchMock)
fetchMock.mockReset().mockImplementation((url: string) => Promise.resolve(new Response(JSON.stringify({ data: url === '/api/admin/sources' ? sources : [] }), { status: 200 })))
})
afterEach(() => { cleanup(); vi.unstubAllGlobals() })
it('charge et sauvegarde plusieurs flux Bloomberg sans déclencher de récupération ni génération', async () => {
render(<Sources />)
const form = await screen.findByRole('form', { name: 'Flux RSS Bloomberg' })
await userEvent.click(within(form).getByRole('button', { name: 'Ajouter un flux RSS Bloomberg' }))
await userEvent.type(within(form).getByLabelText('URL du flux RSS Bloomberg 2'), economics)
await userEvent.click(within(form).getByRole('button', { name: 'Enregistrer les flux RSS Bloomberg' }))
expect(fetchMock).toHaveBeenLastCalledWith('/api/admin/sources/b', expect.objectContaining({ method: 'PUT', body: JSON.stringify({ enabled: true, feed_urls: [latest, economics] }) }))
expect(within(form).getByRole('status')).toHaveTextContent('Aucune récupération ni génération n’a été lancée.')
expect(fetchMock).toHaveBeenCalledTimes(3)
})
it('enregistre les flux d’une source désactivée sans l’activer et sans modifier son plafond', async () => {
render(<Sources />)
const form = await screen.findByRole('form', { name: 'Flux RSS Investing.com' })
expect(within(form).getByLabelText('URL du flux RSS Investing.com 1')).toHaveValue(investing)
await userEvent.click(within(form).getByRole('button', { name: 'Enregistrer les flux RSS Investing.com' }))
expect(fetchMock).toHaveBeenLastCalledWith('/api/admin/sources/i', expect.objectContaining({ method: 'PUT', body: JSON.stringify({ enabled: false, feed_urls: [investing] }) }))
expect(screen.getByRole('button', { name: 'Activer Investing.com' })).toBeEnabled()
expect(screen.getByLabelText('Plafond Investing.com')).toHaveValue(40)
})
it('ne propose pas de configuration supposée quand le DTO ne fournit pas de feeds', async () => {
sources = [{ id: 'b', source_key: 'bloomberg', name: 'Bloomberg', supported: true, operational: true, enabled: true, article_limit: 20 }]
render(<Sources />)
await screen.findByRole('heading', { name: 'Bloomberg' })
expect(screen.queryByRole('form', { name: 'Flux RSS Bloomberg' })).not.toBeInTheDocument()
expect(screen.getByText(/Configuration des flux RSS indisponible/)).toBeInTheDocument()
})
it('relie les rejets de validation serveur à un message assaini et conserve les saisies', async () => {
render(<Sources />)
const form = await screen.findByRole('form', { name: 'Flux RSS Bloomberg' })
fetchMock.mockResolvedValueOnce(new Response(JSON.stringify({ error: 'source_feeds_invalid', url: 'PRIVATE_URL', content: 'PRIVATE_CONTENT' }), { status: 400 }))
await userEvent.click(within(form).getByRole('button', { name: 'Enregistrer les flux RSS Bloomberg' }))
expect(within(form).getByRole('alert')).toHaveTextContent('Les flux RSS ont été refusés.')
expect(within(form).getByRole('alert')).not.toHaveTextContent(/PRIVATE_/)
expect(within(form).getByLabelText('URL du flux RSS Bloomberg 1')).toHaveValue(latest)
})
it('verrouille activation et plafond pendant la sauvegarde des flux de la même source', async () => {
render(<Sources />)
const form = await screen.findByRole('form', { name: 'Flux RSS Bloomberg' })
let complete!: (value: Response) => void
fetchMock.mockImplementationOnce(() => new Promise<Response>(resolve => { complete = resolve }))
await userEvent.click(within(form).getByRole('button', { name: 'Enregistrer les flux RSS Bloomberg' }))
expect(screen.getByRole('button', { name: 'Désactiver Bloomberg' })).toBeDisabled()
expect(screen.getByRole('button', { name: 'Enregistrer le plafond Bloomberg' })).toBeDisabled()
expect(screen.getByRole('button', { name: 'Activer Investing.com' })).toBeEnabled()
complete(new Response(JSON.stringify({ data: { ok: true } }), { status: 200 }))
expect(await within(form).findByText(/Flux RSS enregistrés pour les prochaines synthèses/)).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Désactiver Bloomberg' })).toBeEnabled()
})