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:/'); } 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}`, }; }