Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<HardwareKeyboardCommand>;
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<HardwareKeyboardCommand>;
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(
<HardwareKeyboardCommandProvider pathname="/threads/env/thread-17/terminal?terminalId=term-2">
{null}
</HardwareKeyboardCommandProvider>,
),
);

expect(nativeCommands?.enabledCommands).toContain("terminal");
expect(nativeCommands?.enabledCommands).not.toContain("copyThreadReference");
act(() => nativeCommands?.onCommand("terminal"));
expect(navigation.navigate).not.toHaveBeenCalled();
act(() => root.unmount());
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
},
Expand Down
4 changes: 4 additions & 0 deletions apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends { readonly environmentId: string }>(
environments: ReadonlyArray<T>,
currentId: string | null,
Expand Down
1 change: 1 addition & 0 deletions apps/mobile/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))}/`;
Expand Down
Loading