| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { agentPage, developerResource, pageRepresentation } from "./agent-pages.ts"; |
| 4 | |
| 5 | test("developer discovery aliases point at the installation's machine endpoints", () => { |
| 6 | const addresses = { api: "https://api.example", mcp: "https://mcp.example" }; |
| 7 | assert.equal(developerResource("/openapi.json", addresses), "https://api.example/openapi.json"); |
| 8 | assert.equal(developerResource("/mcp", addresses), "https://mcp.example"); |
| 9 | assert.equal(developerResource("/developers", addresses), "https://docs.g1t.sh/reference/developers/"); |
| 10 | assert.equal(developerResource("/private", addresses), null); |
| 11 | }); |
| 12 | |
| 13 | test("negotiation honors explicit types, quality, specificity, exclusions and browser defaults", () => { |
| 14 | for (const accept of [null, "*/*", "text/html", "text/html,application/xhtml+xml,*/*;q=0.8", "text/markdown;q=0, */*;q=1", "text/html;q=1,text/markdown;q=0.5"]) |
| 15 | assert.equal(pageRepresentation(accept), "html", String(accept)); |
| 16 | for (const accept of ["text/markdown", "TEXT/MARKDOWN; charset=utf-8", "text/html;q=0.5,text/markdown;q=1", "text/markdown,*/*", "text/html;q=0,text/*"]) |
| 17 | assert.equal(pageRepresentation(accept), "markdown", accept); |
| 18 | for (const accept of ["application/json", "text/html;q=0,text/markdown;q=0", "text/markdown;q=bad", "text/markdown;q=1.1"]) |
| 19 | assert.equal(pageRepresentation(accept), "none", accept); |
| 20 | }); |
| 21 | |
| 22 | test("homepage and genuine 404 documents have Markdown with the right status, discovery links and Vary", async () => { |
| 23 | for (const [path, status] of [["/", 200], ["/__missing", 404]] as const) { |
| 24 | const headers = new Headers({ vary: "Accept-Encoding", "content-type": "text/html" }); |
| 25 | const answer = agentPage(new Request(`https://g1t.sh${path}`, { headers: { accept: "text/markdown" } }), status, headers)!; |
| 26 | assert.equal(answer.status, status); |
| 27 | assert.match(answer.headers.get("content-type")!, /^text\/markdown/); |
| 28 | assert.equal(answer.headers.get("vary"), "Accept-Encoding, Accept"); |
| 29 | const body = await answer.text(); |
| 30 | assert.ok(body.length > 20); |
| 31 | assert.match(body, /\[.*\]\(https:\/\/g1t.sh\/llms.txt\)/); |
| 32 | assert.doesNotMatch(body, /<!DOCTYPE|<html/i); |
| 33 | const htmlHeaders = new Headers(); |
| 34 | assert.equal(agentPage(new Request(`https://g1t.sh${path}`, { headers: { accept: "text/html" } }), status, htmlHeaders), null); |
| 35 | assert.equal(htmlHeaders.get("vary"), "Accept"); |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | test("HEAD, unsupported types, private refusals and writes preserve protocol boundaries", async () => { |
| 40 | const head = agentPage(new Request("https://g1t.sh/", { method: "HEAD", headers: { accept: "text/markdown" } }), 200, new Headers())!; |
| 41 | assert.equal(head.headers.get("content-type"), "text/markdown; charset=utf-8"); |
| 42 | assert.equal(await head.text(), ""); |
| 43 | const json = agentPage(new Request("https://g1t.sh/", { headers: { accept: "application/json" } }), 200, new Headers())!; |
| 44 | assert.equal(json.status, 406); |
| 45 | for (const status of [301, 302, 401, 403, 500]) assert.equal(agentPage(new Request("https://g1t.sh/"), status, new Headers()), null); |
| 46 | assert.equal(agentPage(new Request("https://g1t.sh/", { method: "POST" }), 404, new Headers()), null); |
| 47 | assert.equal(agentPage(new Request("https://g1t.sh/private", { headers: { accept: "text/markdown" } }), 200, new Headers()), null); |
| 48 | }); |