Open History to see the last commit
1import { DEFAULT_THEMES, preloadHighlighter } from "@pierre/diffs";
2import { Editor, type EditorFactory } from "@pierre/diffs/edit";
3import { EditProvider, File } from "@pierre/diffs/react";
4import { describe, expect, it, vi } from "vite-plus/test";
5
6import { render } from "../../../test/render";
7
8import { isMacPlatform } from "~/lib/utils";
9import { installFileEditorDismissal } from "./fileEditorDismissal";
10
11describe("installFileEditorDismissal", () => {
12 it("keeps a text selection made outside the file while its search is open", async () => {
13 let editor: Editor<"file" | "file-diff", undefined, undefined> | undefined;
14 const createEditor: EditorFactory<undefined, undefined> = (...args) =>
15 (editor = new Editor(...args));
16 // File paints nothing until the shared highlighter has loaded; once it has, mount renders synchronously.
17 await preloadHighlighter({ themes: Object.values(DEFAULT_THEMES), langs: ["typescript"] });
18 await using screen = await render(
19 <>
20 <p>Assistant reply</p>
21 <div data-testid="file-surface">
22 <EditProvider createEditor={createEditor}>
23 <File file={{ name: "server.ts", contents: "const server = 1;\n" }} edit />
24 </EditProvider>
25 </div>
26 </>,
27 );
28 const shadowRoot = () => screen.container.querySelector("diffs-container")?.shadowRoot;
29 const content = shadowRoot()?.querySelector<HTMLElement>("[data-content]");
30 expect(content).toBeTruthy();
31
32 editor!.setSelections([
33 { start: { line: 0, character: 8 }, end: { line: 0, character: 8 }, direction: "none" },
34 ]);
35 const isMac = isMacPlatform(navigator.platform);
36 content!.dispatchEvent(
37 new KeyboardEvent("keydown", {
38 key: "f",
39 code: "KeyF",
40 metaKey: isMac,
41 ctrlKey: !isMac,
42 bubbles: true,
43 composed: true,
44 cancelable: true,
45 }),
46 );
47 await vi.waitFor(() => expect(CSS.highlights.get("diffs-search-current")?.size).toBe(1));
48
49 using _dismissal = {
50 [Symbol.dispose]: installFileEditorDismissal({
51 root: screen.getByTestId("file-surface").element() as HTMLElement,
52 editor: editor!,
53 isBlocked: () => false,
54 onDismiss: () => {},
55 }),
56 };
57 const reply = screen.getByText("Assistant reply").element();
58 const range = document.createRange();
59 range.selectNodeContents(reply);
60 window.getSelection()!.removeAllRanges();
61 window.getSelection()!.addRange(range);
62 reply.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, composed: true }));
63
64 expect(window.getSelection()?.toString()).toBe("Assistant reply");
65 });
66
67 it("lets clicks beside the open search panel reach the file's code", async () => {
68 let editor: Editor<"file" | "file-diff", undefined, undefined> | undefined;
69 const createEditor: EditorFactory<undefined, undefined> = (...args) =>
70 (editor = new Editor(...args));
71 await preloadHighlighter({ themes: Object.values(DEFAULT_THEMES), langs: ["typescript"] });
72 const contents = Array.from(
73 { length: 12 },
74 (_, index) => `const server${index} = "a line long enough to run under the find bar";\n`,
75 ).join("");
76 await using screen = await render(
77 <div style={{ width: 600 }}>
78 <EditProvider createEditor={createEditor}>
79 <File file={{ name: "server.ts", contents }} edit />
80 </EditProvider>
81 </div>,
82 );
83 const shadowRoot = screen.container.querySelector("diffs-container")?.shadowRoot;
84 const content = shadowRoot?.querySelector<HTMLElement>("[data-content]");
85 expect(content).toBeTruthy();
86
87 editor!.setSelections([
88 { start: { line: 0, character: 8 }, end: { line: 0, character: 8 }, direction: "none" },
89 ]);
90 const isMac = isMacPlatform(navigator.platform);
91 content!.dispatchEvent(
92 new KeyboardEvent("keydown", {
93 key: "f",
94 code: "KeyF",
95 metaKey: isMac,
96 ctrlKey: !isMac,
97 bubbles: true,
98 composed: true,
99 cancelable: true,
100 }),
101 );
102 const widget = await vi.waitFor(() => {
103 const panelHost = [...shadowRoot!.querySelectorAll("*")].find((element) =>
104 element.shadowRoot?.querySelector("[data-search-panel]"),
105 );
106 const found = panelHost?.shadowRoot?.querySelector("[data-editor-widget]");
107 expect(found).toBeTruthy();
108 return found!;
109 });
110 const widgetBox = widget.getBoundingClientRect();
111 const beside = shadowRoot!.elementFromPoint(
112 widgetBox.left - 100,
113 widgetBox.top + widgetBox.height / 2,
114 );
115
116 expect(content!.contains(beside)).toBe(true);
117 });
118});