The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { FileDiffMetadata } from "@pierre/diffs"; |
| 2 | import { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { AsyncResult } from "effect/reactivity"; |
| 4 | import { describe, expect, it } from "vite-plus/test"; |
| 5 | |
| 6 | import { createGitDiffFileContentsLoader } from "./diffFileContents"; |
| 7 | import { getRenderablePatch } from "./diffRendering"; |
| 8 | |
| 9 | const originalLines = Array.from({ length: 40 }, (_, index) => `line ${index + 1}`); |
| 10 | const changedLines = originalLines.map((line, index) => (index === 29 ? "line 30 changed" : line)); |
| 11 | |
| 12 | function toContents(lines: ReadonlyArray<string>) { |
| 13 | return `${lines.join("\n")}\n`; |
| 14 | } |
| 15 | |
| 16 | function parsePartialFileDiff(patch: string): FileDiffMetadata { |
| 17 | const renderable = getRenderablePatch(patch, "diff-panel:light", { |
| 18 | compactPartialHunkOffsets: true, |
| 19 | }); |
| 20 | if (renderable?.kind !== "files") throw new Error("Expected a parsed patch."); |
| 21 | return renderable.files[0]!; |
| 22 | } |
| 23 | |
| 24 | function partialFileDiff(): FileDiffMetadata { |
| 25 | const patch = [ |
| 26 | "diff --git a/src/file01.ts b/src/file01.ts", |
| 27 | "index 1111111..2222222 100644", |
| 28 | "--- a/src/file01.ts", |
| 29 | "+++ b/src/file01.ts", |
| 30 | "@@ -27,7 +27,7 @@", |
| 31 | " line 27", |
| 32 | " line 28", |
| 33 | " line 29", |
| 34 | "-line 30", |
| 35 | "+line 30 changed", |
| 36 | " line 31", |
| 37 | " line 32", |
| 38 | " line 33", |
| 39 | "", |
| 40 | ].join("\n"); |
| 41 | return parsePartialFileDiff(patch); |
| 42 | } |
| 43 | |
| 44 | function loaderReturning(oldContents: string, newContents: string) { |
| 45 | return createGitDiffFileContentsLoader( |
| 46 | async () => AsyncResult.success({ oldContents, newContents }), |
| 47 | { |
| 48 | environmentId: EnvironmentId.make("environment-diff-contents-test"), |
| 49 | cwd: "/repo", |
| 50 | sourceKind: "working-tree", |
| 51 | baseRef: null, |
| 52 | headRef: null, |
| 53 | cacheKey: "diff-hash", |
| 54 | }, |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | describe("createGitDiffFileContentsLoader", () => { |
| 59 | it("loads contents that still reproduce the patch", async () => { |
| 60 | const loaded = await loaderReturning( |
| 61 | toContents(originalLines), |
| 62 | toContents(changedLines), |
| 63 | )(partialFileDiff()); |
| 64 | |
| 65 | expect(loaded.newFile.contents).toBe(toContents(changedLines)); |
| 66 | }); |
| 67 | |
| 68 | it.each([ |
| 69 | { |
| 70 | name: "end before the patch's hunk", |
| 71 | oldLines: originalLines.slice(0, 30), |
| 72 | newLines: changedLines.slice(0, 30), |
| 73 | }, |
| 74 | { |
| 75 | name: "have unchanged tails that no longer match between sides", |
| 76 | oldLines: originalLines, |
| 77 | newLines: [...changedLines, "line 41 appended"], |
| 78 | }, |
| 79 | { |
| 80 | name: "changed hunk lines without moving them", |
| 81 | oldLines: originalLines, |
| 82 | newLines: changedLines.map((line, index) => (index === 30 ? "edited since" : line)), |
| 83 | }, |
| 84 | ])("rejects contents that $name", async ({ oldLines, newLines }) => { |
| 85 | const loader = loaderReturning(toContents(oldLines), toContents(newLines)); |
| 86 | |
| 87 | await expect(loader(partialFileDiff())).rejects.toThrow("changed since the diff was loaded"); |
| 88 | }); |
| 89 | |
| 90 | it.each([ |
| 91 | { |
| 92 | name: "a renamed file", |
| 93 | patch: [ |
| 94 | "diff --git a/ leading.txt b/renamed.txt ", |
| 95 | "similarity index 100%", |
| 96 | "rename from leading.txt", |
| 97 | "rename to renamed.txt ", |
| 98 | "", |
| 99 | ], |
| 100 | oldPath: " leading.txt", |
| 101 | newPath: "renamed.txt ", |
| 102 | }, |
| 103 | { |
| 104 | name: "a modified file", |
| 105 | patch: [ |
| 106 | "diff --git a/trailing.txt b/trailing.txt ", |
| 107 | "index 422c2b7..55dce13 100644", |
| 108 | "--- a/trailing.txt \t", |
| 109 | "+++ b/trailing.txt \t", |
| 110 | "@@ -1,2 +1,2 @@", |
| 111 | " a", |
| 112 | "-b", |
| 113 | "+B", |
| 114 | "", |
| 115 | ], |
| 116 | oldPath: "trailing.txt ", |
| 117 | newPath: "trailing.txt ", |
| 118 | }, |
| 119 | ])("requests the exact whitespace-edged paths of $name", async ({ patch, oldPath, newPath }) => { |
| 120 | const requested: Array<{ oldPath: string; newPath: string }> = []; |
| 121 | const loader = createGitDiffFileContentsLoader( |
| 122 | async ({ input }) => { |
| 123 | requested.push({ oldPath: input.oldPath, newPath: input.newPath }); |
| 124 | return AsyncResult.success({ oldContents: "a\nb\n", newContents: "a\nB\n" }); |
| 125 | }, |
| 126 | { |
| 127 | environmentId: EnvironmentId.make("environment-diff-contents-test"), |
| 128 | cwd: "/repo", |
| 129 | sourceKind: "working-tree", |
| 130 | baseRef: null, |
| 131 | headRef: null, |
| 132 | cacheKey: "diff-hash", |
| 133 | }, |
| 134 | ); |
| 135 | |
| 136 | await loader(parsePartialFileDiff(patch.join("\n"))); |
| 137 | |
| 138 | expect(requested).toEqual([{ oldPath, newPath }]); |
| 139 | }); |
| 140 | |
| 141 | it("loads a whitespace-ignored patch whose context shows one side's whitespace", async () => { |
| 142 | const patch = [ |
| 143 | "diff --git a/src/file01.ts b/src/file01.ts", |
| 144 | "index 1111111..2222222 100644", |
| 145 | "--- a/src/file01.ts", |
| 146 | "+++ b/src/file01.ts", |
| 147 | "@@ -2,4 +2,4 @@ a", |
| 148 | " b", |
| 149 | " c", |
| 150 | " d", |
| 151 | "-e", |
| 152 | "\\ No newline at end of file", |
| 153 | "+E", |
| 154 | "\\ No newline at end of file", |
| 155 | "", |
| 156 | ].join("\n"); |
| 157 | const loaded = await loaderReturning( |
| 158 | "a\nb\n c\nd\ne", |
| 159 | "a\nb\nc\nd\nE", |
| 160 | )(parsePartialFileDiff(patch)); |
| 161 | |
| 162 | expect(loaded.oldFile?.contents).toBe("a\nb\n c\nd\ne"); |
| 163 | }); |
| 164 | }); |