import { expect, test, type Page } from "@playwright/test"; const ORIGIN = "http://127.0.0.1:4173"; async function localOnly(page: Page) { const external: string[] = []; await page.route("**/*", async (route) => { const url = new URL(route.request().url()); if (url.origin !== ORIGIN) { external.push(url.href); await route.abort(); } else await route.continue(); }); return external; } test("runs from a nested path without external requests", async ({ page }) => { const errors: string[] = []; page.on("pageerror", (error) => errors.push(error.message)); page.on("console", (message) => { if (message.type() === "error") errors.push(message.text()); }); const external = await localOnly(page); await page.goto("/deep/nested/rand/"); await expect( page.getByRole("heading", { name: "Random Tools" }), ).toBeVisible(); expect(external).toEqual([]); expect(errors).toEqual([]); }); test("repeats deterministic output without contacting the remote source", async ({ page, }) => { const external = await localOnly(page); await page.goto("/deep/nested/rand/"); await page.getByLabel("Random source").selectOption("deterministic"); await page .getByRole("textbox", { name: "Seed", exact: true }) .fill("browser-recipe"); await page.getByLabel("Count").fill("5"); await page.getByRole("button", { name: "Generate numbers" }).click(); const result = page.locator(".result > pre"); const first = await result.textContent(); expect(first?.trim().split("\n")).toHaveLength(5); await page.getByRole("button", { name: "Generate numbers" }).click(); await expect(result).toHaveText(first ?? ""); await expect( page.getByText(/deterministic-non-cryptographic/u), ).toBeAttached(); expect(external).toEqual([]); }); test("serves the release identity and hardened headers", async ({ request, }) => { const index = await request.get("/deep/nested/rand/"); expect(index.ok()).toBe(true); expect(index.headers()["content-security-policy"]).toContain( "default-src 'self'", ); expect(index.headers()["content-security-policy"]).toContain( "connect-src 'self' https://www.random.org", ); expect(await index.text()).not.toMatch(/\b(?:src|href)=["']\//u); const manifest = await request.get("/deep/nested/rand/toolbox-app.json"); await expect(manifest.json()).resolves.toMatchObject({ id: "de.add-ideas.rand-tools", version: "0.1.0", entry: "./", }); });