Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

nativeReviewDiffAdapter.test.ts

108 lines4.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 buildNativeReviewDiffData,
5 getCachedNativeReviewDiffData,
6} from "./nativeReviewDiffAdapter";
7import type { ReviewInlineComment } from "./reviewCommentSelection";
8import { buildReviewParsedDiff } from "./reviewModel";
9​
10function makeComment(text: string): ReviewInlineComment {
11 return {
12 id: "comment-1",
13 sectionId: "git:working-tree",
14 sectionTitle: "Dirty worktree",
15 filePath: "example.ts",
16 startIndex: 0,
17 endIndex: 0,
18 rangeLabel: "-1",
19 text,
20 diff: "@@ -1,1 +1,0 @@\n-const before = 1;",
21 };
23​
24function filesPatch(paths: ReadonlyArray<string>) {
25 return paths
26 .map((path) =>
27 [
28 `diff --git a/${path} b/${path}`,
29 `--- a/${path}`,
30 `+++ b/${path}`,
31 "@@ -1,2 +1,2 @@",
32 '-const first = renderPanel({ label: "before", enabled: true });',
33 '-const second = renderPanel({ label: "before", enabled: true });',
34 '+const first = renderPanel({ label: "after", enabled: true });',
35 '+const second = renderPanel({ label: "after", enabled: true });',
36 ].join("\n"),
37 )
38 .join("\n");
40​
41describe("getCachedNativeReviewDiffData", () => {
42 it("keeps cached IDs and targets local to each parsed section and file layout", () => {
43 const original = filesPatch(["example.ts", "second.ts"]);
44 const variants = [
45 buildReviewParsedDiff(original, "first-section"),
46 buildReviewParsedDiff(original, "second-section"),
47 buildReviewParsedDiff(
48 filesPatch(["inserted.ts", "example.ts", "second.ts"]),
49 "first-section",
50 ),
51 buildReviewParsedDiff(filesPatch(["second.ts", "example.ts"]), "first-section"),
52 buildReviewParsedDiff(filesPatch(["example.ts"]), "first-section"),
53 buildReviewParsedDiff(
54 original.replaceAll("@@ -1,2 +1,2 @@", "@@ -7,2 +12,2 @@"),
55 "first-section",
56 ),
57 ];
58 for (const diff of [...variants, variants[0]!]) {
59 const input = { parsedDiff: diff, comments: [makeComment("Coordinates")] };
60 expect(getCachedNativeReviewDiffData(input)).toEqual(buildNativeReviewDiffData(input));
61 }
62 });
63});
64​
65describe("buildNativeReviewDiffData row bound", () => {
66 function singleFilePatch(path: string, lineCount: number) {
67 return [
68 `diff --git a/${path} b/${path}`,
69 `--- a/${path}`,
70 `+++ b/${path}`,
71 `@@ -0,0 +1,${lineCount} @@`,
72 ...Array.from({ length: lineCount }, (_, index) => `+const value${index} = ${index};`),
73 ].join("\n");
74 }
75​
76 it("caps each huge file excerpt while preserving full counts and ordinary files", () => {
77 const data = buildNativeReviewDiffData(
78 buildReviewParsedDiff(
79 [singleFilePatch("huge.ts", 200_000), singleFilePatch("ordinary.ts", 1_000)].join("\n"),
80 "native-review-row-bound",
81 ),
82 );
83 const hugeHeader = data.rows.find((row) => row.kind === "file" && row.filePath === "huge.ts");
84 const ordinaryHeader = data.rows.find(
85 (row) => row.kind === "file" && row.filePath === "ordinary.ts",
86 );
87 expect(hugeHeader).toMatchObject({ additions: 200_000, deletions: 0 });
88 expect(ordinaryHeader).toMatchObject({ additions: 1_000, deletions: 0 });
89 expect(data).toMatchObject({ additions: 201_000, deletions: 0 });
90 const hugeRows = data.rows.filter((row) => row.fileId === hugeHeader?.fileId);
91 expect(hugeRows.filter((row) => row.kind === "line" || row.kind === "hunk")).toHaveLength(
92 2_000,
93 );
94 expect(hugeRows.at(-2)).toMatchObject({ kind: "line", newLineNumber: 1_999 });
95 expect(hugeRows.at(-1)).toMatchObject({
96 kind: "notice",
97 text: "This diff is too large to show in full.",
98 });
99 const ordinaryRows = data.rows.filter((row) => row.fileId === ordinaryHeader?.fileId);
100 expect(ordinaryRows.filter((row) => row.kind === "line")).toHaveLength(1_000);
101 expect(ordinaryRows.at(-1)).toMatchObject({
102 kind: "line",
103 newLineNumber: 1_000,
104 content: "const value999 = 999;",
105 });
106 expect(ordinaryRows.some((row) => row.kind === "notice")).toBe(false);
107 });
108});