Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import {
3 DEFAULT_MOBILE_THEME_ID,
4 getMobileThemeVariables,
5 MOBILE_THEME_IDS,
6 type MobileThemeAppearance,
7 type MobileThemeId,
8} from "../../lib/mobileTheme";
9import { readDefaultMobileThemeVariables } from "../../lib/mobileTheme.test-support";
10
11import {
12 buildNativeReviewDiffData,
13 buildNativeReviewSnippetRows,
14 createNativeReviewDiffTheme,
15 getCachedNativeReviewDiffData,
16 type BuildNativeReviewDiffDataInput,
17} from "./nativeReviewDiffAdapter";
18import type { ReviewInlineComment } from "./reviewCommentSelection";
19import { buildReviewParsedDiff } from "./reviewModel";
20import * as ReviewWordDiffs from "./reviewWordDiffs";
21
22const parsedDiff = buildReviewParsedDiff(
23 [
24 "diff --git a/example.ts b/example.ts",
25 "--- a/example.ts",
26 "+++ b/example.ts",
27 "@@ -1 +1 @@",
28 "-const before = 1;",
29 "+const after = 2;",
30 ].join("\n"),
31 "native-review-cache-test",
32);
33
34describe("buildNativeReviewSnippetRows", () => {
35 it("preserves selected code and change types without inventing line numbers", () => {
36 const rows = buildNativeReviewSnippetRows({
37 id: "selection",
38 diff: " unchanged\r\n- before\r\n+ after\r\n",
39 });
40 expect(
41 rows.map((row) => [row.content, row.change, row.oldLineNumber, row.newLineNumber]),
42 ).toEqual([
43 [" unchanged", "context", null, null],
44 [" before", "delete", null, null],
45 [" after", "add", null, null],
46 ]);
47 });
48
49 it("leaves full patches, unrecognized text, and non-diff code to their existing renderers", () => {
50 for (const diff of ["@@ -1 +1 @@\n-old\n+new", "--- a/file\n+++ b/file", "plain text", ""]) {
51 expect(buildNativeReviewSnippetRows({ id: "selection", diff })).toEqual([]);
52 }
53 expect(
54 buildNativeReviewSnippetRows({ id: "code", diff: "+value", fenceLanguage: "typescript" }),
55 ).toEqual([]);
56 });
57});
58
59function makeComment(text: string): ReviewInlineComment {
60 return {
61 id: "comment-1",
62 sectionId: "git:working-tree",
63 sectionTitle: "Dirty worktree",
64 filePath: "example.ts",
65 startIndex: 0,
66 endIndex: 0,
67 rangeLabel: "-1",
68 text,
69 diff: "@@ -1,1 +1,0 @@\n-const before = 1;",
70 };
71}
72
73function buildInput(comments: BuildNativeReviewDiffDataInput["comments"]) {
74 return { parsedDiff, comments } satisfies BuildNativeReviewDiffDataInput;
75}
76
77function filesPatch(paths: ReadonlyArray<string>) {
78 return paths
79 .map((path) =>
80 [
81 `diff --git a/${path} b/${path}`,
82 `--- a/${path}`,
83 `+++ b/${path}`,
84 "@@ -1,2 +1,2 @@",
85 '-const first = renderPanel({ label: "before", enabled: true });',
86 '-const second = renderPanel({ label: "before", enabled: true });',
87 '+const first = renderPanel({ label: "after", enabled: true });',
88 '+const second = renderPanel({ label: "after", enabled: true });',
89 ].join("\n"),
90 )
91 .join("\n");
92}
93
94function appTheme(themeId: MobileThemeId, appearance: MobileThemeAppearance) {
95 return themeId === DEFAULT_MOBILE_THEME_ID || themeId === "material-you"
96 ? readDefaultMobileThemeVariables(appearance)
97 : getMobileThemeVariables(themeId, appearance);
98}
99
100describe("getCachedNativeReviewDiffData", () => {
101 it.each([true, false])(
102 "preserves available diff rows before a notice (has excerpt: %s)",
103 (hasExcerpt) => {
104 if (parsedDiff.kind !== "files") throw new Error("Expected a parsed file diff");
105 const notice = "This file preview was truncated.";
106 const result = buildNativeReviewDiffData({
107 parsedDiff: {
108 ...parsedDiff,
109 files: parsedDiff.files.map((file) => ({
110 ...file,
111 rows: hasExcerpt ? file.rows : [],
112 notice,
113 })),
114 },
115 });
116 const original = buildNativeReviewDiffData({ parsedDiff });
117 expect(result.rows.slice(0, -1)).toEqual(
118 hasExcerpt ? original.rows : original.rows.filter((row) => row.kind === "file"),
119 );
120 expect(result.rows.at(-1)).toMatchObject({ kind: "notice", text: notice });
121 },
122 );
123
124 it("reuses the row model for equivalent empty comment arrays", () => {
125 const first = getCachedNativeReviewDiffData(buildInput([]));
126 const second = getCachedNativeReviewDiffData(buildInput([]));
127
128 expect(second).toBe(first);
129 });
130
131 it("reuses equivalent comment contents and invalidates changed comments", () => {
132 const first = getCachedNativeReviewDiffData(buildInput([makeComment("First")]));
133 const equivalent = getCachedNativeReviewDiffData(buildInput([makeComment("First")]));
134 const changed = getCachedNativeReviewDiffData(buildInput([makeComment("Changed")]));
135
136 expect(equivalent).toBe(first);
137 expect(changed).not.toBe(first);
138 expect(changed.rows.find((row) => row.kind === "comment")?.commentText).toBe("Changed");
139 });
140
141 it("reuses source rows and word matching when a file comment changes", () => {
142 const diff = buildReviewParsedDiff(filesPatch(["example.ts", "second.ts"]), "comment-reuse");
143 const matchWords = vi.spyOn(ReviewWordDiffs, "computeWordAltDiffRanges");
144 try {
145 const base = getCachedNativeReviewDiffData({ parsedDiff: diff });
146 expect(matchWords).toHaveBeenCalledTimes(4);
147 expect(base.rows.filter((row) => row.wordDiffRanges?.length)).toHaveLength(8);
148 const firstComment = makeComment("First file comment");
149 const secondComment = {
150 ...makeComment("Second file comment"),
151 id: "comment-2",
152 filePath: "second.ts",
153 };
154 const first = getCachedNativeReviewDiffData({
155 parsedDiff: diff,
156 comments: [firstComment, secondComment],
157 });
158 const changed = getCachedNativeReviewDiffData({
159 parsedDiff: diff,
160 comments: [{ ...firstComment, text: "Changed first comment" }, secondComment],
161 });
162
163 expect(matchWords).toHaveBeenCalledTimes(4);
164 expect(changed.files).toBe(base.files);
165 expect(changed.commentTargetsByRowId).toBe(base.commentTargetsByRowId);
166 expect(changed.rowIdByCommentLineId).toBe(base.rowIdByCommentLineId);
167 expect(changed.rows.find((row) => row.id === secondComment.id)).toBe(
168 first.rows.find((row) => row.id === secondComment.id),
169 );
170 const sourceRows = changed.rows.filter((row) => row.kind !== "comment");
171 for (const [index, row] of sourceRows.entries()) {
172 expect(row).toBe(base.rows[index]);
173 }
174 const removed = getCachedNativeReviewDiffData({ parsedDiff: diff, comments: [] });
175 expect(removed.rows).toEqual(base.rows);
176 expect(matchWords).toHaveBeenCalledTimes(4);
177 expect(first.rows.find((row) => row.id === firstComment.id)?.commentText).toBe(
178 "First file comment",
179 );
180 } finally {
181 matchWords.mockRestore();
182 }
183 });
184
185 it("updates comment titles and locations without changing source coordinates", () => {
186 const diff = buildReviewParsedDiff(filesPatch(["example.ts", "second.ts"]), "comment-metadata");
187 const comment = makeComment("Review this line");
188 const first = getCachedNativeReviewDiffData({ parsedDiff: diff, comments: [comment] });
189 const renamed = getCachedNativeReviewDiffData({
190 parsedDiff: diff,
191 comments: [{ ...comment, sectionTitle: "Branch comparison" }],
192 });
193 expect(renamed.rows.find((row) => row.id === comment.id)?.commentSectionTitle).toBe(
194 "Branch comparison",
195 );
196 const moved = getCachedNativeReviewDiffData({
197 parsedDiff: diff,
198 comments: [{ ...comment, filePath: "second.ts", endIndex: 99, rangeLabel: "+2" }],
199 });
200 const commentIndex = moved.rows.findIndex((row) => row.id === comment.id);
201 expect(moved.rows[commentIndex]).toMatchObject({
202 kind: "comment",
203 filePath: "second.ts",
204 commentRangeLabel: "+2",
205 });
206 expect(moved.rows[commentIndex - 1]).toMatchObject({
207 kind: "line",
208 change: "add",
209 newLineNumber: 2,
210 });
211 expect(moved.rows.filter((row) => row.kind !== "comment")).toEqual(
212 first.rows.filter((row) => row.kind !== "comment"),
213 );
214 expect(first.rows.find((row) => row.id === comment.id)?.commentSectionTitle).toBe(
215 "Dirty worktree",
216 );
217 });
218
219 it("keeps cached IDs and targets local to each parsed section and file layout", () => {
220 const original = filesPatch(["example.ts", "second.ts"]);
221 const variants = [
222 buildReviewParsedDiff(original, "first-section"),
223 buildReviewParsedDiff(original, "second-section"),
224 buildReviewParsedDiff(
225 filesPatch(["inserted.ts", "example.ts", "second.ts"]),
226 "first-section",
227 ),
228 buildReviewParsedDiff(filesPatch(["second.ts", "example.ts"]), "first-section"),
229 buildReviewParsedDiff(filesPatch(["example.ts"]), "first-section"),
230 buildReviewParsedDiff(
231 original.replaceAll("@@ -1,2 +1,2 @@", "@@ -7,2 +12,2 @@"),
232 "first-section",
233 ),
234 ];
235 for (const diff of [...variants, variants[0]!]) {
236 const input = { parsedDiff: diff, comments: [makeComment("Coordinates")] };
237 expect(getCachedNativeReviewDiffData(input)).toEqual(buildNativeReviewDiffData(input));
238 }
239 });
240});
241
242describe("createNativeReviewDiffTheme", () => {
243 it("serializes every native color as cross-platform opaque hex", () => {
244 for (const themeId of MOBILE_THEME_IDS) {
245 for (const appearance of ["light", "dark"] as const) {
246 const theme = createNativeReviewDiffTheme(
247 appearance,
248 themeId,
249 appTheme(themeId, appearance),
250 );
251 for (const color of Object.values(theme)) {
252 expect(color, `${themeId}/${appearance}`).toMatch(/^#[\da-f]{6}$/i);
253 }
254 }
255 }
256 });
257
258 it.each(["light", "dark"] as const)(
259 "preserves Material You RGBA hex channels and composites alpha in %s",
260 (appearance) => {
261 const variables = {
262 ...appTheme("material-you", appearance),
263 "--color-screen": "#101214FF",
264 "--color-sheet": "#20222480",
265 "--color-md-code-text": "#E3E2E6FF",
266 "--color-foreground-muted": "#C7C5D080",
267 "--color-border": "#44464F80",
268 "--color-primary": "#A8C7FAFF",
269 };
270 const theme = createNativeReviewDiffTheme(appearance, "material-you", variables);
271 expect(theme.background).toBe("#181a1c");
272 expect(theme.headerBackground).toBe(theme.background);
273 expect(theme.text).toBe("#e3e2e6");
274 expect(theme.mutedText).toBe("#707076");
275 expect(theme.border).toBe("#2e3036");
276 expect(theme.hunkText).toBe("#a8c7fa");
277 for (const color of Object.values(theme)) {
278 expect(color).toMatch(/^#[\da-f]{6}$/i);
279 }
280 },
281 );
282
283 it("uses the selected app palette for native code surfaces", () => {
284 const standard = createNativeReviewDiffTheme("dark", "t3-code", appTheme("t3-code", "dark"));
285 const iris = createNativeReviewDiffTheme("dark", "iris", appTheme("iris", "dark"));
286
287 expect(iris.background).not.toBe(standard.background);
288 expect(iris.hunkText).not.toBe(standard.hunkText);
289 expect(iris.addBar).toBe(standard.addBar);
290 expect(iris.deleteBar).toBe(standard.deleteBar);
291 });
292});