Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485import { afterEach, describe, expect, it, vi } from "vitest";import type { BobbinContext } from "$lib/api/client";import { didOf, knotOf } from "$lib/api/syntax";
const ctx = { xrpc: {}, serviceUrl: "https://bobbin.test" } as unknown as BobbinContext;const repoDid = didOf("did:web:knot.test:repo");
vi.mock("$app/environment", () => ({ browser: true }));
const knotPage = (capabilities?: string[]) => ({ ok: true, json: async () => ({ capabilities }) }) as unknown as Response;
const load = (knot: string | undefined) => { vi.resetModules(); vi.doMock("$lib/api/repo", () => ({ resolveRepoKnot: vi.fn(async () => knot) })); return import("./knot");};
afterEach(() => { vi.unstubAllGlobals();});
describe("knotCapabilities", () => { it("calls describeKnot once, caches responses, and shares one probe between concurrent callers", async () => { const urls: string[] = []; vi.stubGlobal( "fetch", vi.fn(async (input: RequestInfo | URL) => { urls.push(String(input)); return knotPage(["knot-social-writes"]); }) ); const { knotCapabilities } = await load("knot.test"); const [first, second] = await Promise.all([ knotCapabilities(knotOf("knot.test")), knotCapabilities(knotOf("knot.test")) ]); await knotCapabilities(knotOf("knot.test")); expect(urls).toEqual(["https://knot.test/xrpc/org.tangled.knot.describeKnot"]); expect(first).toEqual(["knot-social-writes"]); expect(second).toEqual(first); });
it("returns null for malformed probe bodies instead of crashing, whatever alierns send", async () => { for (const body of [null, { capabilities: 42 }, { capabilities: ["ok", 7] }, "nope"]) { vi.stubGlobal( "fetch", vi.fn(async () => ({ ok: true, json: async () => body }) as unknown as Response) ); const served = await load("knot.test"); expect(await served.knotCapabilities(knotOf("knot.test"))).toBe(null); expect(await served.writeTarget(ctx, repoDid)).toEqual({ kind: "pds" }); } });});
describe("writeTarget", () => { it("routes writes to knots only when repos resolve, probes succeed, and social writes are served", async () => { const served = await load("knot.test"); vi.stubGlobal( "fetch", vi.fn(async () => knotPage(["knot-social-writes"])) ); expect(await served.writeTarget(ctx, repoDid)).toEqual({ kind: "knot", knot: "knot.test", repoDid });
vi.stubGlobal( "fetch", vi.fn(async () => knotPage(["knot-acl"])) ); const legacy = await load("knot.test"); expect(await legacy.writeTarget(ctx, repoDid)).toEqual({ kind: "pds" });
vi.stubGlobal( "fetch", vi.fn(async () => ({ ok: false }) as unknown as Response) ); const offline = await load(undefined); expect(await offline.writeTarget(ctx, repoDid)).toEqual({ kind: "pds" }); });});