d878f0db21
统一设备配置和浏览器启动入口,加入窗口焦点守卫,并按运行隔离报告和追踪文件。
123 lines
3.7 KiB
JavaScript
123 lines
3.7 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import assert from 'node:assert/strict';
|
|
import { EventEmitter } from 'node:events';
|
|
import test from 'node:test';
|
|
import {
|
|
FOCUS_EVENTS,
|
|
FOCUS_SAFE_ARGS,
|
|
FocusGuardError,
|
|
classifyFocus,
|
|
createFocusLog,
|
|
installFocusGuards,
|
|
parseForegroundOutput,
|
|
} from '../../scripts/playwright-focus-guard.mjs';
|
|
|
|
test('focus-safe args keep the window offscreen and unthrottled', () => {
|
|
for (const arg of [
|
|
'--window-position=-32000,-32000',
|
|
'--disable-backgrounding-occluded-windows',
|
|
'--disable-renderer-backgrounding',
|
|
'--disable-background-timer-throttling',
|
|
])
|
|
assert.ok(FOCUS_SAFE_ARGS.includes(arg), arg);
|
|
assert.ok(Object.isFrozen(FOCUS_SAFE_ARGS));
|
|
});
|
|
|
|
test('guards forbid newPage / bringToFront and close unexpected pages', async () => {
|
|
const context = new EventEmitter();
|
|
context.newPage = async () => 'opened';
|
|
const page = { bringToFront: async () => 'front' };
|
|
const extra = {
|
|
closed: false,
|
|
url: () => 'https://example.test/popup',
|
|
close: async () => {
|
|
extra.closed = true;
|
|
},
|
|
};
|
|
const log = createFocusLog(() => {});
|
|
const remove = installFocusGuards(context, page, log);
|
|
|
|
await assert.rejects(context.newPage(), FocusGuardError);
|
|
await assert.rejects(page.bringToFront(), FocusGuardError);
|
|
context.emit('page', page);
|
|
context.emit('page', extra);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
assert.equal(extra.closed, true);
|
|
assert.deepEqual(
|
|
log.entries.map((entry) => entry.type),
|
|
[FOCUS_EVENTS.FORBIDDEN, FOCUS_EVENTS.FORBIDDEN, FOCUS_EVENTS.EXTRA_PAGE],
|
|
);
|
|
|
|
remove();
|
|
assert.equal(await context.newPage(), 'opened');
|
|
assert.equal(context.listenerCount('page'), 0);
|
|
});
|
|
|
|
test('startup activation is logged; only a later background-to-foreground transition is fatal', () => {
|
|
assert.deepEqual(
|
|
classifyFocus({
|
|
phase: 'startup',
|
|
previous: 'background',
|
|
current: 'foreground',
|
|
}),
|
|
{ event: FOCUS_EVENTS.STARTUP, fatal: false },
|
|
);
|
|
assert.deepEqual(
|
|
classifyFocus({
|
|
phase: 'running',
|
|
previous: 'foreground',
|
|
current: 'foreground',
|
|
}),
|
|
{ event: FOCUS_EVENTS.STARTUP_PERSIST, fatal: false },
|
|
);
|
|
assert.deepEqual(
|
|
classifyFocus({
|
|
phase: 'running',
|
|
previous: 'background',
|
|
current: 'foreground',
|
|
}),
|
|
{ event: FOCUS_EVENTS.REACTIVATED, fatal: true },
|
|
);
|
|
assert.deepEqual(
|
|
classifyFocus({
|
|
phase: 'running',
|
|
previous: 'background',
|
|
current: 'unknown',
|
|
}),
|
|
{ event: FOCUS_EVENTS.UNKNOWN, fatal: true },
|
|
);
|
|
assert.deepEqual(
|
|
classifyFocus({
|
|
phase: 'running',
|
|
previous: 'background',
|
|
current: 'background',
|
|
}),
|
|
{ event: null, fatal: false },
|
|
);
|
|
});
|
|
|
|
test('foreground probe output maps to background, foreground or unknown', () => {
|
|
assert.equal(parseForegroundOutput('100|200,300\r\n').state, 'background');
|
|
assert.equal(parseForegroundOutput('300|200,300').state, 'foreground');
|
|
assert.equal(parseForegroundOutput('300|').state, 'unknown');
|
|
assert.equal(parseForegroundOutput('garbled').state, 'unknown');
|
|
});
|