Files
TradarrV2/frontend/e2e/ticket27-generation-navigation.spec.mjs

128 lines
7.5 KiB
JavaScript

import { expect, test } from '@playwright/test'
import { readStackManifest } from './stack-manifest.mjs'
test('ticket #27: resumes a running job after leaving Dashboard without a second POST, then displays its result', async ({ page }) => {
const stack = readStackManifest()
await page.context().route('**/*', route => {
const host = new URL(route.request().url()).hostname
return host === '127.0.0.1' || host === '[::1]' ? route.continue() : route.abort('blockedbyclient')
})
const previous = {
id: 'summary-before-27', user_id: 'reader-27', content: 'Synthèse précédente ticket 27.',
ai_provider_id: null, generated_at: '2026-11-23T10:00:00Z',
}
const summary = { ...previous, id: 'summary-after-27', content: 'Résultat terminal observable ticket 27.', generated_at: '2026-11-23T11:00:00Z' }
const outcome = {
id: 'outcome-27', status: 'succeeded', language: 'fr', ranking_mode: 'title',
total_candidates: 3, laya_selected: 2, selected: 2, global_limit: 10,
content_fallbacks: 0, source_stats: [], articles_removed_for_prompt_budget: 1,
}
let job = null
let posts = 0
let recentReads = 0
let jobReads = 0
let completedListReads = 0
let completedHistoryReads = 0
await page.route('**/api/**', async route => {
const path = new URL(route.request().url()).pathname
const method = route.request().method()
const respond = (data, status = 200) => route.fulfill({ status, json: { data } })
if (path === '/api/me/assets' && method === 'GET') return respond([{ id: 'asset-27', name: 'Entreprise test', symbol: 'TEST', user_id: 'reader-27' }])
if (path === '/api/me/preferences' && method === 'GET') return respond({ language: 'fr', ranking_mode: 'title', summary_model_id: 'model-27' })
if (path === '/api/me/summary-models') return respond([{ id: 'model-27', provider: 'Test', model: 'summary-27' }])
if (path === '/api/summaries/generate' && method === 'POST') {
posts++
job = { id: 'job-27', status: 'running', created_at: '2026-11-23T11:00:00Z', finished_at: null, summary_id: null, outcome: null }
return respond(job, 202)
}
if (path === '/api/summaries/generation-jobs/recent') {
recentReads++
return respond(job)
}
if (path === '/api/summaries/generation-jobs/job-27') {
jobReads++
return respond(job)
}
if (path === '/api/summaries') {
if (job?.status === 'succeeded') { completedListReads++; return respond([summary, previous]) }
return respond([previous])
}
if (path === '/api/summaries/generations') {
if (job?.status === 'succeeded') {
completedHistoryReads++
return respond([
{ id: outcome.id, summary_id: summary.id, outcome, created_at: summary.generated_at },
{ id: 'outcome-before-27', summary_id: previous.id, outcome: { ...outcome, id: 'outcome-before-27', articles_removed_for_prompt_budget: 2 }, created_at: previous.generated_at },
])
}
return respond([])
}
return route.continue()
})
const expectNoDiagnostics = async () => {
await expect(page.getByRole('region', { name: 'Bilan de la génération' })).toHaveCount(0)
await expect(page.getByText(/Prochaine synthèse|Budget souhaité|Budget effectif|Réserve de sortie|Détails de la génération|articles examinés|Contenus complets/)).toHaveCount(0)
await expect(page.getByLabel('Indices actuellement suivis')).toHaveCount(0)
await expect(page.getByText('Entreprise test', { exact: true })).toHaveCount(0)
}
await test.step('start one generation and keep the mocked job running', async () => {
await page.goto(new URL('/login', stack.frontendUrl).href)
await page.getByLabel('Adresse email').fill(stack.bootstrapAdmin.email)
await page.getByLabel('Mot de passe').fill(stack.bootstrapAdmin.password)
await page.getByRole('button', { name: 'Se connecter' }).click()
await expect(page).toHaveURL(new URL('/', stack.frontendUrl).href)
await expect(page.getByText(previous.content, { exact: true })).toBeVisible()
await expectNoDiagnostics()
await page.getByRole('button', { name: 'Générer une synthèse', exact: true }).click()
await expect(page.getByRole('status').filter({ hasText: 'Synthèse en cours.' })).toBeVisible()
expect(posts).toBe(1)
})
await test.step('navigate away and return while the same job is still running', async () => {
await page.getByRole('link', { name: 'Mes préférences', exact: true }).filter({ visible: true }).click()
await expect(page).toHaveURL(new URL('/preferences', stack.frontendUrl).href)
await expect(page.getByRole('heading', { name: 'Mes préférences', exact: true })).toBeVisible()
await expect(page.getByLabel('Modèle de synthèse', { exact: true })).toHaveValue('model-27')
await expect(page.getByText(/Budget effectif|Réserve de sortie|Capacité du modèle/)).toHaveCount(0)
await expect(page.getByText('Synthèse en cours. Le traitement peut prendre plusieurs minutes.', { exact: true })).toHaveCount(0)
const readsBeforeReturn = recentReads
await page.getByRole('link', { name: 'Synthèses', exact: true }).filter({ visible: true }).click()
await expect(page).toHaveURL(new URL('/', stack.frontendUrl).href)
await expect(page.getByRole('status').filter({ hasText: 'Synthèse en cours.' })).toBeVisible({ timeout: 3000 })
expect(recentReads).toBeGreaterThan(readsBeforeReturn)
expect(job.status).toBe('running')
const generate = page.getByRole('button', { name: 'Génération en cours', exact: true })
await expect(generate).toBeDisabled()
await expect(page.getByText(previous.content, { exact: true })).toBeVisible()
await expectNoDiagnostics()
// Observe an actual polling request while the job remains active.
const readsBeforePoll = jobReads
await expect.poll(() => jobReads, { timeout: 10_000 }).toBeGreaterThan(readsBeforePoll)
await expect(generate).toBeDisabled()
expect(posts).toBe(1)
})
await test.step('finish the job and observe the refreshed summary and history', async () => {
job = { ...job, status: 'succeeded', finished_at: summary.generated_at, summary_id: summary.id, outcome }
await expect(page.getByText('Nouvelle synthèse disponible.', { exact: true })).toBeVisible({ timeout: 10_000 })
await expect(page.getByText(summary.content, { exact: true })).toBeVisible()
await expect(page.getByRole('status').filter({ hasText: 'Synthèse en cours.' })).toHaveCount(0)
await expect(page.getByRole('button', { name: 'Générer une synthèse', exact: true })).toBeEnabled()
await expectNoDiagnostics()
await expect(page.getByText('1 article ancien a été retiré pour respecter le plafond du prompt.', { exact: true })).toBeVisible()
await expect(page.getByRole('heading', { name: 'Historique', exact: true })).toBeVisible()
await expect(page.getByRole('button', { name: `${previous.content}…`, exact: true })).toBeVisible()
await expect(page.getByText('2 articles parmi les plus anciens ont été retirés pour respecter le plafond du prompt.', { exact: true })).toBeVisible()
await page.getByRole('button', { name: `${previous.content}…`, exact: true }).click()
await expect(page.getByText(previous.content, { exact: true })).toBeVisible()
await expect(page.getByText('2 articles parmi les plus anciens ont été retirés pour respecter le plafond du prompt.', { exact: true })).toHaveCount(2)
await expectNoDiagnostics()
expect(completedListReads).toBeGreaterThan(0)
expect(completedHistoryReads).toBeGreaterThan(0)
expect(posts).toBe(1)
})
})