Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

theme.test.ts

68 lines · 3.1 KB · TypeScript
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<string, string> = {}
	const root = { style: {} as Record<string, string>, 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('<style>'))
	assert.equal((html.match(/data-theme-choice\/>/g) ?? []).length, 6)
	assert.ok(html.includes('aria-controls="theme-menu"'))
	assert.ok(html.includes('<legend>Appearance</legend>'))
})

test('dark tokens override every light token', () => {
	const block = (sel: string) => CSS.slice(CSS.indexOf(sel), CSS.indexOf('}', CSS.indexOf(sel)))
	const names = (b: string) => new Set([...b.matchAll(/--([a-z0-9-]+):/g)].map((m) => m[1]))
	const light = names(block(':root{'))
	const dark = names(block('[data-theme=dark]{'))
	for (const n of ['r', 'sans', 'serif']) light.delete(n)
	assert.ok(block('[data-theme=dark]{').includes('color-scheme:dark'))
	assert.deepEqual([...light].filter((n) => !dark.has(n)), [])
})