Files
Genarrative/src/persistence/gameSettingsStorage.test.ts
T
kdletters 071faa482c 统一 Rust 与 TypeScript 格式化门禁
纳入 AGC Cargo workspace 的统一 rustfmt 检查与格式化入口

完成项目 TypeScript/Prettier 与 Rust 全量格式化

修复 Pingora expected executable 门禁的空白敏感误报

同步开发运维文档与 AGC skill pack 格式化忽略规则
2026-09-01 16:28:34 +08:00

89 lines
2.2 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import {
clampVolume,
DEFAULT_MUSIC_VOLUME,
normalizePlatformTheme,
readSavedSettings,
writeSavedSettings,
} from './gameSettingsStorage';
import type { JsonStorage } from './storage';
function createMemoryStorage(): JsonStorage {
const values = new Map<string, string>();
return {
getItem(key) {
return values.has(key) ? values.get(key)! : null;
},
setItem(key, value) {
values.set(key, value);
},
removeItem(key) {
values.delete(key);
},
};
}
describe('gameSettingsStorage', () => {
afterEach(() => {
vi.unstubAllGlobals();
});
it('falls back to defaults when nothing has been saved', () => {
vi.stubGlobal('window', { localStorage: createMemoryStorage() });
expect(readSavedSettings()).toEqual({
musicVolume: DEFAULT_MUSIC_VOLUME,
platformTheme: 'light',
});
});
it('reads legacy unversioned payloads and clamps the volume', () => {
const storage = createMemoryStorage();
storage.setItem(
'tavernrealms.settings.v1',
JSON.stringify({ musicVolume: 2 }),
);
vi.stubGlobal('window', { localStorage: storage });
expect(readSavedSettings()).toEqual({
musicVolume: 1,
platformTheme: 'light',
});
});
it('reads stored platform theme when available', () => {
const storage = createMemoryStorage();
storage.setItem(
'tavernrealms.settings.v1',
JSON.stringify({ musicVolume: 0.5, platformTheme: 'dark' }),
);
vi.stubGlobal('window', { localStorage: storage });
expect(readSavedSettings()).toEqual({
musicVolume: 0.5,
platformTheme: 'dark',
});
expect(normalizePlatformTheme('unknown')).toBe('light');
});
it('writes versioned settings payloads', () => {
const storage = createMemoryStorage();
vi.stubGlobal('window', { localStorage: storage });
writeSavedSettings({
musicVolume: clampVolume(0.6),
platformTheme: 'dark',
});
expect(
JSON.parse(storage.getItem('tavernrealms.settings.v1') ?? '{}'),
).toEqual({
version: 1,
musicVolume: 0.6,
platformTheme: 'dark',
});
});
});