The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings"; |
| 2 | import { createRef, type RefObject } from "react"; |
| 3 | import { expect, it, vi } from "vite-plus/test"; |
| 4 | import { render } from "../../test/render"; |
| 5 | import { useDictationShortcut, type DictationKeyHandlers } from "./useDictationShortcut"; |
| 6 | |
| 7 | const settings = vi.hoisted(() => ({ mode: "hold" as "hold" | "auto" | "toggle" })); |
| 8 | vi.mock("../hooks/useSettings", () => ({ useClientSettings: () => settings.mode })); |
| 9 | |
| 10 | type ShortcutInput = Parameters<typeof useDictationShortcut>[0]; |
| 11 | |
| 12 | function Probe({ |
| 13 | speech, |
| 14 | targetRef, |
| 15 | subscribeKeys, |
| 16 | }: { |
| 17 | speech: ShortcutInput["speech"]; |
| 18 | targetRef?: RefObject<HTMLElement | null>; |
| 19 | subscribeKeys?: ShortcutInput["subscribeKeys"]; |
| 20 | }) { |
| 21 | useDictationShortcut({ |
| 22 | keybindings: DEFAULT_RESOLVED_KEYBINDINGS, |
| 23 | speech, |
| 24 | disabled: false, |
| 25 | terminalOpen: false, |
| 26 | modelPickerOpen: false, |
| 27 | ...(targetRef ? { targetRef } : {}), |
| 28 | ...(subscribeKeys ? { subscribeKeys, ownsFocus: () => true } : {}), |
| 29 | }); |
| 30 | return null; |
| 31 | } |
| 32 | |
| 33 | const press = (repeat = false) => |
| 34 | window.dispatchEvent( |
| 35 | new KeyboardEvent("keydown", { |
| 36 | key: "D", |
| 37 | code: "KeyD", |
| 38 | ctrlKey: !/Mac/i.test(navigator.platform), |
| 39 | metaKey: /Mac/i.test(navigator.platform), |
| 40 | shiftKey: true, |
| 41 | cancelable: true, |
| 42 | repeat, |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | function actions(available: boolean, mode: typeof settings.mode = "hold") { |
| 47 | settings.mode = mode; |
| 48 | return { |
| 49 | available, |
| 50 | state: { phase: "idle" as const, error: null, errorAction: null }, |
| 51 | start: vi.fn(async () => {}), |
| 52 | stop: vi.fn(async () => {}), |
| 53 | cancel: vi.fn(), |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | it("leaves the browser shortcut alone when dictation is off", async () => { |
| 58 | const speech = actions(false); |
| 59 | await using _screen = await render(<Probe speech={speech} />); |
| 60 | expect(press()).toBe(true); |
| 61 | expect(speech.start).not.toHaveBeenCalled(); |
| 62 | }); |
| 63 | |
| 64 | it("stops on a new press when the OS omitted keyup, but ignores autorepeat", async () => { |
| 65 | const speech = actions(true); |
| 66 | await using screen = await render(<Probe speech={speech} />); |
| 67 | expect(press()).toBe(false); |
| 68 | expect(speech.start).toHaveBeenCalledOnce(); |
| 69 | await screen.rerender( |
| 70 | <Probe speech={{ ...speech, state: { ...speech.state, phase: "recording" } }} />, |
| 71 | ); |
| 72 | press(true); |
| 73 | expect(speech.stop).not.toHaveBeenCalled(); |
| 74 | press(); |
| 75 | expect(speech.stop).toHaveBeenCalledOnce(); |
| 76 | }); |
| 77 | |
| 78 | it("finishes a released hold after startup even if the chord was pressed again while preparing", async () => { |
| 79 | const speech = actions(true); |
| 80 | let finishStart!: () => void; |
| 81 | const startup = new Promise<void>((resolve) => { |
| 82 | finishStart = resolve; |
| 83 | }); |
| 84 | speech.start.mockReturnValue(startup); |
| 85 | await using screen = await render(<Probe speech={speech} />); |
| 86 | press(); |
| 87 | await screen.rerender( |
| 88 | <Probe speech={{ ...speech, state: { ...speech.state, phase: "preparing" } }} />, |
| 89 | ); |
| 90 | window.dispatchEvent(new KeyboardEvent("keyup", { code: "KeyD" })); |
| 91 | press(); |
| 92 | window.dispatchEvent(new KeyboardEvent("keyup", { code: "KeyD" })); |
| 93 | finishStart(); |
| 94 | await startup; |
| 95 | expect(speech.start).toHaveBeenCalledOnce(); |
| 96 | expect(speech.stop).toHaveBeenCalledOnce(); |
| 97 | }); |
| 98 | |
| 99 | it.each([ |
| 100 | { mode: "hold", heldMs: 0, stops: 1 }, |
| 101 | { mode: "auto", heldMs: 300, stops: 1 }, |
| 102 | { mode: "auto", heldMs: 299, stops: 0 }, |
| 103 | ] as const)( |
| 104 | "ends a $mode press held $heldMs ms when a chord modifier is released without the letter keyup", |
| 105 | async ({ mode, heldMs, stops }) => { |
| 106 | const speech = actions(true, mode); |
| 107 | using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); |
| 108 | await using _screen = await render(<Probe speech={speech} />); |
| 109 | using clock = vi.spyOn(performance, "now").mockReturnValue(0); |
| 110 | press(); |
| 111 | clock.mockReturnValue(heldMs); |
| 112 | window.dispatchEvent(new KeyboardEvent("keyup", { key: "Alt", code: "AltLeft" })); |
| 113 | await speech.start.mock.results[0]?.value; |
| 114 | expect(speech.stop).not.toHaveBeenCalled(); |
| 115 | window.dispatchEvent(new KeyboardEvent("keyup", { key: "Meta", code: "MetaLeft" })); |
| 116 | await speech.start.mock.results[0]?.value; |
| 117 | expect(speech.stop).toHaveBeenCalledTimes(stops); |
| 118 | }, |
| 119 | ); |
| 120 | |
| 121 | const escape = () => |
| 122 | new KeyboardEvent("keydown", { key: "Escape", code: "Escape", bubbles: true, cancelable: true }); |
| 123 | |
| 124 | const recording = () => ({ |
| 125 | ...actions(true), |
| 126 | state: { phase: "recording" as const, error: null, errorAction: null }, |
| 127 | }); |
| 128 | |
| 129 | it("lets Escape in another dictation editor reach that editor instead of canceling the recording", async () => { |
| 130 | const speech = recording(); |
| 131 | const commentRef = createRef<HTMLTextAreaElement>(); |
| 132 | const editorEscape = vi.fn(); |
| 133 | await using screen = await render( |
| 134 | <> |
| 135 | <Probe speech={speech} /> |
| 136 | <Probe speech={actions(true)} targetRef={commentRef} /> |
| 137 | <div |
| 138 | role="dialog" |
| 139 | aria-label="Edit citation comment" |
| 140 | onKeyDown={(event) => { |
| 141 | if (event.key === "Escape") editorEscape(); |
| 142 | }} |
| 143 | > |
| 144 | <textarea aria-label="Comment" ref={commentRef} /> |
| 145 | <button type="button">Cancel</button> |
| 146 | </div> |
| 147 | </>, |
| 148 | ); |
| 149 | for (const control of [ |
| 150 | screen.getByRole("textbox", { name: "Comment" }).element(), |
| 151 | screen.getByRole("button", { name: "Cancel" }).element(), |
| 152 | ]) { |
| 153 | if (!(control instanceof HTMLElement)) throw new Error("expected a comment editor control"); |
| 154 | control.focus(); |
| 155 | control.dispatchEvent(escape()); |
| 156 | } |
| 157 | expect(speech.cancel).not.toHaveBeenCalled(); |
| 158 | expect(editorEscape).toHaveBeenCalledTimes(2); |
| 159 | }); |
| 160 | |
| 161 | it("lets Escape in another editor of the recording editor's dialog reach that editor", async () => { |
| 162 | const speech = recording(); |
| 163 | const replyRef = createRef<HTMLTextAreaElement>(); |
| 164 | const otherReplyRef = createRef<HTMLTextAreaElement>(); |
| 165 | await using screen = await render( |
| 166 | <> |
| 167 | <Probe speech={speech} targetRef={replyRef} /> |
| 168 | <Probe speech={actions(true)} targetRef={otherReplyRef} /> |
| 169 | <div role="dialog" aria-label="Pull request"> |
| 170 | <textarea aria-label="Reply A" ref={replyRef} /> |
| 171 | <textarea aria-label="Reply B" ref={otherReplyRef} /> |
| 172 | </div> |
| 173 | </>, |
| 174 | ); |
| 175 | const other = screen.getByRole("textbox", { name: "Reply B" }).element(); |
| 176 | if (!(other instanceof HTMLElement)) throw new Error("expected a reply editor"); |
| 177 | other.focus(); |
| 178 | other.dispatchEvent(escape()); |
| 179 | expect(speech.cancel).not.toHaveBeenCalled(); |
| 180 | }); |
| 181 | |
| 182 | it("cancels the recording on Escape outside other shown dictation editors", async () => { |
| 183 | const speech = recording(); |
| 184 | const reviewRef = createRef<HTMLTextAreaElement>(); |
| 185 | const commentRef = createRef<HTMLTextAreaElement>(); |
| 186 | await using screen = await render( |
| 187 | <> |
| 188 | <Probe speech={speech} targetRef={reviewRef} /> |
| 189 | <Probe speech={actions(true)} targetRef={commentRef} /> |
| 190 | <input aria-label="Search" /> |
| 191 | <div role="dialog" aria-label="Pull request"> |
| 192 | <textarea aria-label="Review" ref={reviewRef} /> |
| 193 | <div hidden> |
| 194 | <textarea aria-label="Comment" ref={commentRef} /> |
| 195 | </div> |
| 196 | <button type="button">Submit</button> |
| 197 | </div> |
| 198 | </>, |
| 199 | ); |
| 200 | for (const control of [ |
| 201 | screen.getByRole("textbox", { name: "Search" }).element(), |
| 202 | screen.getByRole("button", { name: "Submit" }).element(), |
| 203 | ]) { |
| 204 | if (!(control instanceof HTMLElement)) throw new Error("expected a focusable control"); |
| 205 | control.focus(); |
| 206 | control.dispatchEvent(escape()); |
| 207 | } |
| 208 | expect(speech.cancel).toHaveBeenCalledTimes(2); |
| 209 | }); |
| 210 | |
| 211 | it.each([ |
| 212 | ["another editor is recording", false], |
| 213 | ["the preview itself is recording", true], |
| 214 | ])( |
| 215 | "routes Escape relayed from the preview guest to the preview when %s", |
| 216 | async (_name, previewRecords) => { |
| 217 | const composer = previewRecords ? actions(true) : recording(); |
| 218 | const preview = previewRecords ? recording() : actions(true); |
| 219 | let relayed: DictationKeyHandlers | undefined; |
| 220 | await using _screen = await render( |
| 221 | <> |
| 222 | <Probe speech={composer} /> |
| 223 | <Probe |
| 224 | speech={preview} |
| 225 | subscribeKeys={(handlers) => { |
| 226 | relayed = handlers; |
| 227 | return () => {}; |
| 228 | }} |
| 229 | /> |
| 230 | </>, |
| 231 | ); |
| 232 | relayed?.keydown(escape()); |
| 233 | expect(composer.cancel).not.toHaveBeenCalled(); |
| 234 | expect(preview.cancel).toHaveBeenCalledTimes(previewRecords ? 1 : 0); |
| 235 | }, |
| 236 | ); |