The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { CodeViewHandle } from "@pierre/diffs/react"; |
| 2 | import { WorkerPoolManager } from "@pierre/diffs/worker"; |
| 3 | import { useState, type Ref } from "react"; |
| 4 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 5 | |
| 6 | import { act, render } from "../../../test/render"; |
| 7 | |
| 8 | vi.mock(import("../../hooks/useTheme"), async (original) => ({ |
| 9 | ...(await original()), |
| 10 | useRenderedAppearance: (() => "dark") as never, |
| 11 | })); |
| 12 | |
| 13 | import { buildFileDiffEntries, getRenderablePatch } from "../../lib/diffRendering"; |
| 14 | import { getDiffHeaderClickAction } from "./diffHeaderClick"; |
| 15 | import { StyledDiffCodeView } from "./StyledDiffCodeView"; |
| 16 | |
| 17 | /** Records each worker pool's initialization so the test can wait for highlighting. */ |
| 18 | function captureWorkerPools() { |
| 19 | const pools = new Map<WorkerPoolManager, Promise<void>>(); |
| 20 | const initialize = WorkerPoolManager.prototype.initialize; |
| 21 | const spy = vi.spyOn(WorkerPoolManager.prototype, "initialize").mockImplementation(function ( |
| 22 | this: WorkerPoolManager, |
| 23 | ...args |
| 24 | ) { |
| 25 | const pending = initialize.apply(this, args); |
| 26 | pools.set(this, pending); |
| 27 | return pending; |
| 28 | }); |
| 29 | return { pools, [Symbol.dispose]: () => spy.mockRestore() }; |
| 30 | } |
| 31 | |
| 32 | // Exercises the production click helper against Pierre's real shadow DOM. |
| 33 | function RepeatedDiffs({ |
| 34 | entries, |
| 35 | viewerRef, |
| 36 | onOpen, |
| 37 | }: { |
| 38 | entries: ReturnType<typeof buildFileDiffEntries>; |
| 39 | viewerRef: Ref<CodeViewHandle<undefined, undefined>>; |
| 40 | onOpen: (path: string) => void; |
| 41 | }) { |
| 42 | const [collapsed, setCollapsed] = useState<ReadonlySet<string>>(new Set()); |
| 43 | const toggle = (key: string) => |
| 44 | setCollapsed((current) => { |
| 45 | const next = new Set(current); |
| 46 | if (next.has(key)) next.delete(key); |
| 47 | else next.add(key); |
| 48 | return next; |
| 49 | }); |
| 50 | return ( |
| 51 | <div |
| 52 | onClickCapture={(event) => { |
| 53 | const action = getDiffHeaderClickAction(event.nativeEvent); |
| 54 | if (action?.type === "open") onOpen(action.filePath); |
| 55 | else if (action?.type === "toggle") toggle(action.fileKey); |
| 56 | }} |
| 57 | > |
| 58 | <StyledDiffCodeView |
| 59 | viewerRef={viewerRef} |
| 60 | className="h-[600px] w-[800px]" |
| 61 | items={entries.map(({ fileDiff, fileKey, fileVersion }) => ({ |
| 62 | type: "diff", |
| 63 | id: fileKey, |
| 64 | fileDiff, |
| 65 | version: fileVersion + Number(collapsed.has(fileKey)), |
| 66 | collapsed: collapsed.has(fileKey), |
| 67 | }))} |
| 68 | options={{ theme: "pierre-dark", diffStyle: "unified" }} |
| 69 | renderHeaderPrefix={(item) => ( |
| 70 | <button data-diff-file-key={item.id} onClick={() => toggle(item.id)}> |
| 71 | Chevron |
| 72 | </button> |
| 73 | )} |
| 74 | /> |
| 75 | </div> |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | describe("repeated diff blocks", () => { |
| 80 | it("renders every repeated block and collapses only the clicked one", async () => { |
| 81 | const parsed = getRenderablePatch( |
| 82 | ["first", "second", "second"] |
| 83 | .map((content) => |
| 84 | [ |
| 85 | "diff --git a/same.ts b/same.ts", |
| 86 | "--- a/same.ts", |
| 87 | "+++ b/same.ts", |
| 88 | "@@ -1 +1 @@", |
| 89 | "-before", |
| 90 | `+${content}`, |
| 91 | "", |
| 92 | ].join("\n"), |
| 93 | ) |
| 94 | .join(""), |
| 95 | ); |
| 96 | if (parsed?.kind !== "files") throw new Error("Expected file diffs"); |
| 97 | using captured = captureWorkerPools(); |
| 98 | const { pools } = captured; |
| 99 | const entries = buildFileDiffEntries(parsed.files); |
| 100 | const onOpen = vi.fn(); |
| 101 | let viewer: CodeViewHandle<undefined, undefined> | null = null; |
| 102 | await using _screen = await render( |
| 103 | <RepeatedDiffs |
| 104 | entries={entries} |
| 105 | viewerRef={(value) => { |
| 106 | viewer = value; |
| 107 | }} |
| 108 | onOpen={onOpen} |
| 109 | />, |
| 110 | // This viewer fixture does not navigate or read router state. |
| 111 | { router: false }, |
| 112 | ); |
| 113 | const pool = [...pools.keys()][0]!; |
| 114 | await act(async () => { |
| 115 | await pools.get(pool); |
| 116 | await Promise.all(parsed.files.map((file) => pool.primeDiffHighlightCache(file))); |
| 117 | }); |
| 118 | const flush = () => act(async () => viewer?.getInstance()?.render(true)); |
| 119 | await flush(); |
| 120 | const blocks = () => [...document.querySelectorAll("diffs-container")]; |
| 121 | const body = (index: number) => blocks()[index]!.shadowRoot!.querySelector("[data-diff]"); |
| 122 | const click = async (element: Element) => { |
| 123 | await act(async () => |
| 124 | element.dispatchEvent(new MouseEvent("click", { bubbles: true, composed: true })), |
| 125 | ); |
| 126 | await flush(); |
| 127 | }; |
| 128 | const expectExpanded = async (expanded: boolean[]) => { |
| 129 | await vi.waitFor(() => { |
| 130 | expect(blocks()).toHaveLength(3); |
| 131 | expect(blocks().map((_, index) => body(index) !== null)).toEqual(expanded); |
| 132 | }); |
| 133 | }; |
| 134 | await expectExpanded([true, true, true]); |
| 135 | await vi.waitFor(() => { |
| 136 | expect(body(0)?.textContent).toContain("first"); |
| 137 | expect(body(1)?.textContent).toContain("second"); |
| 138 | expect(body(2)?.textContent).toContain("second"); |
| 139 | }); |
| 140 | |
| 141 | await click(blocks()[1]!.shadowRoot!.querySelector("[data-diffs-header]")!); |
| 142 | await expectExpanded([true, false, true]); |
| 143 | await click(blocks()[1]!.shadowRoot!.querySelector("[data-title] bdi")!); |
| 144 | expect(onOpen).toHaveBeenCalledExactlyOnceWith("same.ts"); |
| 145 | await expectExpanded([true, false, true]); |
| 146 | await click(blocks()[1]!.querySelector("button")!); |
| 147 | await expectExpanded([true, true, true]); |
| 148 | await click(blocks()[0]!.shadowRoot!.querySelector("[data-diffs-header]")!); |
| 149 | await expectExpanded([false, true, true]); |
| 150 | }); |
| 151 | }); |