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

nativeReviewDiffHighlighter.test.ts

155 lines4.1 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import type { NativeReviewDiffRow } from "./nativeReviewDiffSurface";
4import type { NativeReviewDiffFile } from "./nativeReviewDiffTypes";
5import { highlightNativeReviewDiffVisibleRows } from "./nativeReviewDiffHighlighter";
6​
7vi.mock("expo", async () => {
8 const { createWasmHighlightsModule } = await import("../../lib/highlights.testing");
9 const highlights = createWasmHighlightsModule();
10 return { requireOptionalNativeModule: () => highlights };
11});
12​
13const TYPESCRIPT_FILE: NativeReviewDiffFile = {
14 id: "file-1",
15 path: "example.ts",
16 language: "typescript",
17 additions: 0,
18 deletions: 0,
19};
20​
21function makeLine(
22 input: Pick<NativeReviewDiffRow, "id" | "content" | "change" | "oldLineNumber" | "newLineNumber">,
23): NativeReviewDiffRow {
24 return {
25 kind: "line",
26 fileId: TYPESCRIPT_FILE.id,
27 ...input,
28 };
30​
31function makeHunk(id: string): NativeReviewDiffRow {
32 return {
33 kind: "hunk",
34 id,
35 fileId: TYPESCRIPT_FILE.id,
36 text: "@@",
37 };
39​
40function highlight(
41 rows: ReadonlyArray<NativeReviewDiffRow>,
42 alreadyHighlightedRowIds?: ReadonlySet<string>,
43) {
44 return highlightNativeReviewDiffVisibleRows({
45 rows,
46 files: [TYPESCRIPT_FILE],
47 scheme: "dark",
48 firstRowIndex: 0,
49 lastRowIndex: rows.length - 1,
50 overscanRows: 0,
51 maxRows: 100,
52 alreadyHighlightedRowIds,
53 });
55​
56describe("highlightNativeReviewDiffVisibleRows", () => {
57 it("does not carry grammar state across hunk boundaries", () => {
58 const exportRow = makeLine({
59 id: "export-row",
60 content: "export async function run() {}",
61 change: "add",
62 oldLineNumber: null,
63 newLineNumber: 100,
64 });
65 const rows = [
66 makeHunk("hunk-1"),
67 makeLine({
68 id: "template-open",
69 content: "const message = `open",
70 change: "context",
71 oldLineNumber: 1,
72 newLineNumber: 1,
73 }),
74 makeHunk("hunk-2"),
75 exportRow,
76 ];
77​
78 const highlighted = highlight(rows);
79 const standalone = highlight([makeHunk("standalone-hunk"), exportRow]);
80​
81 expect(highlighted.tokensByRowId[exportRow.id]).toEqual(standalone.tokensByRowId[exportRow.id]);
82 });
83​
84 it("keeps grammar state across inline comment rows", () => {
85 const openingRow = makeLine({
86 id: "template-open",
87 content: "const message = `open",
88 change: "add",
89 oldLineNumber: null,
90 newLineNumber: 1,
91 });
92 const closingRow = makeLine({
93 id: "template-close",
94 content: "closed`;",
95 change: "add",
96 oldLineNumber: null,
97 newLineNumber: 2,
98 });
99 const trailingRow = makeLine({
100 id: "trailing-row",
101 content: "export const answer = 42;",
102 change: "add",
103 oldLineNumber: null,
104 newLineNumber: 3,
105 });
106 const commentRow: NativeReviewDiffRow = {
107 kind: "comment",
108 id: "comment-1",
109 fileId: TYPESCRIPT_FILE.id,
110 commentText: "Review note",
111 };
112​
113 const withComment = highlight([openingRow, commentRow, closingRow, trailingRow]);
114 const contiguous = highlight([openingRow, closingRow, trailingRow]);
115​
116 expect(withComment.tokensByRowId).toEqual(contiguous.tokensByRowId);
117 expect(withComment.tokensByRowId[closingRow.id]?.[0]?.color).toBe(
118 withComment.tokensByRowId[openingRow.id]?.at(-1)?.color,
119 );
120 });
121​
122 it("does not join unhighlighted rows across cached gaps", () => {
123 const trailingRow = makeLine({
124 id: "trailing-row",
125 content: "export const answer = 42;",
126 change: "add",
127 oldLineNumber: null,
128 newLineNumber: 3,
129 });
130 const rows = [
131 makeLine({
132 id: "template-open",
133 content: "const message = `open",
134 change: "add",
135 oldLineNumber: null,
136 newLineNumber: 1,
137 }),
138 makeLine({
139 id: "template-close",
140 content: "closed`;",
141 change: "add",
142 oldLineNumber: null,
143 newLineNumber: 2,
144 }),
145 trailingRow,
146 ];
147​
148 const highlighted = highlight(rows, new Set(["template-close"]));
149 const standalone = highlight([trailingRow]);
150​
151 expect(highlighted.tokensByRowId[trailingRow.id]).toEqual(
152 standalone.tokensByRowId[trailingRow.id],
153 );
154 });
155});