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