summaryrefslogtreecommitdiff
path: root/web/test/gameLogic.test.ts
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-15 13:45:21 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-15 13:45:21 +0100
commit4f50f2008f4d7c2a635f7f26861fcfc7f7504423 (patch)
tree94e6286e7e22e0467a61b99c18b9ab582fbbe656 /web/test/gameLogic.test.ts
parent3ebbdf6ffa16f751c82e3bd2e74294f4fe843155 (diff)
Phase 3: web UI core loop (M3)
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.
Diffstat (limited to 'web/test/gameLogic.test.ts')
-rw-r--r--web/test/gameLogic.test.ts149
1 files changed, 149 insertions, 0 deletions
diff --git a/web/test/gameLogic.test.ts b/web/test/gameLogic.test.ts
new file mode 100644
index 0000000..263ba14
--- /dev/null
+++ b/web/test/gameLogic.test.ts
@@ -0,0 +1,149 @@
+import { describe, it, expect } from 'vitest';
+import { ELEMENTS, elementState } from '$lib/game/elements';
+import {
+ CONDITIONS,
+ conditionChipClass,
+ conditionLabel,
+ conditionName
+} from '$lib/game/conditions';
+import { classColor, classIcon, monsterImage } from '$lib/game/art';
+import {
+ characterStatCounters,
+ standeeFigureId,
+ standeeStatCounters,
+ type ModifierDeckJson
+} from '$lib/game/stats';
+import { t, hasLocaleKey, availableLocales } from '$lib/i18n';
+
+describe('elements', () => {
+ it('lists the six elements in server enum order', () => {
+ expect(ELEMENTS.map((e) => e.name)).toEqual([
+ 'fire',
+ 'ice',
+ 'air',
+ 'earth',
+ 'light',
+ 'dark'
+ ]);
+ });
+
+ it('reads the element state by index string (absent → inert)', () => {
+ expect(elementState({}, 0)).toBe(2);
+ expect(elementState({ '0': 0, '1': 1 }, 0)).toBe(0);
+ expect(elementState({ '0': 0, '1': 1 }, 1)).toBe(1);
+ expect(elementState({ '0': 9 }, 0)).toBe(2); // out-of-range → inert
+ });
+});
+
+describe('conditions', () => {
+ it('keeps the server Condition index mapping', () => {
+ // stun = 0, wound = 3, chill = 12, retaliate = 24
+ expect(conditionName(0)).toBe('stun');
+ expect(conditionName(3)).toBe('wound');
+ expect(conditionName(12)).toBe('chill');
+ expect(conditionName(24)).toBe('retaliate');
+ });
+
+ it('labels fall back to the index for unknown entries', () => {
+ expect(conditionLabel(0)).toBe('Stunned');
+ expect(conditionLabel(999)).toBe('condition 999');
+ });
+
+ it('chill and plague get coloured chips, the rest are plain', () => {
+ expect(conditionChipClass(12)).toBe('cond cond--chill');
+ expect(conditionChipClass(16)).toBe('cond cond--plague');
+ expect(conditionChipClass(0)).toBe('cond');
+ });
+
+ it('excludes the custom character slots (character1..4)', () => {
+ const names = CONDITIONS.map((c) => c.name);
+ expect(names).not.toContain('character1');
+ expect(names).not.toContain('character4');
+ });
+});
+
+describe('art', () => {
+ it('resolves class icons and monster art under /images/', () => {
+ // gfx values in the data carry no extension; .png is appended (the
+ // reference app builds the same path).
+ expect(classIcon('Berserker')).toBe('/images/class-icons/Berserker.png');
+ expect(monsterImage('Zombie')).toBe('/images/monsters/Zombie.png');
+ });
+
+ it('classColor accepts 0xAARRGGBB and plain hex', () => {
+ expect(classColor('FF9C881A')).toBe('#9C881A');
+ expect(classColor('#a1b2c3')).toBe('#a1b2c3');
+ expect(classColor('garbage')).toBe('#3a3f47');
+ });
+});
+
+describe('stat counters', () => {
+ function deck(cards: { gfx: string }[]): ModifierDeckJson {
+ return { drawPile: cards, discardPile: [] };
+ }
+
+ it('standeeFigureId is name + gfx + standeeNr', () => {
+ const standee = {
+ name: 'Zombie',
+ gfx: 'BLA zombie',
+ standeeNr: 2
+ } as never;
+ expect(standeeFigureId(standee)).toBe('ZombieBLA zombie2');
+ });
+
+ it('counts bless/curse/empower/enfeeble cards in the draw pile', () => {
+ const d = deck([
+ { gfx: 'bless' },
+ { gfx: 'curse' },
+ { gfx: 'in-empower' },
+ { gfx: 'out-enfeeble' },
+ { gfx: 'bless' },
+ { gfx: 'hazard-1' }
+ ]);
+ expect(standeeStatCounters(d)).toEqual({
+ bless: 2,
+ curse: 1,
+ empower: 1,
+ enfeeble: 1
+ });
+ expect(standeeStatCounters(undefined)).toEqual({
+ bless: 0,
+ curse: 0,
+ empower: 0,
+ enfeeble: 0
+ });
+ });
+
+ it('characterStatCounters reads the character\'s own deck', () => {
+ const character = {
+ id: 'Berserker',
+ characterState: {
+ modifierDeck: deck([{ gfx: 'curse' }, { gfx: 'curse' }])
+ }
+ } as never;
+ expect(characterStatCounters(character).curse).toBe(2);
+ });
+});
+
+describe('i18n', () => {
+ it('translates English keys', () => {
+ expect(t('draw')).toBe('Draw');
+ expect(t('menuAddCharacter')).toBe('Add Character');
+ });
+
+ it('substitutes {vars}', () => {
+ expect(t('cmdAddMonster', { name: 'Zombie' })).toBe('Add Zombie');
+ expect(t('cmdIncreaseXp', { figure: 'Berserker', amount: 10 })).toBe(
+ "Increase Berserker's xp by 10"
+ );
+ });
+
+ it('returns the key for unknown keys', () => {
+ expect(t('noSuchKey')).toBe('noSuchKey');
+ });
+
+ it('only English is ported in Phase 3', () => {
+ expect(availableLocales).toEqual(['en']);
+ expect(hasLocaleKey('draw')).toBe(true);
+ });
+});