import { test } from 'node:test'; import assert from 'node:assert/strict'; import { COLOR_TOKENS, parseCustomTheme } from '../src/lib/theme/customThemes.ts'; import { applyTransparency, normalizeAppearance, exportAppearanceTheme, BUILTIN_THEMES, themeModes } from '../src/lib/theme/appearance.ts'; const palette = { bg: '#123', text: '#abcdef', accent: '#AABBCC' }; const theme = { schema: 2, id: 'my-theme', name: 'My theme', colors: palette }; test('Fixed theme palettes retain all supplied colors', () => { assert.deepEqual(parseCustomTheme(theme), theme); }); test('Legacy import selects the current appearance and validates both palettes', () => { const light = { ...palette, bg: '#fff' }; const legacy = { schema: 1, id: theme.id, name: theme.name, light, dark: palette }; assert.deepEqual(parseCustomTheme(legacy).colors, palette); assert.deepEqual(parseCustomTheme(legacy, 'light').colors, light); assert.throws(() => parseCustomTheme({ ...legacy, light: {} })); }); test('Theme files reject CSS injection, unknown tokens, and incomplete palettes', () => { for (const colors of [{ ...palette, bg: 'url(https://invalid.test)' }, { ...palette, position: 'fixed' }, { bg: '#000' }, []]) { assert.throws(() => parseCustomTheme({ ...theme, colors })); } assert.throws(() => parseCustomTheme({ ...theme, id: '../unsafe' })); }); test('Every built-in template contains every current color token and one palette', () => { for (const item of BUILTIN_THEMES) for (const mode of themeModes(item.id)) { const exported = exportAppearanceTheme(item.id, mode); assert.deepEqual(Object.keys(exported.colors).sort(), [...COLOR_TOKENS].sort()); assert.deepEqual(parseCustomTheme(exported), exported); assert.equal('light' in exported, false); assert.equal('dark' in exported, false); } assert.notDeepEqual(exportAppearanceTheme('default', 'light').colors, exportAppearanceTheme('default', 'dark').colors); }); test('Transparency normalizes old, invalid, and extreme saved values', () => { assert.equal(normalizeAppearance(null).transparency, undefined); assert.deepEqual(normalizeAppearance({ transparency: { enabled: false, amount: 27 } }).transparency, { enabled: false, amount: 27 }); for (const [amount, expected] of [[-10, 0], [55, 55], [100, 100], [120, 100], [NaN, 18], [Infinity, 18], [12.8, 13]]) { assert.equal(normalizeAppearance({ transparency: { amount } }).transparency?.amount, expected); } }); test('Theme font roles round-trip and reject CSS injection', () => { const fonts = { ui: 'Arial, sans-serif', prose: '"Times New Roman", serif', heading: 'Georgia, serif', mono: 'Consolas, monospace' }; assert.deepEqual(parseCustomTheme({ ...theme, fonts }).fonts, fonts); for (const fonts of [{ ui: 'url(https://example.test)' }, { ui: 'Arial; color: red' }, { unknown: 'Arial' }, { ui: '' }, { ui: 'Arial,,serif' }, { ui: '"Arial' }, []]) { assert.throws(() => parseCustomTheme({ ...theme, fonts })); } for (const item of BUILTIN_THEMES) { const exported = exportAppearanceTheme(item.id); assert.deepEqual(Object.keys(exported.fonts!).sort(), ['heading', 'mono', 'prose', 'ui']); assert.deepEqual(parseCustomTheme(exported), exported); } assert.match(exportAppearanceTheme('claude').fonts!.heading!, /Poppins/); assert.match(exportAppearanceTheme('harvest-moon').fonts!.ui!, /Candara/); }); test('Full transparency range produces valid surface opacity values', () => { const values = new Map(); const root = { dataset: {}, style: { setProperty: (key: string, value: string) => values.set(key, value) } } as unknown as HTMLElement; for (const amount of [0, 18, 40, 55, 99, 100]) { applyTransparency({ enabled: true, amount }, root); assert.equal(values.get('--rs-surface-opacity'), `${100 - amount}%`); for (const key of ['--rs-surface-opacity', '--rs-editor-surface-opacity', '--rs-hover-surface-opacity']) { const opacity = parseFloat(values.get(key)!); assert(opacity >= 0 && opacity <= 100); } } }); test('Opaque mode retains background ownership and the amount for re-enabling', () => { const values = new Map(); const root = { dataset: {} as Record, style: { setProperty: (key: string, value: string) => values.set(key, value) } } as unknown as HTMLElement; applyTransparency({ enabled: false, amount: 32 }, root); assert.equal(values.get('--rs-surface-opacity'), '100%'); assert.equal(values.get('--rs-editor-surface-opacity'), '100%'); assert.equal(values.get('--rs-transparent-plugin-bg'), 'transparent'); assert.equal(root.dataset.transparency, 'off'); applyTransparency({ enabled: true, amount: 18 }, root); assert.equal(values.get('--rs-surface-opacity'), '82%'); assert.equal(values.get('--rs-editor-surface-opacity'), '90%'); assert.equal(root.dataset.transparency, 'on'); }); test('Saved legacy light themes migrate once and preserve both original palettes', async () => { const { customThemes, CUSTOM_THEMES_KEY } = await import('../src/lib/theme/customThemes.ts'); const legacy = { schema: 1, id: 'saved-light', name: 'Saved light', light: { ...palette, bg: '#fff' }, dark: { ...palette, bg: '#111' } }; const stored = new Map([[CUSTOM_THEMES_KEY, JSON.stringify([legacy])], ['runestone.appearance', JSON.stringify({ theme: 'custom:saved-light', mode: 'light' })]]); const previous = globalThis.localStorage; try { globalThis.localStorage = { getItem: (key: string) => stored.get(key) ?? null, setItem: (key: string, value: string) => stored.set(key, value) } as Storage; assert.deepEqual(customThemes()[0].colors, legacy.light); assert.deepEqual(themeModes('custom:saved-light'), ['light']); assert.equal(normalizeAppearance({ theme: 'custom:saved-light', mode: 'dark' }).mode, 'light'); assert.deepEqual(exportAppearanceTheme('custom:saved-light').colors.bg, '#fff'); assert.equal(JSON.parse(stored.get(CUSTOM_THEMES_KEY)!)[0].schema, 2); assert.deepEqual(JSON.parse(stored.get(`${CUSTOM_THEMES_KEY}.legacy`)!)[0], legacy); assert.deepEqual(customThemes('dark')[0].colors, legacy.light); } finally { globalThis.localStorage = previous; } }); test('Legacy migration storage failure leaves the original palette available', async () => { const { customThemes, CUSTOM_THEMES_KEY } = await import('../src/lib/theme/customThemes.ts'); const legacy = { schema: 1, id: 'quota-light', name: 'Quota light', light: { ...palette, bg: '#fefefe' }, dark: palette }; const raw = JSON.stringify([legacy]); const previous = globalThis.localStorage; let original = raw; try { globalThis.localStorage = { getItem: (key: string) => key === CUSTOM_THEMES_KEY ? original : null, setItem: () => { throw new Error('Quota exceeded'); } } as unknown as Storage; assert.deepEqual(customThemes('light')[0].colors, legacy.light); assert.equal(original, raw); assert.deepEqual(customThemes('dark')[0].colors, legacy.light); } finally { globalThis.localStorage = previous; } });