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); }); });