From 93970f361b831f17d48ff2a345fc78488ac71fba Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 23 Nov 2026 00:51:32 +0100 Subject: Phase 7: hardening and shipping (M7) E2E (Playwright, e2e/): - table fixture: one real compiled server per test (temp saves dir, free port, workers: 1); a Tab helper drives the real UI (menus, rows, initiative, draw, seats) over one WS per fresh browser context - full round: onboarding, secret initiative masked to ?? per connection, draw revealing on every screen, turns, next round, round-2 secrecy - seats: claim/re-claim on owner-leave, reload keeps the session and re-claims, two sessions can't own one character - resilience: a dropped tab resyncs on reconnect (no drift); a SIGKILL'd server restarts with state + seats surviving (incl. the owner's secret initiative and the enabled draw); a throttled tab converges on the same state as the fast tabs; a double-tapped draw runs once, error sent to the sender only - figure edit: set a character level + rename from the status menu Cross-check vs the reference (feature parity): - status menu: level pills (setCharacterLevel) + a name field (changeName), mirroring SetCharacterLevelMenu - ability-card removal: draw-pile-only "send to bottom" (reorderAbilityList -> index 0) + "shuffle undrawn cards" (shuffleDrawnAbilityCard), mirroring RemoveCardMenu - Add Character: already-added classes disabled (Objective/Escort may be added repeatedly), matching _characterAlreadyAdded - every remaining registry command verified reachable from the UI Session persistence (found by the restart test): - the client never learned its session id before the first welcome, so a reconnect minted a new id and orphaned the restored seat; fixed with establishSessionCookie() (a GET /session before the WS upgrade, plus a vite /session proxy) so the x-haven-session cookie survives restarts - the initiative input now prefills from re-synced state (CharacterRow effect, guarded against clobbering a live edit) so a reload/reconnect doesn't blank an entry Performance + deployment: - immutable asset caching (Cache-Control: 1yr, immutable) for the game data / images / in-game translations; no-cache for the SPA index.html so rebuilt apps reach open tabs - server tool/perf_measure.dart: the performance probe (state payload size at full-table scale, WS fan-out bytes per action, traffic per connection) - Dockerfile (server + built web); docs/DEPLOYMENT.md (Docker, persistence, reverse proxy, measured performance, smoke-test list) - README quickstart + production/Docker/testing sections Server: - --saves-dir and --data-dir CLI options (Docker / multi-game hosts), threaded through the stores and data loading Misc: - app.css: height 100% on .row so the turn-done tap zone resolves to a clickable height; fake-socket.ts readyState constants corrected to the real WebSocket values (0 CONNECTING, 1 OPEN) - scripts/ci.sh: the Playwright E2E gate (runs when PLAYWRIGHT is set) - PLAN.md: Phase 7 boxes checked, M7 progress-log entry Verification: svelte-check 0 errors; web 196/196 tests; vite build OK; dart analyze 0 errors / 0 warnings; dart test 604/604; Playwright 9/9; scripts/ci.sh green end-to-end. --- e2e/tests/figure-edit.spec.ts | 37 +++++++ e2e/tests/full-round.spec.ts | 91 +++++++++++++++++ e2e/tests/resilience.spec.ts | 231 ++++++++++++++++++++++++++++++++++++++++++ e2e/tests/seats.spec.ts | 82 +++++++++++++++ 4 files changed, 441 insertions(+) create mode 100644 e2e/tests/figure-edit.spec.ts create mode 100644 e2e/tests/full-round.spec.ts create mode 100644 e2e/tests/resilience.spec.ts create mode 100644 e2e/tests/seats.spec.ts (limited to 'e2e/tests') diff --git a/e2e/tests/figure-edit.spec.ts b/e2e/tests/figure-edit.spec.ts new file mode 100644 index 0000000..9b7bc9b --- /dev/null +++ b/e2e/tests/figure-edit.spec.ts @@ -0,0 +1,37 @@ +/** + * E2E: character level + rename from the status menu (the Phase 7 + * cross-check closed the `setCharacterLevel` / `changeName` gaps vs the + * reference `SetCharacterLevelMenu`). Drives the real UI and asserts the + * server round-trip re-renders on the tab. + */ +import { test, expect } from '../helpers/table.js'; + +test('a player can set a character level and rename it', async ({ table }) => { + const p1 = (await table.newTab()).tab; + await p1.open(table.server.url); + await p1.addCharacter('Hatchet'); + + // Open the status (stat) menu by tapping the row's name. + const dlg = await p1.openStatusMenu('Hatchet'); + + // Level pills 1..N are present; level 1 (the add default) is the current + // one, so its pill is highlighted and disabled. + await expect(dlg.locator('button.pill', { hasText: /^1$/ })).toBeDisabled(); + await expect(dlg.locator('button.pill', { hasText: /^1$/ })).toHaveClass(/pill--on/); + + // Set level 2: the server round-trip re-renders and the level-2 pill + // becomes the highlighted current one. + await dlg.locator('button.pill', { hasText: /^2$/ }).click(); + await expect(dlg.locator('button.pill', { hasText: /^2$/ })).toHaveClass(/pill--on/); + await expect(dlg.locator('button.pill', { hasText: /^2$/ })).toBeDisabled(); + await expect(dlg.locator('button.pill', { hasText: /^1$/ })).toBeEnabled(); + + // Rename via the name field: submit on Enter, the row re-renders with the + // new display name. (Use a name with no overlap with "Hatchet" so the + // substring-matched row helpers read the old and new names distinctly.) + const name = dlg.locator('input.search'); + await name.fill('Molly'); + await name.press('Enter'); + await expect(p1.row('Molly')).toBeVisible(); + await expect(p1.row('Hatchet')).toHaveCount(0); +}); diff --git a/e2e/tests/full-round.spec.ts b/e2e/tests/full-round.spec.ts new file mode 100644 index 0000000..6e3c7e7 --- /dev/null +++ b/e2e/tests/full-round.spec.ts @@ -0,0 +1,91 @@ +/** + * E2E: two players + one spectator play a full round in real browser tabs + * (PLAN.md Phase 7). + * + * Covers: the onboarding flow (a fresh spectator becomes a player by adding + * a character), secret initiative (server-masked per connection), the draw + * (reveal on every screen), turns, and the next round. + */ +import { test, expect } from '../helpers/table.js'; + +test('full round: setup, secret initiative, draw, turns, next round', async ({ + table +}) => { + // --- Player 1 joins the empty table and becomes a player --------------- + const p1 = (await table.newTab()).tab; + await p1.open(table.server.url); + // A fresh session is a spectator: the draw button is disabled and the + // scenario menu is gated. Adding a character is the onboarding path. + expect(await p1.drawEnabled()).toBe(false); + await p1.addCharacter('Hatchet'); + expect(await p1.drawEnabled()).toBe(true); + + // Set the scenario now that p1 is a player. + await p1.selectScenario('#1 Roadside Ambush'); + + // --- Player 2 and a spectator join ------------------------------------- + const p2 = (await table.newTab()).tab; + await p2.open(table.server.url); + await p2.addCharacter('Voidwarden'); + + const spec = (await table.newTab()).tab; + await spec.open(table.server.url); + await spec.row('Hatchet').waitFor({ timeout: 15_000 }); + await spec.row('Voidwarden').waitFor({ timeout: 15_000 }); + // The spectator sees both rows read-only: no inputs, no claim chips + // (both characters are claimed), and a disabled draw button. + expect(await spec.drawEnabled()).toBe(false); + expect(await spec.claimChip('Hatchet').count()).toBe(0); + expect(await spec.claimChip('Voidwarden').count()).toBe(0); + expect(await spec.hasInitiativeInput('Hatchet')).toBe(false); + + // --- Secret initiative --------------------------------------------------- + // Player 1 enters 4. Player 1 sees an editable input with the value; + // player 2 and the spectator receive `null` from the server and see `??`. + await p1.setInitiative('Hatchet', 4); + expect(await p1.hasInitiativeInput('Hatchet')).toBe(true); + await expect(p2.initDisplay('Hatchet')).toHaveText('??'); + await expect(p2.initDisplay('Hatchet')).toHaveClass(/init__secret/); + await expect(spec.initDisplay('Hatchet')).toHaveText('??'); + + // Player 2 enters 2; player 1 sees `??` for Voidwarden in turn. + await p2.setInitiative('Voidwarden', 2); + await expect(p1.initDisplay('Voidwarden')).toHaveText('??'); + await expect(spec.initDisplay('Voidwarden')).toHaveText('??'); + + // --- Draw: every screen flips to playTurns at once ----------------------- + await p1.clickDraw(); + await p1.waitForDrawn(); + await p2.waitForDrawn(); + await spec.waitForDrawn(); + + // Initiative is now public on every screen. + await expect(p1.initDisplay('Hatchet')).toHaveText('4'); + await expect(p1.initDisplay('Voidwarden')).toHaveText('2'); + await expect(p2.initDisplay('Hatchet')).toHaveText('4'); + await expect(p2.initDisplay('Voidwarden')).toHaveText('2'); + await expect(spec.initDisplay('Hatchet')).toHaveText('4'); + await expect(spec.initDisplay('Voidwarden')).toHaveText('2'); + // … and no initiative inputs remain anywhere. + expect(await p1.hasInitiativeInput('Hatchet')).toBe(false); + expect(await p2.hasInitiativeInput('Voidwarden')).toBe(false); + expect(await spec.hasInitiativeInput('Hatchet')).toBe(false); + + // --- Turns --------------------------------------------------------------- + await p1.turnDone('Hatchet'); + await p2.turnDone('Voidwarden'); + + // --- Next round: round 2, initiative secret again ------------------------ + await p1.clickDraw(); // "Next Round" + await p1.waitForRoundChosen(); + await p2.waitForRoundChosen(); + await spec.waitForRoundChosen(); + await expect(p1.roundLabel()).toHaveText(/Round 2\//); + await expect(p2.roundLabel()).toHaveText(/Round 2\//); + await expect(spec.roundLabel()).toHaveText(/Round 2\//); + // The round-2 values are secret again; only the owner's input is enabled. + await expect(p2.initDisplay('Hatchet')).toHaveText('??'); + await expect(spec.initDisplay('Hatchet')).toHaveText('??'); + expect(await p1.hasInitiativeInput('Hatchet')).toBe(true); + expect(await p2.hasInitiativeInput('Hatchet')).toBe(false); +}); diff --git a/e2e/tests/resilience.spec.ts b/e2e/tests/resilience.spec.ts new file mode 100644 index 0000000..27e7a18 --- /dev/null +++ b/e2e/tests/resilience.spec.ts @@ -0,0 +1,231 @@ +/** + * E2E: resilience (PLAN.md Phase 7). + * + * * a tab that drops out (offline) resyncs on reconnect — no drift + * * a SIGKILL'd server restarts mid-game: state AND seats survive, secret + * initiative stays masked for non-owners, the game continues + * * a throttled (slow) tab converges on the same state as the fast tabs + * * a double-tapped draw executes exactly once; only the sender gets the + * error toast + * + * Note on the initiative cell markup: during chooseInitiative the OWNER + * renders an `` (no `.init` div); every other connection renders the + * `.init` div — `??` while the server masks the value, the number after the + * draw. + */ +import type { Page } from '@playwright/test'; +import { test, expect } from '../helpers/table.js'; +import { startServer } from '../helpers/server.js'; +import { Tab } from '../helpers/tab.js'; + +/** ~1 s one-way latency, moderate throughput (CDP network emulation). */ +async function throttle(page: Page) { + const cdp = await page.context().newCDPSession(page); + await cdp.send('Network.emulateNetworkConditions', { + offline: false, + latency: 1000, + downloadThroughput: 500_000, + uploadThroughput: 500_000 + }); +} + +test.describe('Resilience', () => { + test('a dropped tab resyncs when it comes back (no drift)', async ({ + table + }) => { + const p1 = (await table.newTab()).tab; + const p2 = (await table.newTab()).tab; + const specOpen = await table.newTab(); + const spec = specOpen.tab; + const specContext = specOpen.context; + await p1.open(table.server.url); + await p1.addCharacter('Hatchet'); + await p1.selectScenario('#1 Roadside Ambush'); + await p2.open(table.server.url); + await p2.addCharacter('Voidwarden'); + await spec.open(table.server.url); + await spec.row('Hatchet').waitFor({ timeout: 15_000 }); + await spec.row('Voidwarden').waitFor({ timeout: 15_000 }); + + // The spectator's network drops and its tab is lost. Closing the page + // reliably severs the WebSocket (CDP offline emulation alone does not + // tear down an already-established socket). + await specContext.setOffline(true); + await spec.closePage(); + + // The game moves on without it. + await p1.setInitiative('Hatchet', 4); + await p2.setInitiative('Voidwarden', 2); + await p1.clickDraw(); + await p1.waitForDrawn(); + await p2.waitForDrawn(); + + // Back online: a fresh page in the same session (cookie) re-sends + // `hello`, and the full `welcome` overwrites the stale snapshot. + await specContext.setOffline(false); + const spec2 = new Tab(await specContext.newPage()); + await spec2.open(table.server.url); + await spec2.waitForDrawn(); + await expect(spec2.page.locator('.banner[role="status"]')).toBeHidden(); + await expect(spec2.initDisplay('Hatchet')).toHaveText('4'); + await expect(spec2.initDisplay('Voidwarden')).toHaveText('2'); + await expect(spec2.roundLabel()).toHaveText(/Round 1\//); + }); + + test('a SIGKILL’d server restarts mid-game: state and seats survive', async ({ + table + }) => { + const p1 = (await table.newTab()).tab; + const p2 = (await table.newTab()).tab; + const spec = (await table.newTab()).tab; + await p1.open(table.server.url); + await p1.addCharacter('Hatchet'); + await p1.selectScenario('#1 Roadside Ambush'); + await p2.open(table.server.url); + await p2.addCharacter('Voidwarden'); + await spec.open(table.server.url); + await spec.row('Hatchet').waitFor({ timeout: 15_000 }); + + // Mid chooseInitiative: p1 has entered 4 (secret), p2 has not. + await p1.setInitiative('Hatchet', 4); + await expect(p2.initDisplay('Hatchet')).toHaveText('??'); + await expect(spec.initDisplay('Hatchet')).toHaveText('??'); + + // Crash the server (SIGKILL — no graceful shutdown). + await table.server.killAndWait(); + + // Every tab notices the drop and starts reconnecting. + await expect(p1.page.locator('.banner[role="status"]')).toBeVisible(); + await expect(spec.page.locator('.banner[role="status"]')).toBeVisible(); + + // Restart the same server: same port, same saves dir. + const restarted = await startServer({ + port: table.server.port, + savesDir: table.server.savesDir + }); + + try { + // All tabs auto-reconnect and resync. + await p1.page + .locator('.banner[role="status"]') + .waitFor({ state: 'detached', timeout: 30_000 }); + await spec.page + .locator('.banner[role="status"]') + .waitFor({ state: 'detached', timeout: 30_000 }); + + // State survived the crash… + await expect(p1.roundLabel()).toHaveText(/Round 1\//); + await expect(spec.roundLabel()).toHaveText(/Round 1\//); + + // …including the secret entry: p1 (the seat owner) still has the + // editable input with the value; p2 and the spectator see the mask. + // The seats broadcast lands a beat after the state, so auto-wait. + await expect( + p1.row('Hatchet').locator('input.init__input') + ).toHaveCount(1); + await expect(p1.row('Hatchet').locator('input.init__input')).toHaveValue( + '4' + ); + await expect(p2.initDisplay('Hatchet')).toHaveText('??'); + await expect(spec.initDisplay('Hatchet')).toHaveText('??'); + + // Seats survived too: p1 is still a player (can draw) and the + // character is not claimable by p2. + await expect(p1.page.locator('button.drawbtn')).toBeEnabled(); + await expect(p2.claimChip('Hatchet')).toHaveCount(0); + + // And the game continues exactly where it left off. + await p2.setInitiative('Voidwarden', 2); + await p2.clickDraw(); + await p1.waitForDrawn(); + await p2.waitForDrawn(); + await spec.waitForDrawn(); + await expect(p1.initDisplay('Hatchet')).toHaveText('4'); + await expect(spec.initDisplay('Voidwarden')).toHaveText('2'); + } finally { + await restarted.kill(); + } + }); + + test('a throttled tab converges on the same state as the fast tabs', async ({ + table + }) => { + const p1 = (await table.newTab()).tab; + const p2 = (await table.newTab()).tab; + const specOpen = await table.newTab(); + const spec = specOpen.tab; + + // Slow the spectator down ~1 s one-way BEFORE the page (and its WS + // connection) is established. + await throttle(specOpen.page); + await spec.open(table.server.url); + + // The fast tabs run a full round while the spectator lags behind. + await p1.open(table.server.url); + await p1.addCharacter('Hatchet'); + await p1.selectScenario('#1 Roadside Ambush'); + await p2.open(table.server.url); + await p2.addCharacter('Voidwarden'); + await p1.setInitiative('Hatchet', 4); + await p2.setInitiative('Voidwarden', 2); + await p1.clickDraw(); + await p1.waitForDrawn(); + await p2.waitForDrawn(); + + // The full-snapshot protocol means the slow tab lands on the final + // state — no drift, no gaps. + await spec.row('Hatchet').waitFor({ timeout: 30_000 }); + await spec.row('Voidwarden').waitFor({ timeout: 30_000 }); + await spec.waitForDrawn(); + await expect(spec.page.locator('.banner[role="status"]')).toBeHidden(); + await expect(spec.initDisplay('Hatchet')).toHaveText('4'); + await expect(spec.initDisplay('Voidwarden')).toHaveText('2'); + await expect(spec.roundLabel()).toHaveText(/Round 1\//); + }); + + test('a double-tapped draw runs once; only the sender sees the error', async ({ + table + }) => { + const p1Open = await table.newTab(); + const p1 = p1Open.tab; + const p2 = (await table.newTab()).tab; + const spec = (await table.newTab()).tab; + await p1.open(table.server.url); + await p1.addCharacter('Hatchet'); + await p1.selectScenario('#1 Roadside Ambush'); + await p2.open(table.server.url); + await p2.addCharacter('Voidwarden'); + await spec.open(table.server.url); + await spec.row('Hatchet').waitFor({ timeout: 15_000 }); + + await p1.setInitiative('Hatchet', 4); + await p2.setInitiative('Voidwarden', 2); + + // Throttle p1 so the first tap’s update cannot re-render the button + // before the second tap lands — both taps send `draw`. + await throttle(p1Open.page); + await p1.page.locator('button.drawbtn').click({ clickCount: 2 }); + + // The table drew exactly once… + await p1.waitForDrawn(); + await p2.waitForDrawn(); + await spec.waitForDrawn(); + await expect(p2.roundLabel()).toHaveText(/Round 1\//); + await expect(spec.roundLabel()).toHaveText(/Round 1\//); + + // …and the losing tap is reported to the sender only. (The other tabs + // may still show the broadcast round-start scenario message — a + // legitimate toast — but never the sender-only error.) + await expect(p1.toast()).toContainText('already been drawn'); + await expect( + p2.page.locator('.toast[role="status"]', { + hasText: 'already been drawn' + }) + ).toHaveCount(0); + await expect( + spec.page.locator('.toast[role="status"]', { + hasText: 'already been drawn' + }) + ).toHaveCount(0); + }); +}); diff --git a/e2e/tests/seats.spec.ts b/e2e/tests/seats.spec.ts new file mode 100644 index 0000000..432069a --- /dev/null +++ b/e2e/tests/seats.spec.ts @@ -0,0 +1,82 @@ +/** + * E2E: seat claiming (PLAN.md Phase 7 / section 3.3). + * + * * a disconnected owner's character becomes claimable (the Claim chip) + * * claiming grants ownership on every screen (inputs enable) + * * a page reload keeps the session (cookie) but the seat must be re-claimed + * (the server releases seats on disconnect — the Phase 2 behaviour) + */ +import { test, expect } from '../helpers/table.js'; + +test.describe('Seats: claim / re-claim / reload', () => { + test('owner leaves → seat released → another session claims', async ({ + table + }) => { + const p1 = (await table.newTab()).tab; + await p1.open(table.server.url); + await p1.addCharacter('Hatchet'); + expect(await p1.hasInitiativeInput('Hatchet')).toBe(true); + + const p2 = (await table.newTab()).tab; + await p2.open(table.server.url); + await p2.row('Hatchet').waitFor({ timeout: 15_000 }); + // While p1 is connected the seat is taken: no claim chip. + expect(await p2.claimChip('Hatchet').count()).toBe(0); + + // p1 leaves the table (tab closed → the connection drops). + await p1.closePage(); + // The seat is released and broadcast: p2 now sees the claim chip. + await p2.claimChip('Hatchet').waitFor({ timeout: 15_000 }); + + await p2.claim('Hatchet'); + // p2 is the owner now: the chip is gone, inputs enable, and the draw + // button unlocks (a claimed seat makes p2 a player). The claim is a + // server round-trip (seats broadcast → re-render), so these auto-wait. + await expect(p2.claimChip('Hatchet')).toHaveCount(0); + await expect(p2.row('Hatchet').locator('input.init__input')).toHaveCount(1); + await expect(p2.page.locator('button.drawbtn')).toBeEnabled(); + }); + + test('a page reload keeps the session, and the seat is re-claimed', async ({ + table + }) => { + const p1 = (await table.newTab()).tab; + await p1.open(table.server.url); + await p1.addCharacter('Hatchet'); + expect(await p1.hasInitiativeInput('Hatchet')).toBe(true); + + // Reload the same tab (same context → the session cookie survives). + await p1.page.reload(); + await p1.waitForConnected(); + await p1.row('Hatchet').waitFor({ timeout: 15_000 }); + + // The disconnect released the seat: the character is claimable again, + // and the (reconnected) session claims it with one tap. + await p1.claimChip('Hatchet').waitFor({ timeout: 15_000 }); + await p1.claim('Hatchet'); + // The claim round-trip (seats broadcast → re-render) is async. + await expect(p1.row('Hatchet').locator('input.init__input')).toHaveCount(1); + await expect(p1.claimChip('Hatchet')).toHaveCount(0); + }); + + test('two sessions cannot own one character at once', async ({ table }) => { + const p1 = (await table.newTab()).tab; + await p1.open(table.server.url); + await p1.addCharacter('Hatchet'); + + // p1 leaves, p2 claims, p1 comes back and tries to re-claim the same + // character — the server rejects it (it is claimed by p2). + const p2 = (await table.newTab()).tab; + await p2.open(table.server.url); + await p2.row('Hatchet').waitFor({ timeout: 15_000 }); + await p1.closePage(); + await p2.claimChip('Hatchet').waitFor({ timeout: 15_000 }); + await p2.claim('Hatchet'); + + const p1Again = (await table.newTab()).tab; + await p1Again.open(table.server.url); + await p1Again.row('Hatchet').waitFor({ timeout: 15_000 }); + // p2 holds the seat: no claim chip for p1Again. + expect(await p1Again.claimChip('Hatchet').count()).toBe(0); + }); +}); -- cgit v1.3.1