import test from "node:test"; import assert from "node:assert/strict"; import { spawn } from "node:child_process"; import { mkdtemp, mkdir, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import net from "node:net"; const here = fileURLToPath(new URL("..", import.meta.url)); async function freePort() { const server = net.createServer().listen(0, "127.0.0.1"); await new Promise((done) => server.once("listening", done)); const { port } = server.address(); await new Promise((done) => server.close(done)); return port; } async function start(args) { const port = await freePort(); const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), ...args], { cwd: here, env: { ...process.env, PIPELINES_WEB_AUTH: "disabled" }, stdio: ["ignore", "pipe", "inherit"], }); await new Promise((done) => child.stdout.once("data", done)); return { base: `http://127.0.0.1:${port}`, stop: () => child.kill() }; } test("serves the app root, deep routes and the update feed", async (t) => { const dir = await mkdtemp(join(tmpdir(), "serve-")); const root = join(dir, "app"); const updates = join(dir, "updates"); await mkdir(join(root, "canvaskit"), { recursive: true }); await mkdir(updates); await writeFile(join(root, "index.html"), "app"); await writeFile(join(root, "canvaskit", "canvaskit.wasm"), "wasm"); await writeFile(join(updates, "version.json"), '{"versionCode":1}'); await writeFile(join(dir, "secret.txt"), "no"); const server = await start(["--root", root, "--updates", updates]); t.after(server.stop); const index = await fetch(`${server.base}/runs/12`); assert.equal(index.status, 200); assert.match(await index.text(), /app/); const wasm = await fetch(`${server.base}/canvaskit/canvaskit.wasm`); assert.equal(wasm.headers.get("content-type"), "application/wasm"); const feed = await fetch(`${server.base}/updates/version.json`); assert.deepEqual(await feed.json(), { versionCode: 1 }); assert.equal((await fetch(`${server.base}/updates/%2e%2e%2fsecret.txt`)).status, 404); assert.equal((await fetch(`${server.base}/updates/missing.apk`)).status, 404); const icon = await fetch(`${server.base}/favicon.svg`); assert.equal(icon.status, 200); }); test("explains how to build the app when it is missing", async (t) => { const dir = await mkdtemp(join(tmpdir(), "serve-")); const server = await start(["--root", join(dir, "absent")]); t.after(server.stop); const page = await fetch(`${server.base}/`); assert.equal(page.status, 503); assert.match(await page.text(), /\.\/install --web/); }); test("icons and the manifest need no sign-in; the app does", async (t) => { const dir = await mkdtemp(join(tmpdir(), "serve-")); const root = join(dir, "app"); await mkdir(join(root, "icons"), { recursive: true }); await writeFile(join(root, "index.html"), ""); await writeFile(join(root, "favicon.png"), "png"); await writeFile(join(root, "manifest.json"), "{}"); await writeFile(join(root, "icons", "Icon-192.png"), "png"); await writeFile(join(root, "main.dart.js"), "app"); const auth = join(dir, "auth.json"); await writeFile(auth, JSON.stringify({ password: "password-1", secret: "s".repeat(40) })); const port = await freePort(); const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--root", root], { cwd: here, env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth }, stdio: ["ignore", "pipe", "inherit"], }); await new Promise((done) => child.stdout.once("data", done)); t.after(() => child.kill()); const base = `http://127.0.0.1:${port}`; for (const path of ["/favicon.png", "/favicon.svg", "/manifest.json", "/icons/Icon-192.png"]) { assert.equal((await fetch(base + path)).status, 200, path); } assert.equal((await fetch(`${base}/main.dart.js`)).status, 401); assert.equal((await fetch(`${base}/icons/../main.dart.js`)).status, 401); });