summaryrefslogtreecommitdiff
path: root/web/test/gameLogic.test.ts
blob: 891aaffda807ee2c8606cbf648d9ea37e1b553c8 (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
157
158
159
160
161
162
163
import { describe, it, expect } from 'vitest';
import { ELEMENTS, elementState } from '$lib/game/elements';
import {
	CONDITIONS,
	conditionChipClass,
	conditionLabel,
	conditionName
} from '$lib/game/conditions';
import { classColor, classIcon, monsterImage } from '$lib/game/art';
import {
	characterStatCounters,
	standeeFigureId,
	standeeStatCounters
} from '$lib/game/stats';
import type { ModifierDeck } from '$lib/protocol/types';
import { emptyModifierDeck } from './fixtures';
import { t, hasLocaleKey, availableLocales } from '$lib/i18n';

describe('elements', () => {
	it('lists the six elements in server enum order', () => {
		expect(ELEMENTS.map((e) => e.name)).toEqual([
			'fire',
			'ice',
			'air',
			'earth',
			'light',
			'dark'
		]);
	});

	it('reads the element state by index string (absent → inert)', () => {
		expect(elementState({}, 0)).toBe(2);
		expect(elementState({ '0': 0, '1': 1 }, 0)).toBe(0);
		expect(elementState({ '0': 0, '1': 1 }, 1)).toBe(1);
		expect(elementState({ '0': 9 }, 0)).toBe(2); // out-of-range → inert
	});
});

describe('conditions', () => {
	it('keeps the server Condition index mapping', () => {
		// stun = 0, wound = 3, chill = 12, retaliate = 24
		expect(conditionName(0)).toBe('stun');
		expect(conditionName(3)).toBe('wound');
		expect(conditionName(12)).toBe('chill');
		expect(conditionName(24)).toBe('retaliate');
	});

	it('labels fall back to the index for unknown entries', () => {
		expect(conditionLabel(0)).toBe('Stunned');
		expect(conditionLabel(999)).toBe('condition 999');
	});

	it('chill and plague get coloured chips, the rest are plain', () => {
		expect(conditionChipClass(12)).toBe('cond cond--chill');
		expect(conditionChipClass(16)).toBe('cond cond--plague');
		expect(conditionChipClass(0)).toBe('cond');
	});

	it('excludes the custom character slots (character1..4)', () => {
		const names = CONDITIONS.map((c) => c.name);
		expect(names).not.toContain('character1');
		expect(names).not.toContain('character4');
	});
});

describe('art', () => {
	it('resolves class icons and monster art under /images/', () => {
		// gfx values in the data carry no extension; .png is appended (the
		// reference app builds the same path).
		expect(classIcon('Berserker')).toBe('/images/class-icons/Berserker.png');
		expect(monsterImage('Zombie')).toBe('/images/monsters/Zombie.png');
	});

	it('classColor accepts 0xAARRGGBB and plain hex', () => {
		expect(classColor('FF9C881A')).toBe('#9C881A');
		expect(classColor('#a1b2c3')).toBe('#a1b2c3');
		expect(classColor('garbage')).toBe('#3a3f47');
	});
});

describe('stat counters', () => {
	function deck(cards: { gfx: string }[]): ModifierDeck {
		return { ...emptyModifierDeck(), drawPile: cards };
	}

	it('standeeFigureId is name + gfx + standeeNr', () => {
		const standee = {
			name: 'Zombie',
			gfx: 'BLA zombie',
			standeeNr: 2
		} as never;
		expect(standeeFigureId(standee)).toBe('ZombieBLA zombie2');
	});

	it('counts bless/curse/empower/enfeeble cards in the draw pile', () => {
		const d = deck([
			{ gfx: 'bless' },
			{ gfx: 'curse' },
			{ gfx: 'in-empower' },
			{ gfx: 'out-enfeeble' },
			{ gfx: 'bless' },
			{ gfx: 'hazard-1' }
		]);
		expect(standeeStatCounters(d)).toEqual({
			bless: 2,
			curse: 1,
			empower: 1,
			enfeeble: 1
		});
		expect(standeeStatCounters(undefined)).toEqual({
			bless: 0,
			curse: 0,
			empower: 0,
			enfeeble: 0
		});
	});

	it('characterStatCounters reads the character\'s own deck', () => {
		const character = {
			id: 'Berserker',
			characterState: {
				modifierDeck: deck([{ gfx: 'curse' }, { gfx: 'curse' }])
			}
		} as never;
		expect(characterStatCounters(character).curse).toBe(2);
	});
});

describe('i18n', () => {
	it('translates English keys', () => {
		expect(t('draw')).toBe('Draw');
		expect(t('menuAddCharacter')).toBe('Add Character');
	});

	it('substitutes {vars}', () => {
		expect(t('cmdAddMonster', { name: 'Zombie' })).toBe('Add Zombie');
		expect(t('cmdIncreaseXp', { figure: 'Berserker', amount: 10 })).toBe(
			"Increase Berserker's xp by 10"
		);
	});

	it('returns the key for unknown keys', () => {
		expect(t('noSuchKey')).toBe('noSuchKey');
	});

	it('offers every in-game locale; only English has a UI table', () => {
		// Phase 4: in-game card text is translated for all locales, so the
		// picker lists them all; the UI strings fall back to English.
		expect(availableLocales).toEqual([
			'en',
			'de',
			'es',
			'fr',
			'ko',
			'pl',
			'ru',
			'th',
			'zh',
			'zh_Hant'
		]);
		expect(hasLocaleKey('draw')).toBe(true);
	});
});