import { test } from 'node:test'; import assert from 'node:assert/strict'; import { parseCustomTheme } from '../../../src/lib/theme/customThemes.ts'; import { ROLES } from '../../../src/lib/theme/roles.ts'; import { BUILTIN_THEMES as THEME_DATA } from '../../../src/lib/theme/themes/index.ts'; import { applyTransparency, normalizeAppearance, exportAppearanceTheme, resolveTheme, 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('Schema 2 palettes convert to schema 3 and retain all supplied colors', () => { assert.deepEqual(parseCustomTheme(theme), { ...theme, schema: 3, mode: 'dark' }); assert.equal(parseCustomTheme({ ...theme, colors: { ...palette, bg: '#fafafa' } }).mode, 'light'); }); test('Old syntax names convert to the schema 3 roles', () => { const colors = { ...palette, 'syntax-local': '#111111', 'syntax-instance': '#222222', 'syntax-global': '#333333', 'syntax-parameter': '#444444', 'syntax-static': '#555555', 'syntax-keyword': '#666666' }; assert.deepEqual(parseCustomTheme({ ...theme, colors }).colors, { ...palette, 'syntax-variable': '#111111', 'syntax-property': '#222222', 'syntax-constant': '#333333', 'syntax-punctuation': '#333333', 'syntax-keyword': '#666666', }); assert.throws(() => parseCustomTheme({ ...theme, colors: { ...palette, 'syntax-variable': '#111111' } }), /Unknown theme color/); }); test('Schema 3 files name their mode and can take missing colors from a built-in base', () => { const file = { schema: 3, id: 'amber', name: 'Amber', mode: 'dark', base: 'holo', colors: { accent: '#00ff00', 'ansi-blue': '#0000ff' } }; assert.deepEqual(parseCustomTheme(file), { ...file, base: 'harvest-moon' }); assert.deepEqual(parseCustomTheme({ ...file, base: undefined, colors: {} }), { schema: 3, id: 'amber', name: 'Amber', mode: 'dark', colors: {} }); for (const invalid of [{ mode: 'dim' }, { mode: undefined }, { base: 'sakura' }, { base: 'unknown' }, { base: 4 }, { colors: [] }, { colors: { 'accent-x': '#000' } }, { colors: { accent: 'red' } }]) { assert.throws(() => parseCustomTheme({ ...file, ...invalid }), JSON.stringify(invalid)); } const previous = globalThis.localStorage; try { const stored = new Map([['runestone.custom-themes', JSON.stringify([file])]]); globalThis.localStorage = { getItem: (key: string) => stored.get(key) ?? null, setItem: (key: string, value: string) => stored.set(key, value) } as unknown as Storage; const resolved = resolveTheme('custom:amber', 'light'); const harvest = THEME_DATA.find(item => item.id === 'harvest-moon')!; assert.equal(resolved.mode, 'dark'); assert.equal(resolved.colors.bg, harvest.variants.dark.bg); assert.equal(resolved.colors.accent, '#00ff00'); assert.equal(resolved.colors['syntax-keyword'], harvest.variants.dark['syntax-keyword']); assert.equal(resolved.colors.link, '#00ff00', 'A derived role follows the colors of the file'); assert.equal(resolved.colors['ansi-blue'], '#0000ff'); assert.match(resolved.fonts.ui, /Candara/); assert.deepEqual(themeModes('custom:amber'), ['dark']); } finally { globalThis.localStorage = previous; } }); 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 role 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), [...ROLES]); assert.equal(exported.mode, mode); 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 { migrateLegacyPreferences } = await import('../../../src/lib/settings/migration.ts'); const { LEGACY_KEYS } = await import('../../../src/lib/settings/store.ts'); const legacy = { schema: 1, id: 'saved-light', name: 'Saved light', light: { ...palette, bg: '#fff' }, dark: { ...palette, bg: '#111' } }; const old = new Map([[CUSTOM_THEMES_KEY, JSON.stringify([legacy])], ['runestone.appearance', JSON.stringify({ theme: 'custom:saved-light', mode: 'light' })]]); const sections = migrateLegacyPreferences(key => old.get(key) ?? null); assert.equal((sections.customThemes as { schema: number }[])[0].schema, 3); assert.deepEqual(sections.customThemesLegacy, [legacy]); assert.deepEqual(sections.appearance, { theme: 'custom:saved-light', mode: 'light' }); // The browser preview store reads the sections from their old keys. const stored = new Map(Object.entries(sections).map(([section, value]) => [LEGACY_KEYS[section as keyof typeof LEGACY_KEYS], JSON.stringify(value)])); const previous = globalThis.localStorage; try { globalThis.localStorage = { getItem: (key: string) => stored.get(key) ?? null, setItem: (key: string, value: string) => stored.set(key, value) } as unknown 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.deepEqual(customThemes('dark')[0].colors, legacy.light, 'The converted theme keeps its palette in both modes'); } finally { globalThis.localStorage = previous; } }); test('Reading custom themes never writes, and an unconverted version 1 theme uses the requested palette', 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 previous = globalThis.localStorage; try { globalThis.localStorage = { getItem: (key: string) => key === CUSTOM_THEMES_KEY ? JSON.stringify([legacy]) : null, setItem: () => { throw new Error('Quota exceeded'); } } as unknown as Storage; assert.deepEqual(customThemes('light')[0].colors, legacy.light); assert.deepEqual(customThemes('dark')[0].colors, palette); assert.deepEqual(customThemes()[0].colors, palette, 'Without a saved appearance, the dark palette applies'); } finally { globalThis.localStorage = previous; } });