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 { visible } = await import("./match.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 () => { // Every test in this file reaches the server from the same address, and a // playing seat is rationed per address on purpose. Earlier tests here take // that seat and never open a socket to close, so nothing frees it: without // this line the join below is correctly refused and the assertion measures // the leftovers of another test rather than this one's behaviour. releaseAddress("127.0.0.1"); releaseAddress("::1"); releaseAddress("::ffff:127.0.0.1"); const form = new FormData(); form.set("name", ' Wren