32 lines
1.5 KiB
TypeScript
32 lines
1.5 KiB
TypeScript
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
|
|
|
|
export async function mintPlayerSession(page: Page): Promise<string> {
|
|
const res = await page.request.post('/api/test/mint-player-session');
|
|
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<void> {
|
|
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<string> {
|
|
const sessionId = await mintPlayerSession(page);
|
|
await applySession(page, sessionId);
|
|
return sessionId;
|
|
}
|