waybetter-for-x/apps/desktop/tests/themePresets.test.ts
2026-10-01 06:01:13 +07:00

26 lines
1.7 KiB
TypeScript

import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import test from 'node:test';
import { BUILTIN_THEMES } from '../src/inject/themeSpec.ts';
// Native code validates the saved theme against its own preset list; a theme missing there can be
// picked in the panel but fails to save with "VALIDATION: unknown theme preset".
test('native PRESETS list every built-in theme, and only those', () => {
const source = readFileSync(new URL('../src-tauri/src/theme.rs', import.meta.url), 'utf8');
const block = /pub const PRESETS: \[&str; (\d+)\] = \[([\s\S]*?)\];/.exec(source);
assert.ok(block, 'PRESETS constant not found in theme.rs');
const native = [...block[2]!.matchAll(/"([^"]+)"/g)].map(match => match[1]);
assert.equal(Number(block[1]), native.length);
assert.deepEqual(new Set(native), new Set(BUILTIN_THEMES.map(theme => theme.id)));
});
// The tab webview's native background (theme.rs `page_color`) must be the color the runtime paints.
test('native PRESET_PAGE_COLORS match every built-in theme with colors', () => {
const source = readFileSync(new URL('../src-tauri/src/theme.rs', import.meta.url), 'utf8');
const block = /pub const PRESET_PAGE_COLORS: \[\(&str, &str\); (\d+)\] = \[([\s\S]*?)\];/.exec(source);
assert.ok(block, 'PRESET_PAGE_COLORS constant not found in theme.rs');
const native = Object.fromEntries([...block[2]!.matchAll(/\("([^"]+)", "(#[0-9a-f]{6})"\)/g)].map(match => [match[1], match[2]]));
assert.equal(Number(block[1]), Object.keys(native).length);
const expected = Object.fromEntries(BUILTIN_THEMES.filter(theme => theme.colors).map(theme => [theme.id, (theme.colors!.page ?? theme.colors!.bg).toLowerCase()]));
assert.deepEqual(native, expected);
});