Something went wrong. Try again.
Tangled Spindle built on Cloudflare Workers
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107import { exports } from "cloudflare:workers";import { describe, expect, it } from "vitest";import { repoDid } from "./helpers";
const host = "treadle.example.com";
function get(path: string, init?: RequestInit): Promise<Response> { return exports.default.fetch(`https://${host}${path}`, init);}
describe("identity", () => { it("serves the did:web document", async () => { const res = await get("/.well-known/did.json"); expect(res.status).toBe(200); expect(res.headers.get("content-type")).toBe("application/did+json"); expect(await res.json()).toEqual({ "@context": ["https://www.w3.org/ns/did/v1"], id: `did:web:${host}`, service: [ { id: "#tangled_spindle", type: "TangledSpindle", serviceEndpoint: `https://${host}`, }, ], }); });
it("describes the spindle", async () => { const res = await get("/xrpc/org.tangled.spindle.describeSpindle"); expect(res.status).toBe(200); expect(await res.json()).toMatchObject({ did: `did:web:${host}`, capabilities: ["tangled1.0"], }); });});
describe("xrpc", () => { it("rejects unknown methods", async () => { const res = await get("/xrpc/org.tangled.nope"); expect(res.status).toBe(501); expect(await res.json()).toMatchObject({ error: "MethodNotImplemented" }); });
it("answers CORS preflight", async () => { const res = await get("/xrpc/org.tangled.ci.queryPipelines", { method: "OPTIONS", headers: { Origin: "https://tangled.org", "Access-Control-Request-Method": "GET", }, }); expect(res.status).toBe(204); expect(res.headers.get("access-control-allow-origin")).toBe("*"); expect(res.headers.get("access-control-allow-headers")).toBe("Authorization, Content-Type"); });
it("allows cross-origin reads", async () => { const res = await get("/xrpc/org.tangled.spindle.describeSpindle", { headers: { Origin: "https://tangled.org" }, }); expect(res.headers.get("access-control-allow-origin")).toBe("*"); });});
it("serves the workflow docs", async () => { const res = await get("/docs"); expect(res.status).toBe(200); expect(res.headers.get("content-type")).toBe("text/markdown; charset=utf-8"); expect(await res.text()).toMatch(/^# treadle workflows\n[\s\S]*\n## Triggers\n/);});
describe("cache", () => { it.each(["/.well-known/did.json", "/docs"])("caches %s for an hour", async (path) => { const res = await get(path); expect(res.status).toBe(200); expect(res.headers.get("cache-control")).toBe("max-age=3600, must-revalidate"); });
it("caches public queries for 5 seconds", async () => { const res = await get(`/xrpc/org.tangled.ci.queryPipelines?repo=${repoDid}`); expect(res.status).toBe(200); expect(res.headers.get("cache-control")).toBe("max-age=5, must-revalidate"); expect(res.headers.get("access-control-allow-origin")).toBe("*"); });
it("does not cache failed queries", async () => { const res = await get("/xrpc/org.tangled.ci.getPipeline?repo=nope"); expect(res.status).toBe(400); expect(res.headers.get("cache-control")).toBe("no-store"); });
it("does not cache other responses", async () => { const res = await get("/xrpc/org.tangled.spindle.describeSpindle"); expect(res.headers.get("cache-control")).toBeNull(); });});
it.each([ ["elsewhere", "/nope", "GET"], ["for a webhook that is not a POST", "/webhooks/buildkite", "GET"],])("returns 404 %s", async (_, path, method) => { const res = await get(path, { method }); expect(res.status).toBe(404);});