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

45 lines
1.9 KiB
JavaScript
Raw Permalink 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 { SETTINGS_HANDLERS, applyPatch } = require('../lib/settings-handlers.js');
let passed = 0;
function check(name, fn) {
try { fn(); console.log(' ok -', name); passed++; }
catch (e) { console.error(' FAIL -', name, e.message); process.exitCode = 1; }
}
check('autoLaunch handler coerces truthy to boolean', () => {
const r = applyPatch({ autoLaunch: false }, { key: 'autoLaunch', value: 'yes' });
assert.strictEqual(r.autoLaunch, true);
});
check('maxItems accepts valid number', () => {
const r = applyPatch({ maxItems: 500 }, { key: 'maxItems', value: 500 });
assert.strictEqual(r.maxItems, 500);
});
check('displayLimit accepts valid enum and falls back on invalid', () => {
const r1 = applyPatch({ displayLimit: 300 }, { key: 'displayLimit', value: 500 });
assert.strictEqual(r1.displayLimit, 500);
const r2 = applyPatch({}, { key: 'displayLimit', value: 9999 });
assert.strictEqual(r2.displayLimit, 300);
});
check('unknown key is a no-op (returns shallow copy)', () => {
const r = applyPatch({}, { key: 'unknown', value: 1 });
assert.deepStrictEqual(r, {});
});
check('dataDir 刻意没有 handler只能走 settings:choose-dir', () => {
const r = applyPatch({ dataDir: 'D:\\Old' }, { key: 'dataDir', value: 'D:\\New' });
assert.strictEqual(r.dataDir, 'D:\\Old', 'settings:set dataDir 必须是 no-op否则运行中的库与配置分叉');
});
check('hotkey accepts preset values and falls back on invalid', () => {
const r1 = applyPatch({ hotkey: 'Ctrl+Alt+V' }, { key: 'hotkey', value: 'Alt+Shift+V' });
assert.strictEqual(r1.hotkey, 'Alt+Shift+V');
const r2 = applyPatch({ hotkey: 'Ctrl+Alt+V' }, { key: 'hotkey', value: '' });
assert.strictEqual(r2.hotkey, '', '空串是合法的「禁用」');
const r3 = applyPatch({ hotkey: 'Ctrl+Alt+V' }, { key: 'hotkey', value: 'Super+X' });
assert.strictEqual(r3.hotkey, 'Ctrl+Alt+V', '白名单外回落默认');
});