|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts"; |
| 3 | +import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection"; |
| 4 | +import { act } from "react"; |
| 5 | +import { createRoot, type Root } from "react-dom/client"; |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 7 | +import { AssistantSelectionToolbar } from "./AssistantSelectionToolbar"; |
| 8 | + |
| 9 | +let root: Root | undefined; |
| 10 | +beforeEach(() => vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true)); |
| 11 | +afterEach(() => { |
| 12 | + act(() => root?.unmount()); |
| 13 | + root = undefined; |
| 14 | + document.body.replaceChildren(); |
| 15 | + vi.restoreAllMocks(); |
| 16 | + vi.unstubAllGlobals(); |
| 17 | +}); |
| 18 | + |
| 19 | +function setup() { |
| 20 | + const viewport = document.createElement("div"); |
| 21 | + const source = document.createElement("div"); |
| 22 | + source.dataset.assistantCitationSource = "render-tool-item"; |
| 23 | + const frame = document.createElement("iframe"); |
| 24 | + frame.dataset.htmlSelectionBridge = "true"; |
| 25 | + source.append(frame); |
| 26 | + viewport.append(source); |
| 27 | + const mount = document.createElement("div"); |
| 28 | + document.body.append(viewport, mount); |
| 29 | + vi.spyOn(viewport, "getBoundingClientRect").mockReturnValue(new DOMRect(0, 0, 600, 500)); |
| 30 | + vi.spyOn(frame, "getBoundingClientRect").mockReturnValue(new DOMRect(100, 40, 400, 300)); |
| 31 | + const onCite = vi.fn< |
| 32 | + (citation: AssistantCitation, anchor: AssistantCitationSourceAnchor) => boolean |
| 33 | + >(() => true); |
| 34 | + root = createRoot(mount); |
| 35 | + act(() => |
| 36 | + root!.render( |
| 37 | + <AssistantSelectionToolbar |
| 38 | + viewport={viewport} |
| 39 | + threadRef={{ environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") }} |
| 40 | + onCite={onCite} |
| 41 | + />, |
| 42 | + ), |
| 43 | + ); |
| 44 | + frame.focus(); |
| 45 | + const quote = { text: "quoted text", start: 7, end: 18, prefix: "Before ", suffix: " after." }; |
| 46 | + const data = { |
| 47 | + jsonrpc: "2.0", |
| 48 | + method: "t3/selection", |
| 49 | + params: { |
| 50 | + selector: quote, |
| 51 | + rect: { left: 10, top: 20, width: 80, height: 15 }, |
| 52 | + pointer: { x: 70, y: 35 }, |
| 53 | + }, |
| 54 | + }; |
| 55 | + const send = (payload: unknown = data, origin: Window | null = frame.contentWindow) => |
| 56 | + act(() => { |
| 57 | + window.dispatchEvent(new MessageEvent("message", { source: origin, data: payload })); |
| 58 | + }); |
| 59 | + return { frame, onCite, quote, data, send }; |
| 60 | +} |
| 61 | + |
| 62 | +describe("HTML selection Cite toolbar", () => { |
| 63 | + it("adds the iframe quote to the composer with a source anchor in client coordinates", () => { |
| 64 | + const { frame, onCite, quote, send } = setup(); |
| 65 | + const post = vi.spyOn(frame.contentWindow!, "postMessage"); |
| 66 | + send(); |
| 67 | + const button = document.querySelector<HTMLButtonElement>( |
| 68 | + '[aria-label="Cite selection in composer"]', |
| 69 | + )!; |
| 70 | + expect(button).not.toBeNull(); |
| 71 | + expect(button.style.left).toBe("170px"); |
| 72 | + expect(button.style.top).toBe("75px"); |
| 73 | + act(() => button.click()); |
| 74 | + expect(onCite).toHaveBeenCalledOnce(); |
| 75 | + const [citation, anchor] = onCite.mock.calls[0]!; |
| 76 | + expect(citation).toEqual({ |
| 77 | + version: 1, |
| 78 | + environmentId: "env", |
| 79 | + threadId: "thread", |
| 80 | + messageId: MessageId.make("render-tool-item"), |
| 81 | + ...quote, |
| 82 | + }); |
| 83 | + expect(anchor.htmlRender).toBe(frame); |
| 84 | + expect(anchor.range.getBoundingClientRect()).toEqual(new DOMRect(110, 60, 80, 15)); |
| 85 | + expect(post).toHaveBeenCalledWith( |
| 86 | + { method: "t3/selection-command", params: { action: "clear", selector: undefined } }, |
| 87 | + "*", |
| 88 | + ); |
| 89 | + expect(document.querySelector('[aria-label="Cite selection in composer"]')).toBeNull(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("ignores messages from unrelated or unfocused frames", () => { |
| 93 | + const { frame, data, send } = setup(); |
| 94 | + send(data, window); |
| 95 | + expect(document.querySelector('[aria-label="Cite selection in composer"]')).toBeNull(); |
| 96 | + frame.blur(); |
| 97 | + send(); |
| 98 | + expect(document.querySelector('[aria-label="Cite selection in composer"]')).toBeNull(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("offers shortening for oversized selections and dismisses on iframe interaction", () => { |
| 102 | + const { onCite, data, send } = setup(); |
| 103 | + send({ ...data, params: { tooLong: true, rect: data.params.rect, pointer: null } }); |
| 104 | + const button = document.querySelector<HTMLButtonElement>( |
| 105 | + '[aria-label="Selection is too long to cite"]', |
| 106 | + )!; |
| 107 | + expect(button.disabled).toBe(true); |
| 108 | + act(() => button.click()); |
| 109 | + expect(onCite).not.toHaveBeenCalled(); |
| 110 | + send({ ...data, params: null }); |
| 111 | + expect(document.querySelector('[aria-label="Selection is too long to cite"]')).toBeNull(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments