import { createServer } from "node:http"; import { readFile } from "node:fs/promises"; import { createRequire } from "node:module"; import path from "node:path"; const rootDir = path.resolve(import.meta.dirname, "../../.."); const projectDir = path.join(rootDir, "web-rtcp-5axis-xyzbc-trt-sim-plan"); const requireFromApp = createRequire(path.join(projectDir, "app/package.json")); const { chromium } = requireFromApp("playwright"); const server = createServer(async (request, response) => { try { const url = new URL(request.url || "/", "http://127.0.0.1"); const pathname = decodeURIComponent(url.pathname); const filePath = path.join(rootDir, pathname); if (!filePath.startsWith(rootDir)) { response.writeHead(403).end("forbidden"); return; } const body = await readFile(filePath); response.writeHead(200, { "content-type": contentType(filePath) }); response.end(body); } catch (error) { response.writeHead(404).end(error instanceof Error ? error.message : String(error)); } }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); const port = server.address().port; const browser = await chromium.launch({ headless: true, executablePath: process.env.CHROMIUM || undefined, args: ["--disable-gpu", "--no-sandbox", "--enable-unsafe-swiftshader"], }); try { const page = await browser.newPage(); const testUrl = `http://127.0.0.1:${port}/web-rtcp-5axis-xyzbc-trt-sim-plan/tests/browser/xyzbc_trt_all_buttons.html?app=../../app/dist/index.html`; await page.goto(testUrl, { waitUntil: "domcontentloaded", timeout: 30000 }); await page.waitForFunction(() => { const text = document.querySelector("#result")?.textContent || ""; return text.startsWith("xyzbc_trt_all_buttons=ok") || text.startsWith("xyzbc_trt_all_buttons=fail"); }, null, { timeout: 120000 }); const result = await page.locator("#result").textContent(); if (!result?.startsWith("xyzbc_trt_all_buttons=ok")) { throw new Error(result || "all button test did not report a result"); } console.log(result.trim()); } finally { await browser.close(); await new Promise((resolve) => server.close(resolve)); } function contentType(filePath) { if (filePath.endsWith(".html")) return "text/html; charset=utf-8"; if (filePath.endsWith(".js") || filePath.endsWith(".mjs")) return "text/javascript; charset=utf-8"; if (filePath.endsWith(".css")) return "text/css; charset=utf-8"; if (filePath.endsWith(".json")) return "application/json; charset=utf-8"; if (filePath.endsWith(".wasm")) return "application/wasm"; if (filePath.endsWith(".svg")) return "image/svg+xml"; if (filePath.endsWith(".png")) return "image/png"; return "application/octet-stream"; }