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

92 lines
3.1 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 { spawn } from 'node:child_process';
import { readdir, stat, access } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
export async function latestReport(directory) {
const reports = [];
try {
const legacy = await stat(path.join(directory, 'index.html'));
reports.push({ candidate: directory, modified: legacy.mtimeMs });
} catch {
/* Older runners wrote index.html directly in this directory. */
}
for (const entry of await readdir(directory, { withFileTypes: true })) {
if (!entry.isDirectory()) continue;
const candidate = path.join(directory, entry.name);
try {
const info = await stat(path.join(candidate, 'index.html'));
reports.push({ candidate, modified: info.mtimeMs });
} catch {
/* Incomplete runs have no report to open. */
}
}
reports.sort((a, b) => b.modified - a.modified);
if (!reports.length) throw new Error('No completed HTML report found');
return reports[0].candidate;
}
if (
process.argv[1] &&
path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)
) {
const [mode, ...args] = process.argv.slice(2);
const env = { ...process.env };
const cli = fileURLToPath(
new URL('../node_modules/@playwright/test/cli.js', import.meta.url),
);
let command = ['test', ...args];
if (mode === 'headless') env.PW_HEADLESS = '1';
else if (mode === 'extended') env.PW_EXTENDED_LAYOUT = '1';
else if (mode === 'dist') {
env.PW_TARGET = 'local-dist';
env.PW_BASE_URL = 'http://127.0.0.1:4176/';
const root =
env.PW_DIST_DIR || fileURLToPath(new URL('../dist/', import.meta.url));
await access(path.join(root, 'index.html'));
} else if (mode === 'report') {
const explicitDirectory = args[0] && !args[0].startsWith('-');
const directory = explicitDirectory
? args[0]
: await latestReport(
fileURLToPath(new URL('../playwright-report/', import.meta.url)),
);
command = [
'show-report',
directory,
...(explicitDirectory ? args.slice(1) : args),
];
} else throw new Error('Expected headless, extended, dist or report');
const child = spawn(process.execPath, [cli, ...command], {
cwd: fileURLToPath(new URL('../', import.meta.url)),
stdio: 'inherit',
env,
});
child.on('error', (error) => {
console.error(error);
process.exitCode = 1;
});
child.on('exit', (code) => {
process.exitCode = code ?? 1;
});
}