Files
Clipboard/test/theme.test.js
2026-09-12 13:59:12 +08:00

68 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const assert = require('node:assert');
const { test } = require('node:test');
const configMod = require('../config');
test('DEFAULTS 包含 theme/accent/followSystem', () => {
assert.strictEqual(configMod.DEFAULTS.theme, 'linear');
assert.strictEqual(configMod.DEFAULTS.accent, 'purple');
assert.strictEqual(configMod.DEFAULTS.followSystem, false);
});
test('sanitize 接受合法 theme', () => {
assert.strictEqual(configMod.sanitize({ theme: 'midnight' }).theme, 'midnight');
assert.strictEqual(configMod.sanitize({ theme: 'github' }).theme, 'github');
});
test('sanitize 拒绝未知 theme 回落 linear', () => {
assert.strictEqual(configMod.sanitize({ theme: 'foo' }).theme, 'linear');
assert.strictEqual(configMod.sanitize({ theme: null }).theme, 'linear');
assert.strictEqual(configMod.sanitize({}).theme, 'linear');
});
test('sanitize 接受 5 种 accent', () => {
for (const a of ['purple', 'blue', 'green', 'orange', 'red']) {
assert.strictEqual(configMod.sanitize({ accent: a }).accent, a);
}
});
test('sanitize 拒绝未知 accent 回落 purple', () => {
assert.strictEqual(configMod.sanitize({ accent: 'cyan' }).accent, 'purple');
});
test('sanitize 接受 followSystem=true', () => {
assert.strictEqual(configMod.sanitize({ followSystem: true }).followSystem, true);
assert.strictEqual(configMod.sanitize({ followSystem: false }).followSystem, false);
assert.strictEqual(configMod.sanitize({ followSystem: 'yes' }).followSystem, false);
});
// ---------- effectiveTheme跟随系统开关与系统明暗组合 ----------
// 旧版把这段逻辑内联在 main.js 的 broadcastTheme / IPC 处理器里,
// 跟「跟随系统」开关的 IPC 顺序竞争出过显示主题不一致的问题。把判断
// 收到 config.effectiveTheme 后这里能直接单测,确保 4 个组合都正确。
test('effectiveThemefollowSystem=false 时用 raw theme', () => {
const cfg = { theme: 'midnight', followSystem: false };
assert.strictEqual(configMod.effectiveTheme(cfg, true), 'midnight');
assert.strictEqual(configMod.effectiveTheme(cfg, false), 'midnight');
});
test('effectiveThemefollowSystem=true + 深色系统 = linear', () => {
const cfg = { theme: 'midnight', followSystem: true };
assert.strictEqual(configMod.effectiveTheme(cfg, true), 'linear');
});
test('effectiveThemefollowSystem=true + 浅色系统 = light', () => {
const cfg = { theme: 'midnight', followSystem: true };
assert.strictEqual(configMod.effectiveTheme(cfg, false), 'light');
});
test('effectiveThemeraw theme 缺失时回落到默认 linear', () => {
// 极端防御cfg 为 null/undefined 时不让应用崩。
// followSystem 也没有 → 视同关闭,回到 DEFAULTS.themelinear
// 关键是不抛异常 + 返回合法值,不让调用方去 switch-case 'linear' 之类。
assert.strictEqual(configMod.effectiveTheme(null, true), 'linear');
assert.strictEqual(configMod.effectiveTheme(undefined, false), 'linear');
assert.strictEqual(configMod.effectiveTheme({ followSystem: false }, false), 'linear');
});