diff options
Diffstat (limited to 'web/test/line/lineParser.test.ts')
| -rw-r--r-- | web/test/line/lineParser.test.ts | 169 |
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); + }); +}); |
