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

terminalOutput.test.ts

202 lines8.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 appendTerminalOutput,
5 EMPTY_TERMINAL_OUTPUT,
6 normalizeTerminalOutput,
7 TERMINAL_PALETTES,
8 terminalOutputPlainText,
9 terminalOutputResumeText,
10 terminalOutputSpans,
11 terminalOutputTail,
12 type TerminalOutputState,
13} from "./terminalOutput.ts";
14​
15function feed(chunks: ReadonlyArray<string>, maxChars?: number): TerminalOutputState {
16 return chunks.reduce(
17 (state, chunk) => appendTerminalOutput(state, chunk, maxChars),
18 EMPTY_TERMINAL_OUTPUT,
19 );
21​
22describe("appendTerminalOutput", () => {
23 it("keeps only the latest frame of a carriage-return progress bar", () => {
24 expect(feed(["[1/3]", "\r[2/3]", "\r[3/3] done\nnext"]).text).toBe("[3/3] done\nnext");
25 });
26​
27 it("redraws several lines in place when the cursor moves up", () => {
28 const frames = [
29 "layer a: 10%\nlayer b: 10%\n",
30 "\u001b[2A\u001b[2Klayer a: 60%\n\u001b[2Klayer b: 40%\n",
31 "\u001b[2A\u001b[2Klayer a: done\n\u001b[2Klayer b: done\n",
32 ];
33 expect(feed(frames).text).toBe("layer a: done\nlayer b: done\n");
34 });
35​
36 it("treats CRLF as one newline even when the pair is split across chunks", () => {
37 const state = feed(["one\r", "\ntwo\r\n"]);
38 expect(state.text).toBe("one\ntwo\n");
39 expect(state.pending).toBe("");
40 });
41​
42 it("holds an escape sequence split across chunks until it finishes", () => {
43 const first = appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, "red: \u001b[3");
44 expect(first.text).toBe("red: ");
45 expect(feed(["red: \u001b[3", "1mR\u001b[0m"]).text).toBe("red: \u001b[0;31mR\u001b[0m");
46 expect(
47 terminalOutputSpans(feed(["\u001b[38:2::12:", "34:56mR"]).text, TERMINAL_PALETTES.dark),
48 ).toEqual([{ text: "R", style: { color: "rgb(12,34,56)" } }]);
49 });
50​
51 it("keeps a bounded tail that starts on a fresh line", () => {
52 const lines = Array.from({ length: 2_000 }, (_, index) => `line ${index}`).join("\n");
53 const state = feed([lines], 1_000);
54 expect(state.truncated).toBe(true);
55 expect(state.text.length).toBeLessThanOrEqual(1_000);
56 expect(state.text.startsWith("line ")).toBe(true);
57 expect(state.text.endsWith("line 1999")).toBe(true);
58 // A newline a quarter of the budget past the cut is too far to start on.
59 expect(feed(["abcdefgh\nij\nklmnop"], 8).text).toBe("klmnop");
60 expect(feed(["abcdefgh\nijk\nlmnop"], 8).text).toBe("jk\nlmnop");
61 });
62​
63 it("stays bounded across many appends of huge output, including one endless line", () => {
64 let state = EMPTY_TERMINAL_OUTPUT;
65 for (let index = 0; index < 5_000; index += 1) {
66 state = appendTerminalOutput(state, "y\n".repeat(200), 4_096);
67 }
68 expect(state.text.length).toBeLessThanOrEqual(4_096);
69 expect(state.truncated).toBe(true);
70 for (let index = 0; index < 1_000; index += 1) {
71 state = appendTerminalOutput(state, ".".repeat(100), 4_096);
72 }
73 expect(state.text.length).toBeLessThanOrEqual(4_096);
74 expect(state.text.endsWith(".")).toBe(true);
75 });
76​
77 it("hides a long string sequence's payload however it is split into chunks", () => {
78 const input = `a\u001b]0;${"t".repeat(100)}\u0007b`;
79 expect(feed([input]).text).toBe("ab");
80 expect(feed(["a\u001b]0;", "t".repeat(100), "\u0007b"]).text).toBe("ab");
81 expect(feed([`a\u001b]0;${"t".repeat(80)}`, "\u001b", "\\b"]).text).toBe("ab");
82 });
83​
84 it("drops an escape sequence that never terminates and resumes after it", () => {
85 expect(feed(["ok\u001b]", "x".repeat(200), "\nafter"]).text).toBe("ok\nafter");
86 });
87});
88​
89describe("appendTerminalOutput edge cases from review", () => {
90 it("leaves the screen and cursor alone for unsupported erase modes", () => {
91 for (const sequence of ["3K", "4J", "99K", "99J"]) {
92 const input = `a\nb\nc\u001b[${sequence}X`;
93 expect(feed([input]).text).toBe("a\nb\ncX");
94 for (let cut = 1; cut < input.length; cut += 1) {
95 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("a\nb\ncX");
96 }
97 }
98 });
99​
100 it("clears only scrollback on ESC[3J", () => {
101 const lines = Array.from({ length: 70 }, (_, index) => `line ${index}`).join("\n");
102 const state = feed([lines, "\u001b[3J"]);
103 expect(state.text.startsWith("line 6\n")).toBe(true);
104 expect(state.text.endsWith("line 69")).toBe(true);
105 expect(state.truncated).toBe(true);
106 });
107​
108 it("keeps the whole window within the tail when every line is long", () => {
109 const long = `${"x".repeat(900)}\n`;
110 const state = feed([long.repeat(64)], 4_096);
111 const windowChars = state.screen.lines.reduce(
112 (total, line) => total + line.reduce((sum, run) => sum + run.text.length, 0),
113 0,
114 );
115 expect(windowChars).toBeLessThanOrEqual(4_096);
116 expect(state.text.length).toBeLessThanOrEqual(4_096);
117 });
118});
119​
120describe("appendTerminalOutput under adversarial output", () => {
121 it("ignores unsupported cursor-control parameters without corrupting the screen", () => {
122 for (const sequence of ["1.5A", "1.5G", "1$K", "1$J", ">1A", "?25l", "1:2A", "1:2K", "1:2J"]) {
123 const input = `a\nb\nc\u001b[${sequence}X`;
124 expect(feed([input]).text).toBe("a\nb\ncX");
125 for (let cut = 1; cut < input.length; cut += 1) {
126 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("a\nb\ncX");
127 }
128 }
129 });
130​
131 it("gives the same result however the output is split into chunks", () => {
132 const input =
133 "abcdef\rX\u001b[31mred\u001b[0m\nnext line\r\u001b[2Kdone\n" +
134 "\u001b[1;31mbold red\u001b[mplain\u001b[38:2::12:34:56mrgb\u001b[0m\n" +
135 "\u001b[1;31mbold red again\u001b[mplain again\u001b[38:2::12:34:56mrgb again\u001b[0m";
136 for (const maxChars of [3, 1_024]) {
137 const whole = feed([input], maxChars).text;
138 for (let cut = 1; cut < input.length; cut += 1) {
139 expect(feed([input.slice(0, cut), input.slice(cut)], maxChars).text).toBe(whole);
140 }
141 }
142 expect(feed(["abcdef", "\rX"], 3).text).toBe(feed(["abcdef\rX"], 3).text);
143 });
144});
145​
146describe("terminalOutputResumeText", () => {
147 it("lets a viewer that starts from it apply later chunks exactly as the source does", () => {
148 const chunks = [
149 "a: 1%\nb: 1%\n",
150 "\u001b[2A\u001b[2K\u001b[33ma: 5",
151 "0%\u001b[0",
152 "m\n\u001b[2Kb: 50%\n",
153 ];
154 const source = feed(chunks.slice(0, 3));
155 const viewer = appendTerminalOutput(
156 appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source)),
157 chunks[3]!,
158 );
159 expect(viewer.text).toBe(feed(chunks).text);
160 expect(terminalOutputPlainText(viewer.text)).toBe("a: 50%\nb: 50%\n");
161 });
162});
163​
164describe("terminalOutputResumeText with a saved cursor or trimmed output", () => {
165 it("restores a cursor saved before the resync", () => {
166 const source = feed(["abc\u001b7def"]);
167 const viewer = appendTerminalOutput(
168 appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source)),
169 "\u001b8X",
170 );
171 expect(viewer.text).toBe("abcXef");
172 });
173​
174 it("stays on the right line when early output was trimmed away", () => {
175 const lines = Array.from({ length: 40 }, (_, index) => `line ${index}`).join("\n");
176 const source = feed([lines, "\u001b[1A\rLINE"], 120);
177 const viewer = appendTerminalOutput(
178 appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source), 120),
179 " edited",
180 120,
181 );
182 expect(viewer.text).toBe(appendTerminalOutput(source, " edited", 120).text);
183 });
184});
185​
186describe("terminalOutputTail", () => {
187 it("never begins on a lone low surrogate", () => {
188 const text = `${"a".repeat(10)}😀${"b".repeat(4)}`;
189 const tail = terminalOutputTail(text, 5);
190 expect(tail.text).toBe("bbbb");
191 });
192});
193​
194describe("terminalOutputSpans", () => {
195 it("collapses the rest into plain text past the span limit", () => {
196 const text = normalizeTerminalOutput("\u001b[31mx\u001b[32my".repeat(50)).text;
197 const spans = terminalOutputSpans(text, TERMINAL_PALETTES.light, 10);
198 expect(spans.length).toBe(11);
199 expect(spans.at(-1)?.style).toBeNull();
200 expect(spans.map((span) => span.text).join("")).toBe("xy".repeat(50));
201 });
202});