Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222import { describe, expect, it, vi } from "vitest";import { ClientResponseError } from "#lib/api/client.ts";import { classifyRepoAvailability, sidebarOutgrowsFiles, withAuthorHandles, withImportAvailability, type RepoAvailability} from "#lib/api/repoIndex.ts";import type { ImportState } from "#lib/api/repo.ts";import type { CommitSummary } from "#lib/components/repo/types.ts";
const notSynced = () => new ClientResponseError({ status: 404, data: { error: "RepoNotFound" } });
describe("classifyRepoAvailability", () => { it("recognizes a repo the mirror has not synced yet", () => { const result = classifyRepoAvailability( [notSynced(), notSynced(), notSynced()].map((cause) => ({ value: null, error: cause })) ); expect(result).toBe("not-synced"); });
it("does not treat other 404s as not synced", () => { const result = classifyRepoAvailability( ["tree", "log", "branches"].map(() => ({ value: null, error: new ClientResponseError({ status: 404, data: { error: "XRPCNotSupported" } }) })) ); expect(result).toBe("error"); });
it("keeps transport failures as errors", () => { const result = classifyRepoAvailability( ["tree", "log", "branches"].map(() => ({ value: null, error: new TypeError("offline") })) ); expect(result).toBe("error"); });
it("does not mistake a missing ref for an unsynced repo", () => { const result = classifyRepoAvailability([ { value: null, error: notSynced() }, { value: null, error: notSynced() }, { value: { branches: [] }, error: null } ]); expect(result).toBe("ok"); });});
describe("withImportAvailability", () => { it.each([ ["ok", { content: "fetching" as const }, "ok"], ["ok", { content: "partial" as const }, "ok"], ["ok", { content: "failed" as const }, "ok"], ["not-synced", null, "not-synced"], ["not-synced", { content: "fetching" as const }, "importing"], ["not-synced", { content: "partial" as const }, "importing"], ["not-synced", { content: "failed" as const }, "import-failed"], ["empty", null, "empty"], ["error", null, "error"], ["empty", { content: "failed" as const }, "import-failed"], ["error", { content: "failed" as const }, "import-failed"], ["empty", { content: "fetching" as const }, "importing"], ["empty", { content: "partial" as const }, "importing"], ["error", { content: "fetching" as const }, "importing"], ["error", { content: "partial" as const }, "importing"] ] satisfies [RepoAvailability, ImportState | null, RepoAvailability][])( "maps (%s, %j) -> %s", (availability, status, expected) => { expect(withImportAvailability(availability, status)).toBe(expected); } );});
const commit = (c: Partial<CommitSummary>): CommitSummary => ({ hash: "h", shortHash: "h", subject: "s", body: "", authorName: "", authorEmail: "", when: "2026-01-01T00:00:00Z", ...c});
// dispatches the two upstreams withAuthorHandles touches: the internal kv// route (relative) and bobbin's resolveMiniDoc.const fetchMock = vi.fn(async (input: string | URL, _init?: RequestInit) => { const url = String(input); if (url.startsWith("/_internal/email-did")) { return new Response( JSON.stringify({ "alice@example.com": "did:plc:alice", "bob@example.com": "did:plc:bob", "MIXED@Example.com": "did:plc:mixed", "ghost@example.com": null }) ); } if (url.includes("blue.microcosm.identity.resolveMiniDoc")) { const did = new URL(url).searchParams.get("identifier") ?? ""; return new Response(JSON.stringify({ did, handle: `${did.replace("did:plc:", "")}.test` })); } return new Response("unexpected", { status: 404 });});
describe("withAuthorHandles", () => { it("resolves verified emails to a did before hydrating handles", async () => { const commits = [ commit({ authorName: "Alice", authorEmail: "alice@example.com" }), commit({ authorName: "Signed", authorEmail: "did:plc:signed", authorDid: "did:plc:signed" }) ]; const out = await withAuthorHandles( commits, "https://bobbin.test", fetchMock as typeof globalThis.fetch ); expect(out[0]).toMatchObject({ authorDid: "did:plc:alice", authorHandle: "alice.test" }); expect(out[1]).toMatchObject({ authorDid: "did:plc:signed", authorHandle: "signed.test" }); });
it("keeps the requested email case when looking up the kv result", async () => { const out = await withAuthorHandles( [commit({ authorEmail: "MIXED@Example.com" })], "https://bobbin.test", fetchMock as typeof globalThis.fetch ); expect(out[0]).toMatchObject({ authorDid: "did:plc:mixed", authorHandle: "mixed.test" }); });
it("leaves unresolved emails as plain authors", async () => { const out = await withAuthorHandles( [commit({ authorEmail: "ghost@example.com" })], "https://bobbin.test", fetchMock as typeof globalThis.fetch ); expect(out[0].authorDid).toBeUndefined(); expect(out[0].authorHandle).toBeUndefined(); });
it("fails open when the kv route errors", async () => { const broken = vi.fn(async () => new Response("nope", { status: 500 })); const out = await withAuthorHandles( [commit({ authorEmail: "alice@example.com" })], "https://bobbin.test", broken as typeof globalThis.fetch ); expect(out[0].authorDid).toBeUndefined(); });
it("keeps a did from the email pass when handle resolution fails", async () => { const brokenBobbin = vi.fn(async (input: string | URL) => { if (String(input).startsWith("/_internal/email-did")) { return new Response(JSON.stringify({ "alice@example.com": "did:plc:alice" })); } return new Response("nope", { status: 500 }); }); const out = await withAuthorHandles( [commit({ authorEmail: "alice@example.com" })], "https://bobbin.test", brokenBobbin as typeof globalThis.fetch ); expect(out[0]).toMatchObject({ authorDid: "did:plc:alice" }); expect(out[0].authorHandle).toBeUndefined(); });});
describe("sidebarOutgrowsFiles", () => { const files = (count: number) => Array.from({ length: count }, (_, index) => ({ name: `file${index}`, kind: "file" as const, size: 1 })); const commits = (count: number, subject = "s") => Array.from({ length: count }, (_, index) => commit({ hash: `h${index}`, subject })); const branches = [ { name: "main", hash: "h0", isDefault: true }, { name: "dev", hash: "h1", isDefault: false } ]; const tags = [{ name: "v0.1.0", commitHash: "h0" }];
it("moves the readme up for a small repo with a full sidebar", () => { expect( sidebarOutgrowsFiles({ files: files(8), commits: commits(10), branches, tags }) ).toBe(true); });
it("keeps the layout once the file list nears the sidebar height", () => { expect( sidebarOutgrowsFiles({ files: files(28), commits: commits(10), branches, tags }) ).toBe(false); });
it("keeps the layout for a young repo with a short sidebar", () => { expect( sidebarOutgrowsFiles({ files: files(8), commits: commits(3), branches: branches.slice(0, 1), tags: [] }) ).toBe(false); });
it("counts wrapping commit subjects as taller rows", () => { const input = { files: files(22), branches, tags }; expect(sidebarOutgrowsFiles({ ...input, commits: commits(10) })).toBe(false); expect(sidebarOutgrowsFiles({ ...input, commits: commits(10, "x".repeat(70)) })).toBe(true); });});