Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209import { describe, expect, it, vi } from "vitest";import { createBobbinClient } from "./client";import { gitTarget } from "./gitclient";import { loadPullDiff, loadPullInterdiff } from "./pullDiff";import type { MirrorFileDiff } from "./gitmirror";
const src = (path: string, extra: Partial<MirrorFileDiff["lhsSrc"]> = {}) => ({ path, oid: `oid-${path}`, size: 10, isBinary: false, isSubmodule: false, ...extra});
// gitmirror names both sides of an add or delete after the same path and marks the// side that doesn't exist with a null oidconst absent = (path: string) => src(path, { oid: "0".repeat(40), size: 0 });
const json = (body: unknown) => new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } });
const params = { baseRepo: "did:plc:repo" as never, baseCommit: "base", headRepo: "did:plc:repo" as never, headCommit: "head"};
// contents keyed by `<ref>:<path>`, so each side can differconst harness = (diffs: MirrorFileDiff[], contents: Record<string, string>) => { const calls: string[] = []; const fetchMock = vi.fn<typeof globalThis.fetch>().mockImplementation(async (input) => { const url = new URL(String(input)); calls.push(url.pathname); if (url.pathname.endsWith("temp2.getDiff") || url.pathname.endsWith("temp2.getInterdiff")) { return json({ diffs }); } const key = `${url.searchParams.get("ref")}:${url.searchParams.get("path")}`; const content = contents[key]; if (content === undefined) return new Response("nope", { status: 404 }); return json({ path: url.searchParams.get("path"), content, encoding: "utf-8", size: 1 }); });
const ctx = createBobbinClient({ serviceUrl: "https://bobbin.test", fetch: fetchMock }); const git = gitTarget( { bobbinUrl: "https://bobbin.test", knotMirrorUrl: "" } as never, { uri: "at://did:plc:alice/sh.tangled.repo/core", repoDid: "did:plc:repo" }, fetchMock ); return { ctx, git, calls };};
describe("loadPullDiff", () => { it("pairs each file's two sides and sums the stat", async () => { const { ctx, git, calls } = harness( [ { lhsSrc: src("a.ts"), rhsSrc: src("a.ts"), hunks: [{ novelLhs: [0], novelRhs: [0, 1], lines: [{ lhs: 0, rhs: 0 }, { rhs: 1 }] }], hasSyntacticChanges: false }, { lhsSrc: absent("new.ts"), rhsSrc: src("new.ts"), hunks: [{ novelLhs: [], novelRhs: [0], lines: [{ rhs: 0 }] }], hasSyntacticChanges: false }, { lhsSrc: src("gone.ts"), rhsSrc: absent("gone.ts"), hunks: [{ novelLhs: [0], novelRhs: [], lines: [{ lhs: 0 }] }], hasSyntacticChanges: false } ], { "base:a.ts": "one\n", "head:a.ts": "two\nthree\n", "head:new.ts": "hello\n", "base:gone.ts": "bye\n" } );
const diff = await loadPullDiff(ctx, git, params, "unified");
expect(diff.stat).toEqual({ insertions: 3, deletions: 2, files_changed: 3 }); expect(diff.truncated).toBe(0); expect(diff.files[0]).toMatchObject({ name: "a.ts", kind: "changed", oldFile: { name: "a.ts", contents: "one\n" }, newFile: { name: "a.ts", contents: "two\nthree\n" } }); // a file with no left side gets an empty left side, not a failed fetch expect(diff.files[1]).toMatchObject({ name: "new.ts", kind: "new", oldFile: { contents: "" }, newFile: { contents: "hello\n" } }); expect(diff.files[2]).toMatchObject({ name: "gone.ts", kind: "deleted", oldFile: { contents: "bye\n" }, newFile: { contents: "" } }); // two for the changed file, one each for the sides that exist: the absent side // is never fetched, it would 404 expect(calls.filter((path) => path.endsWith("repo.blob"))).toHaveLength(4); // the server prerenders each pair so the first paint ships highlighted expect(diff.prerendered?.["a.ts→a.ts"]).toContain("three"); });
it("notes the files it cannot render instead of dropping them", async () => { const { ctx, git } = harness( [ { lhsSrc: src("logo.png", { isBinary: true }), rhsSrc: src("logo.png", { isBinary: true }), hunks: [], hasSyntacticChanges: false }, { lhsSrc: src("vendor", { isSubmodule: true }), rhsSrc: src("vendor", { isSubmodule: true }), hunks: [], hasSyntacticChanges: false }, { lhsSrc: src("gone.ts"), rhsSrc: src("gone.ts"), hunks: [], hasSyntacticChanges: false } ], {} );
const diff = await loadPullDiff(ctx, git, params, "unified");
expect(diff.files.map((file) => file.note)).toEqual([ "This is a binary file and will not be displayed.", "Submodule pointer, not shown.", // both blob fetches 404 "Contents could not be loaded." ]); expect(diff.files.every((file) => file.oldFile === undefined)).toBe(true); });});
describe("loadPullInterdiff", () => { const interdiffParams = { baseRepo: "did:plc:repo" as never, baseCommit1: "v1base", baseCommit2: "v1head", headRepo: "did:plc:repo" as never, headCommit1: "v2base", headCommit2: "v2head" };
it("takes the left side inline and fetches only the right", async () => { const { ctx, git, calls } = harness( [ { lhsSrc: src("a.ts", { content: "one\n" }), rhsSrc: src("a.ts"), hunks: [{ novelLhs: [0], novelRhs: [0], lines: [{ lhs: 0, rhs: 0 }] }], hasSyntacticChanges: false }, // the rebased tree only exists inside one gitmirror request, so a left // side with no inline text can never be recovered { lhsSrc: src("b.ts"), rhsSrc: src("b.ts"), hunks: [], hasSyntacticChanges: false }, // an added file has no left side to inline in the first place { lhsSrc: absent("c.ts"), rhsSrc: src("c.ts"), hunks: [{ novelLhs: [], novelRhs: [0], lines: [{ rhs: 0 }] }], hasSyntacticChanges: false } ], { "v2head:a.ts": "two\n", "v2head:b.ts": "two\n", "v2head:c.ts": "new\n" } );
const diff = await loadPullInterdiff(ctx, git, interdiffParams, "unified");
expect(diff.files[0]).toMatchObject({ oldFile: { name: "a.ts", contents: "one\n" }, newFile: { name: "a.ts", contents: "two\n" } }); expect(diff.files[1].note).toBe("Contents could not be loaded."); expect(diff.files[2]).toMatchObject({ kind: "new", oldFile: { contents: "" }, newFile: { name: "c.ts", contents: "new\n" } }); // one blob per file, the right side only expect(calls.filter((path) => path.endsWith("repo.blob"))).toHaveLength(3); });});