diff --git a/apps/web/src/speech/useDictationShortcut.browser.test.tsx b/apps/web/src/speech/useDictationShortcut.browser.test.tsx index 8cd17f4f368dc..7af5801a5905d 100644 --- a/apps/web/src/speech/useDictationShortcut.browser.test.tsx +++ b/apps/web/src/speech/useDictationShortcut.browser.test.tsx @@ -3,7 +3,8 @@ import { expect, it, vi } from "vite-plus/test"; import { render } from "../../test/render"; import { useDictationShortcut } from "./useDictationShortcut"; -vi.mock("../hooks/useSettings", () => ({ useClientSettings: () => "hold" })); +const settings = vi.hoisted(() => ({ mode: "hold" as "auto" | "hold" | "toggle" })); +vi.mock("../hooks/useSettings", () => ({ useClientSettings: () => settings.mode })); function Probe({ speech }: { speech: Parameters[0]["speech"] }) { useDictationShortcut({ @@ -29,7 +30,8 @@ const press = (repeat = false) => }), ); -function actions(available: boolean) { +function setup(available: boolean, mode: typeof settings.mode = "hold") { + settings.mode = mode; return { available, state: { phase: "idle" as const, error: null, errorAction: null }, @@ -40,14 +42,14 @@ function actions(available: boolean) { } it("leaves the browser shortcut alone when dictation is off", async () => { - const speech = actions(false); + const speech = setup(false); await using _screen = await render(); expect(press()).toBe(true); expect(speech.start).not.toHaveBeenCalled(); }); it("stops on a new press when the OS omitted keyup, but ignores autorepeat", async () => { - const speech = actions(true); + const speech = setup(true); await using screen = await render(); expect(press()).toBe(false); expect(speech.start).toHaveBeenCalledOnce(); @@ -60,23 +62,71 @@ it("stops on a new press when the OS omitted keyup, but ignores autorepeat", asy expect(speech.stop).toHaveBeenCalledOnce(); }); -it("finishes a released hold after startup even if the chord was pressed again while preparing", async () => { - const speech = actions(true); - let finishStart!: () => void; - const startup = new Promise((resolve) => { - finishStart = resolve; - }); - speech.start.mockReturnValue(startup); - await using screen = await render(); - press(); - await screen.rerender( - , - ); - window.dispatchEvent(new KeyboardEvent("keyup", { code: "KeyD" })); - press(); - window.dispatchEvent(new KeyboardEvent("keyup", { code: "KeyD" })); - finishStart(); - await startup; - expect(speech.start).toHaveBeenCalledOnce(); - expect(speech.stop).toHaveBeenCalledOnce(); -}); +it.each(["KeyD", "MetaLeft"])( + "finishes a hold released with %s after startup even if the chord was pressed again while preparing", + async (code) => { + const speech = setup(true); + using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); + let finishStart!: () => void; + const startup = new Promise((resolve) => { + finishStart = resolve; + }); + speech.start.mockReturnValue(startup); + await using screen = await render(); + press(); + await screen.rerender( + , + ); + window.dispatchEvent(new KeyboardEvent("keyup", { code, key: code === "KeyD" ? "D" : "Meta" })); + press(); + window.dispatchEvent(new KeyboardEvent("keyup", { code, key: code === "KeyD" ? "D" : "Meta" })); + finishStart(); + await startup; + expect(speech.start).toHaveBeenCalledOnce(); + expect(speech.stop).toHaveBeenCalledOnce(); + }, +); + +it.each([ + { mode: "hold", heldMs: 0, stops: true }, + { mode: "auto", heldMs: 300, stops: true }, + { mode: "auto", heldMs: 299, stops: false }, + { mode: "toggle", heldMs: 300, stops: false }, +] as const)( + "handles missing letter keyup in $mode mode after $heldMs ms", + async ({ mode, heldMs, stops }) => { + const speech = setup(true, mode); + using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); + await using screen = await render(); + using now = vi.spyOn(performance, "now").mockReturnValue(0); + press(); + await screen.rerender( + , + ); + now.mockReturnValue(heldMs); + // Neither an unrelated letter nor a modifier outside the chord ends the hold. + window.dispatchEvent( + new KeyboardEvent("keyup", { key: "a", code: "KeyA", metaKey: true, shiftKey: true }), + ); + window.dispatchEvent( + new KeyboardEvent("keyup", { key: "Alt", code: "AltLeft", metaKey: true, shiftKey: true }), + ); + await Promise.resolve(); + expect(speech.stop).not.toHaveBeenCalled(); + + window.dispatchEvent( + new KeyboardEvent("keyup", { key: "Meta", code: "MetaLeft", shiftKey: true }), + ); + await Promise.resolve(); + expect(speech.stop).toHaveBeenCalledTimes(stops ? 1 : 0); + + // Later releases must neither stop twice nor turn an Auto tap into a hold. + now.mockReturnValue(1_000); + window.dispatchEvent(new KeyboardEvent("keyup", { key: "Shift", code: "ShiftLeft" })); + window.dispatchEvent(new KeyboardEvent("keyup", { key: "d", code: "KeyD" })); + await Promise.resolve(); + expect(speech.stop).toHaveBeenCalledTimes(stops ? 1 : 0); + if (!stops) press(); + expect(speech.stop).toHaveBeenCalledOnce(); + }, +); diff --git a/apps/web/src/speech/useDictationShortcut.ts b/apps/web/src/speech/useDictationShortcut.ts index 955bd9e5c4f89..3599f6b30b759 100644 --- a/apps/web/src/speech/useDictationShortcut.ts +++ b/apps/web/src/speech/useDictationShortcut.ts @@ -60,6 +60,7 @@ export function useDictationShortcut(input: ShortcutInput) { const pressed = useRef<{ generation: number; code: string; + modifiers: string[]; startedAt: number; mode: typeof mode; starting: Promise; @@ -72,7 +73,10 @@ export function useDictationShortcut(input: ShortcutInput) { shortcutTargets.set(id.current, () => current.current); const finish = (event: KeyboardEvent | null) => { const press = pressed.current; - if (!press || (event && (event.code || event.key) !== press.code)) return; + if (!press) return; + // macOS may omit the letter keyup while Command is held; modifier release ends the chord too. + if (event && (event.code || event.key) !== press.code && !press.modifiers.includes(event.key)) + return; pressed.current = null; if (shouldFinishDictationOnRelease(press.mode, performance.now() - press.startedAt)) { void press.starting.then(() => { @@ -123,6 +127,7 @@ export function useDictationShortcut(input: ShortcutInput) { pressed.current = { generation: generation.current, code: event.code || event.key, + modifiers: ["Alt", "Control", "Meta", "Shift"].filter((key) => event.getModifierState(key)), startedAt: performance.now(), mode, starting: speech.start(),