droboticflow/tests/browser/run-production-boot.mjs

100 lines
4.0 KiB
JavaScript

import { browserExecutableOptions } from "../../scripts/lib/browser-executable.mjs";
// Boot the built production assets, not a Vite/component fixture. This catches
// dependency resolution failures that tsc/build and aliased fixtures can miss.
import assert from "node:assert/strict";
import { createServer } from "node:http";
import { readFile, stat, mkdir, writeFile } from "node:fs/promises";
import { resolve, sep, extname } from "node:path";
import { chromium } from "playwright";
const directory = resolve(process.argv[2] || "dist");
const destination = resolve(process.argv[3] || "local-data/test-results/production-boot.json");
const report = {
directory,
network: "Loopback static files only; API responses stubbed; no company service or customer data",
pageErrors: [],
unexpectedWrites: [],
};
const mime = {
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".svg": "image/svg+xml",
".png": "image/png",
".ico": "image/x-icon",
".json": "application/json",
".woff2": "font/woff2",
};
const server = createServer(async (req, res) => {
try {
const url = new URL(req.url, "http://localhost");
const file = resolve(directory, "." + decodeURIComponent(url.pathname));
if (file !== directory && !file.startsWith(directory + sep)) {
res.writeHead(403);
res.end();
return;
}
let target = file;
try {
if (!(await stat(target)).isFile()) target = resolve(directory, "index.html");
} catch {
target = resolve(directory, "index.html");
}
const body = await readFile(target);
res.writeHead(200, { "Content-Type": mime[extname(target)] || "application/octet-stream" });
res.end(body);
} catch {
res.writeHead(500);
res.end("Local fixture file unavailable");
}
});
let browser;
try {
await new Promise((ok, fail) => {
server.once("error", fail);
server.listen(0, "127.0.0.1", ok);
});
const origin = "http://127.0.0.1:" + server.address().port;
browser = await chromium.launch({
...browserExecutableOptions(),
headless: true,
args: ["--no-proxy-server"],
});
const context = await browser.newContext({ serviceWorkers: "block" });
await context.route("**/*", async (route) => {
const request = route.request(),
url = new URL(request.url());
if (url.origin !== origin) return route.abort("blockedbyclient");
if (url.pathname.startsWith("/api/")) {
if (!["GET", "HEAD", "OPTIONS"].includes(request.method())) {
report.unexpectedWrites.push({ method: request.method(), path: url.pathname });
return route.abort("blockedbyclient");
}
if (url.pathname === "/api/auth/me") return route.fulfill({ status: 401, json: { error: "login is required" } });
return route.fulfill({ status: 200, json: { items: [], required: false, bootstrapRequired: false } });
}
return route.continue();
});
const page = await context.newPage();
page.on("pageerror", (error) => report.pageErrors.push(error.message));
await page.goto(origin, { waitUntil: "networkidle", timeout: 20000 });
assert.deepEqual(report.pageErrors, [], "Production bundle must boot without runtime errors");
await page.locator('input[type="password"]').waitFor({ state: "visible", timeout: 10000 });
report.rootChildren = await page.locator("#root").evaluate((root) => root.childElementCount);
report.loginFormVisible = await page.locator('input[type="password"]').isVisible();
assert(report.rootChildren > 0 && report.loginFormVisible, "Production login must actually render");
assert.deepEqual(report.unexpectedWrites, [], "Anonymous boot must not mutate state");
report.passed = true;
} catch (error) {
report.passed = false;
report.failure = error.message;
process.exitCode = 1;
} finally {
if (browser) await browser.close();
await new Promise((ok) => server.close(ok));
await mkdir(resolve(destination, ".."), { recursive: true });
await writeFile(destination, JSON.stringify(report, null, 2) + "\n");
console.log(JSON.stringify(report));
}