diff --git a/README.md b/README.md index b67a4b6..2c2c11f 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Checks: ```bash npm test # 95 tests: evaluator, policies, store, fault injection, concurrency, HTTP, flows npm run typecheck # server, tests and UI -npm run test:e2e # 9 Playwright tests: judge journey against the real server (run `npx playwright install chromium` once) +npm run test:e2e # 10 Playwright tests: judge journey against the real server (run `npx playwright install chromium` once) # E2E_BASE_URL=https://… npm run test:e2e runs the same journey as a smoke test against a deployed URL npm run spike:sqlite ``` diff --git a/e2e/judge.spec.ts b/e2e/judge.spec.ts index 22f590a..cf494f5 100644 --- a/e2e/judge.spec.ts +++ b/e2e/judge.spec.ts @@ -113,3 +113,25 @@ test('@mobile renders without horizontal scroll and shows the verdict', async ({ const overflow = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth); expect(overflow).toBeLessThanOrEqual(0); }); + +test('first load: a late session response cannot split the page across two sessions', async ({ page }) => { + // Reproduces the live-network race: boot requests and the scenario load each start a session; if the older + // session's Set-Cookie lands after the scenario was created, later requests read a session that does not own it. + let release!: () => void; + const scenarioCreated = new Promise((r) => { release = r; }); + let posts = 0; + await page.route('**/api/session', async (route) => { + const hold = route.request().method() === 'POST' && posts++ === 0; + const response = await route.fetch(); + if (hold) await Promise.race([scenarioCreated, new Promise((r) => setTimeout(r, 3000))]); + await route.fulfill({ response }); + }); + await page.route('**/api/scenarios/*', async (route) => { + const response = await route.fetch(); + await route.fulfill({ response }); + if (route.request().method() === 'POST') release(); + }); + await page.goto('/'); + await expectTruthful(page, 'DEFER'); + await expect(page.getByRole('alert')).toHaveCount(0); +}); diff --git a/ui/src/api.ts b/ui/src/api.ts index ed4ed95..dba5092 100644 --- a/ui/src/api.ts +++ b/ui/src/api.ts @@ -6,7 +6,10 @@ export class ApiError extends Error { } let sessionPromise: Promise | null = null; -export const newSession = () => (sessionPromise = fetch('/api/session', { method: 'POST' }).then(() => undefined)); +// Sessions are created one after another: two concurrent POSTs let the older response's Set-Cookie land last, +// so later requests would carry a session that does not own the proposal the caller just created. +export const newSession = () => (sessionPromise = (sessionPromise ?? Promise.resolve()).catch(() => {}) + .then(() => fetch('/api/session', { method: 'POST' })).then(() => undefined)); export async function api(method: string, path: string, body?: unknown, retried = false): Promise { if (!sessionPromise) newSession();