/** * 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(); 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); }); });