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/gameDataClient.test.ts | 209 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 web/test/gameDataClient.test.ts (limited to 'web/test/gameDataClient.test.ts') diff --git a/web/test/gameDataClient.test.ts b/web/test/gameDataClient.test.ts new file mode 100644 index 0000000..4249283 --- /dev/null +++ b/web/test/gameDataClient.test.ts @@ -0,0 +1,209 @@ +import { describe, it, expect, vi, afterEach } from 'vitest'; +import { GameDataClient } from '$lib/data/gameData'; + +afterEach(() => vi.unstubAllGlobals()); + +function json(value: unknown) { + return Promise.resolve({ + ok: true, + json: () => Promise.resolve(value) + } as Response); +} + +function withFetch(handler: (url: string) => Promise) { + // Clients are constructed per test, so the per-instance caches are clean. + vi.stubGlobal('fetch', (input: RequestInfo | URL) => { + const url = typeof input === 'string' ? input : input.toString(); + return handler(url); + }); +} + +const ROOMS = { + '1': [ + { 'A Town in Flames': {} }, + { 'The Cellar': {} }, + { 'Spawn Alley': {} }, + { 'The Chapel': {} } + ], + '4A': [ + { 'Frostbite Landing': {} }, + { 'Frozen Harbor': {} } + ] +}; + +describe('GameDataClient.campaigns', () => { + it('reads the campaign list from editions.json', async () => { + withFetch((url) => { + expect(url).toBe('/data/editions/editions.json'); + return json({ editions: ['Gloomhaven', 'Jaws of the Lion'] }); + }); + const data = new GameDataClient(); + await expect(data.campaigns()).resolves.toEqual([ + 'Gloomhaven', + 'Jaws of the Lion' + ]); + }); + + it('caches after the first load', async () => { + let calls = 0; + withFetch(() => { + calls++; + return json({ editions: ['Gloomhaven'] }); + }); + const data = new GameDataClient(); + await data.campaigns(); + await data.campaigns(); + expect(calls).toBe(1); + }); +}); + +describe('GameDataClient.loadCampaign', () => { + it('maps classes, monsters and scenarios', async () => { + withFetch((url) => { + expect(url).toBe('/data/editions/Jaws%20of%20the%20Lion.json'); + return json({ + edition: 'Jaws of the Lion', + classes: [ + { + name: 'Berserker', + edition: 'Jaws of the Lion', + health: [12, 14], + hidden: false, + color: 'FF35ACD5', + colorSecondary: 'ff313959' + }, + { + name: 'Secret', + edition: 'Jaws of the Lion', + health: [10], + hidden: true, + color: ['FF112233'] + } + ], + monsters: { + 'Zombie': { + deck: 'Zombie', + count: 4, + levels: [ + { level: 0, normal: { health: 3 } }, + { level: 2, normal: { health: 5 }, elite: { health: 8 } } + ] + }, + 'Dreadnaught': { + deck: 'Dreadnaught', + count: 2, + display: 'Dreadnaught (Boss)', + levels: [{ level: 0, boss: { health: 40 } }], + gfx: 'Dreadnaught' + } + }, + scenarios: { + '#1 A Town in Flames': { monsters: ['Zombie'] }, + '#2 A Dark Night': {} + } + }); + }); + const data = new GameDataClient(); + const campaign = await data.loadCampaign('Jaws of the Lion'); + expect(campaign.edition).toBe('Jaws of the Lion'); + expect(campaign.classes).toEqual([ + { + id: 'Berserker', + name: 'Berserker', + edition: 'Jaws of the Lion', + hidden: false, + health: [12, 14], + color: 'FF35ACD5', + colorSecondary: 'ff313959', + icon: '/images/class-icons/Berserker.png' + }, + expect.objectContaining({ + id: 'Secret', + hidden: true, + color: 'FF112233' // first of an array + }) + ]); + const zombie = campaign.monsters.find((m) => m.name === 'Zombie'); + expect(zombie).toMatchObject({ + display: 'Zombie', + gfx: 'Zombie', + count: 4, + maxLevel: 2, + isBoss: false, + portrait: '/images/monsters/Zombie.png' + }); + const boss = campaign.monsters.find((m) => m.name === 'Dreadnaught'); + expect(boss).toMatchObject({ + display: 'Dreadnaught (Boss)', + maxLevel: 0, + isBoss: true + }); + expect(campaign.scenarios.map((s) => s.name)).toEqual([ + '#1 A Town in Flames', + '#2 A Dark Night' + ]); + expect(campaign.scenarios[0].monsters).toEqual(['Zombie']); + }); + + it('caches per campaign (one fetch per campaign)', async () => { + let calls = 0; + withFetch(() => { + calls++; + return json({ edition: 'Gloomhaven', classes: [], monsters: {}, scenarios: {} }); + }); + const data = new GameDataClient(); + await data.loadCampaign('Gloomhaven'); + await data.loadCampaign('Gloomhaven'); + expect(calls).toBe(1); + }); + + it('rejects on a missing campaign', async () => { + withFetch(() => + Promise.resolve({ ok: false, json: () => Promise.reject(new Error('nope')) } as Response) + ); + const data = new GameDataClient(); + await expect(data.loadCampaign('Nope')).rejects.toThrow( + 'failed to load campaign Nope' + ); + }); +}); + +describe('GameDataClient.sections', () => { + it('lists the addable rooms for a scenario number', async () => { + withFetch((url) => { + expect(url).toBe('/data/rooms/Jaws%20of%20the%20Lion.json'); + return json(ROOMS); + }); + const data = new GameDataClient(); + await expect(data.sections('Jaws of the Lion', '#1 A Town in Flames')).resolves.toEqual([ + 'The Cellar', + 'The Chapel' + ]); // first room skipped, "Spawn Alley" filtered + }); + + it('handles lettered scenario numbers', async () => { + withFetch(() => json(ROOMS)); + const data = new GameDataClient(); + await expect(data.sections('Jaws of the Lion', '#4A Frostbite')).resolves.toEqual([ + 'Frozen Harbor' + ]); + }); + + it('returns empty for unknown scenarios', async () => { + withFetch(() => json(ROOMS)); + const data = new GameDataClient(); + await expect(data.sections('Jaws of the Lion', '#99 Nowhere')).resolves.toEqual( + [] + ); + }); + + it('returns empty when the rooms file is missing', async () => { + withFetch(() => + Promise.resolve({ ok: false, json: () => Promise.reject(new Error('nope')) } as Response) + ); + const data = new GameDataClient(); + await expect(data.sections('Jaws of the Lion', '#1 A Town in Flames')).resolves.toEqual( + [] + ); + }); +}); -- cgit v1.3.1