Open History to see the last commit
1import { DiffHunksRenderer, hydratePartialDiff, type FileDiffMetadata } from "@pierre/diffs";
2import { excerptRows, fileDiffRows, rowsToPatch } from "@t3tools/client-runtime/review-guide-tour";
3import { EnvironmentId } from "@t3tools/contracts";
4import { AsyncResult } from "effect/reactivity";
5import { expect, it } from "vite-plus/test";
6
7import { createGitDiffFileContentsLoader } from "~/lib/diffFileContents";
8import { getRenderablePatch } from "~/lib/diffRendering";
9import { excerptLoader } from "./GuideExcerpt.logic";
10
11function parse(patch: string): FileDiffMetadata {
12 const renderable = getRenderablePatch(patch, "guide-excerpt-test");
13 if (renderable?.kind !== "files") throw new Error("Expected a parsed patch.");
14 return renderable.files[0]!;
15}
16
17it("expands an excerpt whose file has another hunk that changes its length", async () => {
18 const original = Array.from({ length: 100 }, (_, index) => `line ${index + 1}`);
19 const changed = original.map((line, index) => (index === 29 ? "line 30 changed" : line));
20 changed.splice(80, 0, "later insertion");
21 const file = parse(
22 [
23 "diff --git a/file.ts b/file.ts",
24 "--- a/file.ts",
25 "+++ b/file.ts",
26 "@@ -27,7 +27,7 @@",
27 " line 27",
28 " line 28",
29 " line 29",
30 "-line 30",
31 "+line 30 changed",
32 " line 31",
33 " line 32",
34 " line 33",
35 "@@ -78,6 +78,7 @@",
36 " line 78",
37 " line 79",
38 " line 80",
39 "+later insertion",
40 " line 81",
41 " line 82",
42 " line 83",
43 "",
44 ].join("\n"),
45 );
46 const excerpt = { path: "file.ts", side: "new", start: 30, end: 30, caption: "" } as const;
47 const rows = excerptRows(fileDiffRows(file), excerpt);
48 const excerptDiff = parse(rowsToPatch("file.ts", "file.ts", rows));
49 const loadDiffFiles = createGitDiffFileContentsLoader(
50 async () =>
51 AsyncResult.success({
52 oldContents: `${original.join("\n")}\n`,
53 newContents: `${changed.join("\n")}\n`,
54 }),
55 {
56 environmentId: EnvironmentId.make("environment-guide-excerpt-test"),
57 cwd: "/repo",
58 sourceKind: "working-tree",
59 baseRef: null,
60 headRef: null,
61 cacheKey: "diff-hash",
62 },
63 );
64
65 const files = await excerptLoader(excerpt, file, rows, loadDiffFiles)!(excerptDiff);
66 const hydrated = hydratePartialDiff("clone", excerptDiff, files);
67
68 await expect(
69 new DiffHunksRenderer({ expandUnchanged: true }).asyncRender(hydrated),
70 ).resolves.toBeDefined();
71});
72
73it("leaves a deleted file's excerpt without context expansion", () => {
74 const file = parse(
75 [
76 "diff --git a/file.ts b/file.ts",
77 "deleted file mode 100644",
78 "--- a/file.ts",
79 "+++ /dev/null",
80 "@@ -1,3 +0,0 @@",
81 "-a",
82 "-b",
83 "-c",
84 "",
85 ].join("\n"),
86 );
87 const excerpt = { path: "file.ts", side: "old", start: 2, end: 2, caption: "" } as const;
88 const rows = excerptRows(fileDiffRows(file), excerpt);
89
90 expect(excerptLoader(excerpt, file, rows, async () => Promise.reject(new Error("unused")))).toBe(
91 null,
92 );
93});