import type { Page } from '@playwright/test'; import { expect } from '@playwright/test'; // v1.1.0 (ISS-107) — session helpers for the e2e suite. Every game route is now session-guarded, so // each spec authenticates in a beforeEach: mint a player session server-side via the test hook // (FLIGHT_TEST_HOOKS=1; no real credentials in test code), then apply it BOTH ways the client uses — // localStorage for the app's own fetches (lib/authClient.ts reads it) and context extraHTTPHeaders // so page.request.* calls in specs carry X-Flight-Session too. export const SESSION_KEY = 'flight.session'; // must match lib/authClient.ts // v1.7.0 (ISS-116): optional username — mints a SECOND account with onboarding (the route has // supported the override since v1.1.0; this helper just never forwarded it). No arg = the suite's // usual local player, exactly as before. export async function mintPlayerSession(page: Page, username?: string): Promise { const res = await page.request.post('/api/test/mint-player-session', { data: username ? { username } : {} }); expect(res.status(), 'mint-player-session hook should be enabled in the CI stack (FLIGHT_TEST_HOOKS=1)').toBe(200); return ((await res.json()) as { sessionId: string }).sessionId; } export async function applySession(page: Page, sessionId: string): Promise { await page.context().setExtraHTTPHeaders({ 'X-Flight-Session': sessionId }); // Runs before every navigation's scripts — the app reads the session from localStorage on load. await page.addInitScript( ([key, sid]) => window.localStorage.setItem(key, sid), [SESSION_KEY, sessionId], ); } export async function authenticate(page: Page): Promise { const sessionId = await mintPlayerSession(page); await applySession(page, sessionId); return sessionId; }