const assert = require('node:assert/strict'); const waiting = { id: '00000000-0000-4000-8000-000000000001', title: 'Correct the workflow', kind: 'workflow_change', state: 'pending', needs: 'owner', actions: [], created_at: '2026-01-01T00:00:00Z', }; // Keep the last successful result and protect its tab from competing reads. module.exports = async ({ page, go, shot, tolerate }) => { const now = new Date(); await page.clock.install({ time: now }); await page.clock.pauseAt(new Date(now.getTime() + 60_000)); tolerate(/503.*\/api\/proposals/); const pendingURL = '**/api/proposals?state=pending'; let current = waiting; let failNext; await page.route(pendingURL, async route => { if (failNext) { const failing = failNext; failNext = undefined; await failing; await route.fulfill({ status: 503, contentType: 'application/problem+json', json: { detail: 'Could not refresh proposals' } }); return; } await route.fulfill({ json: [current] }); }); await go('/proposals'); const proposal = page.getByRole('link', { name: waiting.title, exact: true }); const sort = page.getByRole('columnheader', { name: 'Proposal', exact: true }); await proposal.waitFor(); await sort.getByRole('button').press('Enter'); await proposal.focus(); const failure = Promise.withResolvers(); failNext = failure.promise; const refresh = page.waitForRequest(pendingURL); await page.clock.runFor(5000); await refresh; assert.equal(await proposal.isVisible(), true, 'the row should stay visible during a refresh'); failure.resolve(); const error = page.getByRole('alert').filter({ hasText: 'Could not refresh proposals' }); await error.waitFor(); assert.equal(await proposal.isVisible(), true, 'a failed refresh must not replace the table'); assert.equal(await sort.getAttribute('aria-sort'), 'ascending', 'the table should keep the operator’s sort'); assert.equal(await proposal.evaluate(link => link === document.activeElement), true, 'the row should keep keyboard focus'); await shot('failed-refresh-retains-table'); current = { ...waiting, title: 'Workflow correction updated' }; await error.getByRole('button', { name: 'Retry' }).click(); await page.getByRole('link', { name: current.title, exact: true }).waitFor(); await error.waitFor({ state: 'hidden' }); assert.equal(await sort.getAttribute('aria-sort'), 'ascending'); // A failed read belongs to its filter, not to the newly selected tab. failNext = Promise.resolve(); await page.clock.runFor(5000); await error.waitFor(); const published = Promise.withResolvers(); await page.route('**/api/proposals?state=published', async route => { await published.promise; await route.fulfill({ json: [] }); }); const publishedTab = page.getByRole('tab', { name: 'Published' }); await publishedTab.click(); const loading = page.getByRole('status', { name: 'Loading proposals' }); assert.equal(await loading.isVisible(), true, 'a new tab must not inherit the previous tab’s error'); assert.equal(await error.isVisible(), false); assert.equal(await page.getByRole('table').count(), 0, 'rows from the previous tab must not appear'); published.resolve(); const empty = page.getByText('No proposals', { exact: true }); await empty.waitFor(); // A slow read must still take effect after a background poll ticks. const slow = Promise.withResolvers(); const stale = Promise.withResolvers(); let first = true; await page.route(pendingURL, async route => { const read = first ? slow : stale; first = false; await read.promise; await route.fulfill({ json: [waiting] }); }); const switched = page.waitForRequest(pendingURL); await page.getByRole('tab', { name: 'Waiting' }).click(); await switched; await page.clock.runFor(5000); slow.resolve(); await proposal.waitFor({ timeout: 3000 }).catch(() => { assert.fail('a slow read must render its result even after a background poll ticks'); }); // A late response for Waiting cannot replace the Published result. const nextPoll = page.waitForRequest(pendingURL); await page.clock.runFor(5000); await nextPoll; await publishedTab.click(); await empty.waitFor(); const response = page.waitForResponse(pendingURL); stale.resolve(); await (await response).finished(); await page.clock.runFor(50); assert.equal(await proposal.isVisible(), false, 'a response for the previous tab must not replace the current one'); assert.equal(await empty.isVisible(), true); };