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: [], } 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 }]) } return respond([]) } return route.continue() }) 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 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.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() // 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 expect(page.getByRole('region', { name: 'Bilan de la génération' })).toContainText('3 articles examinés') await expect(page.getByRole('heading', { name: 'Historique', exact: true })).toBeVisible() await expect(page.getByRole('button', { name: `${previous.content}…`, exact: true })).toBeVisible() expect(completedListReads).toBeGreaterThan(0) expect(completedHistoryReads).toBeGreaterThan(0) expect(posts).toBe(1) }) })