summaryrefslogtreecommitdiff
path: root/web/test/gameDataClient.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/test/gameDataClient.test.ts')
-rw-r--r--web/test/gameDataClient.test.ts209
1 files changed, 209 insertions, 0 deletions
diff --git a/web/test/gameDataClient.test.ts b/web/test/gameDataClient.test.ts
new file mode 100644
index 0000000..4249283
--- /dev/null
+++ b/web/test/gameDataClient.test.ts
@@ -0,0 +1,209 @@
+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'
+ },
+ {
+ 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'
+ },
+ 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(
+ []
+ );
+ });
+});