Files
monkey-ai-web/test/unit/playwright-focus-guard.test.js
T
zhanjunhao 92380ebab6 chore(tooling): 对齐协作规范与浏览器验收工具
验证:713 项单测、8 视口环境、代码及格式检查通过;未运行生产构建。
2026-11-17 14:48:50 +08:00

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');
});