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