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

nativeReviewDiffAdapter.test.ts

218 lines8.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 createNativeReviewDiffTheme,
14 getCachedNativeReviewDiffData,
15 type BuildNativeReviewDiffDataInput,
16} from "./nativeReviewDiffAdapter";
17import type { ReviewInlineComment } from "./reviewCommentSelection";
18import { buildReviewParsedDiff } from "./reviewModel";
19import * as ReviewWordDiffs from "./reviewWordDiffs";
20​
21const parsedDiff = buildReviewParsedDiff(
22 [
23 "diff --git a/example.ts b/example.ts",
24 "--- a/example.ts",
25 "+++ b/example.ts",
26 "@@ -1 +1 @@",
27 "-const before = 1;",
28 "+const after = 2;",
29 ].join("\n"),
30 "native-review-cache-test",
31);
32​
33function makeComment(text: string): ReviewInlineComment {
34 return {
35 id: "comment-1",
36 sectionId: "git:working-tree",
37 sectionTitle: "Dirty worktree",
38 filePath: "example.ts",
39 startIndex: 0,
40 endIndex: 0,
41 rangeLabel: "-1",
42 text,
43 diff: "@@ -1,1 +1,0 @@\n-const before = 1;",
44 };
46​
47function buildInput(comments: BuildNativeReviewDiffDataInput["comments"]) {
48 return { parsedDiff, comments } satisfies BuildNativeReviewDiffDataInput;
50​
51function 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");
67​
68function appTheme(themeId: MobileThemeId, appearance: MobileThemeAppearance) {
69 return themeId === DEFAULT_MOBILE_THEME_ID
70 ? readDefaultMobileThemeVariables(appearance)
71 : getMobileThemeVariables(themeId, appearance);
73​
74describe("getCachedNativeReviewDiffData", () => {
75 it("reuses the row model for equivalent empty comment arrays", () => {
76 const first = getCachedNativeReviewDiffData(buildInput([]));
77 const second = getCachedNativeReviewDiffData(buildInput([]));
78​
79 expect(second).toBe(first);
80 });
81​
82 it("reuses equivalent comment contents and invalidates changed comments", () => {
83 const first = getCachedNativeReviewDiffData(buildInput([makeComment("First")]));
84 const equivalent = getCachedNativeReviewDiffData(buildInput([makeComment("First")]));
85 const changed = getCachedNativeReviewDiffData(buildInput([makeComment("Changed")]));
86​
87 expect(equivalent).toBe(first);
88 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 }
190 });
191});
192​
193describe("createNativeReviewDiffTheme", () => {
194 it("serializes every native color as cross-platform opaque hex", () => {
195 for (const themeId of MOBILE_THEME_IDS) {
196 for (const appearance of ["light", "dark"] as const) {
197 const theme = createNativeReviewDiffTheme(
198 appearance,
199 themeId,
200 appTheme(themeId, appearance),
201 );
202 for (const color of Object.values(theme)) {
203 expect(color, `${themeId}/${appearance}`).toMatch(/^#[\da-f]{6}$/i);
204 }
205 }
206 }
207 });
208​
209 it("uses the selected app palette for native code surfaces", () => {
210 const standard = createNativeReviewDiffTheme("dark", "t3-code", appTheme("t3-code", "dark"));
211 const iris = createNativeReviewDiffTheme("dark", "iris", appTheme("iris", "dark"));
212​
213 expect(iris.background).not.toBe(standard.background);
214 expect(iris.hunkText).not.toBe(standard.hunkText);
215 expect(iris.addBar).toBe(standard.addBar);
216 expect(iris.deleteBar).toBe(standard.deleteBar);
217 });
218});