Something went wrong. Try again.
Stable episode delivery and analytics on pod.place
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041import { describe, expect, test } from "bun:test";import { SlingshotReader } from "./records";import { CID1, EPISODE_URI, episodeRecord } from "./testing/fixtures";
function mockReader(response: () => Response | Promise<Response>) { return new SlingshotReader("https://slingshot.microcosm.blue", async () => response());}describe("trusted record reads", () => { test("requests the exact DID/collection/rkey without following redirects", async () => { let captured: URL | undefined; let options: RequestInit | undefined; const reader = new SlingshotReader("https://slingshot.microcosm.blue", async (url, init) => { captured = new URL(String(url)); options = init; return Response.json({ uri: EPISODE_URI, cid: CID1, value: episodeRecord() }); }); expect((await reader.get(EPISODE_URI)).uri).toBe(EPISODE_URI); expect(captured!.searchParams.get("collection")).toBe("place.pod.episode"); expect(captured!.searchParams.get("rkey")).toBe("episode1"); expect(options!.redirect).toBe("error"); }); test("handles both XRPC 400 RecordNotFound and HTTP 404", async () => { await expect(mockReader(() => Response.json({ error: "RecordNotFound" }, { status: 400 })).get(EPISODE_URI)).rejects.toMatchObject({ status: 404, code: "record_not_found" }); await expect(mockReader(() => new Response(null, { status: 404 })).get(EPISODE_URI)).rejects.toMatchObject({ status: 404 }); }); test("upstream errors remain unavailable, not deleted", async () => { await expect(mockReader(() => new Response(null, { status: 503 })).get(EPISODE_URI)).rejects.toMatchObject({ status: 503 }); await expect(mockReader(() => Response.json({ error: "InvalidRequest" }, { status: 400 })).get(EPISODE_URI)).rejects.toMatchObject({ status: 503 }); }); test("rejects mismatched URI, missing CID, invalid JSON, and oversized responses", async () => { for (const response of [ () => Response.json({ uri: EPISODE_URI + "x", cid: CID1, value: episodeRecord() }), () => Response.json({ uri: EPISODE_URI, value: episodeRecord() }), () => new Response("not JSON"), () => new Response("x".repeat(1_048_577)), ]) await expect(mockReader(response).get(EPISODE_URI)).rejects.toMatchObject({ status: 502 }); }); test("network failures fail closed", async () => { await expect(mockReader(() => { throw new Error("offline"); }).get(EPISODE_URI)).rejects.toMatchObject({ status: 503 }); });});