import { assertEquals } from "jsr:@std/assert@1.0.19"; import { handler } from "./main.ts"; // ── Routing ─────────────────────────────────────────────────────────────────── Deno.test("GET /register returns 405", async () => { const res = await handler( new Request("http://localhost/register", { method: "GET" }), ); assertEquals(res.status, 405); const body = await res.json(); assertEquals(body.error, "Method not allowed"); }); Deno.test("POST /unknown returns 404", async () => { const res = await handler( new Request("http://localhost/unknown", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({}), }), ); assertEquals(res.status, 404); const body = await res.json(); assertEquals(body.error, "Not found"); }); // ── /register validation ────────────────────────────────────────────────────── Deno.test("POST /register rejects invalid JSON", async () => { const res = await handler( new Request("http://localhost/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: "not json", }), ); assertEquals(res.status, 400); const body = await res.json(); assertEquals(body.error, "Invalid JSON"); }); Deno.test("POST /register rejects missing did", async () => { const res = await handler( new Request("http://localhost/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({}), }), ); assertEquals(res.status, 400); const body = await res.json(); assertEquals(body.error, "Missing required field: did"); }); Deno.test("POST /register rejects non-DID string", async () => { const res = await handler( new Request("http://localhost/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ did: "alice.bsky.social" }), }), ); assertEquals(res.status, 400); const body = await res.json(); assertEquals(body.error, "Invalid DID format"); }); // ── /submit-trail validation ────────────────────────────────────────────────── Deno.test("POST /submit-trail rejects missing did", async () => { const res = await handler( new Request("http://localhost/submit-trail", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ trail: { name: "Test Trail" } }), }), ); assertEquals(res.status, 400); const body = await res.json(); assertEquals(body.error, "Missing required field: did"); }); Deno.test("POST /submit-trail rejects missing trail.name", async () => { const res = await handler( new Request("http://localhost/submit-trail", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ did: "did:plc:test", trail: {} }), }), ); assertEquals(res.status, 400); const body = await res.json(); assertEquals(body.error, "Missing required field: trail.name"); }); Deno.test("POST /submit-trail rejects non-DID string", async () => { const res = await handler( new Request("http://localhost/submit-trail", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ did: "not-a-did", trail: { name: "Trail" } }), }), ); assertEquals(res.status, 400); const body = await res.json(); assertEquals(body.error, "Invalid DID format"); });