From 4f50f2008f4d7c2a635f7f26861fcfc7f7504423 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sun, 15 Nov 2026 13:45:21 +0100 Subject: Phase 3: web UI core loop (M3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Browser game UI on top of the Phase 2 protocol: server-driven, input-only clients with per-connection secret visibility. Web (Svelte 5): - Layout: TopBar (element buttons with use/imbue long-press, title, menu), MainList (character/monster/note rows), BottomBar (draw/next-round + level widget with T/H/X/C values). - CharacterRow with the secret initiative input: own value editable, server-masked values render as ??, revealed on draw. - Per-figure StatusMenu: health/max/xp/bless/curse/empower/enfeeble steppers, condition grid, turn-done — all server commands, player-gated. - Menu dialogs: select scenario, add section, add/remove character, set level/difficulty/solo/auto-level, add/remove monster, note row, action log with rollback, settings (style theme + language). - Player vs spectator UI gated on the live seat table; reconnecting banner, error toasts, connection status plumbing. - i18n: English ported, locale store with localStorage persistence. - Design tokens + the three style themes (frosthaven/gloomhaven/original). - Unit tests: level values, game logic (elements/conditions/art/stats), i18n, the reactive player store, the data client (sections keyed by scenario number), plus the existing connection/protocol/state tests. Server: - Serve the engine's data JSON at /data/ and artwork at /images/ so the client menus and rows read the same assets the engine loads. --- web/test/connection.test.ts | 196 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 web/test/connection.test.ts (limited to 'web/test/connection.test.ts') diff --git a/web/test/connection.test.ts b/web/test/connection.test.ts new file mode 100644 index 0000000..27958ba --- /dev/null +++ b/web/test/connection.test.ts @@ -0,0 +1,196 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; +import { GameConnection } from '$lib/protocol/connection'; +import { FakeSocket, SocketSequence } from './fake-socket'; + +function welcome(overrides: Record = {}) { + return { + t: 'welcome', + v: 1, + session: 's1', + role: 'spectator', + index: 0, + description: null, + event: null, + state: { currentList: [] }, + seats: {}, + ...overrides + }; +} + +describe('GameConnection', () => { + let sockets: SocketSequence; + let conn: GameConnection; + + beforeEach(() => { + vi.useFakeTimers(); + sockets = new SocketSequence(); + conn = new GameConnection({ + transport: sockets.factory, + url: 'ws://test/ws', + reconnectBaseMs: 100, + reconnectMaxMs: 400, + pingIntervalMs: 1_000 + }); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it('handshakes with hello and records the session from welcome', () => { + const statuses: string[] = []; + conn.onStatus = (s) => statuses.push(s); + conn.connect(); + sockets.current.open(); + expect(sockets.current.lastJson()).toEqual({ t: 'hello', v: 1 }); + expect(conn.status).toBe('open'); + + let welcomed: unknown = null; + conn.onMessage = (m) => (welcomed = m); + sockets.current.serverSend(welcome({ session: 'abc' })); + expect(conn.session).toBe('abc'); + expect(welcomed).toMatchObject({ t: 'welcome' }); + expect(statuses).toContain('open'); + }); + + it('forwards update, seats, and error to their handlers', () => { + const messages: string[] = []; + let errorMessage: string | null = null; + conn.onMessage = (m) => messages.push(m.t); + conn.onError = (e) => (errorMessage = e.message); + + conn.connect(); + sockets.current.open(); + sockets.current.serverSend(welcome()); + + sockets.current.serverSend({ + t: 'update', + index: 1, + description: 'd', + event: { type: 'none' }, + state: {} + }); + sockets.current.serverSend({ t: 'seats', seats: { a: null } }); + sockets.current.serverSend({ t: 'error', code: 'rejected', message: 'nope' }); + sockets.current.serverSend({ t: 'pong' }); + + expect(messages).toEqual([ + 'welcome', + 'update', + 'seats', + 'error', + 'pong' + ]); + expect(errorMessage).toBe('nope'); + }); + + it('sends outbound commands, claims, undo and redo', () => { + conn.connect(); + sockets.current.open(); + sockets.current.serverSend(welcome()); + + conn.sendCmd('draw'); + expect(sockets.current.lastJson()).toEqual({ + t: 'cmd', + name: 'draw', + args: {} + }); + + conn.sendCmd('addCharacter', { id: 'Berserker', edition: 'x' }); + expect(sockets.current.lastJson()).toEqual({ + t: 'cmd', + name: 'addCharacter', + args: { id: 'Berserker', edition: 'x' } + }); + + conn.claim('Berserker'); + expect(sockets.current.lastJson()).toEqual({ + t: 'claim', + characterId: 'Berserker' + }); + + conn.undo(); + expect(sockets.current.lastJson()).toEqual({ t: 'undo' }); + conn.redo(); + expect(sockets.current.lastJson()).toEqual({ t: 'redo' }); + }); + + it('pings on the keepalive interval while open', () => { + conn.connect(); + sockets.current.open(); + sockets.current.serverSend(welcome()); + // clear the hello frame so we can see pings + const before = sockets.current.sent.length; + vi.advanceTimersByTime(1_000); + expect(sockets.current.sent.slice(before)).toEqual([ + JSON.stringify({ t: 'ping' }) + ]); + vi.advanceTimersByTime(2_000); + expect(sockets.current.sent.slice(before)).toHaveLength(3); + }); + + it('reconnects, resyncs via a fresh hello, and backs off across churn', () => { + conn.connect(); + const first = sockets.current; + first.open(); + first.serverSend(welcome({ session: 'abc' })); + expect(conn.session).toBe('abc'); + + // Server drops the connection → first retry after the base delay. + first.serverClose(); + expect(conn.status).toBe('reconnecting'); + vi.advanceTimersByTime(99); + expect(sockets.sockets.length).toBe(1); + vi.advanceTimersByTime(1); + expect(sockets.sockets.length).toBe(2); + + // The reconnect re-sends hello (the resync). + const second = sockets.current; + second.open(); + expect(second.lastJson()).toEqual({ t: 'hello', v: 1 }); + expect(conn.status).toBe('open'); + + // A failed second attempt (closes without a welcome) doubles the backoff. + second.serverClose(); + vi.advanceTimersByTime(199); + expect(sockets.sockets.length).toBe(2); + vi.advanceTimersByTime(1); + expect(sockets.sockets.length).toBe(3); + expect(conn.status).toBe('reconnecting'); + + // A real welcome resets the backoff to the base. + const third = sockets.current; + third.open(); + third.serverSend(welcome({ session: 'abc' })); + third.serverClose(); + vi.advanceTimersByTime(99); + expect(sockets.sockets.length).toBe(3); + vi.advanceTimersByTime(1); + expect(sockets.sockets.length).toBe(4); + }); + + it('resync() re-requests a full welcome', () => { + conn.connect(); + sockets.current.open(); + sockets.current.serverSend(welcome()); + conn.resync(); + expect(sockets.current.lastJson()).toEqual({ t: 'hello', v: 1 }); + }); + + it('does not reconnect after an intentional disconnect', () => { + conn.connect(); + sockets.current.open(); + sockets.current.serverSend(welcome()); + conn.disconnect(); + expect(conn.status).toBe('closed'); + vi.advanceTimersByTime(10_000); + expect(sockets.sockets.length).toBe(1); + }); + + it('ignores non-JSON frames', () => { + conn.connect(); + sockets.current.open(); + sockets.current.serverSend('not json'); + expect(conn.session).toBeNull(); + }); +}); -- cgit v1.3.1