import { describe, test, expect, beforeEach, afterEach } from 'bun:test'; import { testDb } from '$lib/server/db/test'; import { dashboardSettings } from '$lib/server/db/schema'; import { getSettings, updateSettings, hashSettings, envSettings, settingsEnvVar } from '$lib/server/stats/settings'; import { SETTINGS_DEFAULTS } from '$lib/types/stats'; async function clear() { await testDb.delete(dashboardSettings); } describe('stats settings', () => { beforeEach(clear); afterEach(clear); test('env var names map from keys', () => { expect(settingsEnvVar('d1_target')).toBe('STATS_D1_TARGET'); }); test('empty table returns built-in defaults', async () => { const settings = await getSettings(testDb); expect(settings).toEqual({ ...SETTINGS_DEFAULTS }); }); test('updateSettings upserts and is reflected on re-read', async () => { await updateSettings({ core_plays_per_week: 7 }, testDb); const settings = await getSettings(testDb); expect(settings.core_plays_per_week).toBe(7); // Updating again overwrites rather than duplicating. await updateSettings({ core_plays_per_week: 2 }, testDb); expect((await getSettings(testDb)).core_plays_per_week).toBe(2); }); test('config hash is stable and changes with settings', () => { const a = hashSettings({ ...SETTINGS_DEFAULTS }); const b = hashSettings({ ...SETTINGS_DEFAULTS }); const c = hashSettings({ ...SETTINGS_DEFAULTS, d1_target: 99 }); expect(a).toBe(b); expect(a).not.toBe(c); expect(a).toHaveLength(12); }); test('envSettings falls back to defaults with no env overrides', () => { expect(envSettings()).toEqual({ ...SETTINGS_DEFAULTS }); }); });