chore(tooling): 对齐协作规范与浏览器验收工具
验证:713 项单测、8 视口环境、代码及格式检查通过;未运行生产构建。
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
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 { randomUUID } from 'node:crypto';
|
||||
|
||||
export const browserStabilityArgs = Object.freeze([
|
||||
'--hide-scrollbars',
|
||||
'--disable-backgrounding-occluded-windows',
|
||||
'--disable-renderer-backgrounding',
|
||||
'--disable-background-timer-throttling',
|
||||
'--disable-features=CalculateNativeWinOcclusion',
|
||||
'--no-first-run',
|
||||
'--no-default-browser-check',
|
||||
]);
|
||||
|
||||
export function positiveInteger(value, name) {
|
||||
const text = String(value);
|
||||
if (
|
||||
!['number', 'string'].includes(typeof value) ||
|
||||
text !== text.trim() ||
|
||||
!/^[1-9]\d*$/.test(text) ||
|
||||
!Number.isSafeInteger(Number(value))
|
||||
) {
|
||||
throw new Error(`${name} must be a positive safe integer (1, 2, 3, ...)`);
|
||||
}
|
||||
return Number(value);
|
||||
}
|
||||
|
||||
function flag(env, name, fallback) {
|
||||
if (env[name] === undefined) return fallback;
|
||||
if (!['0', '1'].includes(env[name]))
|
||||
throw new Error(`${name} must be 0 or 1`);
|
||||
return env[name] === '1';
|
||||
}
|
||||
|
||||
export function browserRuntime(env = process.env) {
|
||||
const isCI = Boolean(env.CI && env.CI !== 'false' && env.CI !== '0');
|
||||
const headless = flag(env, 'PW_HEADLESS', isCI);
|
||||
if (isCI && !headless)
|
||||
throw new Error(
|
||||
'CI requires headless Chrome; PW_HEADLESS=0 is not allowed',
|
||||
);
|
||||
// UI_VISUAL_OFFSCREEN=0 仅限人工本地可视化调试时临时使用。
|
||||
// Agent、CI、无头/自动化环境禁止设置该值,必须保持默认离屏模式
|
||||
// (或 UI_VISUAL_OFFSCREEN=1),避免弹出 UI 或依赖显示环境。
|
||||
const offscreen = flag(env, 'UI_VISUAL_OFFSCREEN', true);
|
||||
const knownAgent = Boolean(
|
||||
env.CODEX_THREAD_ID || env.CLAUDECODE || env.CLAUDE_CODE_ENTRYPOINT,
|
||||
);
|
||||
if (!offscreen && (isCI || headless || knownAgent))
|
||||
throw new Error(
|
||||
'UI_VISUAL_OFFSCREEN=0 is only allowed for manual local headed debugging',
|
||||
);
|
||||
return {
|
||||
isCI,
|
||||
headless,
|
||||
offscreen,
|
||||
launchArgs: [
|
||||
...browserStabilityArgs,
|
||||
...(offscreen ? ['--window-position=-32000,-32000'] : []),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
export function runtimeOptions({
|
||||
defaultPort,
|
||||
defaultWorkers,
|
||||
env = process.env,
|
||||
}) {
|
||||
const target = env.PW_TARGET || 'local-dev';
|
||||
if (!['local-dev', 'local-dist', 'remote'].includes(target))
|
||||
throw new Error('PW_TARGET must be local-dist, local-dev or remote');
|
||||
if (target === 'remote' && !env.PW_BASE_URL)
|
||||
throw new Error('PW_BASE_URL is required for remote tests');
|
||||
const baseURL = new URL(
|
||||
env.PW_BASE_URL || `http://127.0.0.1:${defaultPort}/`,
|
||||
);
|
||||
if (
|
||||
!['http:', 'https:'].includes(baseURL.protocol) ||
|
||||
baseURL.username ||
|
||||
baseURL.password ||
|
||||
baseURL.search ||
|
||||
baseURL.hash
|
||||
) {
|
||||
throw new Error(
|
||||
'PW_BASE_URL must be an HTTP(S) URL without credentials, query or hash',
|
||||
);
|
||||
}
|
||||
if (!baseURL.pathname.endsWith('/'))
|
||||
throw new Error('PW_BASE_URL directory path must end with /');
|
||||
if (
|
||||
target !== 'remote' &&
|
||||
(baseURL.hostname !== '127.0.0.1' ||
|
||||
baseURL.protocol !== 'http:' ||
|
||||
baseURL.pathname !== '/')
|
||||
) {
|
||||
throw new Error('Local targets require http://127.0.0.1:<port>/');
|
||||
}
|
||||
const browser = browserRuntime(env);
|
||||
const { isCI } = browser;
|
||||
const workers =
|
||||
env.PW_WORKERS === undefined
|
||||
? isCI
|
||||
? 1
|
||||
: positiveInteger(defaultWorkers, 'resources.concurrency')
|
||||
: positiveInteger(env.PW_WORKERS, 'PW_WORKERS');
|
||||
const extended = flag(env, 'PW_EXTENDED_LAYOUT', false);
|
||||
const video = flag(env, 'PW_VIDEO', false);
|
||||
const requireAll = flag(env, 'PW_REQUIRE_ALL', false);
|
||||
const runId = env.PW_RUN_ID ?? `${target}-${Date.now()}-${randomUUID()}`;
|
||||
if (
|
||||
typeof runId !== 'string' ||
|
||||
runId !== runId.trim() ||
|
||||
!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,99}$/.test(runId)
|
||||
)
|
||||
throw new Error(
|
||||
'PW_RUN_ID must be a safe directory name of 1-100 letters, digits, underscores or hyphens',
|
||||
);
|
||||
// Workers inherit this ID; every run keeps its traces and reports together.
|
||||
env.PW_RUN_ID = runId;
|
||||
return {
|
||||
target,
|
||||
baseURL,
|
||||
...browser,
|
||||
workers,
|
||||
extended,
|
||||
video,
|
||||
requireAll,
|
||||
runId,
|
||||
outputDir: `test-results/${runId}`,
|
||||
reportDir: `playwright-report/${runId}`,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user