diff --git a/apps/mobile/src/features/keyboard/HardwareKeyboardCommandProvider.terminal.test.tsx b/apps/mobile/src/features/keyboard/HardwareKeyboardCommandProvider.terminal.test.tsx new file mode 100644 index 0000000000000..1d5fa4ab2b696 --- /dev/null +++ b/apps/mobile/src/features/keyboard/HardwareKeyboardCommandProvider.terminal.test.tsx @@ -0,0 +1,58 @@ +import { act } from "react"; +import { createRoot } from "react-dom/client"; +import { expect, it, vi } from "vite-plus/test"; + +import { HardwareKeyboardCommandProvider } from "./HardwareKeyboardCommandProvider"; +import type { HardwareKeyboardCommand } from "./hardwareKeyboardCommands"; + +const navigation = { canGoBack: () => true, navigate: vi.fn() }; +let nativeCommands: { + readonly enabledCommands: ReadonlyArray; + readonly onCommand: (command: HardwareKeyboardCommand) => void; +} | null = null; + +vi.mock("@react-navigation/native", () => ({ + StackActions: { replace: vi.fn() }, + useNavigation: () => navigation, +})); +vi.mock("react-native", () => ({ Platform: { OS: "ios" } })); +vi.mock("../../lib/copyThreadReference", () => ({ copyThreadReference: vi.fn() })); +vi.mock("../../state/entities", () => ({ useThreadShell: () => null })); +vi.mock("../layout/workspace/workspace-selection-store", () => ({ + firstWorkspaceDetailOpen: () => false, + workspaceHasColumns: () => false, +})); +vi.mock("../layout/workspace/workspace-detail-selection", () => ({ + selectWorkspaceDetail: vi.fn(), +})); +vi.mock("../threads/GitActionProgressOverlay", () => ({ GitActionProgressOverlay: () => null })); +vi.mock("./CommandPalette", () => ({ CommandPalette: () => null })); +vi.mock("../../native/T3KeyboardCommands", () => ({ + T3KeyboardCommands: (props: { + readonly enabledCommands: ReadonlyArray; + readonly onCommand: (command: HardwareKeyboardCommand) => void; + readonly children: React.ReactNode; + }) => { + nativeCommands = props; + return props.children; + }, +})); + +it("keeps a terminal opened with a session id as the current terminal", async () => { + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + using _globals = { [Symbol.dispose]: () => vi.unstubAllGlobals() }; + const root = createRoot(document.createElement("div")); + await act(async () => + root.render( + + {null} + , + ), + ); + + expect(nativeCommands?.enabledCommands).toContain("terminal"); + expect(nativeCommands?.enabledCommands).not.toContain("copyThreadReference"); + act(() => nativeCommands?.onCommand("terminal")); + expect(navigation.navigate).not.toHaveBeenCalled(); + act(() => root.unmount()); +}); diff --git a/apps/mobile/src/features/keyboard/HardwareKeyboardCommandProvider.tsx b/apps/mobile/src/features/keyboard/HardwareKeyboardCommandProvider.tsx index c5cd2ca953d71..f9e8c07c7b1f1 100644 --- a/apps/mobile/src/features/keyboard/HardwareKeyboardCommandProvider.tsx +++ b/apps/mobile/src/features/keyboard/HardwareKeyboardCommandProvider.tsx @@ -27,6 +27,7 @@ import type { GitActionProgress } from "../../state/use-vcs-action-state"; import { GitActionProgressOverlay } from "../threads/GitActionProgressOverlay"; import { CommandPalette } from "./CommandPalette"; import { + activeThreadSubroute, dispatchHardwareKeyboardCommand, getHardwareKeyboardCommandRegistrationVersion, getRegisteredHardwareKeyboardCommands, @@ -124,7 +125,7 @@ export function HardwareKeyboardCommandProvider({ commands.add("files"); commands.add("terminal"); commands.add("review"); - if (pathname.split("/")[4] !== "terminal") commands.add("copyThreadReference"); + if (activeThreadSubroute(pathname) !== "terminal") commands.add("copyThreadReference"); } return [...commands]; // oxlint-disable-next-line react/memo-dependencies -- registrationVersion invalidates the snapshot of the external keyboard-command registry. @@ -178,13 +179,14 @@ export function HardwareKeyboardCommandProvider({ const thread = parseActiveThreadPath(pathname); if (!thread) return; - if (command === "files" && !/\/files(?:\/|$)/.test(pathname)) { + const subroute = activeThreadSubroute(pathname); + if (command === "files" && subroute !== "files") { navigation.navigate("ThreadFiles", thread); } - if (command === "terminal" && !/\/terminal(?:\/|$)/.test(pathname)) { + if (command === "terminal" && subroute !== "terminal") { navigation.navigate("ThreadTerminal", thread); } - if (command === "review" && !/\/review(?:\/|$)/.test(pathname)) { + if (command === "review" && subroute !== "review") { navigation.navigate("ThreadReview", thread); } }, diff --git a/apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts b/apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts index 3525e17828099..a426f9b410f11 100644 --- a/apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts +++ b/apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts @@ -90,6 +90,10 @@ export function parseActiveThreadPath(pathname: string): { } } +export function activeThreadSubroute(pathname: string): string | undefined { + return pathname.split(/[?#]/u, 1)[0]?.split("/")[4]; +} + export function nextEnvironmentId( environments: ReadonlyArray, currentId: string | null, diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.ts index 4c06d8c9ba352..66f727f68ec35 100644 --- a/apps/mobile/vite.config.ts +++ b/apps/mobile/vite.config.ts @@ -65,6 +65,7 @@ const reactTests = [ "src/features/devices/device-stream.browser.test.ts", "src/features/keyboard/keyboardControllerState.test.tsx", "src/features/voice-input/useVoiceInputController.test.tsx", + "src/features/keyboard/HardwareKeyboardCommandProvider.terminal.test.tsx", ]; const sourceRoot = `${normalizePath(NodeURL.fileURLToPath(new URL("./src", import.meta.url)))}/`;