summaryrefslogtreecommitdiff
path: root/web/test
diff options
context:
space:
mode:
Diffstat (limited to 'web/test')
-rw-r--r--web/test/fixtures.ts30
-rw-r--r--web/test/i18n.test.ts92
-rw-r--r--web/test/protocol.test.ts38
-rw-r--r--web/test/settings.test.ts112
-rw-r--r--web/test/state.test.ts97
5 files changed, 357 insertions, 12 deletions
diff --git a/web/test/fixtures.ts b/web/test/fixtures.ts
index 805634e..94f402e 100644
--- a/web/test/fixtures.ts
+++ b/web/test/fixtures.ts
@@ -8,7 +8,8 @@ import type {
GameState,
LootDeck,
ModifierDeck,
- SanctuaryDeck
+ SanctuaryDeck,
+ SettingsPayload
} from '$lib/protocol/types';
import { LOOT_POOL_KEYS } from '$lib/protocol/types';
@@ -65,6 +66,33 @@ export function emptySanctuaryDeck(): SanctuaryDeck {
return { multPile: [], flipPile: [] };
}
+/**
+ * A server settings payload in its default shape (the server's
+ * `Settings.forClient()` defaults).
+ */
+export function emptySettings(
+ overrides: Partial<SettingsPayload> = {}
+): SettingsPayload {
+ return {
+ noInit: true,
+ noStandees: false,
+ randomStandees: false,
+ expireConditions: true,
+ autoAddStandees: true,
+ autoAddSpawns: true,
+ hideLootDeck: false,
+ style: 2,
+ showBattleGoalReminder: true,
+ showReminders: true,
+ showAmdDeck: true,
+ fhHazTerrainCalcInOGGloom: true,
+ locale: 'en',
+ saves: [],
+ characterSaves: [],
+ ...overrides
+ };
+}
+
export function emptyGameState(
overrides: Partial<GameState> = {}
): GameState {
diff --git a/web/test/i18n.test.ts b/web/test/i18n.test.ts
new file mode 100644
index 0000000..764b501
--- /dev/null
+++ b/web/test/i18n.test.ts
@@ -0,0 +1,92 @@
+/**
+ * i18n (Phase 6): all ten locale tables load, `t()` falls back to English
+ * (mirroring gen-l10n), and the locale store switches the active table.
+ */
+import { describe, expect, it } from 'vitest';
+import { get } from 'svelte/store';
+import { en } from '$lib/i18n/en';
+import { de } from '$lib/i18n/de';
+import { es } from '$lib/i18n/es';
+import { fr } from '$lib/i18n/fr';
+import { ko } from '$lib/i18n/ko';
+import { pl } from '$lib/i18n/pl';
+import { ru } from '$lib/i18n/ru';
+import { th } from '$lib/i18n/th';
+import { zh } from '$lib/i18n/zh';
+import { zh_Hant } from '$lib/i18n/zh_Hant';
+import {
+ availableLocales,
+ locale,
+ t,
+ hasLocaleKey,
+ type Locale
+} from '$lib/i18n/index';
+
+const tables: Record<Locale, Record<string, string>> = {
+ en,
+ de,
+ es,
+ fr,
+ ko,
+ pl,
+ ru,
+ th,
+ zh,
+ zh_Hant
+};
+
+describe('i18n', () => {
+ it('offers the ten locales from the reference app', () => {
+ expect(availableLocales).toEqual([
+ 'en',
+ 'de',
+ 'es',
+ 'fr',
+ 'ko',
+ 'pl',
+ 'ru',
+ 'th',
+ 'zh',
+ 'zh_Hant'
+ ]);
+ });
+
+ it('covers every table with non-empty strings (en ⊇ others, as in the arbs)', () => {
+ for (const l of availableLocales) {
+ const table = tables[l];
+ const entries = Object.entries(table);
+ expect(entries.length, `${l} has keys`).toBeGreaterThan(100);
+ for (const [key, value] of entries) {
+ expect(typeof value, `${l}.${key}`).toBe('string');
+ expect((value as string).length, `${l}.${key} non-empty`).toBeGreaterThan(0);
+ }
+ }
+ });
+
+ it('translates from the active locale and falls back to English', () => {
+ locale.set('de');
+ // `settingsNoInit` exists in both en and de (with different text).
+ expect(t('settingsNoInit')).toBe(de['settingsNoInit']);
+ expect(t('settingsNoInit')).not.toBe(en['settingsNoInit']);
+ locale.set('en');
+ });
+
+ it('falls back to English for a key a locale lacks', () => {
+ locale.set('de');
+ // A web-only key: only in the English table.
+ expect(t('styleGloomhaven')).toBe('Gloomhaven');
+ expect(hasLocaleKey('styleGloomhaven')).toBe(false);
+ expect(hasLocaleKey('settingsNoInit')).toBe(true);
+ locale.set('en');
+ });
+
+ it('returns the key itself when it is unknown everywhere', () => {
+ expect(t('noSuchKeyAnywhere')).toBe('noSuchKeyAnywhere');
+ });
+
+ it('substitutes variables', () => {
+ expect(t('undoWithDescription', { description: 'Add Blinkblade' })).toBe(
+ 'Undo: Add Blinkblade'
+ );
+ });
+});
diff --git a/web/test/protocol.test.ts b/web/test/protocol.test.ts
index e12df4e..23ca984 100644
--- a/web/test/protocol.test.ts
+++ b/web/test/protocol.test.ts
@@ -2,8 +2,15 @@ import { describe, expect, it } from 'vitest';
import {
claimMessage,
commandMessage,
+ deleteCharacterSaveMessage,
+ deleteSaveMessage,
helloMessage,
- pingMessage
+ loadCharacterSaveMessage,
+ loadSaveMessage,
+ pingMessage,
+ saveCharacterMessage,
+ saveMessage,
+ setSettingMessage
} from '$lib/protocol/types';
describe('wire protocol builders', () => {
@@ -36,4 +43,33 @@ describe('wire protocol builders', () => {
});
expect(pingMessage()).toEqual({ t: 'ping' });
});
+
+ it('builds the Phase 6 settings/save messages', () => {
+ expect(setSettingMessage('locale', 'de')).toEqual({
+ t: 'setSetting',
+ name: 'locale',
+ value: 'de'
+ });
+ expect(setSettingMessage('style', 1)).toEqual({
+ t: 'setSetting',
+ name: 'style',
+ value: 1
+ });
+ expect(saveMessage('alpha')).toEqual({ t: 'save', name: 'alpha' });
+ expect(loadSaveMessage('alpha')).toEqual({ t: 'loadSave', name: 'alpha' });
+ expect(deleteSaveMessage('alpha')).toEqual({ t: 'deleteSave', name: 'alpha' });
+ expect(saveCharacterMessage('Blinky', 'Blinkblade')).toEqual({
+ t: 'saveCharacter',
+ name: 'Blinky',
+ characterId: 'Blinkblade'
+ });
+ expect(loadCharacterSaveMessage('Blinky\nBlinkblade')).toEqual({
+ t: 'loadCharacterSave',
+ saveId: 'Blinky\nBlinkblade'
+ });
+ expect(deleteCharacterSaveMessage('Blinky\nBlinkblade')).toEqual({
+ t: 'deleteCharacterSave',
+ saveId: 'Blinky\nBlinkblade'
+ });
+ });
});
diff --git a/web/test/settings.test.ts b/web/test/settings.test.ts
new file mode 100644
index 0000000..09a28a9
--- /dev/null
+++ b/web/test/settings.test.ts
@@ -0,0 +1,112 @@
+/**
+ * Client-side settings (Phase 6): the localStorage round-trip, the corrupt
+ * / partial-file tolerance, and the server-driven style theme.
+ */
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { get } from 'svelte/store';
+import { emptySettings } from './fixtures';
+
+function fakeStorage(): Storage {
+ const map = new Map<string, string>();
+ return {
+ getItem: (k) => map.get(k) ?? null,
+ setItem: (k, v) => void map.set(k, String(v)),
+ removeItem: (k) => void map.delete(k),
+ clear: () => map.clear(),
+ key: (i) => [...map.keys()][i] ?? null,
+ get length() {
+ return map.size;
+ }
+ };
+}
+
+/** Stub `window.localStorage` and freshly import the settings module. */
+async function importSettings(withStored?: string) {
+ const storage = fakeStorage();
+ if (withStored) storage.setItem('x-haven-settings', withStored);
+ vi.stubGlobal('window', { localStorage: storage });
+ vi.resetModules();
+ const mod = await import('$lib/settings');
+ return { mod, storage };
+}
+
+describe('client settings', () => {
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.resetModules();
+ });
+
+ it('falls back to the defaults without stored data', async () => {
+ const { mod } = await importSettings();
+ expect(get(mod.clientSettings)).toEqual(mod.CLIENT_SETTINGS_DEFAULTS);
+ });
+
+ it('restores stored values and keeps defaults for the rest', async () => {
+ const { mod } = await importSettings(
+ JSON.stringify({ mainListScaling: 1.5, powerMode: 'dimWhenIdle' })
+ );
+ expect(get(mod.clientSettings).mainListScaling).toBe(1.5);
+ expect(get(mod.clientSettings).powerMode).toBe('dimWhenIdle');
+ expect(get(mod.clientSettings).showCustomContent).toBe(true);
+ expect(get(mod.clientSettings).barScaling).toBe(1.0);
+ });
+
+ it('tolerates a corrupt stored value', async () => {
+ const { mod } = await importSettings('{not json');
+ expect(get(mod.clientSettings)).toEqual(mod.CLIENT_SETTINGS_DEFAULTS);
+ });
+
+ it('ignores wrong-typed fields in a partial file', async () => {
+ const { mod } = await importSettings(
+ JSON.stringify({ mainListScaling: 'big', fullScreen: 1, powerMode: 'nope' })
+ );
+ const s = get(mod.clientSettings);
+ expect(s.mainListScaling).toBe(1.0);
+ expect(s.fullScreen).toBe(true);
+ expect(s.powerMode).toBe('normal');
+ });
+
+ it('persists changes to localStorage', async () => {
+ const { mod, storage } = await importSettings();
+ mod.setClientSetting('mainListScaling', 2.2);
+ expect(get(mod.clientSettings).mainListScaling).toBe(2.2);
+ const stored = JSON.parse(storage.getItem('x-haven-settings') ?? '{}');
+ expect(stored.mainListScaling).toBe(2.2);
+ });
+});
+
+describe('style theme', () => {
+ beforeEach(() => {
+ vi.resetModules();
+ });
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it('is the localStorage fallback before the first welcome', async () => {
+ const storage = fakeStorage();
+ storage.setItem('x-haven-style', 'gloomhaven');
+ vi.stubGlobal('window', { localStorage: storage });
+ vi.resetModules();
+ const mod = await import('$lib/settings');
+ expect(get(mod.styleTheme)).toBe('gloomhaven');
+ });
+
+ it('follows the server settings once a welcome has arrived', async () => {
+ const { mod } = await importSettings();
+ expect(get(mod.styleTheme)).toBe('frosthaven');
+ mod.serverSettings.set(emptySettings({ style: 0 }));
+ expect(get(mod.styleTheme)).toBe('frosthaven');
+ mod.serverSettings.set(emptySettings({ style: 1 }));
+ expect(get(mod.styleTheme)).toBe('gloomhaven');
+ mod.serverSettings.set(emptySettings({ style: 2 }));
+ expect(get(mod.styleTheme)).toBe('original');
+ });
+
+ it('maps style indices with styleIndex()', async () => {
+ const { mod } = await importSettings();
+ expect(mod.styleIndex('frosthaven')).toBe(0);
+ expect(mod.styleIndex('gloomhaven')).toBe(1);
+ expect(mod.styleIndex('original')).toBe(2);
+ });
+});
diff --git a/web/test/state.test.ts b/web/test/state.test.ts
index c422810..d1f9b99 100644
--- a/web/test/state.test.ts
+++ b/web/test/state.test.ts
@@ -8,9 +8,10 @@ import {
} from 'vitest';
import { get } from 'svelte/store';
import { GameSession, ownedIds } from '$lib/state/session';
-import type { GameState } from '$lib/protocol/types';
+import type { GameState, UpdateMessage } from '$lib/protocol/types';
+import { serverSettings } from '$lib/settings';
import { SocketSequence } from './fake-socket';
-import { emptyGameState } from './fixtures';
+import { emptyGameState, emptySettings } from './fixtures';
function makeState(overrides: Partial<GameState> = {}): GameState {
return emptyGameState(overrides);
@@ -30,9 +31,31 @@ function welcome(
role,
index,
description: 'last action',
+ historyLength: index + 1,
+ redoDescription: null,
+ undoDescription: 'last action',
event: null,
state,
- seats
+ seats,
+ settings: emptySettings()
+ };
+}
+
+function update(
+ state: GameState,
+ index = 8,
+ overrides: Partial<UpdateMessage> = {}
+): UpdateMessage {
+ return {
+ t: 'update',
+ index,
+ description: 'an action',
+ historyLength: index + 1,
+ redoDescription: null,
+ undoDescription: 'an action',
+ event: { type: 'none' },
+ state,
+ ...overrides
};
}
@@ -42,6 +65,7 @@ describe('GameSession', () => {
beforeEach(() => {
vi.useFakeTimers();
+ serverSettings.set(null);
sockets = new SocketSequence();
session = new GameSession({
transport: sockets.factory,
@@ -81,17 +105,32 @@ describe('GameSession', () => {
expect(get(session.status)).toBe('open');
});
+ it('applies a welcome settings payload and history metadata', () => {
+ welcomeWith(makeState({ round: 3 }), 'sess1', 'player', { B: 'sess1' });
+ expect(get(serverSettings)?.locale).toBe('en');
+ expect(get(serverSettings)?.saves).toEqual([]);
+ expect(get(session.historyLength)).toBe(8);
+ expect(get(session.undoDescription)).toBe('last action');
+ expect(get(session.redoDescription)).toBeNull();
+ });
+
it('replaces state and index on an update', () => {
welcomeWith(makeState({ round: 4 }));
- sockets.current.serverSend({
- t: 'update',
- index: 8,
- description: 'x',
- event: { type: 'none' },
- state: makeState({ round: 5 })
- });
+ sockets.current.serverSend(update(makeState({ round: 5 }), 8));
expect(get(session.state)?.round).toBe(5);
expect(get(session.index)).toBe(8);
+ expect(get(session.historyLength)).toBe(9);
+ expect(get(session.undoDescription)).toBe('an action');
+ });
+
+ it('applies a settings broadcast', () => {
+ welcomeWith(makeState());
+ sockets.current.serverSend({
+ t: 'settings',
+ settings: emptySettings({ locale: 'de', saves: ['alpha'] })
+ });
+ expect(get(serverSettings)?.locale).toBe('de');
+ expect(get(serverSettings)?.saves).toEqual(['alpha']);
});
it('owns() is true only for the session\'s characters', () => {
@@ -148,6 +187,44 @@ describe('GameSession', () => {
characterId: 'Berserker'
});
});
+
+ it('sends the Phase 6 settings/save messages', () => {
+ welcomeWith(makeState());
+ session.setSetting('noInit', false);
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'setSetting',
+ name: 'noInit',
+ value: false
+ });
+ session.setSetting('style', 0);
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'setSetting',
+ name: 'style',
+ value: 0
+ });
+ session.save('alpha');
+ expect(sockets.current.lastJson()).toEqual({ t: 'save', name: 'alpha' });
+ session.loadSave('alpha');
+ expect(sockets.current.lastJson()).toEqual({ t: 'loadSave', name: 'alpha' });
+ session.deleteSave('alpha');
+ expect(sockets.current.lastJson()).toEqual({ t: 'deleteSave', name: 'alpha' });
+ session.saveCharacter('Blinky 2', 'Blinkblade');
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'saveCharacter',
+ name: 'Blinky 2',
+ characterId: 'Blinkblade'
+ });
+ session.loadCharacterSave('Blinky 2\nBlinkblade');
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'loadCharacterSave',
+ saveId: 'Blinky 2\nBlinkblade'
+ });
+ session.deleteCharacterSave('Blinky 2\nBlinkblade');
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'deleteCharacterSave',
+ saveId: 'Blinky 2\nBlinkblade'
+ });
+ });
});
describe('ownedIds', () => {