Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.
Merged
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
62 changes: 62 additions & 0 deletions packages/ui/src/features/terminal/TerminalManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,65 @@ describe("TerminalManager.destroyForTask", () => {
host.remove();
});
});

describe("TerminalManager custom key handling", () => {
const sessionId = "key-handler-test";

beforeEach(() => {
mocks.check.mockReset().mockResolvedValue(true);
mocks.create.mockReset().mockResolvedValue(undefined);
mocks.write.mockReset().mockResolvedValue(undefined);
mocks.resize.mockReset().mockResolvedValue(undefined);
mocks.terminalInstances.length = 0;
});

afterEach(() => {
terminalManager.destroy(sessionId);
});

function keyHandler() {
terminalManager.create({ sessionId, persistenceKey: "task-key" });
const instance = mocks.terminalInstances[0];
return instance.attachCustomKeyEventHandler.mock.calls[0][0] as (
event: KeyboardEvent,
) => boolean;
}

function fakeEvent(overrides: Partial<KeyboardEvent>): KeyboardEvent {
return {
key: "k",
type: "keydown",
ctrlKey: false,
metaKey: false,
shiftKey: false,
preventDefault: vi.fn(),
stopPropagation: vi.fn(),
stopImmediatePropagation: vi.fn(),
...overrides,
} as unknown as KeyboardEvent;
}

it("clears the terminal and stops ctrl+k from bubbling to the command menu", () => {
const handler = keyHandler();
const event = fakeEvent({ key: "k", ctrlKey: true, type: "keydown" });

const result = handler(event);

expect(result).toBe(false);
expect(mocks.terminalInstances[0].clear).toHaveBeenCalledTimes(1);
expect(event.preventDefault).toHaveBeenCalledTimes(1);
expect(event.stopPropagation).toHaveBeenCalledTimes(1);
expect(event.stopImmediatePropagation).toHaveBeenCalledTimes(1);
});

it("lets ctrl+w keep bubbling so global handlers can act", () => {
const handler = keyHandler();
const event = fakeEvent({ key: "w", ctrlKey: true });

const result = handler(event);

expect(result).toBe(false);
expect(event.stopPropagation).not.toHaveBeenCalled();
expect(event.stopImmediatePropagation).not.toHaveBeenCalled();
});
});
4 changes: 4 additions & 0 deletions packages/ui/src/features/terminal/TerminalManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ function attachKeyHandlers(term: XTerm) {

if (event.key === "k" && cmdOrCtrl && event.type === "keydown") {
event.preventDefault();
// Stop the keydown from bubbling to the document-level mod+k hotkey,
// which would otherwise open the command menu on every terminal clear.
event.stopPropagation();
event.stopImmediatePropagation();
term.clear();
return false;
}
Expand Down
Loading