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

terminalOutput.test.ts

179 lines7.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 });
47​
48 it("keeps a bounded tail that starts on a fresh line", () => {
49 const lines = Array.from({ length: 2_000 }, (_, index) => `line ${index}`).join("\n");
50 const state = feed([lines], 1_000);
51 expect(state.truncated).toBe(true);
52 expect(state.text.length).toBeLessThanOrEqual(1_000);
53 expect(state.text.startsWith("line ")).toBe(true);
54 expect(state.text.endsWith("line 1999")).toBe(true);
55 // A newline a quarter of the budget past the cut is too far to start on.
56 expect(feed(["abcdefgh\nij\nklmnop"], 8).text).toBe("klmnop");
57 expect(feed(["abcdefgh\nijk\nlmnop"], 8).text).toBe("jk\nlmnop");
58 });
59​
60 it("stays bounded across many appends of huge output, including one endless line", () => {
61 let state = EMPTY_TERMINAL_OUTPUT;
62 for (let index = 0; index < 5_000; index += 1) {
63 state = appendTerminalOutput(state, "y\n".repeat(200), 4_096);
64 }
65 expect(state.text.length).toBeLessThanOrEqual(4_096);
66 expect(state.truncated).toBe(true);
67 for (let index = 0; index < 1_000; index += 1) {
68 state = appendTerminalOutput(state, ".".repeat(100), 4_096);
69 }
70 expect(state.text.length).toBeLessThanOrEqual(4_096);
71 expect(state.text.endsWith(".")).toBe(true);
72 });
73​
74 it("hides a long string sequence's payload however it is split into chunks", () => {
75 const input = `a\u001b]0;${"t".repeat(100)}\u0007b`;
76 expect(feed([input]).text).toBe("ab");
77 expect(feed(["a\u001b]0;", "t".repeat(100), "\u0007b"]).text).toBe("ab");
78 expect(feed([`a\u001b]0;${"t".repeat(80)}`, "\u001b", "\\b"]).text).toBe("ab");
79 });
80​
81 it("drops an escape sequence that never terminates and resumes after it", () => {
82 expect(feed(["ok\u001b]", "x".repeat(200), "\nafter"]).text).toBe("ok\nafter");
83 });
84});
85​
86describe("appendTerminalOutput edge cases from review", () => {
87 it("clears only scrollback on ESC[3J", () => {
88 const lines = Array.from({ length: 70 }, (_, index) => `line ${index}`).join("\n");
89 const state = feed([lines, "\u001b[3J"]);
90 expect(state.text.startsWith("line 6\n")).toBe(true);
91 expect(state.text.endsWith("line 69")).toBe(true);
92 expect(state.truncated).toBe(true);
93 });
94​
95 it("keeps the whole window within the tail when every line is long", () => {
96 const long = `${"x".repeat(900)}\n`;
97 const state = feed([long.repeat(64)], 4_096);
98 const windowChars = state.screen.lines.reduce(
99 (total, line) => total + line.reduce((sum, run) => sum + run.text.length, 0),
100 0,
101 );
102 expect(windowChars).toBeLessThanOrEqual(4_096);
103 expect(state.text.length).toBeLessThanOrEqual(4_096);
104 });
105});
106​
107describe("appendTerminalOutput under adversarial output", () => {
108 it("gives the same result however the output is split into chunks", () => {
109 const input =
110 "abcdef\rX\u001b[31mred\u001b[0m\nnext line\r\u001b[2Kdone\n" +
111 "\u001b[1;31mbold red\u001b[mplain\u001b[38:2::12:34:56mrgb\u001b[0m\n" +
112 "\u001b[1;31mbold red again\u001b[mplain again\u001b[38:2::12:34:56mrgb again\u001b[0m";
113 for (const maxChars of [3, 1_024]) {
114 const whole = feed([input], maxChars).text;
115 for (let cut = 1; cut < input.length; cut += 1) {
116 expect(feed([input.slice(0, cut), input.slice(cut)], maxChars).text).toBe(whole);
117 }
118 }
119 expect(feed(["abcdef", "\rX"], 3).text).toBe(feed(["abcdef\rX"], 3).text);
120 });
121});
122​
123describe("terminalOutputResumeText", () => {
124 it("lets a viewer that starts from it apply later chunks exactly as the source does", () => {
125 const chunks = [
126 "a: 1%\nb: 1%\n",
127 "\u001b[2A\u001b[2K\u001b[33ma: 5",
128 "0%\u001b[0",
129 "m\n\u001b[2Kb: 50%\n",
130 ];
131 const source = feed(chunks.slice(0, 3));
132 const viewer = appendTerminalOutput(
133 appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source)),
134 chunks[3]!,
135 );
136 expect(viewer.text).toBe(feed(chunks).text);
137 expect(terminalOutputPlainText(viewer.text)).toBe("a: 50%\nb: 50%\n");
138 });
139});
140​
141describe("terminalOutputResumeText with a saved cursor or trimmed output", () => {
142 it("restores a cursor saved before the resync", () => {
143 const source = feed(["abc\u001b7def"]);
144 const viewer = appendTerminalOutput(
145 appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source)),
146 "\u001b8X",
147 );
148 expect(viewer.text).toBe("abcXef");
149 });
150​
151 it("stays on the right line when early output was trimmed away", () => {
152 const lines = Array.from({ length: 40 }, (_, index) => `line ${index}`).join("\n");
153 const source = feed([lines, "\u001b[1A\rLINE"], 120);
154 const viewer = appendTerminalOutput(
155 appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source), 120),
156 " edited",
157 120,
158 );
159 expect(viewer.text).toBe(appendTerminalOutput(source, " edited", 120).text);
160 });
161});
162​
163describe("terminalOutputTail", () => {
164 it("never begins on a lone low surrogate", () => {
165 const text = `${"a".repeat(10)}😀${"b".repeat(4)}`;
166 const tail = terminalOutputTail(text, 5);
167 expect(tail.text).toBe("bbbb");
168 });
169});
170​
171describe("terminalOutputSpans", () => {
172 it("collapses the rest into plain text past the span limit", () => {
173 const text = normalizeTerminalOutput("\u001b[31mx\u001b[32my".repeat(50)).text;
174 const spans = terminalOutputSpans(text, TERMINAL_PALETTES.light, 10);
175 expect(spans.length).toBe(11);
176 expect(spans.at(-1)?.style).toBeNull();
177 expect(spans.map((span) => span.text).join("")).toBe("xy".repeat(50));
178 });
179});