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/state.test.ts | 183 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 web/test/state.test.ts (limited to 'web/test/state.test.ts') diff --git a/web/test/state.test.ts b/web/test/state.test.ts new file mode 100644 index 0000000..3dada4f --- /dev/null +++ b/web/test/state.test.ts @@ -0,0 +1,183 @@ +import { + describe, + it, + expect, + beforeEach, + afterEach, + vi +} from 'vitest'; +import { get } from 'svelte/store'; +import { GameSession, ownedIds } from '$lib/state/session'; +import type { GameState } from '$lib/protocol/types'; +import { SocketSequence } from './fake-socket'; + +function makeState(overrides: Partial = {}): GameState { + return { + level: 1, + solo: false, + autoScenarioLevel: false, + difficulty: 1, + roundState: 0, + round: 1, + totalRounds: 1, + scenario: 'custom', + toastMessage: '', + scenarioSpecialRules: [], + scenarioSectionsAdded: [], + currentCampaign: 'Jaws of the Lion', + currentList: [], + currentAbilityDecks: [], + sanctuaryDeck: {}, + modifierDeck: {}, + modifierDeckAllies: {}, + lootDeck: {}, + unlockedClasses: [], + showAllyDeck: false, + allyDeckInOGGloom: true, + elementState: {}, + ...overrides + }; +} + +function welcome( + state: GameState, + session = 'sess1', + role: 'player' | 'spectator' = 'spectator', + seats: Record = {}, + index = 7 +) { + return { + t: 'welcome', + v: 1, + session, + role, + index, + description: 'last action', + event: null, + state, + seats + }; +} + +describe('GameSession', () => { + let session: GameSession; + let sockets: SocketSequence; + + beforeEach(() => { + vi.useFakeTimers(); + sockets = new SocketSequence(); + session = new GameSession({ + transport: sockets.factory, + url: 'ws://test/ws', + reconnectBaseMs: 100, + pingIntervalMs: 60_000 + }); + session.connect(); + sockets.current.open(); + }); + + afterEach(() => vi.useRealTimers()); + + function welcomeWith( + state: GameState, + sessionId = 'sess1', + role: 'player' | 'spectator' = 'player', + seats: Record = {} + ) { + sockets.current.serverSend( + welcome(state, sessionId, role, seats) + ); + } + + it('starts empty and connecting', () => { + expect(get(session.state)).toBeNull(); + expect(get(session.session)).toBeNull(); + expect(get(session.index)).toBe(-1); + }); + + it('applies a welcome to every store', () => { + welcomeWith(makeState({ round: 3 }), 'sess1', 'player', { B: 'sess1' }); + expect(get(session.session)).toEqual({ id: 'sess1', role: 'player' }); + expect(get(session.state)?.round).toBe(3); + expect(get(session.seats)).toEqual({ B: 'sess1' }); + expect(get(session.index)).toBe(7); + expect(get(session.status)).toBe('open'); + }); + + it('replaces state and index on an update', () => { + welcomeWith(makeState({ round: 4 })); + sockets.current.serverSend({ + t: 'update', + index: 8, + description: 'x', + event: { type: 'none' }, + state: makeState({ round: 5 }) + }); + expect(get(session.state)?.round).toBe(5); + expect(get(session.index)).toBe(8); + }); + + it('owns() is true only for the session\'s characters', () => { + welcomeWith(makeState(), 'sess1', 'player', { + A: 'sess1', + B: 'sess2', + C: null + }); + expect(session.owns('A')).toBe(true); + expect(session.owns('B')).toBe(false); + expect(session.owns('C')).toBe(false); + }); + + it('reports spectator when no seat is claimed', () => { + welcomeWith(makeState(), 'sess1', 'spectator', { A: 'sess2' }); + expect(session.isSpectator()).toBe(true); + expect(session.ownedCharacterIds()).toEqual([]); + }); + + it('lists owned character ids', () => { + welcomeWith(makeState(), 'sess1', 'player', { + A: 'sess1', + B: 'sess1', + C: 'sess2' + }); + expect(session.ownedCharacterIds().sort()).toEqual(['A', 'B']); + }); + + it('surfaces protocol errors and clears them', () => { + sockets.current.serverSend({ + t: 'error', + code: 'forbidden', + message: 'claim a character first' + }); + expect(get(session.lastError)).toEqual({ + code: 'forbidden', + message: 'claim a character first' + }); + session.clearError(); + expect(get(session.lastError)).toBeNull(); + }); + + it('sends commands and claims through the socket', () => { + welcomeWith(makeState()); + session.command('draw'); + expect(sockets.current.lastJson()).toEqual({ + t: 'cmd', + name: 'draw', + args: {} + }); + session.claim('Berserker'); + expect(sockets.current.lastJson()).toEqual({ + t: 'claim', + characterId: 'Berserker' + }); + }); +}); + +describe('ownedIds', () => { + it('returns ids owned by the session', () => { + expect(ownedIds('s', { a: 's', b: 't', c: null })).toEqual(['a']); + }); + it('returns empty for a null session', () => { + expect(ownedIds(null, { a: 's' })).toEqual([]); + }); +}); -- cgit v1.3.1