vc-online/tools/webcheck.ts

86 lines
4 KiB
TypeScript

// Plays the web build in a hidden, muted Chrome the way a player would (real key
// events) and reports what happened: game states, frame stalls, audio level, errors.
// Usage: bun tools/webcheck.ts [url] [--radio] [--shot out.png] [--log <regex>]
import { chromium, type Page } from "playwright-core";
const args = process.argv.slice(2);
const url = args.find((a) => a.startsWith("http")) ?? "http://127.0.0.1:8790/?mute=1";
const shot = args.includes("--shot") ? args[args.indexOf("--shot") + 1] : null;
const radio = args.includes("--radio");
const dpr = args.includes("--dpr") ? Number(args[args.indexOf("--dpr") + 1]) : 1;
const [vw, vh] = (args.includes("--size") ? args[args.indexOf("--size") + 1] : "1280x720").split("x").map(Number);
const logFilter = args.includes("--log") ? new RegExp(args[args.indexOf("--log") + 1], "i") : null;
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
const report: Record<string, unknown> = {};
const errors: string[] = [];
async function waitLog(page: Page, re: RegExp, timeoutMs: number) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
const hit = await page.evaluate((src) => (window as any).vcLog?.some((l: any) => new RegExp(src).test(l.text)) || (src.includes("GS_PLAYING_GAME") && (window as any).vcPlaying), re.source);
if (hit) return Date.now() - start;
await sleep(250);
}
const tail = await page.evaluate(() => (window as any).vcLog?.slice(-15).map((l: any) => l.text));
throw new Error(`timed out waiting for ${re}\n${(tail ?? []).join("\n")}`);
}
async function press(page: Page, key: string, holdMs = 150) {
await page.keyboard.down(key);
await sleep(holdMs);
await page.keyboard.up(key);
await sleep(1000);
}
// Longest gap between frames (ms) while `action` runs.
async function measureStall(page: Page, action: () => Promise<void>) {
await page.evaluate(() => {
const gl = (document.getElementById("canvas") as HTMLCanvasElement).getContext("webgl2")!;
const w = window as any;
w.vcFrames = [];
if (!w.vcClearHooked) {
const clear = gl.clear.bind(gl);
gl.clear = (m: number) => { w.vcFrames.push(performance.now()); clear(m); };
w.vcClearHooked = true;
}
});
await action();
return page.evaluate(() => {
const f: number[] = (window as any).vcFrames;
let worst = 0;
for (let i = 1; i < f.length; i++) worst = Math.max(worst, f[i] - f[i - 1]);
return { frames: f.length, worstGapMs: Math.round(worst) };
});
}
const browser = await chromium.launch({ channel: "chrome", headless: true, args: ["--mute-audio", "--autoplay-policy=no-user-gesture-required"] });
try {
const page = await browser.newPage({ viewport: { width: vw, height: vh }, deviceScaleFactor: dpr });
page.on("pageerror", (e) => errors.push(String(e.stack ?? e).slice(0, 1500)));
await page.goto(url);
report.loadMs = await waitLog(page, /GS_PLAYING_GAME/, 90000);
await sleep(Number(process.env.VC_WAIT ?? 4000));
if (radio) {
await press(page, "f", 300);
await sleep(4000);
const switches = [];
for (let i = 0; i < 4; i++) switches.push(await measureStall(page, async () => { await press(page, "Insert"); await sleep(2500); }));
report.radioSwitches = switches;
}
if (args.includes("--pause")) { await press(page, "Escape", 300); await sleep(1500); }
report.idle = await measureStall(page, () => sleep(3000));
report.audioLevel = await page.evaluate(() => (window as any).vcAudioLevel?.() ?? null);
report.audioFiles = await page.evaluate(() => [...new Set(performance.getEntriesByType("resource")
.map((e) => e.name).filter((n) => /\/audio\//i.test(n)).map((n) => n.split("/").pop()))]);
if (shot) await page.screenshot({ path: shot });
if (logFilter) report.log = await page.evaluate((src) => (window as any).vcLog.filter((l: any) => new RegExp(src, "i").test(l.text)).slice(-30).map((l: any) => l.text), logFilter.source);
report.errors = errors;
} catch (e) {
report.failure = String(e).slice(0, 3000);
report.errors = errors;
} finally {
await browser.close();
}
console.log(JSON.stringify(report, null, 1));