|
1 | 1 | import { |
| 2 | + CodeView, |
| 3 | + FileDiff, |
2 | 4 | FileRenderer, |
3 | 5 | getSharedHighlighter, |
| 6 | + type CodeViewItem, |
4 | 7 | type CodeViewScrollTarget, |
5 | 8 | type FileContents, |
6 | 9 | } from "@pierre/diffs"; |
7 | 10 | import { useWorkerPool, type CodeViewProps } from "@pierre/diffs/react"; |
8 | 11 | import { WorkerPoolManager, type WorkerRequest, type WorkerResponse } from "@pierre/diffs/worker"; |
| 12 | +import DiffsWorker from "@pierre/diffs/worker/worker.js?worker"; |
| 13 | +import { getDiffLineStat, getRenderablePatch } from "../../lib/diffRendering"; |
9 | 14 | import { |
10 | 15 | act, |
11 | 16 | StrictMode, |
@@ -305,6 +310,157 @@ describe("code-view worker lifecycle", () => { |
305 | 310 | }); |
306 | 311 | }); |
307 | 312 |
|
| 313 | +describe("collapsed diff content", () => { |
| 314 | + let pool: WorkerPoolManager; |
| 315 | + let container: HTMLDivElement; |
| 316 | + let viewer: FileDiff<string> | CodeView<string> | undefined; |
| 317 | + |
| 318 | + const loadedDiff = () => { |
| 319 | + const parsed = getRenderablePatch( |
| 320 | + [ |
| 321 | + "diff --git a/before.ts b/after.ts", |
| 322 | + "similarity index 50%", |
| 323 | + "rename from before.ts", |
| 324 | + "rename to after.ts", |
| 325 | + "--- a/before.ts", |
| 326 | + "+++ b/after.ts", |
| 327 | + "@@ -1,2 +1,2 @@", |
| 328 | + " const shared = 1;", |
| 329 | + "-const before = 2;", |
| 330 | + "+const after = 3;", |
| 331 | + ].join("\n"), |
| 332 | + "collapsed-content", |
| 333 | + ); |
| 334 | + if (parsed?.kind !== "files" || !parsed.files[0]) throw new Error("Expected diff"); |
| 335 | + return parsed.files[0]; |
| 336 | + }; |
| 337 | + const code = () => |
| 338 | + [...container.querySelectorAll("diffs-container")] |
| 339 | + .flatMap((element) => [...(element.shadowRoot?.querySelectorAll("[data-line]") ?? [])]) |
| 340 | + .map((line) => line.textContent) |
| 341 | + .join("\n"); |
| 342 | + |
| 343 | + beforeEach(async () => { |
| 344 | + testState.requests = []; |
| 345 | + testState.failWorkers = false; |
| 346 | + testState.holdInitialization = false; |
| 347 | + pool = new WorkerPoolManager( |
| 348 | + { workerFactory: () => new DiffsWorker(), poolSize: 1 }, |
| 349 | + { theme: "pierre-dark", langs: ["typescript"], preferredHighlighter: "shiki-wasm" }, |
| 350 | + ); |
| 351 | + await pool.initialize(); |
| 352 | + container = document.createElement("div"); |
| 353 | + container.style.cssText = "height: 400px; width: 800px; overflow: auto"; |
| 354 | + document.body.append(container); |
| 355 | + }); |
| 356 | + |
| 357 | + afterEach(() => { |
| 358 | + viewer?.cleanUp(); |
| 359 | + viewer = undefined; |
| 360 | + pool.terminate(); |
| 361 | + container.remove(); |
| 362 | + vi.restoreAllMocks(); |
| 363 | + }); |
| 364 | + |
| 365 | + it("defers plain AST and highlight work across collapsed renders until expansion", async () => { |
| 366 | + const fileDiff = loadedDiff(); |
| 367 | + const highlight = vi.spyOn(pool, "highlightDiffAST"); |
| 368 | + const plainAST = vi.spyOn(pool, "getPlainDiffAST"); |
| 369 | + const options = { theme: "pierre-dark", collapsed: true, disableErrorHandling: true } as const; |
| 370 | + const file = new FileDiff<string>(options, pool); |
| 371 | + viewer = file; |
| 372 | + file.render({ fileDiff, containerWrapper: container }); |
| 373 | + file.render({ fileDiff, forceRender: true }); |
| 374 | + expect.soft(highlight.mock.calls.length).toBe(0); |
| 375 | + expect.soft(plainAST.mock.calls.length).toBe(0); |
| 376 | + expect(code()).toBe(""); |
| 377 | + expect(container.querySelector("diffs-container")?.shadowRoot?.textContent).toContain( |
| 378 | + "after.ts", |
| 379 | + ); |
| 380 | + |
| 381 | + file.setOptions({ ...options, collapsed: false }); |
| 382 | + file.render({ fileDiff, forceRender: true }); |
| 383 | + expect(highlight).toHaveBeenCalled(); |
| 384 | + await pool.primeDiffHighlightCache(fileDiff); |
| 385 | + await vi.waitFor(() => expect(code()).toContain("const after = 3;")); |
| 386 | + expect(code()).toContain("const before = 2;"); |
| 387 | + expect(testState.requests.filter((request) => request.type === "diff")).toHaveLength(1); |
| 388 | + }); |
| 389 | + |
| 390 | + it("finds newly loaded collapsed lines and unfolds the match with its annotation and header", async () => { |
| 391 | + const fileDiff = loadedDiff(); |
| 392 | + const item: CodeViewItem<string> = { |
| 393 | + type: "diff", |
| 394 | + id: "renamed", |
| 395 | + fileDiff, |
| 396 | + collapsed: true, |
| 397 | + annotations: [{ side: "additions", lineNumber: 2, metadata: "Review this change" }], |
| 398 | + }; |
| 399 | + const view = new CodeView<string>( |
| 400 | + { |
| 401 | + theme: "pierre-dark", |
| 402 | + disableErrorHandling: true, |
| 403 | + onRevealSearchMatch: (match) => |
| 404 | + view.setItems([{ ...match, collapsed: false, version: (match.version ?? 0) + 1 }]), |
| 405 | + renderHeaderMetadata: (file) => { |
| 406 | + if (!("hunks" in file)) return; |
| 407 | + const metadata = document.createElement("span"); |
| 408 | + const { additions, deletions } = getDiffLineStat([file]); |
| 409 | + metadata.textContent = "+" + additions + " / -" + deletions; |
| 410 | + return metadata; |
| 411 | + }, |
| 412 | + renderAnnotation: (annotation) => { |
| 413 | + const note = document.createElement("span"); |
| 414 | + note.textContent = annotation.metadata ?? ""; |
| 415 | + return note; |
| 416 | + }, |
| 417 | + }, |
| 418 | + pool, |
| 419 | + ); |
| 420 | + viewer = view; |
| 421 | + view.setup(container); |
| 422 | + view.openSearch(); |
| 423 | + const search = container.querySelector("[data-diffs-search]")!.shadowRoot!; |
| 424 | + const input = search.querySelector<HTMLInputElement>("input[data-search]")!; |
| 425 | + input.value = "const after"; |
| 426 | + input.dispatchEvent(new Event("input", { bubbles: true })); |
| 427 | + view.setItems([item]); |
| 428 | + view.render(true); |
| 429 | + expect(code()).toBe(""); |
| 430 | + expect(search.textContent).toContain("1 results"); |
| 431 | + expect(container.textContent).toContain("+1 / -1"); |
| 432 | + |
| 433 | + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); |
| 434 | + view.render(true); |
| 435 | + await vi.waitFor(() => expect(code()).toContain("const after = 3;")); |
| 436 | + expect(search.textContent).toContain("1 of 1"); |
| 437 | + expect(container.textContent).toContain("Review this change"); |
| 438 | + expect(container.textContent).toContain("+1 / -1"); |
| 439 | + expect(container.querySelector("diffs-container")?.shadowRoot?.textContent).toContain( |
| 440 | + "after.ts", |
| 441 | + ); |
| 442 | + |
| 443 | + await pool.primeDiffHighlightCache(fileDiff); |
| 444 | + view.render(true); |
| 445 | + const highlight = vi.spyOn(pool, "highlightDiffAST"); |
| 446 | + const plainAST = vi.spyOn(pool, "getPlainDiffAST"); |
| 447 | + const updatedDiff = { ...fileDiff, name: "updated.ts", cacheKey: "collapsed-update" }; |
| 448 | + view.setItems([{ ...item, fileDiff: updatedDiff, version: 2 }]); |
| 449 | + view.render(true); |
| 450 | + expect(highlight).not.toHaveBeenCalled(); |
| 451 | + expect(plainAST).not.toHaveBeenCalled(); |
| 452 | + expect(code()).toBe(""); |
| 453 | + expect(search.textContent).toContain("1 of 1"); |
| 454 | + expect(container.querySelector("diffs-container")?.shadowRoot?.textContent).toContain( |
| 455 | + "updated.ts", |
| 456 | + ); |
| 457 | + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); |
| 458 | + view.render(true); |
| 459 | + await vi.waitFor(() => expect(code()).toContain("const after = 3;")); |
| 460 | + expect(container.textContent).toContain("Review this change"); |
| 461 | + }); |
| 462 | +}); |
| 463 | + |
308 | 464 | type RevealHandle = NonNullable<Parameters<typeof useCodeViewFileReveal>[0]>; |
309 | 465 | const revealCalls: { |
310 | 466 | scope: string; |
|
0 commit comments