Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213import { describe, expect, it, vi } from "vitest";import type { Cid, ResourceUri } from "@atcute/lexicons/syntax";import type { BobbinContext } from "#lib/api/client.ts";import { awaitDeletedRecord, awaitIndexedOrSaved, awaitIndexedRecord, awaitRecord} from "#lib/api/awaitRecord.ts";
const revision = { uri: "at://did:plc:alice/sh.tangled.repo/demo" as ResourceUri, cid: "bafyrevision" as Cid};
const notSettled = { ok: false as const, status: 504, data: { error: "NotSettled", message: "record has not been ingested yet" }};
const contextFor = (data: Record<string, unknown>) => { const call = vi.fn(async () => ({ ok: true, data })); const ctx = { xrpc: { call } } as unknown as BobbinContext; return { call, ctx };};
describe("awaitRecord", () => { it("forwards a written revision and returns bobbin's result", async () => { const { call, ctx } = contextFor({ status: "indexed", cid: revision.cid });
await expect(awaitRecord(ctx, revision)).resolves.toMatchObject({ status: "indexed" }); expect(call).toHaveBeenCalledWith(expect.anything(), { params: { uri: revision.uri, cid: revision.cid } }); });
it("supports cid-less delete waits", async () => { const { call, ctx } = contextFor({ status: "deleted" });
await expect(awaitRecord(ctx, { uri: revision.uri })).resolves.toMatchObject({ status: "deleted" }); expect(call).toHaveBeenCalledWith(expect.anything(), { params: { uri: revision.uri } }); });});
describe("awaitIndexedRecord", () => { it("shouldn't serve up a (yet to be) unindexed rev from bobbin's perspective", async () => { const { call, ctx } = contextFor({ status: "rejected", detail: "invalid repository" });
await expect(awaitIndexedRecord(ctx, revision)).rejects.toThrow("invalid repository"); expect(call).toHaveBeenCalledTimes(1); });
it("continues to query a bunch when not yet ingested from bobbin side", async () => { vi.useFakeTimers(); try { const replies: unknown[] = [ notSettled, { ok: false, status: 503, data: { error: "Overloaded", message: "too many waiters" } }, { ok: true, data: { status: "indexed", cid: revision.cid } } ]; const call = vi.fn(async () => replies.shift()); const ctx = { xrpc: { call } } as unknown as BobbinContext;
const waiting = expect(awaitIndexedRecord(ctx, revision)).resolves.toBeUndefined(); await vi.advanceTimersByTimeAsync(10_000); await waiting; expect(call).toHaveBeenCalledTimes(3); } finally { vi.useRealTimers(); } });
it("doesn't show NotSettled until whole timeout reached", async () => { vi.useFakeTimers(); try { const call = vi.fn(async () => notSettled); const ctx = { xrpc: { call } } as unknown as BobbinContext;
const waiting = expect(awaitIndexedRecord(ctx, revision)).rejects.toThrow("NotSettled"); await vi.advanceTimersByTimeAsync(60_000); await waiting; expect(call.mock.calls.length).toBeGreaterThan(1); } finally { vi.useRealTimers(); } });
it("picks up querying if conn drop mid wait", async () => { vi.useFakeTimers(); try { const replies: unknown[] = [ new TypeError("NetworkError when attempting to fetch resource."), { ok: true, data: { status: "indexed", cid: revision.cid } } ]; const call = vi.fn(async () => replies.shift()); const ctx = { xrpc: { call } } as unknown as BobbinContext;
const waiting = expect(awaitIndexedRecord(ctx, revision)).resolves.toBeUndefined(); await vi.advanceTimersByTimeAsync(10_000); await waiting; expect(call).toHaveBeenCalledTimes(2); } finally { vi.useRealTimers(); } });
it("doesn't wait for dud URLs for whom there'd be no point polling", async () => { const call = vi.fn(async () => ({ ok: false, status: 400, data: { error: "InvalidParams", message: "uri must be an at-uri" } })); const ctx = { xrpc: { call } } as unknown as BobbinContext;
await expect(awaitIndexedRecord(ctx, revision)).rejects.toThrow("InvalidParams"); expect(call).toHaveBeenCalledTimes(1); });});
describe("awaitIndexedOrSaved", () => { it("reports a saved record when bobbin cannot be reached", async () => { vi.useFakeTimers(); try { const call = vi.fn(async () => { throw new TypeError("NetworkError when attempting to fetch resource."); }); const ctx = { xrpc: { call } } as unknown as BobbinContext;
const waiting = expect(awaitIndexedOrSaved(ctx, revision)).rejects.toThrow(/^Saved\./); await vi.advanceTimersByTimeAsync(60_000); await waiting; expect(call.mock.calls.length).toBeGreaterThan(1); } finally { vi.useRealTimers(); } });});
describe("awaitDeletedRecord", () => { it("should return true if record is gone", async () => { vi.useFakeTimers(); try { const replies: unknown[] = [ { ok: true, data: { status: "indexed", cid: revision.cid } }, { ok: true, data: { status: "deleted" } } ]; const call = vi.fn(async () => replies.shift()); const ctx = { xrpc: { call } } as unknown as BobbinContext;
const waiting = expect(awaitDeletedRecord(ctx, revision.uri)).resolves.toBe(true); await vi.advanceTimersByTimeAsync(10_000); await waiting; expect(call).toHaveBeenCalledTimes(2); } finally { vi.useRealTimers(); } });
it("continues to query if deletion isn't ingested yet", async () => { vi.useFakeTimers(); try { const replies: unknown[] = [ notSettled, notSettled, { ok: true, data: { status: "deleted" } } ]; const call = vi.fn(async () => replies.shift()); const ctx = { xrpc: { call } } as unknown as BobbinContext;
const waiting = expect(awaitDeletedRecord(ctx, revision.uri)).resolves.toBe(true); await vi.advanceTimersByTimeAsync(10_000); await waiting; expect(call).toHaveBeenCalledTimes(3); } finally { vi.useRealTimers(); } });
it("returns false if record still appears to be un-deleted and the poll time runs out", async () => { vi.useFakeTimers(); try { const call = vi.fn(async () => ({ ok: true, data: { status: "indexed" } })); const ctx = { xrpc: { call } } as unknown as BobbinContext;
const waiting = expect(awaitDeletedRecord(ctx, revision.uri)).resolves.toBe(false); await vi.advanceTimersByTimeAsync(60_000); await waiting; expect(call.mock.calls.length).toBeGreaterThan(1); } finally { vi.useRealTimers(); } });
it("returns false if bobbin rejects waiting in the first place", async () => { const call = vi.fn(async () => ({ ok: false, status: 400, data: { error: "InvalidParams", message: "uri must be an at-uri" } })); const ctx = { xrpc: { call } } as unknown as BobbinContext;
await expect(awaitDeletedRecord(ctx, revision.uri)).resolves.toBe(false); expect(call).toHaveBeenCalledTimes(1); });});