1
This commit is contained in:
@@ -3,6 +3,7 @@ import {afterEach, describe, expect, it, vi} from 'vitest';
|
||||
import {
|
||||
clampVolume,
|
||||
DEFAULT_MUSIC_VOLUME,
|
||||
normalizePlatformTheme,
|
||||
readSavedSettings,
|
||||
writeSavedSettings,
|
||||
} from './gameSettingsStorage';
|
||||
@@ -34,6 +35,7 @@ describe('gameSettingsStorage', () => {
|
||||
|
||||
expect(readSavedSettings()).toEqual({
|
||||
musicVolume: DEFAULT_MUSIC_VOLUME,
|
||||
platformTheme: 'light',
|
||||
});
|
||||
});
|
||||
|
||||
@@ -44,18 +46,38 @@ describe('gameSettingsStorage', () => {
|
||||
|
||||
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)});
|
||||
writeSavedSettings({
|
||||
musicVolume: clampVolume(0.6),
|
||||
platformTheme: 'dark',
|
||||
});
|
||||
|
||||
expect(JSON.parse(storage.getItem('tavernrealms.settings.v1') ?? '{}')).toEqual({
|
||||
version: 1,
|
||||
musicVolume: 0.6,
|
||||
platformTheme: 'dark',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import {
|
||||
DEFAULT_MUSIC_VOLUME,
|
||||
DEFAULT_PLATFORM_THEME,
|
||||
type PlatformTheme,
|
||||
type RuntimeSettings,
|
||||
} from '../../packages/shared/src/contracts/runtime';
|
||||
import {isRecord, readStoredJson, writeStoredJson} from './storage';
|
||||
@@ -22,6 +24,10 @@ export function clampVolume(value: number) {
|
||||
return Math.max(0, Math.min(1, value));
|
||||
}
|
||||
|
||||
export function normalizePlatformTheme(value: unknown): PlatformTheme {
|
||||
return value === 'dark' ? 'dark' : DEFAULT_PLATFORM_THEME;
|
||||
}
|
||||
|
||||
function parseSavedSettings(value: unknown): SavedGameSettings | null {
|
||||
if (!isRecord(value)) {
|
||||
return null;
|
||||
@@ -30,12 +36,14 @@ function parseSavedSettings(value: unknown): SavedGameSettings | null {
|
||||
if (value.version === SETTINGS_STORAGE_VERSION && typeof value.musicVolume === 'number') {
|
||||
return {
|
||||
musicVolume: clampVolume(value.musicVolume),
|
||||
platformTheme: normalizePlatformTheme(value.platformTheme),
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof value.musicVolume === 'number') {
|
||||
return {
|
||||
musicVolume: clampVolume(value.musicVolume),
|
||||
platformTheme: normalizePlatformTheme(value.platformTheme),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -49,6 +57,7 @@ export function readSavedSettings() {
|
||||
parse: parseSavedSettings,
|
||||
}) ?? {
|
||||
musicVolume: DEFAULT_MUSIC_VOLUME,
|
||||
platformTheme: DEFAULT_PLATFORM_THEME,
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -57,6 +66,7 @@ export function writeSavedSettings(settings: SavedGameSettings) {
|
||||
const payload: StoredGameSettings = {
|
||||
version: SETTINGS_STORAGE_VERSION,
|
||||
musicVolume: clampVolume(settings.musicVolume),
|
||||
platformTheme: normalizePlatformTheme(settings.platformTheme),
|
||||
};
|
||||
|
||||
return writeStoredJson({
|
||||
|
||||
Reference in New Issue
Block a user