import assert from 'node:assert/strict'; import { test } from 'node:test'; import { parseCustomTheme } from '../src/lib/theme/customThemes.ts'; import { CHARACTER_THEMES, SYNTAX_ROLES } from '../src/lib/theme/characterThemes.ts'; import { parseAppearance, normalizeAppearance, exportAppearanceTheme, BUILTIN_THEMES, THEMES, themeModes } from '../src/lib/theme/appearance.ts'; test('Removed palettes preserve the saved mode with the default palette', () => { for (const theme of ['gpt', 'terminal', 'unknown', 'renamon', 'asuka-school']) { for (const mode of ['light', 'dark'] as const) { assert.deepEqual(parseAppearance(JSON.stringify({ theme, mode })), { theme: 'default', mode }); } } assert.deepEqual(THEMES, ['default', 'claude', 'sakura', 'harvest-moon', 'crimson-pilot', 'silver-serpent']); assert.deepEqual(BUILTIN_THEMES.map(theme => theme.id), THEMES); }); test('Renamed palettes preserve saved choices and backgrounds', () => { const newTabBackground = { imageId: '01234567-89ab-cdef-0123-456789abcdef', position: 'top left', size: 'auto', overlay: -0.4 }; for (const [previous, theme] of [['holo', 'harvest-moon'], ['asuka', 'crimson-pilot'], ['malfoid', 'silver-serpent']]) { for (const mode of ['light', 'dark']) { assert.deepEqual(parseAppearance(JSON.stringify({ theme: previous, mode, newTabBackground })), { theme, mode: 'dark', newTabBackground }); } } for (const theme of ['constructor', '__proto__', 'toString']) { assert.deepEqual(normalizeAppearance({ theme, mode: 'light' }), { theme: 'default', mode: 'light' }); } }); test('Removed character themes retain the saved background', () => { const newTabBackground = { imageId: '01234567-89ab-cdef-0123-456789abcdef', position: 'top left', size: 'auto', overlay: -0.4 }; for (const theme of ['renamon', 'asuka-school']) { assert.deepEqual(parseAppearance(JSON.stringify({ theme, mode: 'light', newTabBackground })), { theme: 'default', mode: 'light', newTabBackground }); } }); function luminance(color: string) { const rgb = color.slice(1).match(/../g)!.map(value => parseInt(value, 16) / 255) .map(value => value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4); return rgb[0] * 0.2126 + rgb[1] * 0.7152 + rgb[2] * 0.0722; } function contrast(foreground: string, background: string) { const values = [luminance(foreground), luminance(background)].sort((a, b) => b - a); return (values[0] + 0.05) / (values[1] + 0.05); } test('Character palettes restore one appearance and export readable surfaces and syntax', () => { for (const theme of Object.keys(CHARACTER_THEMES) as (keyof typeof CHARACTER_THEMES)[]) { const exported = exportAppearanceTheme(theme); for (const mode of ['light', 'dark'] as const) { assert.deepEqual(parseAppearance(JSON.stringify({ theme, mode })), { theme, mode: CHARACTER_THEMES[theme].mode }); const colors = exported.colors; for (const foreground of ['text', 'text-dim', 'text-strong', 'accent'] as const) { for (const background of ['bg', 'bg-elevated', 'bg-active', 'selection'] as const) { if (foreground === 'accent' && background === 'selection') continue; assert(contrast(colors[foreground]!, colors[background]!) >= 4.5, `${theme} ${mode}: ${foreground} on ${background}`); } } } assert.deepEqual(themeModes(theme), [CHARACTER_THEMES[theme].mode]); assert.equal(exported.schema, 2); assert.deepEqual(parseCustomTheme(exported), exported); for (const role of SYNTAX_ROLES) { for (const background of ['bg', 'bg-active'] as const) { assert(contrast(exported.colors[`syntax-${role}`]!, exported.colors[background]!) >= 4.5, `${theme}: ${role} on ${background}`); } } assert(new Set(SYNTAX_ROLES.map(role => exported.colors[`syntax-${role}`])).size >= 7); assert.notDeepEqual(exported.colors, exportAppearanceTheme('default').colors); } }); test('Sakura cannot restore or save a dark appearance', () => { assert.deepEqual(parseAppearance('{"theme":"sakura","mode":"dark"}'), { theme: 'sakura', mode: 'light' }); assert.deepEqual(normalizeAppearance({ theme: 'sakura', mode: 'dark' }), { theme: 'sakura', mode: 'light' }); }); test('Malformed storage returns a valid default appearance', () => { for (const raw of [null, '', '{', 'null', '42', '[]']) { assert.deepEqual(parseAppearance(raw), { theme: 'default', mode: 'dark' }); } }); test('Saved image layout restores valid choices and rejects CSS injection', () => { const imageId = '01234567-89ab-cdef-0123-456789abcdef'; const restored = parseAppearance(JSON.stringify({ newTabBackground: { imageId, position: 'top left', size: 'auto', overlay: -0.4 } })); assert.deepEqual(restored.newTabBackground, { imageId, position: 'top left', size: 'auto', overlay: -0.4 }); const invalid = normalizeAppearance({ newTabBackground: { imageId, position: 'url(https://example.com)', size: '100000px', overlay: Infinity } }); assert.deepEqual(invalid.newTabBackground, { imageId, position: 'center', size: 'cover', overlay: 0 }); assert.deepEqual(normalizeAppearance({ newTabBackground: { imageId } }).newTabBackground, { imageId, overlay: 0, position: 'center', size: 'cover' }); });