Skip to content

Commit 9b198b0

Browse files
committed
perf(mobile): reuse diff rows during comment edits
1 parent 2460649 commit 9b198b0

2 files changed

Lines changed: 247 additions & 63 deletions

File tree

‎apps/mobile/src/features/review/nativeReviewDiffAdapter.test.ts‎

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "vite-plus/test";
1+
import { describe, expect, it, vi } from "vite-plus/test";
22
import {
33
DEFAULT_MOBILE_THEME_ID,
44
getMobileThemeVariables,
@@ -9,12 +9,14 @@ import {
99
import { readDefaultMobileThemeVariables } from "../../lib/mobileTheme.test-support";
1010

1111
import {
12+
buildNativeReviewDiffData,
1213
createNativeReviewDiffTheme,
1314
getCachedNativeReviewDiffData,
1415
type BuildNativeReviewDiffDataInput,
1516
} from "./nativeReviewDiffAdapter";
1617
import type { ReviewInlineComment } from "./reviewCommentSelection";
1718
import { buildReviewParsedDiff } from "./reviewModel";
19+
import * as ReviewWordDiffs from "./reviewWordDiffs";
1820

1921
const parsedDiff = buildReviewParsedDiff(
2022
[
@@ -46,6 +48,23 @@ function buildInput(comments: BuildNativeReviewDiffDataInput["comments"]) {
4648
return { parsedDiff, comments } satisfies BuildNativeReviewDiffDataInput;
4749
}
4850

51+
function filesPatch(paths: ReadonlyArray<string>) {
52+
return paths
53+
.map((path) =>
54+
[
55+
`diff --git a/${path} b/${path}`,
56+
`--- a/${path}`,
57+
`+++ b/${path}`,
58+
"@@ -1,2 +1,2 @@",
59+
'-const first = renderPanel({ label: "before", enabled: true });',
60+
'-const second = renderPanel({ label: "before", enabled: true });',
61+
'+const first = renderPanel({ label: "after", enabled: true });',
62+
'+const second = renderPanel({ label: "after", enabled: true });',
63+
].join("\n"),
64+
)
65+
.join("\n");
66+
}
67+
4968
function appTheme(themeId: MobileThemeId, appearance: MobileThemeAppearance) {
5069
return themeId === DEFAULT_MOBILE_THEME_ID
5170
? readDefaultMobileThemeVariables(appearance)
@@ -67,6 +86,107 @@ describe("getCachedNativeReviewDiffData", () => {
6786

6887
expect(equivalent).toBe(first);
6988
expect(changed).not.toBe(first);
89+
expect(changed.rows.find((row) => row.kind === "comment")?.commentText).toBe("Changed");
90+
});
91+
92+
it("reuses source rows and word matching when a file comment changes", () => {
93+
const diff = buildReviewParsedDiff(filesPatch(["example.ts", "second.ts"]), "comment-reuse");
94+
const matchWords = vi.spyOn(ReviewWordDiffs, "computeWordAltDiffRanges");
95+
try {
96+
const base = getCachedNativeReviewDiffData({ parsedDiff: diff });
97+
expect(matchWords).toHaveBeenCalledTimes(4);
98+
expect(base.rows.filter((row) => row.wordDiffRanges?.length)).toHaveLength(8);
99+
const firstComment = makeComment("First file comment");
100+
const secondComment = {
101+
...makeComment("Second file comment"),
102+
id: "comment-2",
103+
filePath: "second.ts",
104+
};
105+
const first = getCachedNativeReviewDiffData({
106+
parsedDiff: diff,
107+
comments: [firstComment, secondComment],
108+
});
109+
const changed = getCachedNativeReviewDiffData({
110+
parsedDiff: diff,
111+
comments: [{ ...firstComment, text: "Changed first comment" }, secondComment],
112+
});
113+
114+
expect(matchWords).toHaveBeenCalledTimes(4);
115+
expect(changed.files).toBe(base.files);
116+
expect(changed.commentTargetsByRowId).toBe(base.commentTargetsByRowId);
117+
expect(changed.rowIdByCommentLineId).toBe(base.rowIdByCommentLineId);
118+
expect(changed.rows.find((row) => row.id === secondComment.id)).toBe(
119+
first.rows.find((row) => row.id === secondComment.id),
120+
);
121+
const sourceRows = changed.rows.filter((row) => row.kind !== "comment");
122+
for (const [index, row] of sourceRows.entries()) {
123+
expect(row).toBe(base.rows[index]);
124+
}
125+
const removed = getCachedNativeReviewDiffData({ parsedDiff: diff, comments: [] });
126+
expect(removed.rows).toEqual(base.rows);
127+
expect(matchWords).toHaveBeenCalledTimes(4);
128+
expect(first.rows.find((row) => row.id === firstComment.id)?.commentText).toBe(
129+
"First file comment",
130+
);
131+
} finally {
132+
matchWords.mockRestore();
133+
}
134+
});
135+
136+
it("updates comment titles and locations without changing source coordinates", () => {
137+
const diff = buildReviewParsedDiff(filesPatch(["example.ts", "second.ts"]), "comment-metadata");
138+
const comment = makeComment("Review this line");
139+
const first = getCachedNativeReviewDiffData({ parsedDiff: diff, comments: [comment] });
140+
const renamed = getCachedNativeReviewDiffData({
141+
parsedDiff: diff,
142+
comments: [{ ...comment, sectionTitle: "Branch comparison" }],
143+
});
144+
expect(renamed.rows.find((row) => row.id === comment.id)?.commentSectionTitle).toBe(
145+
"Branch comparison",
146+
);
147+
const moved = getCachedNativeReviewDiffData({
148+
parsedDiff: diff,
149+
comments: [{ ...comment, filePath: "second.ts", endIndex: 99, rangeLabel: "+2" }],
150+
});
151+
const commentIndex = moved.rows.findIndex((row) => row.id === comment.id);
152+
expect(moved.rows[commentIndex]).toMatchObject({
153+
kind: "comment",
154+
filePath: "second.ts",
155+
commentRangeLabel: "+2",
156+
});
157+
expect(moved.rows[commentIndex - 1]).toMatchObject({
158+
kind: "line",
159+
change: "add",
160+
newLineNumber: 2,
161+
});
162+
expect(moved.rows.filter((row) => row.kind !== "comment")).toEqual(
163+
first.rows.filter((row) => row.kind !== "comment"),
164+
);
165+
expect(first.rows.find((row) => row.id === comment.id)?.commentSectionTitle).toBe(
166+
"Dirty worktree",
167+
);
168+
});
169+
170+
it("keeps cached IDs and targets local to each parsed section and file layout", () => {
171+
const original = filesPatch(["example.ts", "second.ts"]);
172+
const variants = [
173+
buildReviewParsedDiff(original, "first-section"),
174+
buildReviewParsedDiff(original, "second-section"),
175+
buildReviewParsedDiff(
176+
filesPatch(["inserted.ts", "example.ts", "second.ts"]),
177+
"first-section",
178+
),
179+
buildReviewParsedDiff(filesPatch(["second.ts", "example.ts"]), "first-section"),
180+
buildReviewParsedDiff(filesPatch(["example.ts"]), "first-section"),
181+
buildReviewParsedDiff(
182+
original.replaceAll("@@ -1,2 +1,2 @@", "@@ -7,2 +12,2 @@"),
183+
"first-section",
184+
),
185+
];
186+
for (const diff of [...variants, variants[0]!]) {
187+
const input = { parsedDiff: diff, comments: [makeComment("Coordinates")] };
188+
expect(getCachedNativeReviewDiffData(input)).toEqual(buildNativeReviewDiffData(input));
189+
}
70190
});
71191
});
72192

0 commit comments

Comments
 (0)