import assert from "node:assert/strict"; import { createServer } from "node:http"; import { spawn } from "node:child_process"; import { once } from "node:events"; import { fileURLToPath } from "node:url"; const host = "http://127.0.0.1:3005"; const upstream = createServer((req, res) => { res.setHeader("Content-Type", "application/json"); if (req.url.endsWith("/auth/logout")) { res.statusCode = 204; res.end(); } else if (req.url.includes("/workspaces/space/materials")) { res.end(JSON.stringify({ url: req.url, method: req.method })); } else if (req.url.endsWith("/auth/login")) { res.end( JSON.stringify({ access_token: "test-only-session", expires_in: 28800, token_type: "Bearer", }), ); } else if (req.url.endsWith("/profile")) { res.statusCode = req.headers.authorization === "Bearer test-only-session" ? 200 : 401; res.end( JSON.stringify( res.statusCode === 200 ? { username: "test" } : { error: "invalid_session" }, ), ); } else { res.statusCode = 404; res.end("{}"); } }); await new Promise((r) => upstream.listen(0, "127.0.0.1", r)); const port = upstream.address().port; const app = spawn( process.execPath, [ "node_modules/next/dist/bin/next", "start", "--hostname", "127.0.0.1", "--port", "3005", ], { cwd: fileURLToPath(new URL("../", import.meta.url)), env: { ...process.env, FOUNDRY_API_ORIGIN: "http://127.0.0.1:" + port }, stdio: ["ignore", "pipe", "pipe"], }, ); let startup = ""; app.stdout.on("data", (x) => (startup += x)); app.stderr.on("data", (x) => (startup += x)); try { let ready = false; for (let n = 0; n < 100; n++) { if (app.exitCode !== null) throw new Error("Frontend failed to start: " + startup); try { if ((await fetch(host)).ok) { ready = true; break; } } catch {} await new Promise((r) => setTimeout(r, 100)); } assert(ready, "Frontend did not start"); for (const path of ["/", "/login", "/auth/callback", "/dashboard", "/dashboard?demo=1"]) { const page = await fetch(host + path); assert.equal(page.status, 200, path); const html = await page.text(); assert.match(html, / upstream.close(r)); }