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

nativeReviewDiffAdapter.test.ts

244 lines9.1 KB

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 };
72​
73function buildInput(comments: BuildNativeReviewDiffDataInput["comments"]) {
74 return { parsedDiff, comments } satisfies BuildNativeReviewDiffDataInput;
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");
93​
94function appTheme(themeId: MobileThemeId, appearance: MobileThemeAppearance) {
95 return themeId === DEFAULT_MOBILE_THEME_ID || themeId === "material-you"
96 ? readDefaultMobileThemeVariables(appearance)
97 : getMobileThemeVariables(themeId, appearance);
99​
100describe("getCachedNativeReviewDiffData", () => {
101 it("reuses the row model for equivalent empty comment arrays", () => {
102 const first = getCachedNativeReviewDiffData(buildInput([]));
103 const second = getCachedNativeReviewDiffData(buildInput([]));
104​
105 expect(second).toBe(first);
106 });
107​
108 it("reuses equivalent comment contents and invalidates changed comments", () => {
109 const first = getCachedNativeReviewDiffData(buildInput([makeComment("First")]));
110 const equivalent = getCachedNativeReviewDiffData(buildInput([makeComment("First")]));
111 const changed = getCachedNativeReviewDiffData(buildInput([makeComment("Changed")]));
112​
113 expect(equivalent).toBe(first);
114 expect(changed).not.toBe(first);
115 expect(changed.rows.find((row) => row.kind === "comment")?.commentText).toBe("Changed");
116 });
117​
118 it("reuses source rows and word matching when a file comment changes", () => {
119 const diff = buildReviewParsedDiff(filesPatch(["example.ts", "second.ts"]), "comment-reuse");
120 const matchWords = vi.spyOn(ReviewWordDiffs, "computeWordAltDiffRanges");
121 try {
122 const base = getCachedNativeReviewDiffData({ parsedDiff: diff });
123 expect(matchWords).toHaveBeenCalledTimes(4);
124 expect(base.rows.filter((row) => row.wordDiffRanges?.length)).toHaveLength(8);
125 const firstComment = makeComment("First file comment");
126 const secondComment = {
127 ...makeComment("Second file comment"),
128 id: "comment-2",
129 filePath: "second.ts",
130 };
131 const first = getCachedNativeReviewDiffData({
132 parsedDiff: diff,
133 comments: [firstComment, secondComment],
134 });
135 const changed = getCachedNativeReviewDiffData({
136 parsedDiff: diff,
137 comments: [{ ...firstComment, text: "Changed first comment" }, secondComment],
138 });
139​
140 expect(matchWords).toHaveBeenCalledTimes(4);
141 expect(changed.files).toBe(base.files);
142 expect(changed.commentTargetsByRowId).toBe(base.commentTargetsByRowId);
143 expect(changed.rowIdByCommentLineId).toBe(base.rowIdByCommentLineId);
144 expect(changed.rows.find((row) => row.id === secondComment.id)).toBe(
145 first.rows.find((row) => row.id === secondComment.id),
146 );
147 const sourceRows = changed.rows.filter((row) => row.kind !== "comment");
148 for (const [index, row] of sourceRows.entries()) {
149 expect(row).toBe(base.rows[index]);
150 }
151 const removed = getCachedNativeReviewDiffData({ parsedDiff: diff, comments: [] });
152 expect(removed.rows).toEqual(base.rows);
153 expect(matchWords).toHaveBeenCalledTimes(4);
154 expect(first.rows.find((row) => row.id === firstComment.id)?.commentText).toBe(
155 "First file comment",
156 );
157 } finally {
158 matchWords.mockRestore();
159 }
160 });
161​
162 it("updates comment titles and locations without changing source coordinates", () => {
163 const diff = buildReviewParsedDiff(filesPatch(["example.ts", "second.ts"]), "comment-metadata");
164 const comment = makeComment("Review this line");
165 const first = getCachedNativeReviewDiffData({ parsedDiff: diff, comments: [comment] });
166 const renamed = getCachedNativeReviewDiffData({
167 parsedDiff: diff,
168 comments: [{ ...comment, sectionTitle: "Branch comparison" }],
169 });
170 expect(renamed.rows.find((row) => row.id === comment.id)?.commentSectionTitle).toBe(
171 "Branch comparison",
172 );
173 const moved = getCachedNativeReviewDiffData({
174 parsedDiff: diff,
175 comments: [{ ...comment, filePath: "second.ts", endIndex: 99, rangeLabel: "+2" }],
176 });
177 const commentIndex = moved.rows.findIndex((row) => row.id === comment.id);
178 expect(moved.rows[commentIndex]).toMatchObject({
179 kind: "comment",
180 filePath: "second.ts",
181 commentRangeLabel: "+2",
182 });
183 expect(moved.rows[commentIndex - 1]).toMatchObject({
184 kind: "line",
185 change: "add",
186 newLineNumber: 2,
187 });
188 expect(moved.rows.filter((row) => row.kind !== "comment")).toEqual(
189 first.rows.filter((row) => row.kind !== "comment"),
190 );
191 expect(first.rows.find((row) => row.id === comment.id)?.commentSectionTitle).toBe(
192 "Dirty worktree",
193 );
194 });
195​
196 it("keeps cached IDs and targets local to each parsed section and file layout", () => {
197 const original = filesPatch(["example.ts", "second.ts"]);
198 const variants = [
199 buildReviewParsedDiff(original, "first-section"),
200 buildReviewParsedDiff(original, "second-section"),
201 buildReviewParsedDiff(
202 filesPatch(["inserted.ts", "example.ts", "second.ts"]),
203 "first-section",
204 ),
205 buildReviewParsedDiff(filesPatch(["second.ts", "example.ts"]), "first-section"),
206 buildReviewParsedDiff(filesPatch(["example.ts"]), "first-section"),
207 buildReviewParsedDiff(
208 original.replaceAll("@@ -1,2 +1,2 @@", "@@ -7,2 +12,2 @@"),
209 "first-section",
210 ),
211 ];
212 for (const diff of [...variants, variants[0]!]) {
213 const input = { parsedDiff: diff, comments: [makeComment("Coordinates")] };
214 expect(getCachedNativeReviewDiffData(input)).toEqual(buildNativeReviewDiffData(input));
215 }
216 });
217});
218​
219describe("createNativeReviewDiffTheme", () => {
220 it("serializes every native color as cross-platform opaque hex", () => {
221 for (const themeId of MOBILE_THEME_IDS) {
222 for (const appearance of ["light", "dark"] as const) {
223 const theme = createNativeReviewDiffTheme(
224 appearance,
225 themeId,
226 appTheme(themeId, appearance),
227 );
228 for (const color of Object.values(theme)) {
229 expect(color, `${themeId}/${appearance}`).toMatch(/^#[\da-f]{6}$/i);
230 }
231 }
232 }
233 });
234​
235 it("uses the selected app palette for native code surfaces", () => {
236 const standard = createNativeReviewDiffTheme("dark", "t3-code", appTheme("t3-code", "dark"));
237 const iris = createNativeReviewDiffTheme("dark", "iris", appTheme("iris", "dark"));
238​
239 expect(iris.background).not.toBe(standard.background);
240 expect(iris.hunkText).not.toBe(standard.hunkText);
241 expect(iris.addBar).toBe(standard.addBar);
242 expect(iris.deleteBar).toBe(standard.deleteBar);
243 });
244});