import test from "node:test"; import assert from "node:assert/strict"; import { callPost, setupOpsCorpus } from "./_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/_web.test.ts" // // web-search and web-fetch (release 27 S3): the refusals answered before any // request leaves the machine. The reads themselves are tested against a fake // web in common/lib/web/web.test.ts. const corpus = await setupOpsCorpus(null); type Post = Parameters[0]; const route = async (name: string) => (await import(`./${name}/route`)).POST as Post; test.after(() => corpus.cleanup()); test("web-search: the query is required, n a whole number, unknown keys refused", async () => { const search = await route("web-search"); const none = await callPost(search, {}); assert.equal(none.status, 400); assert.match(String(none.body.error), /"query" is required/); const badN = await callPost(search, { query: "x", n: 0 }); assert.equal(badN.status, 400); assert.match(String(badN.body.error), /"n" must be a whole number above zero/); const extra = await callPost(search, { query: "x", searchUrl: "http://127.0.0.1/" }); assert.equal(extra.status, 400); assert.match(String(extra.body.error), /unknown key\(s\): searchUrl/); }); test("web-fetch: http(s) only, and never a private or loopback host — refused before any request", async () => { const fetchRoute = await route("web-fetch"); for (const [url, why] of [ ["file:///etc/passwd", /only http and https/], ["http://localhost:3001/api/ops/settings", /localhost is a private host/], ["http://127.0.0.1:3001/", /127\.0\.0\.1 is a private address/], ["http://169.254.169.254/latest/meta-data", /private address/], ["http://[::1]:3001/", /private address/], ] as const) { const r = await callPost(fetchRoute, { url }); assert.equal(r.status, 400, url); assert.match(String(r.body.error), why, url); } const noUrl = await callPost(fetchRoute, {}); assert.equal(noUrl.status, 400); assert.match(String(noUrl.body.error), /"url" is required/); });