first pass analytics redo

This commit is contained in:
George Powell committed 2026-09-22 12:15:56 -04:00
1 parent b656fd04c0
commit 45ffa39f2a
69 files changed
+6400 -1138

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
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 });
});
});