summaryrefslogtreecommitdiff
path: root/web/test/line/frosthavenConverter.test.ts
blob: 89101b3440302255bdeb0b70df79652d10cbf161 (plain)
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
import { describe, it, expect } from 'vitest';
import {
	expandEmbeddedNewlines,
	convertLinesToFH,
	shouldOverflow
} from '$lib/game/line/frosthavenConverter';

const conv = (lines: string[], applyStats = false) =>
	convertLinesToFH(lines, applyStats);

describe('expandEmbeddedNewlines', () => {
	it('passes through lines without a newline', () =>
		expect(expandEmbeddedNewlines(['Move 3'])).toEqual(['Move 3']));
	it('splits on \\n', () =>
		expect(expandEmbeddedNewlines(['a\nb'])).toEqual(['a', 'b']));
	it('propagates a ^ prefix to continuation lines', () =>
		expect(expandEmbeddedNewlines(['^a\nb'])).toEqual(['^a', '^b']));
	it('propagates a ^^ prefix', () =>
		expect(expandEmbeddedNewlines(['^^a\nb'])).toEqual(['^^a', '^^b']));
});

describe('convertLinesToFH — keyword replacements', () => {
	it('"Affect" → "%target%"', () =>
		expect(conv(['Affect 2'])).toContain('%target% 2'));
	it('"damage" → "%damage%"', () =>
		expect(conv(['Deal damage here'])[0]).toContain('%damage%'));
	it('"damaged" is not doubled', () =>
		expect(conv(['damaged item'])[0]).toBe('damaged item'));
	it('"Target" → "%target%"', () => expect(conv(['Target 2'])[0]).toBe('%target% 2'));
	it('Affect pipeline leaves no "Affect"', () => {
		const result = conv(['Affect all', 'Affect']);
		for (const line of result) expect(line).not.toContain('Affect');
		expect(result[0]).toContain('%target%');
	});
	it('collapses "% " to "%"', () => expect(conv(['50% health'])[0]).toBe('50%health'));
	it('preserves plain lines', () => expect(conv(['Move 3'])).toContain('Move 3'));
});

describe('convertLinesToFH — applyStats flag', () => {
	it('applyStats=false collapses " + "', () => expect(conv(['3 + 1'])[0]).toBe('3+1'));
	it('applyStats=false collapses " - "', () => expect(conv(['3 - 1'])[0]).toBe('3-1'));
	it('applyStats=true preserves " + "', () =>
		expect(conv(['3 + 1'], true)[0]).toBe('3 + 1'));
	it('applyStats=true preserves " - "', () =>
		expect(conv(['3 - 1'], true)[0]).toBe('3 - 1'));
});

describe('convertLinesToFH — [newLine]', () => {
	it('converts to an empty string', () => expect(conv(['text', '[newLine]'])).toContain(''));
});

describe('convertLinesToFH — * lines', () => {
	it('* line passes through', () =>
		expect(conv(['*Move 3', '^%poison%']).some((l) => l.startsWith('*'))).toBe(true));
	it('* after a real subline adds [subLineEnd]', () =>
		expect(conv(['Move 3', '^%poison%', '*Attack'])).toContain('[subLineEnd]'));
	it('no trailing [subLineEnd] after a leading *', () =>
		expect(conv(['*Move 3']).at(-1)).not.toBe('[subLineEnd]'));
});

describe('convertLinesToFH — subline markers', () => {
	it('^%token% after a normal line inserts ![subLineStart]', () =>
		expect(conv(['Move 3', '^%poison%'])).toContain('![subLineStart]'));
	it('trailing [subLineEnd] when open at end', () =>
		expect(conv(['Move 3', '^%poison%']).at(-1)).toBe('[subLineEnd]'));
	it('^ line matching the subline pattern gets a ! prefix', () =>
		expect(conv(['Move 3', '^%poison%']).some((l) => l.startsWith('!^'))).toBe(true));
	it('^%target% after a normal line triggers a subline', () =>
		expect(conv(['Attack 3', '^%target% 2'])).toContain('![subLineStart]'));
	it('^Self after a normal line triggers a subline', () =>
		expect(conv(['Heal 2', '^Self'])).toContain('![subLineStart]'));
	it('^All attacks does NOT trigger a subline', () =>
		expect(conv(['Move 3', '^All attacks on you'])).not.toContain('![subLineStart]'));
	it('second ^ inside a subline is also prefixed', () => {
		const exclam = conv(['Move 3', '^%poison%', '^%wound%']).filter(
			(l) => l.startsWith('!') && l.includes('%')
		);
		expect(exclam.length).toBeGreaterThanOrEqual(2);
	});
	it('empty list returns empty list', () => expect(conv([])).toEqual([]));
	it('single non-special line has no markers', () => {
		const result = conv(['Move 3']);
		expect(result).not.toContain('[subLineStart]');
		expect(result).not.toContain('![subLineStart]');
		expect(result).not.toContain('[subLineEnd]');
	});
});

describe('convertLinesToFH — element use / conditional', () => {
	it('[r] before a %use line does not crash', () =>
		expect(conv(['Attack 2', '[r]', '%use%', '^%poison%', '[/r]'])).not.toEqual([]));
	it('[/r] resets the conditional flag', () =>
		expect(
			conv(['[r]', '%use%', '%fire%', '[/r]', 'Move 3', '^%poison%'])
		).toContain('![subLineStart]'));
});

describe('convertLinesToFH — advanced subline patterns', () => {
	it('^Normal starts a subline', () =>
		expect(conv(['Attack 3', '^Normal attack'])).toContain('![subLineStart]'));
	it('^all starts a subline', () =>
		expect(conv(['Attack 3', '^all enemies'])).toContain('![subLineStart]'));
	it('^All (not "attacks" / "targets") starts a subline', () =>
		expect(conv(['Attack 3', '^All adjacent enemies'])).toContain('![subLineStart]'));
	it('^All targets does not start a subline', () =>
		expect(conv(['Move 3', '^All targets suffer'])).not.toContain('![subLineStart]'));
	it('isSubLine clears after a non-subline ^ line', () => {
		const result = conv(['Move 3', '^All attacks on you', 'Attack 2']);
		expect(result).not.toContain('![subLineStart]');
		expect(result).not.toContain('[subLineEnd]');
	});
	it('a new main line closes an open subline', () =>
		expect(conv(['Move 3', '^%poison%', 'Attack 2'])).toContain('[subLineEnd]'));
});

describe('shouldOverflow', () => {
	const overflowTokens = [
		'pierce',
		'brittle',
		'curse',
		'enfeeble',
		'bless',
		'invisible',
		'strengthen',
		'bane',
		'push',
		'pull',
		'poison',
		'wound',
		'infect',
		'chill',
		'disarm',
		'immobilize',
		'stun',
		'impair',
		'safeguard',
		'muddle'
	];
	it('returns true for known FH condition tokens', () => {
		for (const token of overflowTokens)
			expect(shouldOverflow(true, token)).toBe(true);
	});
	it('returns false for non-FH style', () => {
		for (const token of overflowTokens)
			expect(shouldOverflow(false, token)).toBe(false);
	});
	it('returns false for non-overflow tokens', () => {
		expect(shouldOverflow(true, 'attack')).toBe(false);
		expect(shouldOverflow(true, 'move')).toBe(false);
		expect(shouldOverflow(true, '')).toBe(false);
	});
	it('matches tokens containing poison / wound', () => {
		expect(shouldOverflow(true, 'acid-poison')).toBe(true);
		expect(shouldOverflow(true, 'wound-infect')).toBe(true);
	});
});