The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { hydratePartialDiff } from "@pierre/diffs"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | import { resolveDiffReviewPosition } from "../reviewCommentContext"; |
| 4 | import { |
| 5 | buildFileDiffContentVersion, |
| 6 | buildFileDiffEntries, |
| 7 | buildFileDiffIdentityKey, |
| 8 | buildFileDiffRenderKey, |
| 9 | getDiffLineStat, |
| 10 | getRenderablePatch, |
| 11 | resolveFileDiffPath, |
| 12 | resolveFileDiffPreviousPath, |
| 13 | } from "./diffRendering"; |
| 14 | |
| 15 | describe("getRenderablePatch", () => { |
| 16 | it("hides indentation changes around inserted JSX without moving review lines", () => { |
| 17 | const patch = [ |
| 18 | "diff --git a/item.tsx b/item.tsx", |
| 19 | "--- a/item.tsx", |
| 20 | "+++ b/item.tsx", |
| 21 | "@@ -40,5 +40,7 @@", |
| 22 | ' <ItemContent className="min-w-0">', |
| 23 | "- <ItemTitle>", |
| 24 | '- <h4 className="wrap-break-word">{name}</h4>', |
| 25 | "- </ItemTitle>", |
| 26 | "+ {showName && (", |
| 27 | "+ <ItemTitle>", |
| 28 | '+ <h4 className="wrap-break-word">{name}</h4>', |
| 29 | "+ </ItemTitle>", |
| 30 | "+ )}", |
| 31 | " </ItemContent>", |
| 32 | "@@ -80 +82 @@", |
| 33 | "-const value = 1;", |
| 34 | "+const value = 2;", |
| 35 | ].join("\n"); |
| 36 | const shown = getRenderablePatch(patch, "pr", { compactPartialHunkOffsets: true }); |
| 37 | const hidden = getRenderablePatch(patch, "pr", { |
| 38 | compactPartialHunkOffsets: true, |
| 39 | ignoreWhitespace: true, |
| 40 | }); |
| 41 | expect(shown?.kind).toBe("files"); |
| 42 | expect(hidden?.kind).toBe("files"); |
| 43 | if (shown?.kind !== "files" || hidden?.kind !== "files") return; |
| 44 | expect(getDiffLineStat(shown.files)).toEqual({ additions: 6, deletions: 4 }); |
| 45 | expect(getDiffLineStat(hidden.files)).toEqual({ additions: 3, deletions: 1 }); |
| 46 | const file = hidden.files[0]!; |
| 47 | expect(file.additionLines).toEqual(shown.files[0]!.additionLines); |
| 48 | expect(file.deletionLines).toEqual(shown.files[0]!.deletionLines); |
| 49 | expect(file.cacheKey).not.toBe(shown.files[0]!.cacheKey); |
| 50 | expect(resolveDiffReviewPosition(hidden.sourceFiles[0]!, 43, "additions")).toEqual({ |
| 51 | kind: "added", |
| 52 | newLine: 43, |
| 53 | }); |
| 54 | expect(resolveDiffReviewPosition(hidden.sourceFiles[0]!, 42, "deletions")).toEqual({ |
| 55 | kind: "deleted", |
| 56 | oldLine: 42, |
| 57 | }); |
| 58 | expect(file.hunks[0]?.hunkContent).toContainEqual({ |
| 59 | type: "context", |
| 60 | lines: 3, |
| 61 | additionLineIndex: 2, |
| 62 | deletionLineIndex: 1, |
| 63 | }); |
| 64 | expect(resolveDiffReviewPosition(file, 41, "additions")).toEqual({ |
| 65 | kind: "added", |
| 66 | newLine: 41, |
| 67 | }); |
| 68 | expect(resolveDiffReviewPosition(file, 43, "additions")).toEqual({ |
| 69 | kind: "context", |
| 70 | oldLine: 42, |
| 71 | newLine: 43, |
| 72 | side: "right", |
| 73 | }); |
| 74 | expect(resolveDiffReviewPosition(file, 42, "deletions")).toEqual({ |
| 75 | kind: "context", |
| 76 | oldLine: 42, |
| 77 | newLine: 43, |
| 78 | side: "left", |
| 79 | }); |
| 80 | expect(file.hunks[1]).toMatchObject({ |
| 81 | additionStart: 82, |
| 82 | deletionStart: 80, |
| 83 | splitLineStart: 7, |
| 84 | unifiedLineStart: 7, |
| 85 | }); |
| 86 | const prefix = "unchanged\n".repeat(39); |
| 87 | const gap = "unchanged\n".repeat(35); |
| 88 | const hydrated = hydratePartialDiff("clone", file, { |
| 89 | oldFile: { |
| 90 | name: file.name, |
| 91 | contents: prefix + file.deletionLines.slice(0, 5).join("") + gap + "const value = 1;\n", |
| 92 | }, |
| 93 | newFile: { |
| 94 | name: file.name, |
| 95 | contents: prefix + file.additionLines.slice(0, 7).join("") + gap + "const value = 2;\n", |
| 96 | }, |
| 97 | }); |
| 98 | expect(getDiffLineStat([hydrated])).toEqual({ additions: 3, deletions: 1 }); |
| 99 | expect(resolveDiffReviewPosition(hydrated, 43, "additions")).toEqual( |
| 100 | resolveDiffReviewPosition(file, 43, "additions"), |
| 101 | ); |
| 102 | }); |
| 103 | |
| 104 | it.each([ |
| 105 | [" const x = 1;\t", "\tconst x=1;", 0, 0], |
| 106 | ["const x = 1;", "const x = 2;", 1, 1], |
| 107 | ["const x = 1;", "const x = 1;\n", 1, 0], |
| 108 | ])("filters whitespace in %j to %j", (before, after, additions, deletions) => { |
| 109 | const patch = [ |
| 110 | "diff --git a/example.ts b/example.ts", |
| 111 | "--- a/example.ts", |
| 112 | "+++ b/example.ts", |
| 113 | `@@ -1 +1,${after.split("\n").length} @@`, |
| 114 | `-${before}`, |
| 115 | ...after.split("\n").map((line) => `+${line}`), |
| 116 | "", |
| 117 | ].join("\n"); |
| 118 | const filtered = getRenderablePatch(patch, "pr", { ignoreWhitespace: true }); |
| 119 | expect(filtered?.kind).toBe("files"); |
| 120 | if (filtered?.kind !== "files") return; |
| 121 | expect(getDiffLineStat(filtered.files)).toEqual({ additions, deletions }); |
| 122 | }); |
| 123 | |
| 124 | it.each(["+", "-"])("keeps %s blank lines without a final newline", (sign) => { |
| 125 | const parsed = getRenderablePatch( |
| 126 | [ |
| 127 | "diff --git a/blank.txt b/blank.txt", |
| 128 | "--- a/blank.txt", |
| 129 | "+++ b/blank.txt", |
| 130 | sign === "+" ? "@@ -0,0 +1 @@" : "@@ -1 +0,0 @@", |
| 131 | `${sign} `, |
| 132 | "\\ No newline at end of file", |
| 133 | ].join("\n"), |
| 134 | "pr", |
| 135 | { ignoreWhitespace: true }, |
| 136 | ); |
| 137 | expect(parsed?.kind).toBe("files"); |
| 138 | if (parsed?.kind !== "files") return; |
| 139 | expect(getDiffLineStat(parsed.files)).toEqual({ |
| 140 | additions: sign === "+" ? 1 : 0, |
| 141 | deletions: sign === "-" ? 1 : 0, |
| 142 | }); |
| 143 | }); |
| 144 | |
| 145 | it.each([ |
| 146 | ["a/example.ts", "a/example.ts", "change"], |
| 147 | ["b/example.ts", "b/example.ts", "change"], |
| 148 | ["a/before.ts", "b/after.ts", "rename-changed"], |
| 149 | ])("preserves repository paths from %s to %s", (previousPath, path, type) => { |
| 150 | const parsed = getRenderablePatch( |
| 151 | [ |
| 152 | `diff --git a/${previousPath} b/${path}`, |
| 153 | ...(previousPath === path |
| 154 | ? [] |
| 155 | : ["similarity index 50%", `rename from ${previousPath}`, `rename to ${path}`]), |
| 156 | `--- a/${previousPath}`, |
| 157 | `+++ b/${path}`, |
| 158 | "@@ -1 +1 @@", |
| 159 | "-before", |
| 160 | "+after", |
| 161 | ].join("\n"), |
| 162 | ); |
| 163 | expect(parsed?.kind).toBe("files"); |
| 164 | if (parsed?.kind !== "files") return; |
| 165 | const file = parsed.files[0]; |
| 166 | expect(file).toBeDefined(); |
| 167 | if (!file) return; |
| 168 | expect(resolveFileDiffPath(file)).toBe(path); |
| 169 | expect(resolveFileDiffPreviousPath(file)).toBe(previousPath); |
| 170 | expect(buildFileDiffIdentityKey(file)).toBe(`${previousPath}\0${path}\0${type}`); |
| 171 | }); |
| 172 | |
| 173 | it("compacts partial hunk render offsets for virtualized review diffs", () => { |
| 174 | const patch = [ |
| 175 | "diff --git a/example.ts b/example.ts", |
| 176 | "index 1111111..2222222 100644", |
| 177 | "--- a/example.ts", |
| 178 | "+++ b/example.ts", |
| 179 | "@@ -48,4 +48,4 @@", |
| 180 | " context", |
| 181 | "-before", |
| 182 | "+after", |
| 183 | " context", |
| 184 | " context", |
| 185 | "@@ -80,3 +80,4 @@", |
| 186 | " context", |
| 187 | "+added", |
| 188 | " context", |
| 189 | " context", |
| 190 | ].join("\n"); |
| 191 | |
| 192 | const parsed = getRenderablePatch(patch, "review", { |
| 193 | compactPartialHunkOffsets: true, |
| 194 | }); |
| 195 | expect(parsed?.kind).toBe("files"); |
| 196 | if (parsed?.kind !== "files") return; |
| 197 | |
| 198 | const file = parsed.files[0]; |
| 199 | expect(file?.hunks[0]?.collapsedBefore).toBe(47); |
| 200 | expect(file?.hunks[0]?.unifiedLineStart).toBe(0); |
| 201 | expect(file?.hunks[1]?.collapsedBefore).toBeGreaterThan(0); |
| 202 | expect(file?.hunks[1]?.unifiedLineStart).toBe(file?.hunks[0]?.unifiedLineCount); |
| 203 | expect(file?.unifiedLineCount).toBe( |
| 204 | file?.hunks.reduce((total, hunk) => total + hunk.unifiedLineCount, 0), |
| 205 | ); |
| 206 | }); |
| 207 | |
| 208 | it("retains source-file offsets for checkpoint diffs", () => { |
| 209 | const patch = [ |
| 210 | "diff --git a/example.ts b/example.ts", |
| 211 | "--- a/example.ts", |
| 212 | "+++ b/example.ts", |
| 213 | "@@ -48,1 +48,1 @@", |
| 214 | "-before", |
| 215 | "+after", |
| 216 | ].join("\n"); |
| 217 | |
| 218 | const parsed = getRenderablePatch(patch, "checkpoint"); |
| 219 | expect(parsed?.kind).toBe("files"); |
| 220 | if (parsed?.kind !== "files") return; |
| 221 | expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47); |
| 222 | }); |
| 223 | |
| 224 | it("keeps whitespace at the end of the last hunk", () => { |
| 225 | const patch = (lastLines: string[]) => |
| 226 | [ |
| 227 | "diff --git a/example.ts b/example.ts", |
| 228 | "--- a/example.ts", |
| 229 | "+++ b/example.ts", |
| 230 | ...lastLines, |
| 231 | "", |
| 232 | ].join("\n"); |
| 233 | |
| 234 | const trailing = getRenderablePatch(patch(["@@ -1 +1 @@", "-a", "+ b \t"]), "review"); |
| 235 | expect(trailing?.kind).toBe("files"); |
| 236 | if (trailing?.kind !== "files") return; |
| 237 | expect(trailing.files[0]?.additionLines).toEqual([" b \t\n"]); |
| 238 | |
| 239 | const blankContext = getRenderablePatch(patch(["@@ -1,2 +1,2 @@", "-a", "+b", " "]), "review"); |
| 240 | expect(blankContext?.kind).toBe("files"); |
| 241 | if (blankContext?.kind !== "files") return; |
| 242 | expect(blankContext.files[0]?.additionLines).toEqual(["b\n", "\n"]); |
| 243 | expect(blankContext.files[0]?.hunks[0]?.unifiedLineCount).toBe(3); |
| 244 | |
| 245 | expect(getRenderablePatch(" \n\t\n", "review")).toBeNull(); |
| 246 | }); |
| 247 | |
| 248 | it("caches patches that differ only in trailing whitespace separately", () => { |
| 249 | const patch = (added: string) => |
| 250 | [ |
| 251 | "diff --git a/example.ts b/example.ts", |
| 252 | "--- a/example.ts", |
| 253 | "+++ b/example.ts", |
| 254 | "@@ -1 +1 @@", |
| 255 | "-a", |
| 256 | `+${added}`, |
| 257 | "", |
| 258 | ].join("\n"); |
| 259 | |
| 260 | const plain = getRenderablePatch(patch("b"), "review"); |
| 261 | const tabbed = getRenderablePatch(patch("b\t"), "review"); |
| 262 | expect(plain?.kind).toBe("files"); |
| 263 | expect(tabbed?.kind).toBe("files"); |
| 264 | if (plain?.kind !== "files" || tabbed?.kind !== "files") return; |
| 265 | expect(tabbed.files[0]?.cacheKey).not.toBe(plain.files[0]?.cacheKey); |
| 266 | }); |
| 267 | }); |
| 268 | |
| 269 | describe("diff file reconciliation", () => { |
| 270 | it("retains repeated blocks and their keys when content changes or other files appear", () => { |
| 271 | const parse = (blocks: [string, string][]) => { |
| 272 | const parsed = getRenderablePatch( |
| 273 | blocks |
| 274 | .map(([path, content]) => |
| 275 | [ |
| 276 | `diff --git a/${path} b/${path}`, |
| 277 | `--- a/${path}`, |
| 278 | `+++ b/${path}`, |
| 279 | "@@ -1 +1 @@", |
| 280 | "-before", |
| 281 | `+${content}`, |
| 282 | "", |
| 283 | ].join("\n"), |
| 284 | ) |
| 285 | .join(""), |
| 286 | ); |
| 287 | if (parsed?.kind !== "files") throw new Error("Expected file diffs"); |
| 288 | return parsed.files; |
| 289 | }; |
| 290 | const files = parse([ |
| 291 | ["same.ts", "first"], |
| 292 | ["same.ts", "second"], |
| 293 | ]); |
| 294 | const before = buildFileDiffEntries([...files, files[1]!]); |
| 295 | expect(before.map((entry) => entry.fileDiff)).toEqual([...files, files[1]]); |
| 296 | expect(new Set(before.map((entry) => entry.fileKey)).size).toBe(3); |
| 297 | expect(before[0]?.fileKey).toBe(buildFileDiffIdentityKey(files[0]!)); |
| 298 | expect(buildFileDiffEntries([...files, files[1]!])).toEqual(before); |
| 299 | |
| 300 | const after = buildFileDiffEntries( |
| 301 | parse([ |
| 302 | ["unrelated.ts", "new"], |
| 303 | ["same.ts", "first"], |
| 304 | ["same.ts", "updated second"], |
| 305 | ["same.ts", "second"], |
| 306 | ]), |
| 307 | ).slice(1); |
| 308 | expect(after.map((entry) => entry.fileKey)).toEqual(before.map((entry) => entry.fileKey)); |
| 309 | expect(after[0]?.fileVersion).toBe(before[0]?.fileVersion); |
| 310 | expect(after[1]?.fileVersion).not.toBe(before[1]?.fileVersion); |
| 311 | expect(after[2]?.fileVersion).toBe(before[2]?.fileVersion); |
| 312 | }); |
| 313 | |
| 314 | it("keeps Pierre's render key stable when a partial diff hydrates", () => { |
| 315 | const patch = [ |
| 316 | "diff --git a/example.ts b/example.ts", |
| 317 | "--- a/example.ts", |
| 318 | "+++ b/example.ts", |
| 319 | "@@ -1 +1 @@", |
| 320 | "-before", |
| 321 | "+after", |
| 322 | ].join("\n"); |
| 323 | const parsed = getRenderablePatch(patch, "hydrated-key"); |
| 324 | expect(parsed?.kind).toBe("files"); |
| 325 | if (parsed?.kind !== "files") return; |
| 326 | |
| 327 | const file = parsed.files[0]; |
| 328 | expect(file).toBeDefined(); |
| 329 | if (!file) return; |
| 330 | const key = buildFileDiffRenderKey(file); |
| 331 | file.cacheKey = `${file.cacheKey}:hydrated`; |
| 332 | |
| 333 | expect(buildFileDiffRenderKey(file)).toBe(key); |
| 334 | }); |
| 335 | |
| 336 | it("gives a type change its own identity per block", () => { |
| 337 | const patch = [ |
| 338 | "diff --git a/AGENTS.md b/AGENTS.md", |
| 339 | "deleted file mode 100644", |
| 340 | "--- a/AGENTS.md", |
| 341 | "+++ /dev/null", |
| 342 | "@@ -1 +0,0 @@", |
| 343 | "-duplicated instructions", |
| 344 | "diff --git a/AGENTS.md b/AGENTS.md", |
| 345 | "new file mode 120000", |
| 346 | "--- /dev/null", |
| 347 | "+++ b/AGENTS.md", |
| 348 | "@@ -0,0 +1 @@", |
| 349 | "+CLAUDE.md", |
| 350 | ].join("\n"); |
| 351 | const parsed = getRenderablePatch(patch, "type-change"); |
| 352 | expect(parsed?.kind).toBe("files"); |
| 353 | if (parsed?.kind !== "files") return; |
| 354 | const [deleted, added] = parsed.files; |
| 355 | expect(deleted?.type).toBe("deleted"); |
| 356 | expect(added?.type).toBe("new"); |
| 357 | if (!deleted || !added) return; |
| 358 | |
| 359 | expect(buildFileDiffIdentityKey(deleted)).not.toBe(buildFileDiffIdentityKey(added)); |
| 360 | expect(new Set(parsed.files.map(buildFileDiffIdentityKey)).size).toBe(parsed.files.length); |
| 361 | }); |
| 362 | |
| 363 | it("keeps identities stable and versions local to the changed file", () => { |
| 364 | const patch = (secondLine: string) => |
| 365 | [ |
| 366 | "diff --git a/unchanged.ts b/unchanged.ts", |
| 367 | "--- a/unchanged.ts", |
| 368 | "+++ b/unchanged.ts", |
| 369 | "@@ -1 +1 @@", |
| 370 | "-before", |
| 371 | "+after", |
| 372 | "diff --git a/changed.ts b/changed.ts", |
| 373 | "--- a/changed.ts", |
| 374 | "+++ b/changed.ts", |
| 375 | "@@ -1 +1 @@", |
| 376 | "-old", |
| 377 | `+${secondLine}`, |
| 378 | ].join("\n"); |
| 379 | const before = getRenderablePatch(patch("new"), "before"); |
| 380 | const after = getRenderablePatch(patch("newer"), "after"); |
| 381 | expect(before?.kind).toBe("files"); |
| 382 | expect(after?.kind).toBe("files"); |
| 383 | if (before?.kind !== "files" || after?.kind !== "files") return; |
| 384 | |
| 385 | const [beforeUnchanged, beforeChanged] = before.files; |
| 386 | const [afterUnchanged, afterChanged] = after.files; |
| 387 | expect(beforeUnchanged).toBeDefined(); |
| 388 | expect(beforeChanged).toBeDefined(); |
| 389 | expect(afterUnchanged).toBeDefined(); |
| 390 | expect(afterChanged).toBeDefined(); |
| 391 | if (!beforeUnchanged || !beforeChanged || !afterUnchanged || !afterChanged) return; |
| 392 | |
| 393 | expect(buildFileDiffIdentityKey(afterUnchanged)).toBe( |
| 394 | buildFileDiffIdentityKey(beforeUnchanged), |
| 395 | ); |
| 396 | expect(buildFileDiffIdentityKey(afterChanged)).toBe(buildFileDiffIdentityKey(beforeChanged)); |
| 397 | expect(buildFileDiffContentVersion(afterUnchanged)).toBe( |
| 398 | buildFileDiffContentVersion(beforeUnchanged), |
| 399 | ); |
| 400 | expect(buildFileDiffContentVersion(afterChanged)).not.toBe( |
| 401 | buildFileDiffContentVersion(beforeChanged), |
| 402 | ); |
| 403 | }); |
| 404 | }); |
| 405 | |
| 406 | describe("a file whose name a patch header cannot carry plainly", () => { |
| 407 | const quotedPatch = (written: string) => |
| 408 | [ |
| 409 | `diff --git "a/${written}" "b/${written}"`, |
| 410 | "index 1111111..2222222 100644", |
| 411 | `--- "a/${written}"`, |
| 412 | `+++ "b/${written}"`, |
| 413 | "@@ -1 +1 @@", |
| 414 | "-before", |
| 415 | "+after", |
| 416 | "", |
| 417 | ].join("\n"); |
| 418 | |
| 419 | const pathOf = (patch: string) => { |
| 420 | const parsed = getRenderablePatch(patch, "review"); |
| 421 | expect(parsed?.kind).toBe("files"); |
| 422 | if (parsed?.kind !== "files") throw new Error("patch did not parse as files"); |
| 423 | const file = parsed.files[0]; |
| 424 | expect(file).toBeDefined(); |
| 425 | if (!file) throw new Error("patch carried no file"); |
| 426 | return resolveFileDiffPath(file); |
| 427 | }; |
| 428 | |
| 429 | it("reads the octal a host with core.quotePath on writes for a name outside ASCII", () => { |
| 430 | expect(pathOf(quotedPatch("caf\\303\\251/r\\303\\251sum\\303\\251.ts"))).toBe("café/résumé.ts"); |
| 431 | }); |
| 432 | |
| 433 | it("reads both sides of a rename under the names they really have", () => { |
| 434 | const patch = [ |
| 435 | 'diff --git "a/old\\tname.ts" "b/new\\tname.ts"', |
| 436 | "similarity index 90%", |
| 437 | 'rename from "old\\tname.ts"', |
| 438 | 'rename to "new\\tname.ts"', |
| 439 | "", |
| 440 | ].join("\n"); |
| 441 | |
| 442 | const parsed = getRenderablePatch(patch, "review"); |
| 443 | expect(parsed?.kind).toBe("files"); |
| 444 | if (parsed?.kind !== "files") return; |
| 445 | const file = parsed.files[0]; |
| 446 | expect(file).toBeDefined(); |
| 447 | if (!file) return; |
| 448 | |
| 449 | expect(resolveFileDiffPath(file)).toBe("new\tname.ts"); |
| 450 | expect(resolveFileDiffPreviousPath(file)).toBe("old\tname.ts"); |
| 451 | }); |
| 452 | }); |