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