Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2
3import { highlightCode } from "../../lib/highlights";
4
5import type { NativeReviewDiffRow } from "./nativeReviewDiffSurface";
6import type { NativeReviewDiffFile } from "./nativeReviewDiffTypes";
7import { highlightNativeReviewDiffVisibleRows } from "./nativeReviewDiffHighlighter";
8import { buildNativeReviewDiffData } from "../review/nativeReviewDiffAdapter";
9import { buildReviewParsedDiff } from "../review/reviewModel";
10
11const tokenization = vi.hoisted(() => ({
12 calls: [] as string[],
13 afterCall: undefined as (() => void) | undefined,
14}));
15
16vi.mock("react-native-nitro-modules", async () => {
17 const { createWasmHighlightsModule } = await import("../../lib/highlights.testing");
18 const module = createWasmHighlightsModule();
19 return {
20 NitroModules: {
21 hasHybridObject: () => true,
22 createHybridObject: () => ({
23 tokenize(code: string, language: number) {
24 tokenization.calls.push(code);
25 const records = module.tokenize(code, language);
26 tokenization.afterCall?.();
27 return records;
28 },
29 }),
30 },
31 };
32});
33
34afterEach(() => {
35 tokenization.calls = [];
36 tokenization.afterCall = undefined;
37});
38
39const TYPESCRIPT_FILE: NativeReviewDiffFile = {
40 id: "file-1",
41 path: "example.ts",
42 language: "typescript",
43 additions: 0,
44 deletions: 0,
45};
46
47function makeLine(
48 input: Pick<NativeReviewDiffRow, "id" | "content" | "change" | "oldLineNumber" | "newLineNumber">,
49): NativeReviewDiffRow {
50 return {
51 kind: "line",
52 fileId: TYPESCRIPT_FILE.id,
53 ...input,
54 };
55}
56
57function makeHunk(id: string): NativeReviewDiffRow {
58 return {
59 kind: "hunk",
60 id,
61 fileId: TYPESCRIPT_FILE.id,
62 text: "@@",
63 };
64}
65
66function highlight(
67 rows: ReadonlyArray<NativeReviewDiffRow>,
68 alreadyHighlightedRowIds?: ReadonlySet<string>,
69) {
70 return highlightNativeReviewDiffVisibleRows({
71 rows,
72 files: [TYPESCRIPT_FILE],
73 scheme: "dark",
74 firstRowIndex: 0,
75 lastRowIndex: rows.length - 1,
76 overscanRows: 0,
77 maxRows: 100,
78 alreadyHighlightedRowIds,
79 });
80}
81
82describe("highlightNativeReviewDiffVisibleRows", () => {
83 it("keeps overscan within the visible files, including a file header at the viewport edge", async () => {
84 const loadedFiles: string[] = [];
85 const files = Array.from({ length: 14 }, (_, index) => {
86 const id = `file-${index}`;
87 const source = Array.from({ length: 3 }, (_, line) => `const value${line} = ${line};`);
88 return {
89 ...TYPESCRIPT_FILE,
90 id,
91 loadContents: async () => {
92 loadedFiles.push(id);
93 return { oldContents: "", newContents: source.join("\n") };
94 },
95 rows: [
96 { kind: "file", id: `${id}-header`, fileId: id },
97 ...source.map((content, line) => ({
98 ...makeLine({
99 id: `${id}-line-${line + 1}`,
100 content,
101 change: "add",
102 oldLineNumber: null,
103 newLineNumber: line + 1,
104 }),
105 fileId: id,
106 })),
107 ] satisfies NativeReviewDiffRow[],
108 };
109 });
110 const input = {
111 rows: files.flatMap((file) => file.rows),
112 files,
113 scheme: "dark" as const,
114 firstRowIndex: 30,
115 lastRowIndex: 32,
116 };
117 const viewport = await highlightNativeReviewDiffVisibleRows({ ...input, overscanRows: 0 });
118 const visibleIds = ["file-7-line-2", "file-7-line-3"];
119 expect(Object.keys(viewport.tokensByRowId)).toEqual(visibleIds);
120 const overscan = await highlightNativeReviewDiffVisibleRows({
121 ...input,
122 overscanRows: 160,
123 alreadyHighlightedRowIds: new Set(visibleIds),
124 });
125 expect(Object.keys(overscan.tokensByRowId).sort()).toEqual([
126 "file-7-line-1",
127 "file-8-line-1",
128 "file-8-line-2",
129 "file-8-line-3",
130 ]);
131 for (const tokens of Object.values({ ...viewport.tokensByRowId, ...overscan.tokensByRowId })) {
132 expect(tokens.some((token) => token.color !== null)).toBe(true);
133 }
134 expect([...new Set(loadedFiles)].sort()).toEqual(["file-7", "file-8"]);
135 });
136
137 it("skips collapsed code before the row cap and restores it on expansion", async () => {
138 const collapsedSource = Array.from(
139 { length: 100_000 },
140 (_, index) => `int hidden${index} = 1;`,
141 );
142 const expandedSource = [
143 "/* open",
144 ...Array.from({ length: 222 }, () => "still a comment"),
145 "*/ int visible = 1;",
146 ];
147 const collapsedLoad = vi.fn(async () => ({
148 oldContents: "",
149 newContents: collapsedSource.join("\n"),
150 }));
151 const expandedLoad = vi.fn(async () => ({
152 oldContents: "",
153 newContents: expandedSource.join("\n"),
154 }));
155 const files = [
156 {
157 ...TYPESCRIPT_FILE,
158 id: "collapsed",
159 language: "c",
160 path: "highlights.c",
161 loadContents: collapsedLoad,
162 },
163 {
164 ...TYPESCRIPT_FILE,
165 id: "expanded",
166 language: "c",
167 path: "highlights.h",
168 loadContents: expandedLoad,
169 },
170 ];
171 const rows = [collapsedSource, expandedSource].flatMap((source, fileIndex) => {
172 const fileId = files[fileIndex]!.id;
173 return [
174 { kind: "file", id: `${fileId}-header`, fileId },
175 ...source.map((content, index) => ({
176 ...makeLine({
177 id: `${fileId}-${index}`,
178 content,
179 change: "add",
180 newLineNumber: index + 1,
181 }),
182 fileId,
183 })),
184 ] satisfies NativeReviewDiffRow[];
185 });
186 const input = {
187 rows,
188 files,
189 scheme: "dark" as const,
190 firstRowIndex: 0,
191 lastRowIndex: rows.length - 1,
192 };
193 // A collapsed header and the next expanded header can share an iOS viewport,
194 // with 100,000 zero-height code rows between their canonical indices.
195 const viewport = await highlightNativeReviewDiffVisibleRows({
196 ...input,
197 collapsedFileIds: new Set(["collapsed"]),
198 overscanRows: 0,
199 });
200 expect(Object.keys(viewport.tokensByRowId)).toEqual(
201 expandedSource.map((_, index) => `expanded-${index}`),
202 );
203 const overscan = await highlightNativeReviewDiffVisibleRows({
204 ...input,
205 collapsedFileIds: new Set(["collapsed"]),
206 lastRowIndex: 100_002,
207 alreadyHighlightedRowIds: new Set(["expanded-0"]),
208 });
209 expect(Object.keys(overscan.tokensByRowId)).toEqual(
210 Array.from({ length: 160 }, (_, index) => `expanded-${index + 1}`),
211 );
212 expect(overscan.tokensByRowId["expanded-1"]).toEqual(
213 highlightCode(expandedSource.join("\n"), "c", "dark")[1],
214 );
215 expect(collapsedLoad).not.toHaveBeenCalled();
216 expect(expandedLoad).toHaveBeenCalled();
217
218 const expanded = await highlightNativeReviewDiffVisibleRows({
219 ...input,
220 lastRowIndex: 400,
221 collapsedFileIds: new Set(),
222 overscanRows: 0,
223 });
224 expect(expanded.rowCount).toBe(360);
225 expect(expanded.tokensByRowId["collapsed-0"]).toBeDefined();
226 expect(collapsedLoad).toHaveBeenCalledTimes(1);
227 });
228
229 it("delivers the dominant viewport file before fetching a smaller blocked neighbor", async () => {
230 const neighborStarted = Promise.withResolvers<void>();
231 const neighborResponse = Promise.withResolvers<{ oldContents: string; newContents: string }>();
232 const source = Array.from({ length: 27 }, (_, index) => `const value${index} = true;`).join(
233 "\n",
234 );
235 const chunks = vi.fn();
236 const rows = [
237 ...Array.from({ length: 5 }, (_, index) => ({
238 ...makeLine({
239 id: `neighbor-${index}`,
240 content: `const value${index} = true;`,
241 change: "add",
242 oldLineNumber: null,
243 newLineNumber: index + 1,
244 }),
245 fileId: "neighbor",
246 })),
247 ...Array.from({ length: 27 }, (_, index) =>
248 makeLine({
249 id: `selected-${index}`,
250 content: `const value${index} = true;`,
251 change: "delete",
252 oldLineNumber: index + 1,
253 newLineNumber: null,
254 }),
255 ),
256 ];
257 const highlighting = highlightNativeReviewDiffVisibleRows({
258 rows,
259 files: [
260 {
261 ...TYPESCRIPT_FILE,
262 id: "neighbor",
263 loadContents: () => {
264 neighborStarted.resolve();
265 return neighborResponse.promise;
266 },
267 },
268 {
269 ...TYPESCRIPT_FILE,
270 loadContents: async () => ({ oldContents: source, newContents: "" }),
271 },
272 ],
273 scheme: "dark",
274 firstRowIndex: 0,
275 lastRowIndex: rows.length - 1,
276 overscanRows: 0,
277 onChunk: chunks,
278 });
279 try {
280 await neighborStarted.promise;
281 expect(chunks).toHaveBeenCalledTimes(1);
282 expect(Object.keys(chunks.mock.calls[0]![0].tokensByRowId)).toEqual(
283 Array.from({ length: 27 }, (_, index) => `selected-${index}`),
284 );
285 expect(
286 chunks.mock.calls[0]![0].tokensByRowId["selected-0"].some(
287 (token: { color: string | null }) => token.color !== null,
288 ),
289 ).toBe(true);
290 } finally {
291 neighborResponse.resolve({ oldContents: "", newContents: source });
292 await highlighting;
293 }
294 });
295
296 it("does not carry grammar state across hunk boundaries", async () => {
297 const exportRow = makeLine({
298 id: "export-row",
299 content: "export async function run() {}",
300 change: "add",
301 oldLineNumber: null,
302 newLineNumber: 100,
303 });
304 const rows = [
305 makeHunk("hunk-1"),
306 makeLine({
307 id: "import-open",
308 content: "import {",
309 change: "context",
310 oldLineNumber: 1,
311 newLineNumber: 1,
312 }),
313 makeLine({
314 id: "import-entry",
315 content: " Model,",
316 change: "context",
317 oldLineNumber: 2,
318 newLineNumber: 2,
319 }),
320 makeHunk("hunk-2"),
321 exportRow,
322 ];
323
324 const [highlighted, standalone] = await Promise.all([
325 highlight(rows),
326 highlight([makeHunk("standalone-hunk"), exportRow]),
327 ]);
328
329 expect(highlighted.tokensByRowId[exportRow.id]).toEqual(standalone.tokensByRowId[exportRow.id]);
330 });
331
332 it("keeps grammar state across inline comment rows", async () => {
333 const openingRow = makeLine({
334 id: "template-open",
335 content: "const message = `open",
336 change: "add",
337 oldLineNumber: null,
338 newLineNumber: 1,
339 });
340 const closingRow = makeLine({
341 id: "template-close",
342 content: "closed`;",
343 change: "add",
344 oldLineNumber: null,
345 newLineNumber: 2,
346 });
347 const trailingRow = makeLine({
348 id: "trailing-row",
349 content: "export const answer = 42;",
350 change: "add",
351 oldLineNumber: null,
352 newLineNumber: 3,
353 });
354 const commentRow: NativeReviewDiffRow = {
355 kind: "comment",
356 id: "comment-1",
357 fileId: TYPESCRIPT_FILE.id,
358 commentText: "Review note",
359 };
360
361 const [withComment, contiguous] = await Promise.all([
362 highlight([openingRow, commentRow, closingRow, trailingRow]),
363 highlight([openingRow, closingRow, trailingRow]),
364 ]);
365
366 expect(withComment.tokensByRowId).toEqual(contiguous.tokensByRowId);
367 });
368
369 it("does not join unhighlighted rows across cached gaps", async () => {
370 const trailingRow = makeLine({
371 id: "trailing-row",
372 content: "export const answer = 42;",
373 change: "add",
374 oldLineNumber: null,
375 newLineNumber: 3,
376 });
377 const rows = [
378 makeLine({
379 id: "template-open",
380 content: "const message = `open",
381 change: "add",
382 oldLineNumber: null,
383 newLineNumber: 1,
384 }),
385 makeLine({
386 id: "template-close",
387 content: "closed`;",
388 change: "add",
389 oldLineNumber: null,
390 newLineNumber: 2,
391 }),
392 trailingRow,
393 ];
394
395 const [highlighted, standalone] = await Promise.all([
396 highlight(rows, new Set(["template-close"])),
397 highlight([trailingRow]),
398 ]);
399
400 expect(highlighted.tokensByRowId[trailingRow.id]).toEqual(
401 standalone.tokensByRowId[trailingRow.id],
402 );
403 });
404});
405
406// These complete sources are independent of the diff row selection below.
407const OLD_SOURCE = [
408 "export function render(name: string) {",
409 " const heading = `Hello ${name}",
410 "Welcome to the archive.",
411 "Your saved reports follow.",
412 " `;",
413 " /* Historical formatting rules:",
414 " * Keep the title and summary together.",
415 " * Preserve links in the footer.",
416 " */",
417 " return heading.trim();",
418 "}",
419];
420const NEW_SOURCE = [
421 "export function render(name: string) {",
422 " const heading = `Greetings ${name}",
423 "Welcome to the workspace.",
424 "Your saved reports follow.",
425 " `;",
426 " /* Current formatting rules:",
427 " * Keep the title and summary together.",
428 " * Highlight links in the footer.",
429 " */",
430 " return heading.trim();",
431 "}",
432];
433
434function authoritativeTokens(source: ReadonlyArray<string>) {
435 return highlightCode(source.join("\n"), "typescript", "dark");
436}
437
438function sourceRow(change: "context" | "add" | "delete", lineNumber: number) {
439 return makeLine({
440 id: `${change}-${lineNumber}`,
441 content: (change === "delete" ? OLD_SOURCE : NEW_SOURCE)[lineNumber - 1]!,
442 change,
443 oldLineNumber: change === "add" ? null : lineNumber,
444 newLineNumber: change === "delete" ? null : lineNumber,
445 });
446}
447
448function completeDiffRows() {
449 return [
450 sourceRow("context", 1),
451 sourceRow("delete", 2),
452 sourceRow("delete", 3),
453 sourceRow("add", 2),
454 sourceRow("add", 3),
455 sourceRow("context", 4),
456 sourceRow("context", 5),
457 sourceRow("delete", 6),
458 sourceRow("add", 6),
459 sourceRow("context", 7),
460 sourceRow("delete", 8),
461 sourceRow("add", 8),
462 sourceRow("context", 9),
463 sourceRow("context", 10),
464 sourceRow("context", 11),
465 ];
466}
467
468describe("source context", () => {
469 it("highlights parsed CRLF patch rows using omitted full-source templates and comments", async () => {
470 const newSource = [...OLD_SOURCE];
471 newSource[2] = NEW_SOURCE[2]!;
472 newSource[7] = NEW_SOURCE[7]!;
473 const parsed = buildReviewParsedDiff(
474 [
475 "diff --git a/example.ts b/example.ts",
476 "--- a/example.ts",
477 "+++ b/example.ts",
478 "@@ -3,2 +3,2 @@",
479 `-${OLD_SOURCE[2]}\r`,
480 `+${newSource[2]}\r`,
481 ` ${newSource[3]}\r`,
482 "@@ -7,3 +7,3 @@",
483 ` ${newSource[6]}\r`,
484 `-${OLD_SOURCE[7]}\r`,
485 `+${newSource[7]}\r`,
486 ` ${newSource[8]}\r`,
487 "",
488 ].join("\n"),
489 "crlf-context",
490 );
491 expect(parsed.kind).toBe("files");
492 const { rows, files } = buildNativeReviewDiffData(parsed);
493 const lineRows = rows.filter((row) => row.kind === "line");
494 expect(lineRows).toHaveLength(7);
495 const originalContents = lineRows.map((row) => row.content);
496 expect(originalContents.every((content) => content?.endsWith("\r"))).toBe(true);
497 const oldTokens = await authoritativeTokens(OLD_SOURCE);
498 const newTokens = await authoritativeTokens(newSource);
499 const result = await highlightNativeReviewDiffVisibleRows({
500 rows,
501 files: files.map((file) => ({
502 ...file,
503 loadContents: async () => ({
504 oldContents: `${OLD_SOURCE.join("\r\n")}\r\n`,
505 newContents: `${newSource.join("\r\n")}\r\n`,
506 }),
507 })),
508 scheme: "dark",
509 firstRowIndex: 0,
510 lastRowIndex: rows.length - 1,
511 overscanRows: 0,
512 });
513 for (const row of lineRows) {
514 const expected =
515 row.change === "delete"
516 ? oldTokens[row.oldLineNumber! - 1]
517 : newTokens[row.newLineNumber! - 1];
518 const tokens = result.tokensByRowId[row.id];
519 expect(tokens, row.id).toEqual(expected);
520 expect(
521 tokens?.every((token) => token.color !== null),
522 row.id,
523 ).toBe(true);
524 expect(tokens?.[0]?.color, row.id).toBe(
525 (row.newLineNumber ?? row.oldLineNumber)! <= 4 ? "#5ecc71" : "#737373",
526 );
527 }
528 expect(lineRows.map((row) => row.content)).toEqual(originalContents);
529 expect(result.retryableRowIds).toEqual([]);
530 });
531
532 it("matches full OLD/NEW source across replacement runs and shared multiline context", async () => {
533 const rows = completeDiffRows();
534 const oldTokens = await authoritativeTokens(OLD_SOURCE);
535 const newTokens = await authoritativeTokens(NEW_SOURCE);
536 const result = await highlightNativeReviewDiffVisibleRows({
537 rows,
538 files: [TYPESCRIPT_FILE],
539 scheme: "dark",
540 firstRowIndex: 0,
541 lastRowIndex: rows.length - 1,
542 overscanRows: 0,
543 });
544 for (const row of rows) {
545 const expected =
546 row.change === "delete"
547 ? oldTokens[row.oldLineNumber! - 1]
548 : newTokens[row.newLineNumber! - 1];
549 expect(result.tokensByRowId[row.id], row.id).toEqual(expected);
550 }
551 });
552
553 it("uses NEW grammar for shared rows when OLD remains inside a comment", async () => {
554 const oldSource = ["/* Retired renderer", "const value = 3;", "finish();"];
555 const newSource = ["// Active renderer", "const value = 3;", "finish();"];
556 const rows = [
557 makeLine({
558 id: "old-open",
559 content: oldSource[0],
560 change: "delete",
561 oldLineNumber: 1,
562 newLineNumber: null,
563 }),
564 makeLine({
565 id: "new-open",
566 content: newSource[0],
567 change: "add",
568 oldLineNumber: null,
569 newLineNumber: 1,
570 }),
571 makeLine({
572 id: "shared",
573 content: newSource[1],
574 change: "context",
575 oldLineNumber: 2,
576 newLineNumber: 2,
577 }),
578 makeLine({
579 id: "old-tail",
580 content: oldSource[2],
581 change: "delete",
582 oldLineNumber: 3,
583 newLineNumber: null,
584 }),
585 makeLine({
586 id: "new-tail",
587 content: newSource[2],
588 change: "add",
589 oldLineNumber: null,
590 newLineNumber: 3,
591 }),
592 ];
593 const oldTokens = await authoritativeTokens(oldSource);
594 const newTokens = await authoritativeTokens(newSource);
595 const result = await highlightNativeReviewDiffVisibleRows({
596 rows,
597 files: [TYPESCRIPT_FILE],
598 scheme: "dark",
599 firstRowIndex: 0,
600 lastRowIndex: rows.length - 1,
601 overscanRows: 0,
602 });
603 expect(oldTokens[1]).not.toEqual(newTokens[1]);
604 expect(result.tokensByRowId.shared).toEqual(newTokens[1]);
605 expect(result.tokensByRowId["old-tail"]).toEqual(oldTokens[2]);
606 expect(result.tokensByRowId["new-tail"]).toEqual(newTokens[2]);
607 });
608
609 it("leaves mismatched and unavailable source revisions plain", async () => {
610 const rows = [sourceRow("add", 3)];
611 for (const source of [null, { oldContents: "", newContents: "different revision" }]) {
612 const result = await highlightNativeReviewDiffVisibleRows({
613 rows,
614 files: [{ ...TYPESCRIPT_FILE, loadContents: async () => source }],
615 scheme: "dark",
616 firstRowIndex: 0,
617 lastRowIndex: 0,
618 overscanRows: 0,
619 });
620 expect(result.tokensByRowId[rows[0]!.id]).toEqual([
621 { content: NEW_SOURCE[2], color: null, fontStyle: null },
622 ]);
623 }
624 });
625});
626
627it("reuses full native records across viewport and theme changes and evicts old entries", async () => {
628 const sources = Array.from({ length: 9 }, (_, i) => ({
629 oldContents: "",
630 newContents: `/* ${i}\ninside\n*/\nconst tail = 2;`,
631 }));
632 const rows = [
633 makeLine({
634 id: "body",
635 content: "inside",
636 change: "add",
637 oldLineNumber: null,
638 newLineNumber: 2,
639 }),
640 makeLine({
641 id: "tail",
642 content: "const tail = 2;",
643 change: "add",
644 oldLineNumber: null,
645 newLineNumber: 4,
646 }),
647 ];
648 const input = {
649 rows,
650 scheme: "dark" as const,
651 firstRowIndex: 0,
652 lastRowIndex: 0,
653 overscanRows: 0,
654 };
655 const files = [{ ...TYPESCRIPT_FILE, loadContents: async () => sources[0]! }];
656 const initial = await highlightNativeReviewDiffVisibleRows({ ...input, files });
657 const moved = await highlightNativeReviewDiffVisibleRows({
658 ...input,
659 files,
660 firstRowIndex: 1,
661 lastRowIndex: 1,
662 });
663 await highlightNativeReviewDiffVisibleRows({ ...input, files, scheme: "light" });
664 expect(tokenization.calls).toEqual([sources[0]!.newContents]);
665 expect(initial.tokensByRowId.body).toEqual(
666 highlightCode(sources[0]!.newContents, "typescript", "dark")[1],
667 );
668 expect(moved.tokensByRowId.tail).toEqual(
669 highlightCode(sources[0]!.newContents, "typescript", "dark")[3],
670 );
671 tokenization.calls = [];
672 for (const source of sources.slice(1)) {
673 await highlightNativeReviewDiffVisibleRows({
674 ...input,
675 files: [{ ...TYPESCRIPT_FILE, loadContents: async () => source }],
676 });
677 }
678 await highlightNativeReviewDiffVisibleRows({ ...input, files });
679 expect(tokenization.calls).toHaveLength(9);
680 expect(tokenization.calls.at(-1)).toBe(sources[0]!.newContents);
681});
682
683it("reuses segment records across themes and replaces records when the language changes", async () => {
684 const rows = [
685 makeLine({
686 id: "line",
687 content: "const value = 1;",
688 change: "add",
689 oldLineNumber: null,
690 newLineNumber: 1,
691 }),
692 ];
693 const input = {
694 rows,
695 files: [TYPESCRIPT_FILE],
696 scheme: "dark" as const,
697 firstRowIndex: 0,
698 lastRowIndex: 0,
699 overscanRows: 0,
700 };
701 const first = await highlightNativeReviewDiffVisibleRows(input);
702 await highlightNativeReviewDiffVisibleRows({ ...input, scheme: "light" });
703 expect(tokenization.calls).toEqual([rows[0]!.content]);
704 await highlightNativeReviewDiffVisibleRows({
705 ...input,
706 files: [{ ...TYPESCRIPT_FILE, language: "c" }],
707 });
708 const restored = await highlightNativeReviewDiffVisibleRows(input);
709 expect(tokenization.calls).toHaveLength(3);
710 expect(restored.tokensByRowId).toEqual(first.tokensByRowId);
711});
712
713it("uses full grammar after an oversized line and expands only selected rows in a 100k-line C source", async () => {
714 const contents = [
715 " ".repeat(1_001) + "/*",
716 ...Array.from({ length: 99_997 }, (_, i) => `comment ${i}: int value = 1;`),
717 "*/",
718 "int done = 2;",
719 ].join("\n");
720 expect(contents.length).toBeGreaterThan(500_000);
721 const source = { oldContents: "", newContents: contents };
722 const rows = [
723 makeLine({
724 id: "body",
725 content: "comment 99996: int value = 1;",
726 change: "add",
727 oldLineNumber: null,
728 newLineNumber: 99_998,
729 }),
730 makeLine({
731 id: "end",
732 content: "int done = 2;",
733 change: "add",
734 oldLineNumber: null,
735 newLineNumber: 100_000,
736 }),
737 ];
738 const input = {
739 rows,
740 files: [{ ...TYPESCRIPT_FILE, language: "c", loadContents: async () => source }],
741 scheme: "dark" as const,
742 firstRowIndex: 0,
743 lastRowIndex: 0,
744 overscanRows: 0,
745 };
746 const result = await highlightNativeReviewDiffVisibleRows(input);
747 expect(Object.keys(result.tokensByRowId)).toEqual(["body"]);
748 expect(result.tokensByRowId.body).toEqual([
749 { content: rows[0]!.content, color: "#737373", fontStyle: 0 },
750 ]);
751 const moved = await highlightNativeReviewDiffVisibleRows({
752 ...input,
753 firstRowIndex: 1,
754 lastRowIndex: 1,
755 });
756 expect(moved.tokensByRowId.end?.some((token) => token.color !== "#737373")).toBe(true);
757 expect(tokenization.calls).toEqual([contents]);
758});
759
760it("publishes no cancelled file chunk and never tokenizes its next side", async () => {
761 const controller = new AbortController();
762 tokenization.afterCall = () => controller.abort();
763 const chunks = vi.fn();
764 const result = await highlightNativeReviewDiffVisibleRows({
765 rows: [sourceRow("delete", 3), sourceRow("add", 3)],
766 files: [
767 {
768 ...TYPESCRIPT_FILE,
769 loadContents: async () => ({
770 oldContents: OLD_SOURCE.join("\n"),
771 newContents: NEW_SOURCE.join("\n"),
772 }),
773 },
774 ],
775 scheme: "dark",
776 firstRowIndex: 0,
777 lastRowIndex: 1,
778 overscanRows: 0,
779 signal: controller.signal,
780 onChunk: chunks,
781 });
782 expect(tokenization.calls).toHaveLength(1);
783 expect(chunks).not.toHaveBeenCalled();
784 expect(result.tokensByRowId).toEqual({});
785 expect(result.rowCount).toBe(0);
786});
787
788it("does not tokenize or publish a source that arrives after cancellation", async () => {
789 const controller = new AbortController();
790 const started = Promise.withResolvers<void>();
791 const response = Promise.withResolvers<{ oldContents: string; newContents: string }>();
792 const chunks = vi.fn();
793 const work = highlightNativeReviewDiffVisibleRows({
794 rows: [sourceRow("add", 3)],
795 files: [
796 {
797 ...TYPESCRIPT_FILE,
798 loadContents: () => {
799 started.resolve();
800 return response.promise;
801 },
802 },
803 ],
804 scheme: "dark",
805 firstRowIndex: 0,
806 lastRowIndex: 0,
807 signal: controller.signal,
808 onChunk: chunks,
809 });
810 await started.promise;
811 controller.abort();
812 response.resolve({ oldContents: "", newContents: NEW_SOURCE.join("\n") });
813 expect((await work).rowCount).toBe(0);
814 expect(tokenization.calls).toEqual([]);
815 expect(chunks).not.toHaveBeenCalled();
816});