The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { highlightCode } from "../../lib/highlights"; |
| 4 | |
| 5 | import type { NativeReviewDiffRow } from "./nativeReviewDiffSurface"; |
| 6 | import type { NativeReviewDiffFile } from "./nativeReviewDiffTypes"; |
| 7 | import { highlightNativeReviewDiffVisibleRows } from "./nativeReviewDiffHighlighter"; |
| 8 | import { buildNativeReviewDiffData } from "../review/nativeReviewDiffAdapter"; |
| 9 | import { buildReviewParsedDiff } from "../review/reviewModel"; |
| 10 | |
| 11 | const tokenization = vi.hoisted(() => ({ |
| 12 | calls: [] as string[], |
| 13 | afterCall: undefined as (() => void) | undefined, |
| 14 | })); |
| 15 | |
| 16 | vi.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 | |
| 34 | afterEach(() => { |
| 35 | tokenization.calls = []; |
| 36 | tokenization.afterCall = undefined; |
| 37 | }); |
| 38 | |
| 39 | const TYPESCRIPT_FILE: NativeReviewDiffFile = { |
| 40 | id: "file-1", |
| 41 | path: "example.ts", |
| 42 | language: "typescript", |
| 43 | additions: 0, |
| 44 | deletions: 0, |
| 45 | }; |
| 46 | |
| 47 | function 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 | |
| 57 | function makeHunk(id: string): NativeReviewDiffRow { |
| 58 | return { |
| 59 | kind: "hunk", |
| 60 | id, |
| 61 | fileId: TYPESCRIPT_FILE.id, |
| 62 | text: "@@", |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | function 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 | |
| 82 | describe("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 | // The blocked neighbor is colored from its patch while its source loads. |
| 291 | await vi.waitFor(() => expect(chunks).toHaveBeenCalledTimes(2)); |
| 292 | const provisional = chunks.mock.calls[1]![0]; |
| 293 | const neighborIds = Array.from({ length: 5 }, (_, index) => `neighbor-${index}`); |
| 294 | expect(Object.keys(provisional.tokensByRowId)).toEqual(neighborIds); |
| 295 | expect(provisional.retryableRowIds).toEqual(neighborIds); |
| 296 | } finally { |
| 297 | neighborResponse.resolve({ oldContents: "", newContents: source }); |
| 298 | await highlighting; |
| 299 | } |
| 300 | }); |
| 301 | |
| 302 | it("does not carry grammar state across hunk boundaries", async () => { |
| 303 | const exportRow = makeLine({ |
| 304 | id: "export-row", |
| 305 | content: "export async function run() {}", |
| 306 | change: "add", |
| 307 | oldLineNumber: null, |
| 308 | newLineNumber: 100, |
| 309 | }); |
| 310 | const rows = [ |
| 311 | makeHunk("hunk-1"), |
| 312 | makeLine({ |
| 313 | id: "import-open", |
| 314 | content: "import {", |
| 315 | change: "context", |
| 316 | oldLineNumber: 1, |
| 317 | newLineNumber: 1, |
| 318 | }), |
| 319 | makeLine({ |
| 320 | id: "import-entry", |
| 321 | content: " Model,", |
| 322 | change: "context", |
| 323 | oldLineNumber: 2, |
| 324 | newLineNumber: 2, |
| 325 | }), |
| 326 | makeHunk("hunk-2"), |
| 327 | exportRow, |
| 328 | ]; |
| 329 | |
| 330 | const [highlighted, standalone] = await Promise.all([ |
| 331 | highlight(rows), |
| 332 | highlight([makeHunk("standalone-hunk"), exportRow]), |
| 333 | ]); |
| 334 | |
| 335 | expect(highlighted.tokensByRowId[exportRow.id]).toEqual(standalone.tokensByRowId[exportRow.id]); |
| 336 | }); |
| 337 | |
| 338 | it("keeps grammar state across inline comment rows", async () => { |
| 339 | const openingRow = makeLine({ |
| 340 | id: "template-open", |
| 341 | content: "const message = `open", |
| 342 | change: "add", |
| 343 | oldLineNumber: null, |
| 344 | newLineNumber: 1, |
| 345 | }); |
| 346 | const closingRow = makeLine({ |
| 347 | id: "template-close", |
| 348 | content: "closed`;", |
| 349 | change: "add", |
| 350 | oldLineNumber: null, |
| 351 | newLineNumber: 2, |
| 352 | }); |
| 353 | const trailingRow = makeLine({ |
| 354 | id: "trailing-row", |
| 355 | content: "export const answer = 42;", |
| 356 | change: "add", |
| 357 | oldLineNumber: null, |
| 358 | newLineNumber: 3, |
| 359 | }); |
| 360 | const commentRow: NativeReviewDiffRow = { |
| 361 | kind: "comment", |
| 362 | id: "comment-1", |
| 363 | fileId: TYPESCRIPT_FILE.id, |
| 364 | commentText: "Review note", |
| 365 | }; |
| 366 | |
| 367 | const [withComment, contiguous] = await Promise.all([ |
| 368 | highlight([openingRow, commentRow, closingRow, trailingRow]), |
| 369 | highlight([openingRow, closingRow, trailingRow]), |
| 370 | ]); |
| 371 | |
| 372 | expect(withComment.tokensByRowId).toEqual(contiguous.tokensByRowId); |
| 373 | }); |
| 374 | |
| 375 | it("does not join unhighlighted rows across cached gaps", async () => { |
| 376 | const trailingRow = makeLine({ |
| 377 | id: "trailing-row", |
| 378 | content: "export const answer = 42;", |
| 379 | change: "add", |
| 380 | oldLineNumber: null, |
| 381 | newLineNumber: 3, |
| 382 | }); |
| 383 | const rows = [ |
| 384 | makeLine({ |
| 385 | id: "template-open", |
| 386 | content: "const message = `open", |
| 387 | change: "add", |
| 388 | oldLineNumber: null, |
| 389 | newLineNumber: 1, |
| 390 | }), |
| 391 | makeLine({ |
| 392 | id: "template-close", |
| 393 | content: "closed`;", |
| 394 | change: "add", |
| 395 | oldLineNumber: null, |
| 396 | newLineNumber: 2, |
| 397 | }), |
| 398 | trailingRow, |
| 399 | ]; |
| 400 | |
| 401 | const [highlighted, standalone] = await Promise.all([ |
| 402 | highlight(rows, new Set(["template-close"])), |
| 403 | highlight([trailingRow]), |
| 404 | ]); |
| 405 | |
| 406 | expect(highlighted.tokensByRowId[trailingRow.id]).toEqual( |
| 407 | standalone.tokensByRowId[trailingRow.id], |
| 408 | ); |
| 409 | }); |
| 410 | }); |
| 411 | |
| 412 | // These complete sources are independent of the diff row selection below. |
| 413 | const OLD_SOURCE = [ |
| 414 | "export function render(name: string) {", |
| 415 | " const heading = `Hello ${name}", |
| 416 | "Welcome to the archive.", |
| 417 | "Your saved reports follow.", |
| 418 | " `;", |
| 419 | " /* Historical formatting rules:", |
| 420 | " * Keep the title and summary together.", |
| 421 | " * Preserve links in the footer.", |
| 422 | " */", |
| 423 | " return heading.trim();", |
| 424 | "}", |
| 425 | ]; |
| 426 | const NEW_SOURCE = [ |
| 427 | "export function render(name: string) {", |
| 428 | " const heading = `Greetings ${name}", |
| 429 | "Welcome to the workspace.", |
| 430 | "Your saved reports follow.", |
| 431 | " `;", |
| 432 | " /* Current formatting rules:", |
| 433 | " * Keep the title and summary together.", |
| 434 | " * Highlight links in the footer.", |
| 435 | " */", |
| 436 | " return heading.trim();", |
| 437 | "}", |
| 438 | ]; |
| 439 | |
| 440 | function authoritativeTokens(source: ReadonlyArray<string>) { |
| 441 | return highlightCode(source.join("\n"), "typescript", "dark"); |
| 442 | } |
| 443 | |
| 444 | function sourceRow(change: "context" | "add" | "delete", lineNumber: number) { |
| 445 | return makeLine({ |
| 446 | id: `${change}-${lineNumber}`, |
| 447 | content: (change === "delete" ? OLD_SOURCE : NEW_SOURCE)[lineNumber - 1]!, |
| 448 | change, |
| 449 | oldLineNumber: change === "add" ? null : lineNumber, |
| 450 | newLineNumber: change === "delete" ? null : lineNumber, |
| 451 | }); |
| 452 | } |
| 453 | |
| 454 | function completeDiffRows() { |
| 455 | return [ |
| 456 | sourceRow("context", 1), |
| 457 | sourceRow("delete", 2), |
| 458 | sourceRow("delete", 3), |
| 459 | sourceRow("add", 2), |
| 460 | sourceRow("add", 3), |
| 461 | sourceRow("context", 4), |
| 462 | sourceRow("context", 5), |
| 463 | sourceRow("delete", 6), |
| 464 | sourceRow("add", 6), |
| 465 | sourceRow("context", 7), |
| 466 | sourceRow("delete", 8), |
| 467 | sourceRow("add", 8), |
| 468 | sourceRow("context", 9), |
| 469 | sourceRow("context", 10), |
| 470 | sourceRow("context", 11), |
| 471 | ]; |
| 472 | } |
| 473 | |
| 474 | describe("source context", () => { |
| 475 | it("highlights parsed CRLF patch rows using omitted full-source templates and comments", async () => { |
| 476 | const newSource = [...OLD_SOURCE]; |
| 477 | newSource[2] = NEW_SOURCE[2]!; |
| 478 | newSource[7] = NEW_SOURCE[7]!; |
| 479 | const parsed = buildReviewParsedDiff( |
| 480 | [ |
| 481 | "diff --git a/example.ts b/example.ts", |
| 482 | "--- a/example.ts", |
| 483 | "+++ b/example.ts", |
| 484 | "@@ -3,2 +3,2 @@", |
| 485 | `-${OLD_SOURCE[2]}\r`, |
| 486 | `+${newSource[2]}\r`, |
| 487 | ` ${newSource[3]}\r`, |
| 488 | "@@ -7,3 +7,3 @@", |
| 489 | ` ${newSource[6]}\r`, |
| 490 | `-${OLD_SOURCE[7]}\r`, |
| 491 | `+${newSource[7]}\r`, |
| 492 | ` ${newSource[8]}\r`, |
| 493 | "", |
| 494 | ].join("\n"), |
| 495 | "crlf-context", |
| 496 | ); |
| 497 | expect(parsed.kind).toBe("files"); |
| 498 | const { rows, files } = buildNativeReviewDiffData(parsed); |
| 499 | const lineRows = rows.filter((row) => row.kind === "line"); |
| 500 | expect(lineRows).toHaveLength(7); |
| 501 | const originalContents = lineRows.map((row) => row.content); |
| 502 | expect(originalContents.every((content) => content?.endsWith("\r"))).toBe(true); |
| 503 | const oldTokens = await authoritativeTokens(OLD_SOURCE); |
| 504 | const newTokens = await authoritativeTokens(newSource); |
| 505 | const result = await highlightNativeReviewDiffVisibleRows({ |
| 506 | rows, |
| 507 | files: files.map((file) => ({ |
| 508 | ...file, |
| 509 | loadContents: async () => ({ |
| 510 | oldContents: `${OLD_SOURCE.join("\r\n")}\r\n`, |
| 511 | newContents: `${newSource.join("\r\n")}\r\n`, |
| 512 | }), |
| 513 | })), |
| 514 | scheme: "dark", |
| 515 | firstRowIndex: 0, |
| 516 | lastRowIndex: rows.length - 1, |
| 517 | overscanRows: 0, |
| 518 | }); |
| 519 | for (const row of lineRows) { |
| 520 | const expected = |
| 521 | row.change === "delete" |
| 522 | ? oldTokens[row.oldLineNumber! - 1] |
| 523 | : newTokens[row.newLineNumber! - 1]; |
| 524 | const tokens = result.tokensByRowId[row.id]; |
| 525 | expect(tokens, row.id).toEqual(expected); |
| 526 | expect( |
| 527 | tokens?.every((token) => token.color !== null), |
| 528 | row.id, |
| 529 | ).toBe(true); |
| 530 | expect(tokens?.[0]?.color, row.id).toBe( |
| 531 | (row.newLineNumber ?? row.oldLineNumber)! <= 4 ? "#5ecc71" : "#737373", |
| 532 | ); |
| 533 | } |
| 534 | expect(lineRows.map((row) => row.content)).toEqual(originalContents); |
| 535 | expect(result.retryableRowIds).toEqual([]); |
| 536 | }); |
| 537 | |
| 538 | it("matches full OLD/NEW source across replacement runs and shared multiline context", async () => { |
| 539 | const rows = completeDiffRows(); |
| 540 | const oldTokens = await authoritativeTokens(OLD_SOURCE); |
| 541 | const newTokens = await authoritativeTokens(NEW_SOURCE); |
| 542 | const result = await highlightNativeReviewDiffVisibleRows({ |
| 543 | rows, |
| 544 | files: [TYPESCRIPT_FILE], |
| 545 | scheme: "dark", |
| 546 | firstRowIndex: 0, |
| 547 | lastRowIndex: rows.length - 1, |
| 548 | overscanRows: 0, |
| 549 | }); |
| 550 | for (const row of rows) { |
| 551 | const expected = |
| 552 | row.change === "delete" |
| 553 | ? oldTokens[row.oldLineNumber! - 1] |
| 554 | : newTokens[row.newLineNumber! - 1]; |
| 555 | expect(result.tokensByRowId[row.id], row.id).toEqual(expected); |
| 556 | } |
| 557 | }); |
| 558 | |
| 559 | it("uses NEW grammar for shared rows when OLD remains inside a comment", async () => { |
| 560 | const oldSource = ["/* Retired renderer", "const value = 3;", "finish();"]; |
| 561 | const newSource = ["// Active renderer", "const value = 3;", "finish();"]; |
| 562 | const rows = [ |
| 563 | makeLine({ |
| 564 | id: "old-open", |
| 565 | content: oldSource[0], |
| 566 | change: "delete", |
| 567 | oldLineNumber: 1, |
| 568 | newLineNumber: null, |
| 569 | }), |
| 570 | makeLine({ |
| 571 | id: "new-open", |
| 572 | content: newSource[0], |
| 573 | change: "add", |
| 574 | oldLineNumber: null, |
| 575 | newLineNumber: 1, |
| 576 | }), |
| 577 | makeLine({ |
| 578 | id: "shared", |
| 579 | content: newSource[1], |
| 580 | change: "context", |
| 581 | oldLineNumber: 2, |
| 582 | newLineNumber: 2, |
| 583 | }), |
| 584 | makeLine({ |
| 585 | id: "old-tail", |
| 586 | content: oldSource[2], |
| 587 | change: "delete", |
| 588 | oldLineNumber: 3, |
| 589 | newLineNumber: null, |
| 590 | }), |
| 591 | makeLine({ |
| 592 | id: "new-tail", |
| 593 | content: newSource[2], |
| 594 | change: "add", |
| 595 | oldLineNumber: null, |
| 596 | newLineNumber: 3, |
| 597 | }), |
| 598 | ]; |
| 599 | const oldTokens = await authoritativeTokens(oldSource); |
| 600 | const newTokens = await authoritativeTokens(newSource); |
| 601 | const result = await highlightNativeReviewDiffVisibleRows({ |
| 602 | rows, |
| 603 | files: [TYPESCRIPT_FILE], |
| 604 | scheme: "dark", |
| 605 | firstRowIndex: 0, |
| 606 | lastRowIndex: rows.length - 1, |
| 607 | overscanRows: 0, |
| 608 | }); |
| 609 | expect(oldTokens[1]).not.toEqual(newTokens[1]); |
| 610 | expect(result.tokensByRowId.shared).toEqual(newTokens[1]); |
| 611 | expect(result.tokensByRowId["old-tail"]).toEqual(oldTokens[2]); |
| 612 | expect(result.tokensByRowId["new-tail"]).toEqual(newTokens[2]); |
| 613 | }); |
| 614 | |
| 615 | it("uses patch grammar for mismatched and unavailable source revisions", async () => { |
| 616 | const rows = [sourceRow("add", 3)]; |
| 617 | const patchTokens = (await highlight(rows)).tokensByRowId[rows[0]!.id]; |
| 618 | expect(patchTokens!.some((token) => token.color !== null)).toBe(true); |
| 619 | for (const source of [null, { oldContents: "", newContents: "different revision" }]) { |
| 620 | const result = await highlightNativeReviewDiffVisibleRows({ |
| 621 | rows, |
| 622 | files: [{ ...TYPESCRIPT_FILE, loadContents: async () => source }], |
| 623 | scheme: "dark", |
| 624 | firstRowIndex: 0, |
| 625 | lastRowIndex: 0, |
| 626 | overscanRows: 0, |
| 627 | }); |
| 628 | expect(result.tokensByRowId[rows[0]!.id]).toEqual(patchTokens); |
| 629 | } |
| 630 | }); |
| 631 | }); |
| 632 | |
| 633 | it("reuses full native records across viewport and theme changes and evicts old entries", async () => { |
| 634 | const sources = Array.from({ length: 9 }, (_, i) => ({ |
| 635 | oldContents: "", |
| 636 | newContents: `/* ${i}\ninside\n*/\nconst tail = 2;`, |
| 637 | })); |
| 638 | const rows = [ |
| 639 | makeLine({ |
| 640 | id: "body", |
| 641 | content: "inside", |
| 642 | change: "add", |
| 643 | oldLineNumber: null, |
| 644 | newLineNumber: 2, |
| 645 | }), |
| 646 | makeLine({ |
| 647 | id: "tail", |
| 648 | content: "const tail = 2;", |
| 649 | change: "add", |
| 650 | oldLineNumber: null, |
| 651 | newLineNumber: 4, |
| 652 | }), |
| 653 | ]; |
| 654 | const input = { |
| 655 | rows, |
| 656 | scheme: "dark" as const, |
| 657 | firstRowIndex: 0, |
| 658 | lastRowIndex: 0, |
| 659 | overscanRows: 0, |
| 660 | }; |
| 661 | const files = [{ ...TYPESCRIPT_FILE, loadContents: async () => sources[0]! }]; |
| 662 | const initial = await highlightNativeReviewDiffVisibleRows({ ...input, files }); |
| 663 | const moved = await highlightNativeReviewDiffVisibleRows({ |
| 664 | ...input, |
| 665 | files, |
| 666 | firstRowIndex: 1, |
| 667 | lastRowIndex: 1, |
| 668 | }); |
| 669 | await highlightNativeReviewDiffVisibleRows({ ...input, files, scheme: "light" }); |
| 670 | expect(tokenization.calls).toEqual([sources[0]!.newContents]); |
| 671 | expect(initial.tokensByRowId.body).toEqual( |
| 672 | highlightCode(sources[0]!.newContents, "typescript", "dark")[1], |
| 673 | ); |
| 674 | expect(moved.tokensByRowId.tail).toEqual( |
| 675 | highlightCode(sources[0]!.newContents, "typescript", "dark")[3], |
| 676 | ); |
| 677 | tokenization.calls = []; |
| 678 | for (const source of sources.slice(1)) { |
| 679 | await highlightNativeReviewDiffVisibleRows({ |
| 680 | ...input, |
| 681 | files: [{ ...TYPESCRIPT_FILE, loadContents: async () => source }], |
| 682 | }); |
| 683 | } |
| 684 | await highlightNativeReviewDiffVisibleRows({ ...input, files }); |
| 685 | expect(tokenization.calls).toHaveLength(9); |
| 686 | expect(tokenization.calls.at(-1)).toBe(sources[0]!.newContents); |
| 687 | }); |
| 688 | |
| 689 | it("reuses segment records across themes and replaces records when the language changes", async () => { |
| 690 | const rows = [ |
| 691 | makeLine({ |
| 692 | id: "line", |
| 693 | content: "const value = 1;", |
| 694 | change: "add", |
| 695 | oldLineNumber: null, |
| 696 | newLineNumber: 1, |
| 697 | }), |
| 698 | ]; |
| 699 | const input = { |
| 700 | rows, |
| 701 | files: [TYPESCRIPT_FILE], |
| 702 | scheme: "dark" as const, |
| 703 | firstRowIndex: 0, |
| 704 | lastRowIndex: 0, |
| 705 | overscanRows: 0, |
| 706 | }; |
| 707 | const first = await highlightNativeReviewDiffVisibleRows(input); |
| 708 | await highlightNativeReviewDiffVisibleRows({ ...input, scheme: "light" }); |
| 709 | expect(tokenization.calls).toEqual([rows[0]!.content]); |
| 710 | await highlightNativeReviewDiffVisibleRows({ |
| 711 | ...input, |
| 712 | files: [{ ...TYPESCRIPT_FILE, language: "c" }], |
| 713 | }); |
| 714 | const restored = await highlightNativeReviewDiffVisibleRows(input); |
| 715 | expect(tokenization.calls).toHaveLength(3); |
| 716 | expect(restored.tokensByRowId).toEqual(first.tokensByRowId); |
| 717 | }); |
| 718 | |
| 719 | it("uses full grammar after an oversized line and expands only selected rows in a 100k-line C source", async () => { |
| 720 | const contents = [ |
| 721 | " ".repeat(1_001) + "/*", |
| 722 | ...Array.from({ length: 99_997 }, (_, i) => `comment ${i}: int value = 1;`), |
| 723 | "*/", |
| 724 | "int done = 2;", |
| 725 | ].join("\n"); |
| 726 | expect(contents.length).toBeGreaterThan(500_000); |
| 727 | const source = { oldContents: "", newContents: contents }; |
| 728 | const rows = [ |
| 729 | makeLine({ |
| 730 | id: "body", |
| 731 | content: "comment 99996: int value = 1;", |
| 732 | change: "add", |
| 733 | oldLineNumber: null, |
| 734 | newLineNumber: 99_998, |
| 735 | }), |
| 736 | makeLine({ |
| 737 | id: "end", |
| 738 | content: "int done = 2;", |
| 739 | change: "add", |
| 740 | oldLineNumber: null, |
| 741 | newLineNumber: 100_000, |
| 742 | }), |
| 743 | ]; |
| 744 | const input = { |
| 745 | rows, |
| 746 | files: [{ ...TYPESCRIPT_FILE, language: "c", loadContents: async () => source }], |
| 747 | scheme: "dark" as const, |
| 748 | firstRowIndex: 0, |
| 749 | lastRowIndex: 0, |
| 750 | overscanRows: 0, |
| 751 | }; |
| 752 | const result = await highlightNativeReviewDiffVisibleRows(input); |
| 753 | expect(Object.keys(result.tokensByRowId)).toEqual(["body"]); |
| 754 | expect(result.tokensByRowId.body).toEqual([ |
| 755 | { content: rows[0]!.content, color: "#737373", fontStyle: 0 }, |
| 756 | ]); |
| 757 | const moved = await highlightNativeReviewDiffVisibleRows({ |
| 758 | ...input, |
| 759 | firstRowIndex: 1, |
| 760 | lastRowIndex: 1, |
| 761 | }); |
| 762 | expect(moved.tokensByRowId.end?.some((token) => token.color !== "#737373")).toBe(true); |
| 763 | expect(tokenization.calls).toEqual([contents]); |
| 764 | }); |
| 765 | |
| 766 | it("publishes no cancelled file chunk and never tokenizes its next side", async () => { |
| 767 | const controller = new AbortController(); |
| 768 | tokenization.afterCall = () => controller.abort(); |
| 769 | const chunks = vi.fn(); |
| 770 | const result = await highlightNativeReviewDiffVisibleRows({ |
| 771 | rows: [sourceRow("delete", 3), sourceRow("add", 3)], |
| 772 | files: [ |
| 773 | { |
| 774 | ...TYPESCRIPT_FILE, |
| 775 | loadContents: async () => ({ |
| 776 | oldContents: OLD_SOURCE.join("\n"), |
| 777 | newContents: NEW_SOURCE.join("\n"), |
| 778 | }), |
| 779 | }, |
| 780 | ], |
| 781 | scheme: "dark", |
| 782 | firstRowIndex: 0, |
| 783 | lastRowIndex: 1, |
| 784 | overscanRows: 0, |
| 785 | signal: controller.signal, |
| 786 | onChunk: chunks, |
| 787 | }); |
| 788 | expect(tokenization.calls).toHaveLength(1); |
| 789 | expect(chunks).not.toHaveBeenCalled(); |
| 790 | expect(result.tokensByRowId).toEqual({}); |
| 791 | expect(result.rowCount).toBe(0); |
| 792 | }); |
| 793 | |
| 794 | it("does not tokenize or publish a source that arrives after cancellation", async () => { |
| 795 | const controller = new AbortController(); |
| 796 | const started = Promise.withResolvers<void>(); |
| 797 | const response = Promise.withResolvers<{ oldContents: string; newContents: string }>(); |
| 798 | const chunks = vi.fn(); |
| 799 | const work = highlightNativeReviewDiffVisibleRows({ |
| 800 | rows: [sourceRow("add", 3)], |
| 801 | files: [ |
| 802 | { |
| 803 | ...TYPESCRIPT_FILE, |
| 804 | loadContents: () => { |
| 805 | started.resolve(); |
| 806 | return response.promise; |
| 807 | }, |
| 808 | }, |
| 809 | ], |
| 810 | scheme: "dark", |
| 811 | firstRowIndex: 0, |
| 812 | lastRowIndex: 0, |
| 813 | signal: controller.signal, |
| 814 | onChunk: chunks, |
| 815 | }); |
| 816 | await started.promise; |
| 817 | controller.abort(); |
| 818 | response.resolve({ oldContents: "", newContents: NEW_SOURCE.join("\n") }); |
| 819 | expect((await work).rowCount).toBe(0); |
| 820 | expect(tokenization.calls).toEqual([]); |
| 821 | expect(chunks).not.toHaveBeenCalled(); |
| 822 | }); |