Files
kandev/apps/web/lib/terminal/send-shell-input.test.ts
T
José Almeida 9dc7f8a43b fix(web): address review feedback on mobile pickers + multi-terminal
Bots flagged a real first-mount bug and a handful of smaller items;
addressed each:

- mobile-terminal-pane: track xterm in state instead of a ref so the
  active-sender registration effect re-runs once xterm is ready. With
  the ref, the effect ran once on mount, found xtermRef.current === null
  while PassthroughTerminal was still initializing (notably under the
  ResizeObserver fallback path when the container starts at 0×0), and
  silently never registered — leaving the key-bar wired to the default
  shell instead of the visible terminal.
- send-shell-input: wrap the activeSender call in try/catch so a stale
  registry entry (xterm GC'd mid-frame) falls back to the WS path
  instead of dropping the keystroke and silently consuming modifiers.
  Added tests for the activeSender routing branch (success, modifier
  consumption, fallback on throw).
- mobile-sessions-section: remove the dead `showCloseConfirm ? "Delete" : "Delete"`
  ternary and the unused prop on SessionActionsMenu.
- use-mobile-terminals: clear the auto-create guard when addTerminal()
  rejects so the user gets a retry instead of an empty terminal list.
- mobile-repo-pill: sort taskRepos by position before falling back to
  the first repo, so the pill label matches the picker's ordering when
  no session is active yet.
- session-task-switcher-sheet-hooks: catch loadTaskSessionsForTask
  rejection in handleSelectTask (was an unhandled rejection that left
  the sheet open on backend errors), and stabilize useCallback deps in
  handleWorkspaceChange by spreading the SheetNavOptions fields so a
  fresh literal each render doesn't defeat memoization. Extracted
  selectTaskWithoutPrimarySession to keep useSheetActions under the
  function-size lint cap.
2026-05-06 01:56:17 +01:00

116 lines
4.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { useShellModifiersStore } from "./shell-modifiers";
import { setActiveTerminalSender } from "./mobile-active-terminal";
const sendMock = vi.fn();
let clientFactory: () => { send: typeof sendMock } | null = () => ({ send: sendMock });
vi.mock("@/lib/ws/connection", () => ({
getWebSocketClient: () => clientFactory(),
}));
// Imported after the mock so it picks up the mocked module.
const { sendShellInput } = await import("./send-shell-input");
const SESSION = "sess-1";
beforeEach(() => {
sendMock.mockReset();
clientFactory = () => ({ send: sendMock });
useShellModifiersStore.getState().reset();
setActiveTerminalSender(null);
});
function lastFrameData(): string | undefined {
const call = sendMock.mock.calls.at(-1)?.[0] as { payload?: { data?: string } } | undefined;
return call?.payload?.data;
}
describe("sendShellInput", () => {
it("no-ops on empty data", () => {
sendShellInput(SESSION, "");
expect(sendMock).not.toHaveBeenCalled();
});
it("sends raw data and shapes the WS frame", () => {
sendShellInput(SESSION, "ls");
expect(sendMock).toHaveBeenCalledWith({
type: "request",
action: "shell.input",
payload: { session_id: SESSION, data: "ls" },
});
});
it("applies the Ctrl chord transform when Ctrl is latched and consumes it", () => {
useShellModifiersStore.getState().toggleCtrl();
sendShellInput(SESSION, "c");
expect(lastFrameData()).toBe("\x03");
expect(useShellModifiersStore.getState().ctrl).toEqual({ latched: false, sticky: false });
});
it("keeps Ctrl latched when sticky after a chord", () => {
useShellModifiersStore.getState().toggleCtrl();
useShellModifiersStore.getState().toggleCtrl();
expect(useShellModifiersStore.getState().ctrl.sticky).toBe(true);
sendShellInput(SESSION, "a");
expect(lastFrameData()).toBe("\x01");
expect(useShellModifiersStore.getState().ctrl).toEqual({ latched: true, sticky: true });
});
it("translates Shift+Tab to CSI Z and consumes Shift", () => {
useShellModifiersStore.getState().toggleShift();
sendShellInput(SESSION, "\t");
expect(lastFrameData()).toBe("\x1b[Z");
expect(useShellModifiersStore.getState().shift).toEqual({ latched: false, sticky: false });
});
it("Shift on a non-Tab character is a passthrough; modifier still consumes", () => {
useShellModifiersStore.getState().toggleShift();
sendShellInput(SESSION, "a");
expect(lastFrameData()).toBe("a");
expect(useShellModifiersStore.getState().shift).toEqual({ latched: false, sticky: false });
});
it("when the WS client is null: nothing sent AND modifiers stay armed", () => {
clientFactory = () => null;
useShellModifiersStore.getState().toggleCtrl();
sendShellInput(SESSION, "c");
expect(sendMock).not.toHaveBeenCalled();
expect(useShellModifiersStore.getState().ctrl).toEqual({ latched: true, sticky: false });
});
describe("with an active mobile terminal sender registered", () => {
it("routes input through the active sender and bypasses WS", () => {
const sender = vi.fn();
setActiveTerminalSender(sender);
sendShellInput(SESSION, "ls");
expect(sender).toHaveBeenCalledWith("ls");
expect(sendMock).not.toHaveBeenCalled();
});
it("applies modifiers and consumes them after a successful sender call", () => {
const sender = vi.fn();
setActiveTerminalSender(sender);
useShellModifiersStore.getState().toggleCtrl();
sendShellInput(SESSION, "c");
expect(sender).toHaveBeenCalledWith("\x03");
expect(useShellModifiersStore.getState().ctrl).toEqual({ latched: false, sticky: false });
});
it("falls back to WS when the sender throws", () => {
const sender = vi.fn(() => {
throw new Error("xterm gone");
});
setActiveTerminalSender(sender);
sendShellInput(SESSION, "ls");
expect(sender).toHaveBeenCalledWith("ls");
expect(sendMock).toHaveBeenCalledWith({
type: "request",
action: "shell.input",
payload: { session_id: SESSION, data: "ls" },
});
});
});
});