Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AssistantSelectionToolbar.browser.test.tsx

144 lines5.1 KB

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