Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalInput.test.ts

81 lines2.8 KB

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