import { expect, test } from "bun:test";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
process.env.FORGE_APP_DATA = mkdtempSync(join(tmpdir(), "app-"));
process.env.PORT = "0";
const { server } = await import("./server.ts");
const { match, snapshot, advance, takeSeat, release, releaseAddress } = await import("./arena.ts");
const base = `http://${server.hostname}:${server.port}`;
test("serves a live match on the first paint, with no script needed", async () => {
const response = await fetch(base + "/");
expect(response.status).toBe(200);
const body = await response.text();
expect(body).toContain("
val
");
// The score, the round and both team sheets are in the HTML itself.
expect(body).toContain("red " + match().score.red);
expect(body).toContain("round " + match().round);
for (const player of match().players) expect(body).toContain(player.name);
});
test("the page hands out a session cookie and the script is named by content", async () => {
const response = await fetch(base + "/", { redirect: "manual" });
const cookie = response.headers.get("set-cookie") ?? "";
expect(cookie).toContain("val_session=");
expect(cookie).toContain("HttpOnly");
const body = await response.text();
const src = /src="(\/static\/val\.[0-9a-f]+\.js)"/.exec(body);
expect(src).not.toBe(null);
const script = await fetch(base + src![1]!);
expect(script.status).toBe(200);
expect(script.headers.get("cache-control")).toContain("immutable");
expect(await script.text()).toContain("WebSocket");
});
test("a name is taken from a same-origin form and sanitised", async () => {
const form = new FormData();
form.set("name", ' Wren