summaryrefslogtreecommitdiff
path: root/web/test/gameDataClient.test.ts
blob: d5f0ce2e99887d6b78ddaec8583cb5d65e2220f0 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { describe, it, expect, vi, afterEach } from 'vitest';
import { GameDataClient } from '$lib/data/gameData';

afterEach(() => vi.unstubAllGlobals());

function json(value: unknown) {
	return Promise.resolve({
		ok: true,
		json: () => Promise.resolve(value)
	} as Response);
}

function withFetch(handler: (url: string) => Promise<Response>) {
	// Clients are constructed per test, so the per-instance caches are clean.
	vi.stubGlobal('fetch', (input: RequestInfo | URL) => {
		const url = typeof input === 'string' ? input : input.toString();
		return handler(url);
	});
}

const ROOMS = {
	'1': [
		{ 'A Town in Flames': {} },
		{ 'The Cellar': {} },
		{ 'Spawn Alley': {} },
		{ 'The Chapel': {} }
	],
	'4A': [
		{ 'Frostbite Landing': {} },
		{ 'Frozen Harbor': {} }
	]
};

describe('GameDataClient.campaigns', () => {
	it('reads the campaign list from editions.json', async () => {
		withFetch((url) => {
			expect(url).toBe('/data/editions/editions.json');
			return json({ editions: ['Gloomhaven', 'Jaws of the Lion'] });
		});
		const data = new GameDataClient();
		await expect(data.campaigns()).resolves.toEqual([
			'Gloomhaven',
			'Jaws of the Lion'
		]);
	});

	it('caches after the first load', async () => {
		let calls = 0;
		withFetch(() => {
			calls++;
			return json({ editions: ['Gloomhaven'] });
		});
		const data = new GameDataClient();
		await data.campaigns();
		await data.campaigns();
		expect(calls).toBe(1);
	});
});

describe('GameDataClient.loadCampaign', () => {
	it('maps classes, monsters and scenarios', async () => {
		withFetch((url) => {
			expect(url).toBe('/data/editions/Jaws%20of%20the%20Lion.json');
			return json({
				edition: 'Jaws of the Lion',
				classes: [
					{
						name: 'Berserker',
						edition: 'Jaws of the Lion',
						health: [12, 14],
						hidden: false,
						color: 'FF35ACD5',
						colorSecondary: 'ff313959',
						perks: [{ adds: ['berserker-1'], removes: ['minus1'] }],
						perksFH: [{ adds: ['berserker-fh'] }]
					},
					{
						name: 'Secret',
						edition: 'Jaws of the Lion',
						health: [10],
						hidden: true,
						color: ['FF112233']
					}
				],
				monsters: {
					'Zombie': {
						deck: 'Zombie',
						count: 4,
						levels: [
							{ level: 0, normal: { health: 3 } },
							{ level: 2, normal: { health: 5 }, elite: { health: 8 } }
						]
					},
					'Dreadnaught': {
						deck: 'Dreadnaught',
						count: 2,
						display: 'Dreadnaught (Boss)',
						levels: [{ level: 0, boss: { health: 40 } }],
						gfx: 'Dreadnaught'
					}
				},
				scenarios: {
					'#1 A Town in Flames': { monsters: ['Zombie'] },
					'#2 A Dark Night': {}
				}
			});
		});
		const data = new GameDataClient();
		const campaign = await data.loadCampaign('Jaws of the Lion');
		expect(campaign.edition).toBe('Jaws of the Lion');
		expect(campaign.classes).toEqual([
			{
				id: 'Berserker',
				name: 'Berserker',
				edition: 'Jaws of the Lion',
				hidden: false,
				health: [12, 14],
				color: 'FF35ACD5',
				colorSecondary: 'ff313959',
				icon: '/images/class-icons/Berserker.png',
				perks: [['berserker-1']],
				perksFH: [['berserker-fh']],
				perkDetails: [{ text: '', adds: ['berserker-1'], removes: ['minus1'] }],
				perkDetailsFH: [{ text: '', adds: ['berserker-fh'], removes: [] }]
			},
			expect.objectContaining({
				id: 'Secret',
				hidden: true,
				color: 'FF112233' // first of an array
			})
		]);
		const zombie = campaign.monsters.find((m) => m.name === 'Zombie');
		expect(zombie).toMatchObject({
			display: 'Zombie',
			gfx: 'Zombie',
			count: 4,
			maxLevel: 2,
			isBoss: false,
			portrait: '/images/monsters/Zombie.png'
		});
		const boss = campaign.monsters.find((m) => m.name === 'Dreadnaught');
		expect(boss).toMatchObject({
			display: 'Dreadnaught (Boss)',
			maxLevel: 0,
			isBoss: true
		});
		expect(campaign.scenarios.map((s) => s.name)).toEqual([
			'#1 A Town in Flames',
			'#2 A Dark Night'
		]);
		expect(campaign.scenarios[0].monsters).toEqual(['Zombie']);
	});

	it('caches per campaign (one fetch per campaign)', async () => {
		let calls = 0;
		withFetch(() => {
			calls++;
			return json({ edition: 'Gloomhaven', classes: [], monsters: {}, scenarios: {} });
		});
		const data = new GameDataClient();
		await data.loadCampaign('Gloomhaven');
		await data.loadCampaign('Gloomhaven');
		expect(calls).toBe(1);
	});

	it('rejects on a missing campaign', async () => {
		withFetch(() =>
			Promise.resolve({ ok: false, json: () => Promise.reject(new Error('nope')) } as Response)
		);
		const data = new GameDataClient();
		await expect(data.loadCampaign('Nope')).rejects.toThrow(
			'failed to load campaign Nope'
		);
	});
});

describe('GameDataClient.sections', () => {
	it('lists the addable rooms for a scenario number', async () => {
		withFetch((url) => {
			expect(url).toBe('/data/rooms/Jaws%20of%20the%20Lion.json');
			return json(ROOMS);
		});
		const data = new GameDataClient();
		await expect(data.sections('Jaws of the Lion', '#1 A Town in Flames')).resolves.toEqual([
			'The Cellar',
			'The Chapel'
		]); // first room skipped, "Spawn Alley" filtered
	});

	it('handles lettered scenario numbers', async () => {
		withFetch(() => json(ROOMS));
		const data = new GameDataClient();
		await expect(data.sections('Jaws of the Lion', '#4A Frostbite')).resolves.toEqual([
			'Frozen Harbor'
		]);
	});

	it('returns empty for unknown scenarios', async () => {
		withFetch(() => json(ROOMS));
		const data = new GameDataClient();
		await expect(data.sections('Jaws of the Lion', '#99 Nowhere')).resolves.toEqual(
			[]
		);
	});

	it('returns empty when the rooms file is missing', async () => {
		withFetch(() =>
			Promise.resolve({ ok: false, json: () => Promise.reject(new Error('nope')) } as Response)
		);
		const data = new GameDataClient();
		await expect(data.sections('Jaws of the Lion', '#1 A Town in Flames')).resolves.toEqual(
			[]
		);
	});
});