Files
cad-building-showcase/scripts/web_smoke_test.mjs
zhengwr 6c207b0538
Some checks failed
ci / test (macos-latest, 3.10) (push) Has been cancelled
ci / test (macos-latest, 3.12) (push) Has been cancelled
ci / test (ubuntu-latest, 3.10) (push) Has been cancelled
ci / test (ubuntu-latest, 3.12) (push) Has been cancelled
ci / test (windows-latest, 3.10) (push) Has been cancelled
ci / test (windows-latest, 3.12) (push) Has been cancelled
ci / browser-smoke (push) Has been cancelled
test: exercise floor state in browser smoke
2026-07-21 15:30:24 +08:00

116 lines
4.8 KiB
JavaScript

#!/usr/bin/env node
/** Browser smoke test for the served Three.js showcase viewer. */
import fs from "node:fs/promises";
import path from "node:path";
function arg(name, fallback = null) {
const index = process.argv.indexOf(name);
return index >= 0 && process.argv[index + 1] ? process.argv[index + 1] : fallback;
}
const url = arg("--url");
const out = arg("--out", "browser_smoke_report.json");
const shots = arg("--screenshots-dir", "browser_smoke");
const thumbnailOut = arg("--thumbnail-out");
if (!url) {
throw new Error("Usage: web_smoke_test.mjs --url http://127.0.0.1:8188 --out report.json");
}
let chromium;
try {
({ chromium } = await import("playwright"));
} catch (error) {
throw new Error("Missing Playwright. Install with: npm install -D playwright && npx playwright install chromium", { cause: error });
}
await fs.mkdir(shots, { recursive: true });
if (thumbnailOut) await fs.mkdir(path.dirname(thumbnailOut), { recursive: true });
const browser = await chromium.launch({ headless: true });
const consoleErrors = [];
const pageErrors = [];
const failedRequests = [];
const checks = [];
async function inspect(viewport, name) {
const page = await browser.newPage({ viewport });
page.on("console", (message) => {
if (message.type() === "error") consoleErrors.push(message.text());
});
page.on("pageerror", (error) => pageErrors.push(error.message));
page.on("requestfailed", (request) => failedRequests.push(`${request.url()} ${request.failure()?.errorText || "failed"}`));
await page.goto(url, { waitUntil: "networkidle", timeout: 60000 });
await page.waitForSelector("#loading", { state: "hidden", timeout: 60000 });
const actualViewport = await page.evaluate(() => ({ width: window.innerWidth, height: window.innerHeight }));
const viewportMatches = actualViewport.width === viewport.width && actualViewport.height === viewport.height;
checks.push({
name: `${name}:viewport`,
status: viewportMatches ? "PASS" : "FAIL",
detail: `${actualViewport.width}x${actualViewport.height} expected ${viewport.width}x${viewport.height}`,
});
const selectors = ["#floor-controls", "#category-controls", "#explode", "#section", "#snapshot"];
for (const selector of selectors) {
const visible = await page.locator(selector).isVisible();
checks.push({ name: `${name}:${selector}`, status: visible ? "PASS" : "FAIL" });
}
await page.waitForTimeout(1200);
const before = await page.evaluate(() => window.__renders || 0);
await page.waitForTimeout(1500);
const after = await page.evaluate(() => window.__renders || 0);
checks.push({ name: `${name}:idle-render`, status: after === before ? "PASS" : "FAIL", detail: `${before}->${after}` });
if (name === "desktop") {
const buttons = page.locator("#floor-controls button");
const buttonCount = await buttons.count();
if (buttonCount > 0) {
const button = buttons.nth(0);
const label = (await button.innerText()).trim();
await button.click();
await page.waitForTimeout(150);
const hashWritten = await page.evaluate(() => window.location.hash.length > 1);
await button.click();
await page.waitForTimeout(150);
checks.push({
name: "desktop:floor-state",
status: hashWritten ? "PASS" : "FAIL",
detail: hashWritten ? `Floor label serialized to URL hash: ${label}` : `URL hash was not written: ${label}`,
});
}
}
if (name === "desktop" && thumbnailOut) {
// Export the renderer itself. Cropping a page screenshot retains UI panels
// and makes Agents guess at a dark-background crop; the canvas is already
// the canonical customer model view. Zoom out a fixed amount so the white
// model remains a compact subject on the dark exhibit background.
const canvas = page.locator("#scene");
await canvas.hover();
await page.mouse.wheel(0, 1800);
await page.waitForTimeout(500);
const dataUrl = await canvas.evaluate((element) => element.toDataURL("image/png"));
await fs.writeFile(thumbnailOut, Buffer.from(dataUrl.split(",", 2)[1], "base64"));
}
await page.screenshot({ path: path.join(shots, `${name}.png`), fullPage: true });
await page.close();
}
try {
await inspect({ width: 1440, height: 900 }, "desktop");
await inspect({ width: 390, height: 844 }, "mobile");
} finally {
await browser.close();
}
const failedChecks = checks.filter((item) => item.status === "FAIL");
const status = failedChecks.length || consoleErrors.length || pageErrors.length || failedRequests.length ? "FAIL" : "PASS";
const report = {
status,
detail: `${checks.length - failedChecks.length}/${checks.length} checks passed`,
url,
checks,
console_errors: consoleErrors,
page_errors: pageErrors,
failed_requests: failedRequests,
};
await fs.writeFile(out, JSON.stringify(report, null, 2));
process.stdout.write(`${JSON.stringify({ status, report: out })}\n`);
if (status === "FAIL") process.exitCode = 3;