import assert from "node:assert/strict"; import test from "node:test"; import { getEndorsements, listBlogPosts } from "./data.ts"; const originalFetch = globalThis.fetch; const originalDid = process.env.CAMERON_DID; const originalService = process.env.ATP_SERVICE; const originalTimeout = process.env.PDS_FETCH_TIMEOUT_MS; function restoreEnvironment(): void { globalThis.fetch = originalFetch; if (originalDid === undefined) delete process.env.CAMERON_DID; else process.env.CAMERON_DID = originalDid; if (originalService === undefined) delete process.env.ATP_SERVICE; else process.env.ATP_SERVICE = originalService; if (originalTimeout === undefined) delete process.env.PDS_FETCH_TIMEOUT_MS; else process.env.PDS_FETCH_TIMEOUT_MS = originalTimeout; } test("shares one in-flight PDS record request across concurrent Blog renders", async () => { process.env.CAMERON_DID = "did:example:pds-singleflight"; process.env.ATP_SERVICE = "https://pds.example"; let fetchCalls = 0; let releaseFetch: (() => void) | undefined; globalThis.fetch = (() => { fetchCalls += 1; return new Promise((resolve) => { releaseFetch = () => resolve(Response.json({ records: [{ uri: "at://did:example:pds-singleflight/site.standard.document/post", value: { site: "at://did:plc:gfrmhdmjvxn2sjedzboeudef/site.standard.publication/3md7ylshxzk2y", title: "Post", path: "/post", publishedAt: "2026-07-27T00:00:00.000Z", tags: ["blog"], textContent: "Body", }, }] })); }); }) as typeof fetch; try { const first = listBlogPosts(); const second = listBlogPosts(); await new Promise((resolve) => setImmediate(resolve)); assert.equal(fetchCalls, 1); releaseFetch?.(); assert.deepEqual(await first, await second); } finally { restoreEnvironment(); } }); test("uses Blog publication membership instead of requiring an optional blog tag", async () => { process.env.CAMERON_DID = "did:example:pds-blog-membership"; process.env.ATP_SERVICE = "https://pds.example"; globalThis.fetch = (async () => Response.json({ records: [ { uri: "at://did:example:pds-blog-membership/site.standard.document/agent-sdk", value: { site: "at://did:plc:gfrmhdmjvxn2sjedzboeudef/site.standard.publication/3md7ylshxzk2y", title: "The Letta Agent SDK", path: "/agent-sdk", publishedAt: "2026-08-18T19:59:27.602Z", tags: ["Letta", "artificial intelligence", "Agents"], textContent: "A Blog post whose topic tags do not include blog.", }, }, { uri: "at://did:example:pds-blog-membership/site.standard.document/not-camerons-blog", value: { site: "at://did:example:pds-blog-membership/site.standard.publication/other", title: "Another publication", path: "/other", publishedAt: "2026-08-18T20:00:00.000Z", tags: ["blog"], textContent: "A blog-tagged record outside Cameron's Blog publication.", }, }, ] })) as typeof fetch; try { const posts = await listBlogPosts(); assert.equal(posts.length, 1); assert.equal(posts[0]?.title, "The Letta Agent SDK"); assert.deepEqual(posts[0]?.tags, ["Letta", "artificial intelligence", "Agents"]); } finally { restoreEnvironment(); } }); test("paginates the document collection before filtering Blog records", async () => { process.env.CAMERON_DID = "did:example:pds-blog-pagination"; process.env.ATP_SERVICE = "https://pds.example"; const publication = "at://did:plc:gfrmhdmjvxn2sjedzboeudef/site.standard.publication/3md7ylshxzk2y"; const requests: URL[] = []; const blogRecord = (index: number) => ({ uri: `at://did:example:pds-blog-pagination/site.standard.document/blog-${index}`, value: { site: publication, title: `Blog ${index}`, path: `/blog-${index}`, publishedAt: `2026-01-${String((index % 28) + 1).padStart(2, "0")}T00:00:00.000Z`, tags: ["blog"], textContent: `Body ${index}`, }, }); const knowledgeRecord = (index: number) => ({ uri: `at://did:example:pds-blog-pagination/site.standard.document/knowledge-${index}`, value: { site: "at://did:example:pds-blog-pagination/site.standard.publication/knowledge", title: `Knowledge ${index}`, path: `/knowledge/${index}`, publishedAt: "2026-01-01T00:00:00.000Z", tags: ["knowledge"], textContent: `Knowledge ${index}`, }, }); globalThis.fetch = (async (input) => { const url = new URL(String(input)); requests.push(url); if (!url.searchParams.has("cursor")) { return Response.json({ records: [ ...Array.from({ length: 92 }, (_, index) => knowledgeRecord(index)), ...Array.from({ length: 8 }, (_, index) => blogRecord(index)), ], cursor: "page-two", }); } if (url.searchParams.get("cursor") === "page-two") { return Response.json({ records: Array.from({ length: 33 }, (_, index) => blogRecord(index + 8)), cursor: "terminal-probe", }); } assert.equal(url.searchParams.get("cursor"), "terminal-probe"); return Response.json({ records: [] }); }) as typeof fetch; try { const posts = await listBlogPosts(); assert.equal(posts.length, 41); assert.equal(requests.length, 3); assert.equal(requests[0]?.searchParams.get("limit"), "100"); assert.equal(requests[1]?.searchParams.get("cursor"), "page-two"); assert.equal(requests[2]?.searchParams.get("cursor"), "terminal-probe"); } finally { restoreEnvironment(); } }); test("bounds required PDS record reads when the service stalls", async () => { process.env.CAMERON_DID = "did:example:pds-timeout"; process.env.ATP_SERVICE = "https://pds.example"; process.env.PDS_FETCH_TIMEOUT_MS = "20"; globalThis.fetch = ((_input, init) => new Promise((_resolve, reject) => { const fallback = setTimeout( () => reject(new Error("PDS fetch was not aborted")), 500, ); init?.signal?.addEventListener( "abort", () => { clearTimeout(fallback); reject(init.signal?.reason ?? new Error("aborted")); }, { once: true }, ); })) as typeof fetch; try { const startedAt = Date.now(); await assert.rejects(listBlogPosts(), /timeout|aborted/i); assert.ok(Date.now() - startedAt < 250); } finally { restoreEnvironment(); } }); test("serves last-known Blog records after the cache expires during a PDS outage", async () => { process.env.CAMERON_DID = "did:example:pds-stale"; process.env.ATP_SERVICE = "https://pds.example"; process.env.PDS_FETCH_TIMEOUT_MS = "20"; const originalDateNow = Date.now; let now = 1_785_192_000_000; Date.now = () => now; globalThis.fetch = (async () => Response.json({ records: [{ uri: "at://did:example:pds-stale/site.standard.document/post", value: { site: "at://did:plc:gfrmhdmjvxn2sjedzboeudef/site.standard.publication/3md7ylshxzk2y", title: "Stale Post", path: "/stale-post", publishedAt: "2026-07-27T00:00:00.000Z", tags: ["blog"], textContent: "Still useful during an outage.", }, }] })) as typeof fetch; try { const fresh = await listBlogPosts(); assert.equal(fresh[0]?.title, "Stale Post"); now += 5 * 60 * 1_000 + 1; globalThis.fetch = ((_input, init) => new Promise((_resolve, reject) => { const fallback = setTimeout( () => reject(new Error("PDS fetch was not aborted")), 500, ); init?.signal?.addEventListener( "abort", () => { clearTimeout(fallback); reject(init.signal?.reason ?? new Error("aborted")); }, { once: true }, ); })) as typeof fetch; const stale = await listBlogPosts(); assert.deepEqual(stale, fresh); } finally { Date.now = originalDateNow; restoreEnvironment(); } }); test("does not promote an optional outage sentinel into last-known records", async () => { process.env.CAMERON_DID = "did:example:pds-optional-empty"; process.env.ATP_SERVICE = "https://pds.example"; process.env.PDS_FETCH_TIMEOUT_MS = "20"; const originalDateNow = Date.now; const originalWarn = console.warn; let now = 1_785_192_000_000; const warnings: string[] = []; Date.now = () => now; console.warn = (message) => warnings.push(String(message)); globalThis.fetch = ((_input, init) => new Promise((_resolve, reject) => { const fallback = setTimeout( () => reject(new Error("PDS fetch was not aborted")), 500, ); init?.signal?.addEventListener( "abort", () => { clearTimeout(fallback); reject(init.signal?.reason ?? new Error("aborted")); }, { once: true }, ); })) as typeof fetch; try { assert.deepEqual(await getEndorsements(), []); assert.deepEqual(await getEndorsements(), []); now += 31_000; assert.deepEqual(await getEndorsements(), []); assert.equal(warnings.length, 2); assert.ok(warnings.every((warning) => warning.includes("empty optional surface"))); } finally { Date.now = originalDateNow; console.warn = originalWarn; restoreEnvironment(); } }); test("uses one wall-clock deadline across DID resolution and PDS reads", async () => { process.env.CAMERON_DID = "did:example:pds-deadline"; delete process.env.ATP_SERVICE; process.env.PDS_FETCH_TIMEOUT_MS = "50"; const startedAt = Date.now(); globalThis.fetch = ((input, init) => { if (String(input).startsWith("https://plc.directory/")) { return new Promise((resolve) => { setTimeout(() => resolve(Response.json({ service: [{ id: "#atproto_pds", type: "AtprotoPersonalDataServer", serviceEndpoint: "https://pds.example", }] })), 30); }); } return new Promise((_resolve, reject) => { const fallback = setTimeout( () => reject(new Error("PDS fetch was not aborted")), 500, ); init?.signal?.addEventListener( "abort", () => { clearTimeout(fallback); reject(init.signal?.reason ?? new Error("aborted")); }, { once: true }, ); }); }) as typeof fetch; try { await assert.rejects(listBlogPosts(), /timeout|aborted/i); assert.ok(Date.now() - startedAt < 75); } finally { restoreEnvironment(); } });