summaryrefslogtreecommitdiff
path: root/web/test/line/lineParser.test.ts
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-16 22:19:39 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-16 22:19:39 +0100
commit94e2850fb73005f3fb30af8489905e5cdfb6fac5 (patch)
tree1aea8e7e6683af770f0c57c0fb654003778c9f16 /web/test/line/lineParser.test.ts
parent4f50f2008f4d7c2a635f7f26861fcfc7f7504423 (diff)
Phase 4: cards and decks (M4)
Full card play in the browser: ability-line rendering, monster ability decks, the full modifier-deck (AMD) suite, and the loot deck — all server commands, faithful to the reference flows and layout. Web (Svelte 5): - Line renderer + in-game translations: $lib/game/line/ renders the reference line_builder output (emphasis, range markers, element styling); gameI18n.ts loads the /i18n/ in-game ability-text tables (the client-side TranslationService equivalent) - Card components: stat cards, monster ability cards (OG + FH styles, rendered lines, element/flying tokens), modifier cards (gfx resolver incl. P<n> perk graphics), loot cards (party-size value rules, owner, enhancements) - Monster ability decks: AbilityCardSlot in each monster row (front of the current card while active in playTurns, else the back with the draw-pile count); AbilityCardsMenu (reveal top cards, draw extra, extra shuffle, activate/inactivate, drag-reorder, remove); card zoom - Modifier decks: ModifierDeckWidget (tap to draw, face-up revealed top card, empty-box class icon for the current character's own deck, discard stack); ModifierDeckHeader (bad omen, corrosive spew, ±1/−2/+0, imbue 1/2, reveal with the Cassandra 8-button cap, save/don't-save revealed cards, removed count, Hail perk, CS sanctuary/party cards, faction icons, bless/curse and the empower/enfeeble counters); ModifierDeckMenu (draw-pile drag-reorder + send-to-bottom/shuffle/remove, discard remove/return-top, removed pile with return, faction grid with gray-out, perks menu with the Frosthaven opt-in); card zoom - Loot deck: LootDeckWidget (player-gated draw, current-character icon, discard stack, hidden when both piles empty); LootCardsMenu (set owner, per-character loot totals, add card, enhance with the reference material/herb/coin splits, special cards 1418/1419, return top/bottom) - Bottom DeckArea: loot deck left; the Character Decks toggle with the reference open-state cycling (oneOpen/allOpen/noOpen per round state; one-deck view for the current character during playTurns); ally deck behind shouldShowAlliesDeck; global deck - Pure helper modules ported from the reference: rules.ts (FH card styling, inactive-for-rule, ally-deck gate, deck resolution), modifierDeck.ts (pile derivations, faction cards), perks.ts (active perk set, card-text tokens, descriptions, add/remove gating), cards/lootCard.ts (values + per-character totals) - App wiring: campaign data loaded on currentCampaign change (guarded against out-of-order fetches); ability/AMD/loot menus and zooms driven by live-derived state so open menus track pile changes; Escape closes every dialog Server: - /i18n/ static mount: the in-game ability-text translation files the card renderer resolves (the reference TranslationService data) - Loot deck JSON gains pools (pool card ids per pool — the client derives card gfx from pool key + index and reads the enhancements map; ids only, since pool cards are the same live instances that mutate in the piles) - Modifier deck JSON gains removables (the counter totals the header reads) plus load-time round-trip for saves written before the field existed Docs: - PLAN.md: Phase 3 and Phase 4 marked complete (corrected the 'character ability decks' mislabel — ability decks are monster-only), progress log entries for M3 and M4 svelte-check 0 errors; web tests 177/177; build OK; dart analyze 0 errors; dart test 592/592; scripts/ci.sh green.
Diffstat (limited to 'web/test/line/lineParser.test.ts')
-rw-r--r--web/test/line/lineParser.test.ts169
1 files changed, 169 insertions, 0 deletions
diff --git a/web/test/line/lineParser.test.ts b/web/test/line/lineParser.test.ts
new file mode 100644
index 0000000..ad95240
--- /dev/null
+++ b/web/test/line/lineParser.test.ts
@@ -0,0 +1,169 @@
+import { describe, it, expect } from 'vitest';
+import { parseLines } from '$lib/game/line/lineParser';
+import type {
+ ColumnNode,
+ LineBuildOptions,
+ Node,
+ RowNode,
+ SubLineNode
+} from '$lib/game/line/types';
+import type { StatMonster } from '$lib/game/line/statApplier';
+
+function opts(partial: Partial<LineBuildOptions> = {}): LineBuildOptions {
+ return {
+ left: false,
+ applyStats: false,
+ applyAll: false,
+ align: 'center',
+ frosthavenStyle: false,
+ scale: 1,
+ monster: null,
+ ...partial
+ };
+}
+
+/** The single top-level row of a one-line card. */
+function topRow(col: ColumnNode): RowNode {
+ const first = col.children[0];
+ if (!first || first.kind !== 'row')
+ throw new Error(`expected a top row, got ${first?.kind}`);
+ return first;
+}
+
+function texts(node: Node): string[] {
+ if (node.kind === 'text') return [node.text];
+ if ('children' in node) return node.children.flatMap(texts);
+ return [];
+}
+
+const zealot = (standeeTypes: number[] = [1]): StatMonster => ({
+ level: {
+ normal: { attack: 2, move: 2 },
+ elite: { attack: 3, move: 2 }
+ },
+ standeeTypes: standeeTypes as never,
+ isActive: false
+});
+
+describe('parseLines — plain text', () => {
+ it('a single line becomes one row of one text segment', () => {
+ const col = parseLines(['Move 3'], opts());
+ expect(col.children).toHaveLength(1);
+ const row = topRow(col);
+ expect(row.children.map((n) => n.kind)).toEqual(['text']);
+ expect((row.children[0] as { text: string }).text).toBe('Move 3');
+ });
+ it('multiple lines become multiple rows', () => {
+ const col = parseLines(['Move 3', 'Attack 2'], opts());
+ expect(col.children.map((n) => n.kind)).toEqual(['row', 'row']);
+ });
+});
+
+describe('parseLines — tokens', () => {
+ it('GH style shows the translated label beside the icon', () => {
+ const row = topRow(parseLines(['%attack% 2'], opts()));
+ expect(row.children.map((n) => n.kind)).toEqual(['text', 'token', 'text']);
+ expect((row.children[0] as { text: string }).text).toBe('Attack');
+ expect((row.children[1] as { token: string }).token).toBe('attack');
+ });
+ it('FH style renders the token icon-only (no label)', () => {
+ const row = topRow(parseLines(['%attack% 2'], opts({ frosthavenStyle: true })));
+ expect(row.children.map((n) => n.kind)).toEqual(['token', 'text']);
+ });
+ it('a standalone token yields just the icon', () => {
+ const row = topRow(parseLines(['%poison%'], opts({ frosthavenStyle: true })));
+ expect(row.children.map((n) => n.kind)).toEqual(['token']);
+ expect((row.children[0] as { overflow: boolean }).overflow).toBe(true);
+ });
+});
+
+describe('parseLines — sub-lines (FH)', () => {
+ it('boxes a ^%token% sub-line to the right of the main line', () => {
+ const col = parseLines(['Move 3', '^%poison%'], opts({ frosthavenStyle: true }));
+ const row = topRow(col);
+ expect(row.children.map((n) => n.kind)).toEqual(['row', 'subline']);
+ const sub = row.children[1] as SubLineNode;
+ expect(sub.children.map((n) => n.kind)).toEqual(['row']);
+ expect((sub.children[0] as RowNode).children.map((n) => n.kind)).toEqual([
+ 'token'
+ ]);
+ });
+ it('consecutive ^ sub-line tokens share one row (no line break)', () => {
+ const col = parseLines(
+ ['Move 3', '^%poison%', '^%wound%'],
+ opts({ frosthavenStyle: true })
+ );
+ const row = topRow(col);
+ const sub = row.children[1] as SubLineNode;
+ expect(sub.children.map((n) => n.kind)).toEqual(['row']);
+ expect((sub.children[0] as RowNode).children.map((n) => n.kind)).toEqual([
+ 'token',
+ 'token'
+ ]);
+ });
+});
+
+describe('parseLines — element images & dividers', () => {
+ it('a ¤ element line becomes an image node', () => {
+ const col = parseLines(['¤fire'], opts({ frosthavenStyle: true }));
+ expect(col.children.map((n) => n.kind)).toEqual(['image']);
+ expect((col.children[0] as { element: boolean }).element).toBe(true);
+ });
+ it('a *.... line becomes a divider in FH style', () => {
+ const col = parseLines(['*....'], opts({ frosthavenStyle: true }));
+ expect(col.children.map((n) => n.kind)).toEqual(['divider']);
+ expect((col.children[0] as { thin: boolean }).thin).toBe(false);
+ });
+});
+
+describe('parseLines — columns', () => {
+ it('[c]…[/c] groups its lines into a column node', () => {
+ const col = parseLines(['[c]', 'A', 'B', '[/c]'], opts());
+ expect(col.children.map((n) => n.kind)).toEqual(['column']);
+ const inner = col.children[0] as { children: Node[] };
+ expect(inner.children.map((n) => n.kind)).toEqual(['row', 'row']);
+ expect(texts(inner.children[0] as Node)).toEqual(['A']);
+ });
+});
+
+describe('parseLines — element use / conditional', () => {
+ it('an element+%use% block in a row becomes a conditional box', () => {
+ const col = parseLines(
+ ['[r]', '%fire%%use%', '[/r]'],
+ opts({ frosthavenStyle: true })
+ );
+ expect(col.children.map((n) => n.kind)).toEqual(['conditional']);
+ const cond = col.children[0] as {
+ elementUse: boolean;
+ children: Node[];
+ };
+ expect(cond.elementUse).toBe(true);
+ // The [r] group wraps its inner line in a row (faithful to the
+ // reference's Row(children: widgetsInRow)).
+ const group = cond.children[0] as RowNode;
+ const inner = group.children[0] as RowNode;
+ expect(inner.children.map((n) => n.kind)).toEqual(['elementUse', 'text']);
+ });
+});
+
+describe('parseLines — elite stat split', () => {
+ it('splits %attack% into a normal and an elite row', () => {
+ const col = parseLines(
+ ['%attack% +0'],
+ opts({ applyStats: true, applyAll: false, monster: zealot([0, 1]) })
+ );
+ const all = texts(col).join(' ');
+ // normal (2) and elite (3) values both present.
+ expect(all).toContain('2');
+ expect(all).toContain('3');
+ });
+ it('a lone elite standee shows only the elite value', () => {
+ const col = parseLines(
+ ['%attack% +0'],
+ opts({ applyStats: true, applyAll: false, monster: zealot([1]) })
+ );
+ const row = topRow(col);
+ const styles = row.children.map((n) => (n as { style?: string }).style ?? '');
+ expect(styles.some((s) => s.startsWith('elite'))).toBe(true);
+ });
+});