okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

nekomimi.petweb-extract: the Go service running on the Mac mini (vassago)5a90548

main
7.4 KiB110 linesraw
1import { test } from "node:test"
2import assert from "node:assert/strict"
3import { spawn } from "node:child_process"
4import { mkdtemp, readFile, mkdir, rm } from "node:fs/promises"
5import { tmpdir } from "node:os"
6import path from "node:path"
7import { fileURLToPath } from "node:url"
8import { chromium } from "playwright-core"
9
10test("dashboard login, logs, all playground endpoints, and session boundaries", { timeout: 120000 }, async () => {
11  const root = fileURLToPath(new URL("../../", import.meta.url))
12  const scratch = await mkdtemp(path.join(tmpdir(), "web-extract-ui-"))
13  const fixturePath = path.join(scratch, "fixture.json")
14  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"] })
15  let serverOutput = ""
16  server.stdout.on("data", chunk => serverOutput += chunk)
17  server.stderr.on("data", chunk => serverOutput += chunk)
18  let browser
19  try {
20    let fixture
21    for (let i = 0; i < 200; i++) {
22      try { fixture = JSON.parse(await readFile(fixturePath, "utf8")); break } catch {}
23      if (server.exitCode != null) throw new Error(serverOutput)
24      await new Promise(resolve => setTimeout(resolve, 100))
25    }
26    assert.ok(fixture, "fixture server did not start")
27    browser = await chromium.launch({ executablePath: process.env.CHROME_PATH || "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", headless: true })
28    const context = await browser.newContext({ viewport: { width: 1440, height: 1000 } })
29    const page = await context.newPage()
30    const errors = []
31    page.on("pageerror", error => errors.push(error.message))
32    page.on("console", log => { if (log.type() === "error" && !log.text().includes("Failed to load resource")) errors.push(log.text()) })
33    const screenshot = async name => {
34      if (!process.env.ADMIN_SCREENSHOTS) return
35      await mkdir(process.env.ADMIN_SCREENSHOTS, { recursive: true })
36      await page.screenshot({ path: path.join(process.env.ADMIN_SCREENSHOTS, `${name}.png`), fullPage: true })
37    }
38    await page.goto(`${fixture.url}/admin/`)
39    await page.getByLabel("admin password", { exact: true }).fill("wrong-password")
40    await page.getByRole("button", { name: "sign in", exact: true }).click()
41    await page.getByText("incorrect password", { exact: true }).waitFor()
42    await screenshot("login")
43    await page.getByLabel("admin password", { exact: true }).fill(fixture.password)
44    await page.getByRole("button", { name: "sign in", exact: true }).click()
45    await page.getByRole("heading", { name: "request activity", exact: true }).waitFor()
46    await page.getByRole("button", { name: "older", exact: true }).click()
47    await page.getByRole("button", { name: "newer", exact: true }).click()
48    await page.getByLabel("request errors only").check()
49    await page.getByRole("button", { name: "no query recorded", exact: true }).waitFor()
50    await page.waitForFunction(() => document.querySelectorAll("tbody tr").length === 1)
51    assert.equal(await page.locator("tbody tr").count(), 1)
52    await page.getByLabel("request errors only").uncheck()
53    await page.getByRole("button", { name: "fixture request limits 31", exact: true }).waitFor()
54    await screenshot("activity-desktop")
55    await page.getByRole("tab", { name: "playground", exact: true }).click()
56    await page.getByLabel("query", { exact: true }).fill("what are the request and file-upload limits?")
57    await page.getByLabel("number of results", { exact: true }).fill("1")
58    await page.getByRole("button", { name: "run query", exact: true }).click()
59    await page.getByText("http 200", { exact: true }).waitFor()
60    await page.getByText("requests may contain up to 8 mib; file uploads have a separate 64 mib limit.", { exact: true }).waitFor()
61    await screenshot("playground-desktop")
62    await page.getByRole("tab", { name: "trace", exact: true }).click()
63    await page.getByText("2 kept / 3 judged / 3 source passages", { exact: false }).waitFor()
64    await page.getByRole("tab", { name: "summarizer (1)", exact: true }).click()
65    await page.locator("summary").filter({ hasText: "preview_summary" }).click()
66    await page.getByRole("heading", { name: "raw response", exact: true }).waitFor()
67    await page.getByRole("tab", { name: "results", exact: true }).click()
68    await page.getByLabel("endpoint", { exact: true }).selectOption("contents")
69    await page.getByLabel("urls · one per line").fill(`${fixture.sourceURL}\n${fixture.sourceURL}/missing`)
70    await page.getByRole("button", { name: "run query", exact: true }).click()
71    await page.getByText("CRAWL_HTTP_ERROR", { exact: true }).waitFor()
72    await page.getByRole("tab", { name: "json", exact: true }).click()
73    await page.getByRole("heading", { name: "sent request", exact: true }).waitFor()
74    await page.getByLabel("endpoint", { exact: true }).selectOption("answer")
75    await page.getByRole("button", { name: "run query", exact: true }).click()
76    await page.getByText("http 200", { exact: true }).waitFor()
77    await page.getByRole("tab", { name: "results", exact: true }).click()
78    await page.getByText("requests may contain up to 8 mib; file uploads have a separate 64 mib limit.", { exact: true }).first().waitFor()
79    await page.setViewportSize({ width: 390, height: 844 })
80    await screenshot("playground-mobile")
81    assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > window.innerWidth), false, "mobile overflow")
82    await page.getByRole("tab", { name: "activity", exact: true }).click()
83    await page.getByRole("button", { name: "refresh", exact: true }).click()
84    await page.getByRole("button", { name: "what are the request and file-upload limits?", exact: true }).first().waitFor()
85    await screenshot("activity-mobile")
86    assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > window.innerWidth), false, "activity overflow")
87    const tableRegion = page.getByRole("region", { name: "scrollable request activity", exact: true })
88    await tableRegion.focus()
89    await tableRegion.press("ArrowRight")
90    await page.waitForFunction(() => document.querySelector('[aria-label="scrollable request activity"]').scrollLeft > 0)
91    await page.getByRole("tab", { name: "playground", exact: true }).click()
92    await page.locator("summary").filter({ hasText: /^request json$/ }).click()
93    await page.getByLabel("edit json instead of form").check()
94    await page.getByRole("textbox", { name: "request json", exact: true }).fill('{"query":""}')
95    await page.getByRole("button", { name: "run query", exact: true }).click()
96    await page.getByText("http 400", { exact: true }).waitFor()
97    const oldCookie = (await context.cookies()).find(cookie => cookie.name === "web_extract_admin")
98    assert.ok(oldCookie?.httpOnly)
99    await page.getByRole("button", { name: "sign out", exact: true }).click()
100    await page.getByLabel("admin password", { exact: true }).waitFor()
101    const rejected = await context.request.get(`${fixture.url}/admin/api/overview`, { headers: { Cookie: `${oldCookie.name}=${oldCookie.value}` } })
102    assert.equal(rejected.status(), 401)
103    assert.deepEqual(errors, [])
104  } finally {
105    await browser?.close()
106    try { process.kill(-server.pid, "SIGTERM") } catch {}
107    await new Promise(resolve => server.exitCode != null ? resolve() : server.once("exit", resolve))
108    await rm(scratch, { recursive: true, force: true })
109  }
110})