summaryrefslogtreecommitdiff
path: root/web/test/settings.test.ts
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-19 08:53:51 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-19 08:53:51 +0100
commitb884441ba0d10bdd3559748507a0991888f66194 (patch)
tree074321d353c1153e8d95191eecf5a61b2eec1096 /web/test/settings.test.ts
parentd64cf3514e5ff1ddb1c7f3ddac9a769db3e7dde0 (diff)
Phase 6: settings, saves, localization parity (M6)
Diffstat (limited to 'web/test/settings.test.ts')
-rw-r--r--web/test/settings.test.ts112
1 files changed, 112 insertions, 0 deletions
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);
+ });
+});