import { test } from "node:test" import assert from "node:assert/strict" import { spawn } from "node:child_process" import { mkdtemp, readFile, mkdir, rm } from "node:fs/promises" import { tmpdir } from "node:os" import path from "node:path" import { fileURLToPath } from "node:url" import { chromium } from "playwright-core" test("dashboard login, logs, all playground endpoints, and session boundaries", { timeout: 120000 }, async () => { const root = fileURLToPath(new URL("../../", import.meta.url)) const scratch = await mkdtemp(path.join(tmpdir(), "web-extract-ui-")) const fixturePath = path.join(scratch, "fixture.json") const server = spawn("go", ["test", "./internal/app", "-run", "^TestAdminBrowserFixture$", "-count=1"], { cwd: root, env: { ...process.env, ADMIN_BROWSER_FIXTURE: fixturePath }, detached: true, stdio: ["ignore", "pipe", "pipe"] }) let serverOutput = "" server.stdout.on("data", chunk => serverOutput += chunk) server.stderr.on("data", chunk => serverOutput += chunk) let browser try { let fixture for (let i = 0; i < 200; i++) { try { fixture = JSON.parse(await readFile(fixturePath, "utf8")); break } catch {} if (server.exitCode != null) throw new Error(serverOutput) await new Promise(resolve => setTimeout(resolve, 100)) } assert.ok(fixture, "fixture server did not start") browser = await chromium.launch({ executablePath: process.env.CHROME_PATH || "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", headless: true }) const context = await browser.newContext({ viewport: { width: 1440, height: 1000 } }) const page = await context.newPage() const errors = [] page.on("pageerror", error => errors.push(error.message)) page.on("console", log => { if (log.type() === "error" && !log.text().includes("Failed to load resource")) errors.push(log.text()) }) const screenshot = async name => { if (!process.env.ADMIN_SCREENSHOTS) return await mkdir(process.env.ADMIN_SCREENSHOTS, { recursive: true }) await page.screenshot({ path: path.join(process.env.ADMIN_SCREENSHOTS, `${name}.png`), fullPage: true }) } await page.goto(`${fixture.url}/admin/`) await page.getByLabel("admin password", { exact: true }).fill("wrong-password") await page.getByRole("button", { name: "sign in", exact: true }).click() await page.getByText("incorrect password", { exact: true }).waitFor() await screenshot("login") await page.getByLabel("admin password", { exact: true }).fill(fixture.password) await page.getByRole("button", { name: "sign in", exact: true }).click() await page.getByRole("heading", { name: "request activity", exact: true }).waitFor() await page.getByRole("button", { name: "older", exact: true }).click() await page.getByRole("button", { name: "newer", exact: true }).click() await page.getByLabel("request errors only").check() await page.getByRole("button", { name: "no query recorded", exact: true }).waitFor() await page.waitForFunction(() => document.querySelectorAll("tbody tr").length === 1) assert.equal(await page.locator("tbody tr").count(), 1) await page.getByLabel("request errors only").uncheck() await page.getByRole("button", { name: "fixture request limits 31", exact: true }).waitFor() await screenshot("activity-desktop") await page.getByRole("tab", { name: "playground", exact: true }).click() await page.getByLabel("query", { exact: true }).fill("what are the request and file-upload limits?") await page.getByLabel("number of results", { exact: true }).fill("1") await page.getByRole("button", { name: "run query", exact: true }).click() await page.getByText("http 200", { exact: true }).waitFor() await page.getByText("requests may contain up to 8 mib; file uploads have a separate 64 mib limit.", { exact: true }).waitFor() await screenshot("playground-desktop") await page.getByRole("tab", { name: "trace", exact: true }).click() await page.getByText("2 kept / 3 judged / 3 source passages", { exact: false }).waitFor() await page.getByRole("tab", { name: "summarizer (1)", exact: true }).click() await page.locator("summary").filter({ hasText: "preview_summary" }).click() await page.getByRole("heading", { name: "raw response", exact: true }).waitFor() await page.getByRole("tab", { name: "results", exact: true }).click() await page.getByLabel("endpoint", { exact: true }).selectOption("contents") await page.getByLabel("urls ยท one per line").fill(`${fixture.sourceURL}\n${fixture.sourceURL}/missing`) await page.getByRole("button", { name: "run query", exact: true }).click() await page.getByText("CRAWL_HTTP_ERROR", { exact: true }).waitFor() await page.getByRole("tab", { name: "json", exact: true }).click() await page.getByRole("heading", { name: "sent request", exact: true }).waitFor() await page.getByLabel("endpoint", { exact: true }).selectOption("answer") await page.getByRole("button", { name: "run query", exact: true }).click() await page.getByText("http 200", { exact: true }).waitFor() await page.getByRole("tab", { name: "results", exact: true }).click() await page.getByText("requests may contain up to 8 mib; file uploads have a separate 64 mib limit.", { exact: true }).first().waitFor() await page.setViewportSize({ width: 390, height: 844 }) await screenshot("playground-mobile") assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > window.innerWidth), false, "mobile overflow") await page.getByRole("tab", { name: "activity", exact: true }).click() await page.getByRole("button", { name: "refresh", exact: true }).click() await page.getByRole("button", { name: "what are the request and file-upload limits?", exact: true }).first().waitFor() await screenshot("activity-mobile") assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > window.innerWidth), false, "activity overflow") const tableRegion = page.getByRole("region", { name: "scrollable request activity", exact: true }) await tableRegion.focus() await tableRegion.press("ArrowRight") await page.waitForFunction(() => document.querySelector('[aria-label="scrollable request activity"]').scrollLeft > 0) await page.getByRole("tab", { name: "playground", exact: true }).click() await page.locator("summary").filter({ hasText: /^request json$/ }).click() await page.getByLabel("edit json instead of form").check() await page.getByRole("textbox", { name: "request json", exact: true }).fill('{"query":""}') await page.getByRole("button", { name: "run query", exact: true }).click() await page.getByText("http 400", { exact: true }).waitFor() const oldCookie = (await context.cookies()).find(cookie => cookie.name === "web_extract_admin") assert.ok(oldCookie?.httpOnly) await page.getByRole("button", { name: "sign out", exact: true }).click() await page.getByLabel("admin password", { exact: true }).waitFor() const rejected = await context.request.get(`${fixture.url}/admin/api/overview`, { headers: { Cookie: `${oldCookie.name}=${oldCookie.value}` } }) assert.equal(rejected.status(), 401) assert.deepEqual(errors, []) } finally { await browser?.close() try { process.kill(-server.pid, "SIGTERM") } catch {} await new Promise(resolve => server.exitCode != null ? resolve() : server.once("exit", resolve)) await rm(scratch, { recursive: true, force: true }) } })