Open History to see the last commit
1import { useEffect } from "react";
2import { expect, it } from "vite-plus/test";
3import { userEvent } from "vite-plus/test/browser";
4
5import { render } from "../../../test/render";
6import type { ChatComposerHandle } from "./ChatComposer";
7import { useChatShortcuts } from "./useChatShortcuts";
8
9const shortcutContext = {
10 terminalFocus: false,
11 terminalOpen: false,
12 previewFocus: false,
13 previewOpen: false,
14 isWeb: true,
15 isDesktop: false,
16 modelPickerOpen: false,
17};
18
19const focusOnMount = (element: HTMLElement | null) => element?.focus();
20
21// Stands in for the right panel's launcher, which needs the whole app around it. Like the
22// real one it takes focus, advertises its letters in `data-surface-launcher-keys`, and
23// claims one from its own window capture listener unless the key was already handled.
24function Launcher(props: { keys: string; received: string[] }) {
25 useEffect(() => {
26 const claim = (event: KeyboardEvent) => {
27 if (event.defaultPrevented) return;
28 if (!props.keys.toLowerCase().split("").includes(event.key.toLowerCase())) return;
29 event.preventDefault();
30 props.received.push(event.key);
31 };
32 window.addEventListener("keydown", claim, true);
33 return () => window.removeEventListener("keydown", claim, true);
34 }, [props.keys, props.received]);
35 return (
36 <div
37 ref={focusOnMount}
38 tabIndex={0}
39 aria-label="Open a surface"
40 data-surface-launcher-keys={props.keys}
41 />
42 );
43}
44
45// The chat's shortcut listener registers when the chat mounts, so a launcher that opens
46// later registers after it. Tests mount the chat first and add the launcher by rerendering.
47function Chat(props: { composerKeys: string[]; launcher?: { keys: string; received: string[] } }) {
48 useChatShortcuts({
49 enabled: true,
50 keybindings: [],
51 scriptKeybindings: [],
52 getShortcutContext: () => shortcutContext,
53 threadFindActive: false,
54 // Type-to-focus only calls insertTextAtEnd, so the stand-in composer implements just that.
55 composerRef: {
56 current: {
57 insertTextAtEnd: (key: string) => {
58 props.composerKeys.push(key);
59 return true;
60 },
61 } as Partial<ChatComposerHandle> as ChatComposerHandle,
62 },
63 branchToolbarRef: { current: null },
64 closeThreadFind: () => {},
65 commands: {},
66 fallback: () => undefined,
67 });
68 return props.launcher ? <Launcher {...props.launcher} /> : null;
69}
70
71// router: false because the chat shortcuts and the launcher stand-in never navigate.
72const options = { router: false } as const;
73
74it("leaves the launcher's letters to the launcher, in either case", async () => {
75 const composerKeys: string[] = [];
76 const received: string[] = [];
77 await using screen = await render(<Chat composerKeys={composerKeys} />, options);
78 await screen.rerender(<Chat composerKeys={composerKeys} launcher={{ keys: "BT", received }} />);
79
80 await userEvent.keyboard("tT");
81
82 expect(received).toEqual(["t", "T"]);
83 expect(composerKeys).toEqual([]);
84});
85
86it("still types other letters into the composer while the launcher is open", async () => {
87 const composerKeys: string[] = [];
88 const received: string[] = [];
89 await using screen = await render(<Chat composerKeys={composerKeys} />, options);
90 await screen.rerender(<Chat composerKeys={composerKeys} launcher={{ keys: "BT", received }} />);
91
92 await userEvent.keyboard("a");
93
94 expect(composerKeys).toEqual(["a"]);
95 expect(received).toEqual([]);
96});