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

terminalInput.test.ts

68 lines2.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 chunkTerminalWrite,
5 encodeTerminalPaste,
6 resolveModifiedTerminalInput,
7 TERMINAL_WRITE_MAX_LENGTH,
8} from "./terminalInput";
9​
10const byte = (code: number) => String.fromCharCode(code);
11const ESC = byte(0x1b);
12const CTRL_V = byte(0x16);
13​
14describe("resolveModifiedTerminalInput", () => {
15 it("pastes on ctrl+v for windows, linux, and unknown hosts", () => {
16 for (const hostPlatform of ["windows", "linux", "unknown"] as const) {
17 expect(resolveModifiedTerminalInput({ data: "v", modifier: "ctrl", hostPlatform })).toEqual({
18 kind: "paste",
19 });
20 expect(resolveModifiedTerminalInput({ data: "V", modifier: "ctrl", hostPlatform })).toEqual({
21 kind: "paste",
22 });
23 }
24 });
25​
26 it("pastes on cmd+v and forwards raw ctrl+v on mac hosts", () => {
27 expect(
28 resolveModifiedTerminalInput({ data: "v", modifier: "meta", hostPlatform: "mac" }),
29 ).toEqual({ kind: "paste" });
30 expect(
31 resolveModifiedTerminalInput({ data: "v", modifier: "ctrl", hostPlatform: "mac" }),
32 ).toEqual({ kind: "write", data: CTRL_V });
33 });
34});
35​
36describe("encodeTerminalPaste", () => {
37 it("turns LF and CRLF line breaks into a single carriage return each", () => {
38 expect(encodeTerminalPaste("one\ntwo\r\nthree\n")).toBe("one\rtwo\rthree\r");
39 });
40​
41 it("replaces unsafe control bytes with spaces but keeps tabs", () => {
42 expect(encodeTerminalPaste(`a${byte(0)}b${ESC}c${byte(0x7f)}d\te`)).toBe("a b c d\te");
43 });
44​
45 it("never lets a bracketed-paste end marker reach the shell", () => {
46 expect(encodeTerminalPaste(`safe${ESC}[201~; rm -rf /\n`)).toBe("safe [201~; rm -rf /\r");
47 });
48});
49​
50describe("chunkTerminalWrite", () => {
51 it("splits oversized writes so every chunk fits the contract", () => {
52 const chunks = chunkTerminalWrite("y".repeat(TERMINAL_WRITE_MAX_LENGTH * 2 + 5));
53 expect(chunks.map((chunk) => chunk.length)).toEqual([
54 TERMINAL_WRITE_MAX_LENGTH,
55 TERMINAL_WRITE_MAX_LENGTH,
56 5,
57 ]);
58 expect(chunks.join("")).toHaveLength(TERMINAL_WRITE_MAX_LENGTH * 2 + 5);
59 });
60​
61 it("does not cut a surrogate pair in half at the boundary", () => {
62 const data = `${"z".repeat(TERMINAL_WRITE_MAX_LENGTH - 1)}😀tail`;
63 const chunks = chunkTerminalWrite(data);
64 expect(chunks[0]).toHaveLength(TERMINAL_WRITE_MAX_LENGTH - 1);
65 expect(chunks[1]).toBe("😀tail");
66 expect(chunks.join("")).toBe(data);
67 });
68});