8a48765a46
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
104 lines
3.0 KiB
JavaScript
104 lines
3.0 KiB
JavaScript
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');
|
|
});
|