import assert from 'node:assert/strict' import { test } from 'node:test' import { THEME_BOOT_JS, THEME_KEY, parseTheme, resolveThemePreference } from './theme.ts' import { renderShell } from './ui.ts' import { CSS } from './styles.ts' test('parseTheme falls back to system', () => { assert.equal(parseTheme('light'), 'light') assert.equal(parseTheme('dark'), 'dark') assert.equal(parseTheme('system'), 'system') assert.equal(parseTheme('neon'), 'system') assert.equal(parseTheme(null), 'system') }) test('resolveThemePreference follows the system only for system', () => { assert.equal(resolveThemePreference('system', true), 'dark') assert.equal(resolveThemePreference('system', false), 'light') assert.equal(resolveThemePreference('light', true), 'light') assert.equal(resolveThemePreference('dark', false), 'dark') assert.equal(resolveThemePreference('junk', true), 'dark') }) function runBoot(stored: string | null, systemDark: boolean, throwStorage = false) { const attrs: Record = {} const root = { style: {} as Record, setAttribute: (k: string, v: string) => (attrs[k] = v) } const localStorage = { getItem: (k: string) => { if (throwStorage) throw new Error('blocked') return k === THEME_KEY ? stored : null }, } const matchMedia = () => ({ matches: systemDark }) new Function('document', 'localStorage', 'matchMedia', 'window', THEME_BOOT_JS)( { documentElement: root }, localStorage, matchMedia, { matchMedia }, ) return { theme: attrs['data-theme'], pref: attrs['data-theme-pref'], scheme: root.style.colorScheme } } test('head boot script resolves the stored preference', () => { assert.deepEqual(runBoot('dark', false), { theme: 'dark', pref: 'dark', scheme: 'dark' }) assert.deepEqual(runBoot('light', true), { theme: 'light', pref: 'light', scheme: 'light' }) assert.deepEqual(runBoot(null, true), { theme: 'dark', pref: 'system', scheme: 'dark' }) assert.deepEqual(runBoot('bogus', false), { theme: 'light', pref: 'system', scheme: 'light' }) // Blocked storage leaves the CSS light default untouched. assert.deepEqual(runBoot('dark', true, true), { theme: undefined, pref: undefined, scheme: undefined }) }) test('boot script runs before the stylesheet and theme controls render', () => { const html = renderShell({ basePath: '', view: 'settings', title: 'Settings' }) const boot = html.indexOf(THEME_BOOT_JS) assert.ok(boot > 0 && boot < html.indexOf('