import assert from 'node:assert/strict'; import { test } from 'node:test'; import { parseCustomTheme } from '../../../src/lib/theme/customThemes.ts'; import { SYNTAX_ROLES } from '../../../src/lib/theme/roles.ts'; import { parseAppearance, normalizeAppearance, exportAppearanceTheme, BUILTIN_THEMES, THEMES, themeModes } from '../../../src/lib/theme/appearance.ts'; import { sameSettingsValue } from '../../../src/lib/settings/store.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 }); } }); // tests/unit/theme/contrast.spec.ts checks the readability of every palette. test('Single-mode palettes restore their one appearance and export a valid theme', () => { for (const { id } of BUILTIN_THEMES.filter(item => themeModes(item.id).length === 1)) { const [only] = themeModes(id); const exported = exportAppearanceTheme(id); for (const mode of ['light', 'dark'] as const) { assert.deepEqual(parseAppearance(JSON.stringify({ theme: id, mode })), { theme: id, mode: only }); } assert.equal(exported.schema, 3); assert.deepEqual(parseCustomTheme(exported), exported); for (const role of SYNTAX_ROLES) assert.match(exported.colors[`syntax-${role}`] ?? '', /^#[0-9a-f]{6}$/); 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' }); }); test('New tab backgrounds keep supported image data and reject active or remote content', () => { const value = { theme: 'claude', mode: 'light', newTabBackground: { image: 'data:image/png;base64,YQ==', overlay: 0, position: 'center', size: 'cover' } }; assert.deepEqual(parseAppearance(JSON.stringify(value)), value); for (const image of ['https://example.com/photo.png', 'javascript:alert(1)', 'data:image/svg+xml;base64,YQ==', 'data:image/png;base64,' + 'A'.repeat(3_000_000)]) { assert.equal(normalizeAppearance({ ...value, newTabBackground: { color: '#224466', image } }).newTabBackground, undefined); } assert.equal(normalizeAppearance({ ...value, newTabBackground: { color: 'red' } }).newTabBackground, undefined); assert.equal(normalizeAppearance({ ...value, newTabBackground: undefined }).newTabBackground, undefined); }); test('Background image references clamp the overlay and discard obsolete colors', () => { const imageId = 'd10adda6-a2ec-4d73-a0a6-2cb1ed43bebc'; const layout = { position: 'center', size: 'cover' }; assert.deepEqual(normalizeAppearance({ newTabBackground: { imageId, color: '#224466', overlay: 3 } }).newTabBackground, { imageId, overlay: 1, ...layout }); assert.deepEqual(normalizeAppearance({ newTabBackground: { imageId, overlay: -3 } }).newTabBackground, { imageId, overlay: -1, ...layout }); assert.deepEqual(normalizeAppearance({ newTabBackground: { imageId, overlay: NaN } }).newTabBackground, { imageId, overlay: 0, ...layout }); assert.equal(normalizeAppearance({ newTabBackground: { imageId: 'invalid' } }).newTabBackground, undefined); }); test('A valid appearance from the host needs no new save, although the host sorted its keys', () => { // ThemeSync saves the normalized appearance only when it differs from the stored value. // A key-order difference made two windows save the same value to each other without end. const imageId = 'd10adda6-a2ec-4d73-a0a6-2cb1ed43bebc'; const fromHost = [ { mode: 'dark', theme: 'harvest-moon', transparency: { amount: 18, enabled: true } }, { mode: 'light', newTabBackground: { imageId, overlay: 0.2, position: 'center', size: 'cover' }, theme: 'default' }, ]; for (const stored of fromHost) { const normalized = normalizeAppearance(stored); assert.notEqual(JSON.stringify(normalized), JSON.stringify(stored), 'The fixture has a different key order'); assert.ok(sameSettingsValue(stored, normalized), JSON.stringify(stored)); } assert.ok(!sameSettingsValue({ mode: 'dark', theme: 'holo' }, normalizeAppearance({ mode: 'dark', theme: 'holo' })), 'A renamed theme still gets saved'); });