1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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);
});
});
|