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

60 lines
2.6 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 fs = require('node:fs');
const path = require('node:path');
// Pure file-content check (this test doesn't require electron).
const main = fs.readFileSync(path.join(__dirname, '..', 'main.js'), 'utf8');
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('main.js contains exactly one createFramelessWindow definition', () => {
const re = /function createFramelessWindow\s*\(/g;
const matches = main.match(re) || [];
assert.strictEqual(matches.length, 1, `found ${matches.length} occurrences, expected 1`);
});
check('createMainWindow and openSettingsWindow both call createFramelessWindow', () => {
const calls = main.match(/createFramelessWindow\s*\(/g) || [];
// 1 definition + at least 2 callers (main + settings) = 3+ occurrences
assert.ok(calls.length >= 3, `expected >=3 occurrences, got ${calls.length}`);
});
check('createFramelessWindow 不再硬编码 #FFFFFF', () => {
// 抓出 createFramelessWindow 的整个函数体
const fn = main.match(/function createFramelessWindow\s*\([\s\S]*?\n\}/);
assert.ok(fn, '找不到 createFramelessWindow');
assert.ok(
!/backgroundColor:\s*['"]#FFFFFF['"]/.test(fn[0]),
'createFramelessWindow 还硬编码 #FFFFFF 首帧背景色'
);
});
check('createFramelessWindow 引用 themeBgColor', () => {
const fn = main.match(/function createFramelessWindow\s*\([\s\S]*?\n\}/);
assert.ok(fn, '找不到 createFramelessWindow');
assert.ok(
/themeBgColor\s*\(/.test(fn[0]),
'createFramelessWindow 没引用 themeBgColor'
);
});
check('createFramelessWindow 拒绝新开窗口请求(外链不能弹裸窗口)', () => {
// 关于页链接走 shell.openExternal这里是纵深防御漏过点击拦截的
// 打开方式(中键 / 拖拽)也不能弹出一个没有 preload 的外部窗口。
const fn = main.match(/function createFramelessWindow\s*\([\s\S]*?\n\}/);
assert.ok(fn, '找不到 createFramelessWindow');
assert.match(fn[0], /setWindowOpenHandler\(/, '未接线 setWindowOpenHandler');
assert.match(fn[0], /action:\s*['"]deny['"]/, '新开窗口未被拒绝');
});
check('createFramelessWindow 拦截页内导航(外部 URL 不能把窗口顶掉)', () => {
const fn = main.match(/function createFramelessWindow\s*\([\s\S]*?\n\}/);
assert.ok(fn, '找不到 createFramelessWindow');
assert.match(fn[0], /will-navigate/, '未接线 will-navigate');
assert.match(fn[0], /preventDefault\(\)/, '导航未被阻止');
});