|
1 | | -import { describe, expect, it } from "vite-plus/test"; |
| 1 | +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
2 | 2 |
|
3 | 3 | import type { NativeReviewDiffRow } from "./nativeReviewDiffSurface"; |
4 | 4 | import type { NativeReviewDiffFile } from "./nativeReviewDiffTypes"; |
5 | | -import { highlightNativeReviewDiffVisibleRows } from "./nativeReviewDiffHighlighter"; |
| 5 | +import { |
| 6 | + highlightNativeReviewDiffVisibleRows, |
| 7 | + streamNativeReviewDiffTokens, |
| 8 | + type NativeReviewDiffTokenChunk, |
| 9 | +} from "./nativeReviewDiffHighlighter"; |
| 10 | + |
| 11 | +const tokenization = vi.hoisted(() => ({ |
| 12 | + calls: [] as string[], |
| 13 | + afterCall: undefined as (() => void) | undefined, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("@shikijs/core", async (importOriginal) => { |
| 17 | + const original = await importOriginal<typeof import("@shikijs/core")>(); |
| 18 | + return { |
| 19 | + ...original, |
| 20 | + createHighlighterCore: async (...args: Parameters<typeof original.createHighlighterCore>) => { |
| 21 | + const highlighter = await original.createHighlighterCore(...args); |
| 22 | + return { |
| 23 | + ...highlighter, |
| 24 | + codeToTokensBase: (...input: Parameters<typeof highlighter.codeToTokensBase>) => { |
| 25 | + tokenization.calls.push(input[0]); |
| 26 | + const result = highlighter.codeToTokensBase(...input); |
| 27 | + tokenization.afterCall?.(); |
| 28 | + return result; |
| 29 | + }, |
| 30 | + }; |
| 31 | + }, |
| 32 | + }; |
| 33 | +}); |
| 34 | + |
| 35 | +// Exercise the native entry path without requiring an iOS or Android runtime. |
| 36 | +vi.mock("react-native-shiki-engine", async () => { |
| 37 | + const { createJavaScriptRegexEngine } = await import("@shikijs/engine-javascript"); |
| 38 | + return { isNativeEngineAvailable: () => true, createNativeEngine: createJavaScriptRegexEngine }; |
| 39 | +}); |
| 40 | + |
| 41 | +afterEach(() => { |
| 42 | + tokenization.calls = []; |
| 43 | + tokenization.afterCall = undefined; |
| 44 | +}); |
6 | 45 |
|
7 | 46 | const TYPESCRIPT_FILE: NativeReviewDiffFile = { |
8 | 47 | id: "file-1", |
@@ -187,3 +226,113 @@ describe("highlightNativeReviewDiffVisibleRows", () => { |
187 | 226 | ); |
188 | 227 | }); |
189 | 228 | }); |
| 229 | + |
| 230 | +describe.each(["native", "javascript"] as const)("%s highlighting budgets", (engine) => { |
| 231 | + const highlightRows = (rows: ReadonlyArray<NativeReviewDiffRow>, signal?: AbortSignal) => |
| 232 | + highlightNativeReviewDiffVisibleRows({ |
| 233 | + rows, |
| 234 | + files: [TYPESCRIPT_FILE], |
| 235 | + scheme: "dark", |
| 236 | + engine, |
| 237 | + firstRowIndex: 0, |
| 238 | + lastRowIndex: rows.length - 1, |
| 239 | + overscanRows: 0, |
| 240 | + signal, |
| 241 | + }); |
| 242 | + |
| 243 | + const line = (id: number, content: string) => |
| 244 | + makeLine({ |
| 245 | + id: `line-${id}`, |
| 246 | + content, |
| 247 | + change: "add", |
| 248 | + oldLineNumber: null, |
| 249 | + newLineNumber: id, |
| 250 | + }); |
| 251 | + |
| 252 | + it("still highlights a line at the length limit", async () => { |
| 253 | + const content = `// ${"x".repeat(997)}`; |
| 254 | + const result = await highlightRows([line(1, content)]); |
| 255 | + |
| 256 | + expect(tokenization.calls).toEqual([content]); |
| 257 | + expect(result.tokensByRowId["line-1"]?.some((token) => token.color !== null)).toBe(true); |
| 258 | + }); |
| 259 | + |
| 260 | + it("keeps long lines and unknown following syntax plain until the next hunk", async () => { |
| 261 | + const longLine = `${"x".repeat(1_001)} /*`; |
| 262 | + const rows = [ |
| 263 | + line(1, "export const before = 1;"), |
| 264 | + line(2, longLine), |
| 265 | + { kind: "comment", id: "note", commentText: "Check this", fileId: TYPESCRIPT_FILE.id }, |
| 266 | + line(3, "inside the comment */"), |
| 267 | + makeHunk("next-hunk"), |
| 268 | + line(100, "export const after = 2;"), |
| 269 | + ] satisfies ReadonlyArray<NativeReviewDiffRow>; |
| 270 | + const result = await highlightRows(rows); |
| 271 | + |
| 272 | + expect(result.engine).toBe(engine); |
| 273 | + expect(Object.keys(result.tokensByRowId)).toEqual(["line-1", "line-2", "line-3", "line-100"]); |
| 274 | + expect(result.tokensByRowId["line-2"]).toEqual([ |
| 275 | + { content: longLine, color: null, fontStyle: null }, |
| 276 | + ]); |
| 277 | + expect(result.tokensByRowId["line-3"]).toEqual([ |
| 278 | + { content: "inside the comment */", color: null, fontStyle: null }, |
| 279 | + ]); |
| 280 | + expect(result.tokensByRowId["line-1"]?.some((token) => token.color !== null)).toBe(true); |
| 281 | + expect(result.tokensByRowId["line-100"]?.some((token) => token.color !== null)).toBe(true); |
| 282 | + expect(tokenization.calls.some((code) => code.includes(longLine))).toBe(false); |
| 283 | + }); |
| 284 | + |
| 285 | + it("preserves multiline grammar and row mapping across character-limited batches", async () => { |
| 286 | + const opening = line(1, "const message = `open"); |
| 287 | + const body = Array.from({ length: 40 }, (_, index) => line(index + 2, "inside ".repeat(45))); |
| 288 | + const closing = line(42, "closed`; "); |
| 289 | + const trailing = line(43, "export const after = 2;"); |
| 290 | + const result = await highlightRows([opening, ...body, closing, trailing]); |
| 291 | + const calls = [...tokenization.calls]; |
| 292 | + const expected = await highlightRows([ |
| 293 | + opening, |
| 294 | + { ...closing, newLineNumber: 2 }, |
| 295 | + { ...trailing, newLineNumber: 3 }, |
| 296 | + ]); |
| 297 | + |
| 298 | + expect(calls.length).toBeGreaterThan(1); |
| 299 | + expect(calls.every((code) => code.length <= 8_000)).toBe(true); |
| 300 | + expect(result.rowCount).toBe(43); |
| 301 | + for (const row of [opening, ...body, closing, trailing]) { |
| 302 | + expect(result.tokensByRowId[row.id]?.map((token) => token.content).join("")).toBe( |
| 303 | + row.content, |
| 304 | + ); |
| 305 | + } |
| 306 | + expect(result.tokensByRowId[closing.id]).toEqual(expected.tokensByRowId[closing.id]); |
| 307 | + expect(result.tokensByRowId[trailing.id]).toEqual(expected.tokensByRowId[trailing.id]); |
| 308 | + }); |
| 309 | + |
| 310 | + it("stops before later batches and publishes no partial result after cancellation", async () => { |
| 311 | + const controller = new AbortController(); |
| 312 | + tokenization.afterCall = () => controller.abort(); |
| 313 | + const rows = Array.from({ length: 30 }, (_, index) => line(index + 1, `// ${"x".repeat(400)}`)); |
| 314 | + |
| 315 | + const result = await highlightRows(rows, controller.signal); |
| 316 | + |
| 317 | + expect(tokenization.calls).toHaveLength(1); |
| 318 | + expect(result.rowCount).toBe(0); |
| 319 | + expect(result.tokensByRowId).toEqual({}); |
| 320 | + }); |
| 321 | + |
| 322 | + it("applies the same long-line guard to streamed token chunks", async () => { |
| 323 | + const content = "x".repeat(10_000); |
| 324 | + const chunks: NativeReviewDiffTokenChunk[] = []; |
| 325 | + |
| 326 | + await streamNativeReviewDiffTokens({ |
| 327 | + rows: [line(1, content)], |
| 328 | + files: [TYPESCRIPT_FILE], |
| 329 | + scheme: "dark", |
| 330 | + engine, |
| 331 | + onChunk: (chunk) => chunks.push(chunk), |
| 332 | + }); |
| 333 | + |
| 334 | + expect(chunks).toHaveLength(1); |
| 335 | + expect(chunks[0]?.tokensByRowId["line-1"]).toEqual([{ content, color: null, fontStyle: null }]); |
| 336 | + expect(tokenization.calls).toHaveLength(0); |
| 337 | + }); |
| 338 | +}); |
0 commit comments