The best way to control Claude Code, Codex and more from any computer or phone

main
ts 75 lines 2.55 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2
3 import { encodeTerminalPaste, resolveModifiedTerminalInput } from "./terminalInput";
4
5 const byte = (code: number) => String.fromCharCode(code);
6 const ESC = byte(0x1b);
7 const CTRL_V = byte(0x16);
8
9 describe("resolveModifiedTerminalInput", () => {
10 it.each(["A", "B", "C", "D"])("encodes ctrl+arrow %s with the cursor modifier", (arrow) => {
11 expect(
12 resolveModifiedTerminalInput({
13 data: `${ESC}[${arrow}`,
14 modifier: "ctrl",
15 hostPlatform: "linux",
16 }),
17 ).toEqual({ kind: "write", data: `${ESC}[1;5${arrow}` });
18 });
19
20 it.each(["A", "B", "C", "D"])("encodes meta+arrow %s with the cursor modifier", (arrow) => {
21 expect(
22 resolveModifiedTerminalInput({
23 data: `${ESC}[${arrow}`,
24 modifier: "meta",
25 hostPlatform: "linux",
26 }),
27 ).toEqual({ kind: "write", data: `${ESC}[1;3${arrow}` });
28 });
29
30 it.each(["a", "", ESC, `${ESC}[H`, `${ESC}[1;5D`, `${ESC}[D\n`])(
31 "preserves non-arrow modifier handling for %j",
32 (data) => {
33 expect(
34 resolveModifiedTerminalInput({ data, modifier: "meta", hostPlatform: "linux" }),
35 ).toEqual({
36 kind: "write",
37 data: `${ESC}${data}`,
38 });
39 },
40 );
41
42 it("pastes on ctrl+v for windows, linux, and unknown hosts", () => {
43 for (const hostPlatform of ["windows", "linux", "unknown"] as const) {
44 expect(resolveModifiedTerminalInput({ data: "v", modifier: "ctrl", hostPlatform })).toEqual({
45 kind: "paste",
46 });
47 expect(resolveModifiedTerminalInput({ data: "V", modifier: "ctrl", hostPlatform })).toEqual({
48 kind: "paste",
49 });
50 }
51 });
52
53 it("pastes on cmd+v and forwards raw ctrl+v on mac hosts", () => {
54 expect(
55 resolveModifiedTerminalInput({ data: "v", modifier: "meta", hostPlatform: "mac" }),
56 ).toEqual({ kind: "paste" });
57 expect(
58 resolveModifiedTerminalInput({ data: "v", modifier: "ctrl", hostPlatform: "mac" }),
59 ).toEqual({ kind: "write", data: CTRL_V });
60 });
61 });
62
63 describe("encodeTerminalPaste", () => {
64 it("turns LF and CRLF line breaks into a single carriage return each", () => {
65 expect(encodeTerminalPaste("one\ntwo\r\nthree\n")).toBe("one\rtwo\rthree\r");
66 });
67
68 it("replaces unsafe control bytes with spaces but keeps tabs", () => {
69 expect(encodeTerminalPaste(`a${byte(0)}b${ESC}c${byte(0x7f)}d\te`)).toBe("a b c d\te");
70 });
71
72 it("never lets a bracketed-paste end marker reach the shell", () => {
73 expect(encodeTerminalPaste(`safe${ESC}[201~; rm -rf /\n`)).toBe("safe [201~; rm -rf /\r");
74 });
75 });