The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { ELEMENT_PICKED_CHANNEL, START_PICK_CHANNEL } from "./GuestProtocol.ts"; |
| 4 | import "./PickPreload.ts"; |
| 5 | |
| 6 | const ipcListeners = vi.hoisted(() => new Map<string, Array<(...args: unknown[]) => void>>()); |
| 7 | const ipcSend = vi.hoisted(() => ({ calls: [] as unknown[][] })); |
| 8 | |
| 9 | vi.mock("electron", () => ({ |
| 10 | ipcRenderer: { |
| 11 | on: (channel: string, listener: (...args: unknown[]) => void) => { |
| 12 | ipcListeners.set(channel, [...(ipcListeners.get(channel) ?? []), listener]); |
| 13 | }, |
| 14 | off: () => {}, |
| 15 | send: (...args: unknown[]) => { |
| 16 | ipcSend.calls.push(args); |
| 17 | }, |
| 18 | invoke: async () => undefined, |
| 19 | }, |
| 20 | })); |
| 21 | |
| 22 | vi.mock("react-grab/primitives", () => ({ getElementContext: async () => null })); |
| 23 | |
| 24 | const startAnnotation = (): ShadowRoot => { |
| 25 | vi.stubGlobal( |
| 26 | "ResizeObserver", |
| 27 | class { |
| 28 | observe() {} |
| 29 | disconnect() {} |
| 30 | }, |
| 31 | ); |
| 32 | // jsdom has no popover API. |
| 33 | HTMLElement.prototype.showPopover ??= () => {}; |
| 34 | HTMLElement.prototype.hidePopover ??= () => {}; |
| 35 | using attachShadow = vi.spyOn(Element.prototype, "attachShadow"); |
| 36 | for (const listener of ipcListeners.get(START_PICK_CHANNEL) ?? []) listener({}, undefined, false); |
| 37 | const shadowRoot = attachShadow.mock.results[0]?.value; |
| 38 | if (!(shadowRoot instanceof ShadowRoot)) throw new Error("annotation overlay did not start"); |
| 39 | return shadowRoot; |
| 40 | }; |
| 41 | |
| 42 | it("switches tools from the keyboard after a toolbar button is clicked", () => { |
| 43 | const shadowRoot = startAnnotation(); |
| 44 | const region = Array.from(shadowRoot.querySelectorAll("button")).find( |
| 45 | (button) => button.textContent === "Region", |
| 46 | ); |
| 47 | if (!region) throw new Error("Region toolbar button not found"); |
| 48 | |
| 49 | region.focus(); |
| 50 | region.click(); |
| 51 | expect(document.documentElement.getAttribute("data-t3code-annotation-tool")).toBe("marquee"); |
| 52 | |
| 53 | region.dispatchEvent(new KeyboardEvent("keydown", { key: "v", bubbles: true, composed: true })); |
| 54 | |
| 55 | expect( |
| 56 | document.documentElement.getAttribute("data-t3code-annotation-tool"), |
| 57 | "V should switch to Select while focus is on a toolbar button", |
| 58 | ).toBe("select"); |
| 59 | }); |
| 60 | |
| 61 | it("keeps the tool while typing a tool key in the comment", () => { |
| 62 | const shadowRoot = startAnnotation(); |
| 63 | const comment = shadowRoot.querySelector("textarea"); |
| 64 | if (!comment) throw new Error("comment textarea not found"); |
| 65 | |
| 66 | comment.focus(); |
| 67 | comment.dispatchEvent(new KeyboardEvent("keydown", { key: "r", bubbles: true, composed: true })); |
| 68 | |
| 69 | expect(document.documentElement.getAttribute("data-t3code-annotation-tool")).toBe("select"); |
| 70 | }); |
| 71 | |
| 72 | it("keeps comment keystrokes away from the page's capture-phase shortcuts", () => { |
| 73 | const pageShortcut = vi.fn((event: KeyboardEvent) => event.preventDefault()); |
| 74 | window.addEventListener("keydown", pageShortcut, { capture: true }); |
| 75 | const shadowRoot = startAnnotation(); |
| 76 | const comment = shadowRoot.querySelector("textarea"); |
| 77 | if (!comment) throw new Error("comment textarea not found"); |
| 78 | |
| 79 | comment.focus(); |
| 80 | const space = new KeyboardEvent("keydown", { |
| 81 | key: " ", |
| 82 | code: "Space", |
| 83 | bubbles: true, |
| 84 | cancelable: true, |
| 85 | composed: true, |
| 86 | }); |
| 87 | comment.dispatchEvent(space); |
| 88 | window.removeEventListener("keydown", pageShortcut, { capture: true }); |
| 89 | |
| 90 | expect(pageShortcut).not.toHaveBeenCalled(); |
| 91 | expect(space.defaultPrevented).toBe(false); |
| 92 | }); |
| 93 | |
| 94 | it("keeps the annotation when Escape dismisses an IME candidate in the comment", () => { |
| 95 | const shadowRoot = startAnnotation(); |
| 96 | const comment = shadowRoot.querySelector("textarea"); |
| 97 | if (!comment) throw new Error("comment textarea not found"); |
| 98 | |
| 99 | comment.value = "draft"; |
| 100 | comment.focus(); |
| 101 | comment.dispatchEvent( |
| 102 | new KeyboardEvent("keydown", { |
| 103 | key: "Escape", |
| 104 | isComposing: true, |
| 105 | bubbles: true, |
| 106 | cancelable: true, |
| 107 | composed: true, |
| 108 | }), |
| 109 | ); |
| 110 | |
| 111 | expect(shadowRoot.host.isConnected).toBe(true); |
| 112 | expect(comment.value).toBe("draft"); |
| 113 | }); |
| 114 | |
| 115 | it.each([ |
| 116 | { scrolled: false, rect: { x: 40, y: 60, width: 200, height: 120 } }, |
| 117 | // The last rect would point at whatever now sits there, so no crop beats a wrong one. |
| 118 | { scrolled: true, rect: { x: 0, y: 0, width: 0, height: 0 } }, |
| 119 | ])( |
| 120 | "submits a replaced element's last rect unless the page scrolled ($scrolled)", |
| 121 | async ({ scrolled, rect }) => { |
| 122 | ipcSend.calls.length = 0; |
| 123 | const card = document.body.appendChild(document.createElement("div")); |
| 124 | const pickedRect = new DOMRect(40, 60, 200, 120); |
| 125 | // Browsers report a detached element at 0x0 in the corner. |
| 126 | using _rect = vi |
| 127 | .spyOn(card, "getBoundingClientRect") |
| 128 | .mockImplementation(() => (card.isConnected ? pickedRect : new DOMRect())); |
| 129 | document.elementsFromPoint = () => [card]; |
| 130 | using listeners = vi.spyOn(HTMLButtonElement.prototype, "addEventListener"); |
| 131 | const shadowRoot = startAnnotation(); |
| 132 | |
| 133 | card.dispatchEvent( |
| 134 | new PointerEvent("pointerdown", { button: 0, clientX: 100, clientY: 100, bubbles: true }), |
| 135 | ); |
| 136 | // A polling page re-renders while the comment is written and replaces the picked node. |
| 137 | card.replaceWith(document.createElement("div")); |
| 138 | if (scrolled) window.dispatchEvent(new Event("scroll")); |
| 139 | const attach = Array.from(shadowRoot.querySelectorAll("button")).find( |
| 140 | (button) => button.textContent === "Attach", |
| 141 | ); |
| 142 | if (!attach) throw new Error("Attach button not found"); |
| 143 | attach.click(); |
| 144 | expect(attach.disabled).toBe(false); |
| 145 | expect(ipcSend.calls.some(([channel]) => channel === ELEMENT_PICKED_CHANNEL)).toBe(false); |
| 146 | const listenerIndex = listeners.mock.contexts.findIndex( |
| 147 | (target, index) => target === attach && listeners.mock.calls[index]?.[0] === "click", |
| 148 | ); |
| 149 | const listener = listeners.mock.calls[listenerIndex]?.[1]; |
| 150 | if (typeof listener !== "function") throw new Error("Attach click listener not found"); |
| 151 | // The DOM test environment has no native input source. |
| 152 | listener.call(attach, { isTrusted: true } as MouseEvent); |
| 153 | |
| 154 | await vi.waitFor(() => |
| 155 | expect(ipcSend.calls.some(([channel]) => channel === ELEMENT_PICKED_CHANNEL)).toBe(true), |
| 156 | ); |
| 157 | const [, annotation] = |
| 158 | ipcSend.calls.find(([channel]) => channel === ELEMENT_PICKED_CHANNEL) ?? []; |
| 159 | expect((annotation as { elements: Array<{ rect: unknown }> }).elements[0]?.rect).toEqual(rect); |
| 160 | }, |
| 161 | ); |