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. --- web/src/App.svelte | 8 +- web/src/app.css | 5 ++ web/src/lib/protocol/connection.ts | 6 +- web/src/lib/protocol/session.ts | 25 +++++++ web/src/lib/ui/CharacterRow.svelte | 20 ++++- web/src/lib/ui/StatusMenu.svelte | 106 +++++++++++++++++++++++++++ web/src/lib/ui/decks/AbilityCardsMenu.svelte | 29 ++++++++ web/src/lib/ui/menus/AddCharacterMenu.svelte | 51 +++++++++++-- web/test/connection.test.ts | 16 ++++ web/test/fake-socket.ts | 7 +- web/vite.config.ts | 3 + 11 files changed, 260 insertions(+), 16 deletions(-) create mode 100644 web/src/lib/protocol/session.ts (limited to 'web') diff --git a/web/src/App.svelte b/web/src/App.svelte index bdeb998..814a47a 100644 --- a/web/src/App.svelte +++ b/web/src/App.svelte @@ -13,6 +13,7 @@ import { onMount, onDestroy } from 'svelte'; import { get } from 'svelte/store'; import { GameSession } from '$lib/state/session'; + import { establishSessionCookie } from '$lib/protocol/session'; import { clientSettings, styleTheme } from '$lib/settings'; import { gameData, type CampaignData, type MonsterData } from '$lib/data/gameData'; import { @@ -57,7 +58,12 @@ const session = new GameSession(); - onMount(() => session.connect()); + // Establish the persistent session cookie before the WS connects, so the + // upgrade carries it and the seat survives reloads and server restarts. + onMount(async () => { + await establishSessionCookie(); + session.connect(); + }); onDestroy(() => session.disconnect()); const gameState = session.state; diff --git a/web/src/app.css b/web/src/app.css index 8f3ad54..e66b09e 100644 --- a/web/src/app.css +++ b/web/src/app.css @@ -254,6 +254,11 @@ button { display: flex; align-items: center; width: 100%; + /* A definite height so the row's full-height children (the art block and + the right-hand initiative/turn-done tap zone) resolve `height: 100%` + to something clickable — with an auto height the tap zone collapsed + to 0px and could never be tapped. */ + height: 100%; } .row__art { diff --git a/web/src/lib/protocol/connection.ts b/web/src/lib/protocol/connection.ts index a553481..cbf86f5 100644 --- a/web/src/lib/protocol/connection.ts +++ b/web/src/lib/protocol/connection.ts @@ -47,7 +47,8 @@ export interface Transport { onmessage: ((ev: { data: unknown }) => void) | null; send(data: string): void; close(): void; - readyState: number; // 0 OPEN + /** `WebSocket.OPEN` — 1. (0 is CONNECTING; 2 CLOSING; 3 CLOSED.) */ + readyState: number; } export interface ConnectionOptions { @@ -62,7 +63,8 @@ export interface ConnectionOptions { pingIntervalMs?: number; } -const OPEN = 0; +/** `WebSocket.OPEN` — the socket is connected and can carry frames. */ +const OPEN = 1; export class GameConnection { status: ConnectionStatus = 'connecting'; diff --git a/web/src/lib/protocol/session.ts b/web/src/lib/protocol/session.ts new file mode 100644 index 0000000..1025288 --- /dev/null +++ b/web/src/lib/protocol/session.ts @@ -0,0 +1,25 @@ +/** + * Establish the persistent session cookie (PLAN.md section 3.3) before the + * game WebSocket connects. + * + * The `/session` endpoint mints a session id on the first call and sets the + * `x-haven-session` cookie (HttpOnly). The browser then carries it on every + * same-origin request — including the `/ws` upgrade — so a player keeps the + * same session id across page reloads *and* server restarts. That is what + * lets the server's restored seats (persisted and keyed by session id) land + * back on the right player after a crash; without the cookie the upgrade + * would mint a fresh session and the restored seat would be orphaned. + * + * The id also arrives in the `welcome` message, but that is too late to seed + * the cookie the upgrade depends on — hence this explicit bootstrap call. + */ +export async function establishSessionCookie(): Promise { + try { + // `same-origin` credentials send/receive the cookie; the response + // sets it if the browser has none yet, and is a no-op set otherwise. + await fetch('/session', { credentials: 'same-origin' }); + } catch { + // Offline or blocked: the WS upgrade will still (re)mint a session; + // the tab just starts as a fresh session instead of a restored one. + } +} diff --git a/web/src/lib/ui/CharacterRow.svelte b/web/src/lib/ui/CharacterRow.svelte index 043c225..b4f9b48 100644 --- a/web/src/lib/ui/CharacterRow.svelte +++ b/web/src/lib/ui/CharacterRow.svelte @@ -73,6 +73,16 @@ let initInput = $state(null); let initDraft = $state(''); + // Seed the editable field from (re)synced state so a reload or reconnect + // doesn't blank an entry we already made. Skip while the field is focused + // (clicking it clears it for re-entry) so we never clobber a live edit. + $effect(() => { + const v = cs.initiative; + if (v == null || v <= 0) return; + if (initInput && document.activeElement === initInput) return; + if (String(v) !== initDraft) initDraft = String(v); + }); + // `??` for a value masked by the server; the revealed number otherwise. const initDisplay = $derived( cs.initiative === null ? '??' : cs.initiative > 0 ? String(cs.initiative) : '' @@ -223,11 +233,13 @@ }} > - -{#if unclaimed} - -{/if} + + {#if unclaimed} + + {/if} + diff --git a/web/src/lib/ui/decks/AbilityCardsMenu.svelte b/web/src/lib/ui/decks/AbilityCardsMenu.svelte index 0cf416c..d1aae99 100644 --- a/web/src/lib/ui/decks/AbilityCardsMenu.svelte +++ b/web/src/lib/ui/decks/AbilityCardsMenu.svelte @@ -211,6 +211,11 @@ {#if removeTarget} {@const rt = removeTarget} {@const card = fullCard(rt)} + + {@const inDrawPile = deck.drawPile.some((r) => r.nr === rt.nr)} + {@const drawPileIndex = deck.drawPile.findIndex((r) => r.nr === rt.nr)} {/if} diff --git a/web/src/lib/ui/menus/AddCharacterMenu.svelte b/web/src/lib/ui/menus/AddCharacterMenu.svelte index 8383f13..06a24f8 100644 --- a/web/src/lib/ui/menus/AddCharacterMenu.svelte +++ b/web/src/lib/ui/menus/AddCharacterMenu.svelte @@ -7,6 +7,7 @@ @@ -74,9 +113,9 @@
{t('web:level')} - + {level} - +
{#if campaignData === null} @@ -87,7 +126,7 @@