|
| 1 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | +import { useState, type ComponentProps } from "react"; |
| 3 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 4 | +import { page, userEvent } from "vite-plus/test/browser"; |
| 5 | + |
| 6 | +import { render } from "../../../test/render"; |
| 7 | +import { AssistantSelectionToolbar } from "./AssistantSelectionToolbar"; |
| 8 | +import type { ChatComposerHandle } from "./ChatComposer"; |
| 9 | +import { useChatShortcuts } from "./useChatShortcuts"; |
| 10 | + |
| 11 | +type OnCite = ComponentProps<typeof AssistantSelectionToolbar>["onCite"]; |
| 12 | + |
| 13 | +const threadRef = { |
| 14 | + environmentId: EnvironmentId.make("environment"), |
| 15 | + threadId: ThreadId.make("thread"), |
| 16 | +}; |
| 17 | + |
| 18 | +// The chat's window capture listener runs before the toolbar's document listener, so |
| 19 | +// type-to-focus sees every key first. The stand-in composer records what it receives. |
| 20 | +function ChatShortcuts(props: { composerKeys: string[] }) { |
| 21 | + useChatShortcuts({ |
| 22 | + enabled: true, |
| 23 | + keybindings: [], |
| 24 | + scriptKeybindings: [], |
| 25 | + getShortcutContext: () => ({ |
| 26 | + terminalFocus: false, |
| 27 | + terminalOpen: false, |
| 28 | + previewFocus: false, |
| 29 | + previewOpen: false, |
| 30 | + isWeb: true, |
| 31 | + isDesktop: false, |
| 32 | + modelPickerOpen: false, |
| 33 | + }), |
| 34 | + threadFindActive: false, |
| 35 | + composerRef: { |
| 36 | + current: { |
| 37 | + insertTextAtEnd: (key: string) => { |
| 38 | + props.composerKeys.push(key); |
| 39 | + return true; |
| 40 | + }, |
| 41 | + } as Partial<ChatComposerHandle> as ChatComposerHandle, |
| 42 | + }, |
| 43 | + branchToolbarRef: { current: null }, |
| 44 | + closeThreadFind: () => {}, |
| 45 | + commands: {}, |
| 46 | + fallback: () => undefined, |
| 47 | + }); |
| 48 | + return null; |
| 49 | +} |
| 50 | + |
| 51 | +function Transcript(props: { onCite: OnCite; composerKeys?: string[] }) { |
| 52 | + const [viewport, setViewport] = useState<HTMLElement | null>(null); |
| 53 | + return ( |
| 54 | + <> |
| 55 | + {props.composerKeys ? <ChatShortcuts composerKeys={props.composerKeys} /> : null} |
| 56 | + <input aria-label="Find in thread" /> |
| 57 | + <div ref={setViewport}> |
| 58 | + <div data-assistant-citation-source="message-1"> |
| 59 | + <p>Render the citation chip</p> |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + <AssistantSelectionToolbar viewport={viewport} threadRef={threadRef} onCite={props.onCite} /> |
| 63 | + </> |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +// router: false because the transcript never navigates. |
| 68 | +const options = { router: false } as const; |
| 69 | +const citeButton = page.getByRole("button", { name: "Cite selection in composer" }); |
| 70 | + |
| 71 | +async function selectAssistantText(container: HTMLElement) { |
| 72 | + const text = container.querySelector("p")!.firstChild!; |
| 73 | + window.getSelection()!.setBaseAndExtent(text, 7, text, 15); |
| 74 | + await expect.element(citeButton).toBeVisible(); |
| 75 | +} |
| 76 | + |
| 77 | +describe("AssistantSelectionToolbar", () => { |
| 78 | + it.each(["r", "R"])("cites the selection when %s is pressed", async (key) => { |
| 79 | + const onCite = vi.fn<OnCite>(() => true); |
| 80 | + await using screen = await render(<Transcript onCite={onCite} />, options); |
| 81 | + await selectAssistantText(screen.container); |
| 82 | + |
| 83 | + await userEvent.keyboard(key); |
| 84 | + |
| 85 | + expect(onCite).toHaveBeenCalledOnce(); |
| 86 | + expect(onCite.mock.lastCall![0]).toMatchObject({ messageId: "message-1", text: "the cita" }); |
| 87 | + expect(window.getSelection()!.isCollapsed).toBe(true); |
| 88 | + await expect.element(citeButton).not.toBeInTheDocument(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("cites with R instead of typing it into the composer", async () => { |
| 92 | + const onCite = vi.fn<OnCite>(() => true); |
| 93 | + const composerKeys: string[] = []; |
| 94 | + await using screen = await render( |
| 95 | + <Transcript onCite={onCite} composerKeys={composerKeys} />, |
| 96 | + options, |
| 97 | + ); |
| 98 | + await selectAssistantText(screen.container); |
| 99 | + |
| 100 | + await userEvent.keyboard("r"); |
| 101 | + |
| 102 | + expect(onCite).toHaveBeenCalledOnce(); |
| 103 | + expect(composerKeys).toEqual([]); |
| 104 | + }); |
| 105 | + |
| 106 | + it("types R into the composer when nothing is selected", async () => { |
| 107 | + const onCite = vi.fn<OnCite>(() => true); |
| 108 | + const composerKeys: string[] = []; |
| 109 | + await using _screen = await render( |
| 110 | + <Transcript onCite={onCite} composerKeys={composerKeys} />, |
| 111 | + options, |
| 112 | + ); |
| 113 | + |
| 114 | + await userEvent.keyboard("r"); |
| 115 | + |
| 116 | + expect(composerKeys).toEqual(["r"]); |
| 117 | + expect(onCite).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("lets a focused text field keep R", async () => { |
| 121 | + const onCite = vi.fn<OnCite>(() => true); |
| 122 | + await using screen = await render(<Transcript onCite={onCite} />, options); |
| 123 | + await selectAssistantText(screen.container); |
| 124 | + const find = screen.getByRole("textbox", { name: "Find in thread" }); |
| 125 | + await userEvent.click(find); |
| 126 | + |
| 127 | + await userEvent.keyboard("r"); |
| 128 | + |
| 129 | + await expect.element(find).toHaveValue("r"); |
| 130 | + expect(onCite).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + // Dispatched rather than typed so a real Ctrl+R cannot reload the test page. |
| 134 | + it.each([ |
| 135 | + ["Cmd+R", { metaKey: true }], |
| 136 | + ["Ctrl+R", { ctrlKey: true }], |
| 137 | + ["Alt+R", { altKey: true }], |
| 138 | + ])("leaves %s to the browser", async (_name, modifier) => { |
| 139 | + const onCite = vi.fn<OnCite>(() => true); |
| 140 | + await using screen = await render(<Transcript onCite={onCite} />, options); |
| 141 | + await selectAssistantText(screen.container); |
| 142 | + |
| 143 | + const event = new KeyboardEvent("keydown", { |
| 144 | + key: "r", |
| 145 | + bubbles: true, |
| 146 | + cancelable: true, |
| 147 | + ...modifier, |
| 148 | + }); |
| 149 | + document.body.dispatchEvent(event); |
| 150 | + |
| 151 | + expect(event.defaultPrevented).toBe(false); |
| 152 | + expect(onCite).not.toHaveBeenCalled(); |
| 153 | + }); |
| 154 | + |
| 155 | + it("still focuses the Cite action on Tab", async () => { |
| 156 | + const onCite = vi.fn<OnCite>(() => true); |
| 157 | + await using screen = await render(<Transcript onCite={onCite} />, options); |
| 158 | + await selectAssistantText(screen.container); |
| 159 | + |
| 160 | + await userEvent.keyboard("{Tab}"); |
| 161 | + |
| 162 | + await expect.element(citeButton).toHaveFocus(); |
| 163 | + expect(onCite).not.toHaveBeenCalled(); |
| 164 | + }); |
| 165 | +}); |
0 commit comments