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