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_OUTPUT_TAIL_CHARS,
8 TERMINAL_PALETTES,
9 terminalOutputCollapseRedraws,
10 terminalOutputPlainText,
11 terminalOutputResumeText,
12 terminalOutputSpans,
13 terminalOutputTail,
14 type TerminalOutputState,
15} from "./terminalOutput.ts";
16
17function feed(chunks: ReadonlyArray<string>, maxChars?: number): TerminalOutputState {
18 return chunks.reduce(
19 (state, chunk) => appendTerminalOutput(state, chunk, maxChars),
20 EMPTY_TERMINAL_OUTPUT,
21 );
22}
23
24describe("terminalOutputCollapseRedraws", () => {
25 it.each([
26 ["12345\r进度", "进度5"],
27 ["a界b\rXY", "XY b"],
28 ["e\u0301a\rZ", "Za"],
29 ["e\u0301\rx", "x"],
30 ["a\r\u0301\ra done", "\u0301a done"],
31 ["\u0301\r\u0302ab\rx", "\u0301\u0302xb"],
32 ["\u0301abc\r", "\u0301abc"],
33 ["\u0301abc\r\u0302", "\u0301\u0302abc"],
34 ])("uses terminal columns for a saved redraw: %j", (input, expected) => {
35 expect(terminalOutputCollapseRedraws(input)).toBe(expected);
36 expect(terminalOutputPlainText(feed([input]).text)).toBe(expected);
37 });
38});
39
40describe("appendTerminalOutput", () => {
41 const red = "\u001b[0;31mX\u001b[0m";
42 it.each([
43 ["abc\u001b[\r31mX", red + "bc"],
44 ["abc\u001b\r[31mX", red + "bc"],
45 ["abc\u001b[\u0007\r31mX", red + "bc"],
46 ["abc\u001b\n[31mX", "abc\n" + red],
47 ["abcde\u001b\b\b[31mX", "abc" + red + "e"],
48 ["abc\u001b\r\u001b[31mX", red + "bc"],
49 ["abc\u001b\rXhidden\u001b\\Z", "Zbc"],
50 ])("applies line controls inside an escape across every split: %j", (input, expected) => {
51 for (let cut = 0; cut <= input.length; cut += 1) {
52 const state = appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, input.slice(0, cut));
53 const resumed = feed([terminalOutputResumeText(state)]);
54 expect(appendTerminalOutput(state, input.slice(cut)).text).toBe(expected);
55 expect(appendTerminalOutput(resumed, input.slice(cut)).text).toBe(expected);
56 }
57 });
58
59 it("keeps only the latest frame of a carriage-return progress bar", () => {
60 expect(feed(["[1/3]", "\r[2/3]", "\r[3/3] done\nnext"]).text).toBe("[3/3] done\nnext");
61 });
62
63 it.each([
64 ["😀\u001b[1Gx", "x "],
65 ["界a\u001b[3GZ", "界Z"],
66 ["e\u0301a\u001b[2GZ", "e\u0301Z"],
67 ["𐐀a\u001b[2GZ", "𐐀Z"],
68 ["界a\bZ", "界Z"],
69 ["界a\u001b[2GZ", " Za"],
70 ["a界b\rXY", "XY b"],
71 ["e\u0301a\rZ", "Za"],
72 ["界a\u001b[2G\u001b[K", " "],
73 ["界a\u001b[1G\u001b[1K", " a"],
74 ["\u001b[31m界\u001b[32ma\u001b[3GZ", "界Z"],
75 ["e\u001b[31m\u0301a\u001b[1GZ", "Za"],
76 ])("overwrites Unicode at terminal columns: %j", (input, expected) => {
77 expect(terminalOutputPlainText(feed([input]).text)).toBe(expected);
78 for (let cut = 1; cut < input.length; cut += 1) {
79 const source = feed([input.slice(0, cut)]);
80 const chunk = input.slice(cut);
81 expect(terminalOutputPlainText(appendTerminalOutput(source, chunk).text)).toBe(expected);
82 expect(terminalOutputPlainText(feed([terminalOutputResumeText(source), chunk]).text)).toBe(
83 expected,
84 );
85 }
86 });
87
88 it("redraws several lines in place when the cursor moves up", () => {
89 const frames = [
90 "layer a: 10%\nlayer b: 10%\n",
91 "\u001b[2A\u001b[2Klayer a: 60%\n\u001b[2Klayer b: 40%\n",
92 "\u001b[2A\u001b[2Klayer a: done\n\u001b[2Klayer b: done\n",
93 ];
94 expect(feed(frames).text).toBe("layer a: done\nlayer b: done\n");
95 });
96
97 it("treats CRLF as one newline even when the pair is split across chunks", () => {
98 const state = feed(["one\r", "\ntwo\r\n"]);
99 expect(state.text).toBe("one\ntwo\n");
100 expect(state.pending).toBe("");
101 });
102
103 it("holds an escape sequence split across chunks until it finishes", () => {
104 const first = appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, "red: \u001b[3");
105 expect(first.text).toBe("red: ");
106 expect(feed(["red: \u001b[3", "1mR\u001b[0m"]).text).toBe("red: \u001b[0;31mR\u001b[0m");
107 expect(
108 terminalOutputSpans(feed(["\u001b[38:2::12:", "34:56mR"]).text, TERMINAL_PALETTES.dark),
109 ).toEqual([{ text: "R", style: { color: "rgb(12,34,56)" } }]);
110 });
111
112 it.each([
113 ["A\u001b\u0000", "[31mB\u001b[0mC"],
114 ["A\u001b[3\u0000", "1mB\u001b[0mC"],
115 ["A\u001b(\u0000", "B\u001b[31mB\u001b[0mC"],
116 [`A\u001b(${"\u0000".repeat(63)}`, "B\u001b[31mB\u001b[0mC"],
117 [`A\u001b[3${"\u0000".repeat(63)}`, "1mB\u001b[0mC"],
118 ])("ignores NUL padding inside a split escape: %j", (first, second) => {
119 const expected = feed(["A\u001b[31mB\u001b[0mC"]).text;
120 expect(feed([first, second]).text).toBe(expected);
121 expect(feed([first + second]).text).toBe(expected);
122 });
123
124 it.each(["\u001b[3", "\u001b]0;title"])(
125 "keeps output after CAN or SUB cancels %j, wherever the chunk splits",
126 (sequence) => {
127 for (const cancel of ["\u0018", "\u001a"]) {
128 const input = `before${sequence}${cancel}Ready`;
129 for (let cut = 0; cut <= input.length; cut += 1) {
130 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("beforeReady");
131 }
132 }
133 },
134 );
135
136 it.each([
137 ["构建失败\nError", "before建失败\nError"],
138 ["😀 ok", "before ok"],
139 ["\ud83dX", "beforeX"],
140 ])("keeps output after non-ASCII %j aborts an unfinished CSI", (tail, expected) => {
141 const input = `before\u001b[3${tail}`;
142 for (let cut = 0; cut <= input.length; cut += 1) {
143 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe(expected);
144 }
145 });
146
147 it("keeps a bounded tail that starts on a fresh line", () => {
148 const lines = Array.from({ length: 2_000 }, (_, index) => `line ${index}`).join("\n");
149 const state = feed([lines], 1_000);
150 expect(state.truncated).toBe(true);
151 expect(state.text.length).toBeLessThanOrEqual(1_000);
152 expect(state.text.startsWith("line ")).toBe(true);
153 expect(state.text.endsWith("line 1999")).toBe(true);
154 // A newline a quarter of the budget past the cut is too far to start on.
155 expect(feed(["abcdefgh\nij\nklmnop"], 8).text).toBe("klmnop");
156 expect(feed(["abcdefgh\nijk\nlmnop"], 8).text).toBe("jk\nlmnop");
157 });
158
159 it("keeps the first complete line when output exactly fills the tail", () => {
160 const exactDefault = `ok\n${"x".repeat(TERMINAL_OUTPUT_TAIL_CHARS - 3)}`;
161 expect(normalizeTerminalOutput(`junk\n${exactDefault}`)).toEqual({
162 text: exactDefault,
163 truncated: true,
164 });
165
166 // Cut landing right between a settled part ending in "\n", an empty settledTail, and the window.
167 const seeded = feed([`${"old\n".repeat(70)}`], 20);
168 const nextWindow =
169 seeded.screen.lines.map((line) => line.map((run) => run.text).join("")).join("\n") + "z";
170 const exactWindow = appendTerminalOutput(seeded, "z", nextWindow.length);
171 expect(seeded.screen.settled[0].endsWith("\n")).toBe(true);
172 expect(seeded.screen.settled[1]).toBe("");
173 expect(exactWindow.text).toBe(nextWindow);
174 expect(exactWindow.truncated).toBe(true);
175 });
176
177 it("keeps Unicode tails bounded without shifting saved cursor columns", () => {
178 const source = feed(["界界e\u0301\u001b7ab"], 4);
179 expect(source.text).toBe("e\u0301ab");
180 expect(source.screen.col).toBe(3);
181 expect(source.screen.saved?.col).toBe(1);
182 expect(appendTerminalOutput(source, "\u001b8Z", 4).text).toBe("e\u0301Zb");
183 expect(feed([terminalOutputResumeText(source), "\u001b8Z"], 4).text).toBe("e\u0301Zb");
184 expect(feed(["𐐀a"], 2).text).toBe("a");
185 expect(feed(["e\u0301ab"], 3).text).toBe("ab");
186 const wide = feed(["界".repeat(40_000), "\u001b7"]);
187 expect(feed([terminalOutputResumeText(wide)]).screen.col).toBe(wide.screen.col);
188 });
189
190 it.each([".", "界", "😀", "\u001b[31m界\u001b[0m"])(
191 "stays bounded across many appends of huge output, including one endless line of %s",
192 (character) => {
193 let state = EMPTY_TERMINAL_OUTPUT;
194 for (let index = 0; index < 5_000; index += 1) {
195 state = appendTerminalOutput(state, "y\n".repeat(200), 4_096);
196 }
197 expect(state.text.length).toBeLessThanOrEqual(4_096);
198 expect(state.truncated).toBe(true);
199 for (let index = 0; index < 1_000; index += 1) {
200 state = appendTerminalOutput(state, character.repeat(100), 4_096);
201 }
202 expect(terminalOutputPlainText(state.text).length).toBeLessThanOrEqual(4_096);
203 expect(terminalOutputPlainText(state.text).endsWith(terminalOutputPlainText(character))).toBe(
204 true,
205 );
206 },
207 );
208
209 it("hides a long string sequence's payload however it is split into chunks", () => {
210 const input = `a\u001b]0;${"t".repeat(100)}\u0007b`;
211 expect(feed([input]).text).toBe("ab");
212 expect(feed(["a\u001b]0;", "t".repeat(100), "\u0007b"]).text).toBe("ab");
213 expect(feed([`a\u001b]0;${"t".repeat(80)}`, "\u001b", "\\b"]).text).toBe("ab");
214 });
215
216 it("keeps a DCS payload hidden past a BEL until ST, wherever the chunk splits", () => {
217 const dcs = "\u001bPqA\u0007BBB\u001b\\OK";
218 for (let cut = 0; cut <= dcs.length; cut += 1) {
219 const state = feed([dcs.slice(0, cut)]);
220 expect(appendTerminalOutput(state, dcs.slice(cut)).text).toBe("OK");
221 expect(feed([terminalOutputResumeText(state), dcs.slice(cut)]).text).toBe("OK");
222 }
223 });
224
225 it.each([
226 ["\u001b]8;;https://example.test\u009cvisible\u001b]8;;\u009c", "visible"],
227 ["\u009b31mRED\u009b0m", "\u001b[0;31mRED\u001b[0m"],
228 [
229 "\u001bPq\u009cOK\u009dhidden\u0007\u0090\u009c\u009e\u009c\u009f\u009c\u0098\u009cdone",
230 "OKdone",
231 ],
232 [`\u001b]0;${"a".repeat(100)}8;left\u009cright\u0007visible`, "rightvisible"],
233 ])("treats C1 introducers and ST like their ESC forms at every split: %j", (input, expected) => {
234 expect(normalizeTerminalOutput(input).text).toBe(expected);
235 for (let cut = 0; cut <= input.length; cut += 1) {
236 const state = feed([input.slice(0, cut)]);
237 const done = appendTerminalOutput(state, input.slice(cut));
238 expect(done.text).toBe(expected);
239 expect(done.pending).toBe("");
240 expect(feed([terminalOutputResumeText(state), input.slice(cut)]).text).toBe(expected);
241 }
242 });
243
244 it("drops an escape sequence that never terminates and resumes after it", () => {
245 expect(feed(["ok\u001b]", "x".repeat(200), "\nafter"]).text).toBe("ok\nafter");
246 expect(feed(["ok\u001b]0;title\u001b", "\u001b[31mred"]).text).toBe(
247 "ok\u001b[0;31mred\u001b[0m",
248 );
249 for (const input of ["a\u001b[1;\u001b[32mb\u001b[0m", "a\u001b[\u001b[32mb"]) {
250 expect(normalizeTerminalOutput(input).text).toBe("a\u001b[0;32mb\u001b[0m");
251 for (let cut = 1; cut < input.length; cut += 1) {
252 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("a\u001b[0;32mb\u001b[0m");
253 }
254 }
255 });
256});
257
258describe("appendTerminalOutput edge cases from review", () => {
259 const boldRed = { color: TERMINAL_PALETTES.dark[1], fontWeight: "600" } as const;
260 it.each([
261 [
262 "a curly underline",
263 "\u001b[1;31m\u001b[4:3mX",
264 { ...boldRed, textDecorationLine: "underline" },
265 ],
266 ["underline off in colon form", "\u001b[1;31m\u001b[4:0mX", boldRed],
267 ["a true-colour underline colour", "\u001b[1;31m\u001b[58;2;255;0;0mX", boldRed],
268 ["a 256-colour underline colour", "\u001b[58;5;31mX", null],
269 [
270 "a colon-form underline colour",
271 "\u001b[4m\u001b[58:2::255:0:0mX",
272 { textDecorationLine: "underline" },
273 ],
274 ["extra fields in a colon-form indexed underline colour", "\u001b[58:5:0:32mX", null],
275 [
276 "extra fields in a colon-form indexed colour",
277 "\u001b[38:5:1:4mX",
278 { color: TERMINAL_PALETTES.dark[1] },
279 ],
280 [
281 "a true colour with its fields after colons",
282 "\u001b[38;2:255:0:0;1mX",
283 { color: "rgb(255,0,0)", fontWeight: "600" },
284 ],
285 [
286 "an indexed colour with its index after a colon",
287 "\u001b[38;5:1;4mX",
288 { color: TERMINAL_PALETTES.dark[1], textDecorationLine: "underline" },
289 ],
290 [
291 "a semicolon-form colour value that looks like a colour code",
292 "\u001b[38;5;38;2:1:2:3mX",
293 { color: "rgb(0,175,215)", opacity: 0.65 },
294 ],
295 ])("reads %s without disturbing the other attributes", (_, input, style) => {
296 const text = normalizeTerminalOutput(input).text;
297 expect(terminalOutputSpans(text, TERMINAL_PALETTES.dark)).toEqual([{ text: "X", style }]);
298 for (let cut = 1; cut < input.length; cut += 1) {
299 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe(text);
300 }
301 });
302
303 it("leaves the screen and cursor alone for unsupported erase modes", () => {
304 for (const sequence of ["3K", "4J", "99K", "99J"]) {
305 const input = `a\nb\nc\u001b[${sequence}X`;
306 expect(feed([input]).text).toBe("a\nb\ncX");
307 for (let cut = 1; cut < input.length; cut += 1) {
308 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("a\nb\ncX");
309 }
310 }
311 });
312
313 it("clears only scrollback on ESC[3J", () => {
314 const lines = Array.from({ length: 70 }, (_, index) => `line ${index}`).join("\n");
315 const state = feed([lines, "\u001b[3J"]);
316 expect(state.text.startsWith("line 6\n")).toBe(true);
317 expect(state.text.endsWith("line 69")).toBe(true);
318 expect(state.truncated).toBe(true);
319 });
320
321 it("keeps the whole window within the tail when every line is long", () => {
322 const long = `${"x".repeat(900)}\n`;
323 const state = feed([long.repeat(64)], 4_096);
324 const windowChars = state.screen.lines.reduce(
325 (total, line) => total + line.reduce((sum, run) => sum + run.text.length, 0),
326 0,
327 );
328 expect(windowChars).toBeLessThanOrEqual(4_096);
329 expect(state.text.length).toBeLessThanOrEqual(4_096);
330 for (const prefix of ["😀", "e\u0301"]) {
331 expect(feed([`a\n${prefix}bx\nZ\u001b[2A`], 4).text).toBe("bx\nZ");
332 }
333 });
334});
335
336describe("appendTerminalOutput under adversarial output", () => {
337 it("ignores unsupported cursor-control parameters without corrupting the screen", () => {
338 for (const sequence of ["1.5A", "1.5G", "1$K", "1$J", ">1A", "?25l", "1:2A", "1:2K", "1:2J"]) {
339 const input = `a\nb\nc\u001b[${sequence}X`;
340 expect(feed([input]).text).toBe("a\nb\ncX");
341 for (let cut = 1; cut < input.length; cut += 1) {
342 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("a\nb\ncX");
343 }
344 }
345 });
346
347 it("drops escape sequences with intermediate bytes without printing their final byte", () => {
348 for (const sequence of ["#8", "#6", "%G", " F", "-A", ".A", "(%5", "(B"]) {
349 const input = `a\u001b${sequence}b`;
350 expect(feed([input]).text).toBe("ab");
351 for (let cut = 1; cut < input.length; cut += 1) {
352 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("ab");
353 }
354 }
355 for (const [interrupted, expected] of [
356 ["a\u001b#\u001b[31mb", feed(["a\u001b[31mb"]).text],
357 ["a\u001b#\n8b", "a\n8b"],
358 ] as const) {
359 for (let cut = 0; cut < interrupted.length; cut += 1) {
360 expect(feed([interrupted.slice(0, cut), interrupted.slice(cut)]).text).toBe(expected);
361 }
362 }
363 });
364
365 it("consumes an escape followed by an emoji whole, however it is split", () => {
366 for (const [input, expected] of [
367 ["a\u001b😀b", "ab"],
368 ["a\u001b\ud83dX", "aX"],
369 ] as const) {
370 expect(normalizeTerminalOutput(input).text).toBe(expected);
371 for (let cut = 1; cut < input.length; cut += 1) {
372 expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe(expected);
373 }
374 }
375 });
376
377 it("gives the same result however the output is split into chunks", () => {
378 const input =
379 "abcdef\rX\u001b[31mred\u001b[0m\nnext line\r\u001b[2Kdone\n" +
380 "\u001b[1;31mbold red\u001b[mplain\u001b[38:2::12:34:56mrgb\u001b[0m\n" +
381 "\u001b[1;31mbold red again\u001b[mplain again\u001b[38:2::12:34:56mrgb again\u001b[0m";
382 for (const maxChars of [3, 1_024]) {
383 const whole = feed([input], maxChars).text;
384 for (let cut = 1; cut < input.length; cut += 1) {
385 expect(feed([input.slice(0, cut), input.slice(cut)], maxChars).text).toBe(whole);
386 }
387 }
388 expect(feed(["abcdef", "\rX"], 3).text).toBe(feed(["abcdef\rX"], 3).text);
389 expect(feed(["a\r\u0301", "a"]).text).toBe("\u0301a");
390 });
391});
392
393describe("terminalOutputResumeText", () => {
394 it("lets a viewer that starts from it apply later chunks exactly as the source does", () => {
395 const chunks = [
396 "a: 1%\nb: 1%\n",
397 "\u001b[2A\u001b[2K\u001b[33ma: 5",
398 "0%\u001b[0",
399 "m\n\u001b[2Kb: 50%\n",
400 ];
401 const source = feed(chunks.slice(0, 3));
402 const viewer = appendTerminalOutput(
403 appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source)),
404 chunks[3]!,
405 );
406 expect(viewer.text).toBe(feed(chunks).text);
407 expect(terminalOutputPlainText(viewer.text)).toBe("a: 50%\nb: 50%\n");
408 });
409});
410
411describe("terminalOutputResumeText with a saved cursor or trimmed output", () => {
412 it("restores a cursor saved before the resync", () => {
413 const source = feed(["界e\u0301\u001b7def"]);
414 const viewer = appendTerminalOutput(
415 appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source)),
416 "\u001b8X",
417 );
418 expect(viewer.text).toBe("界e\u0301Xef");
419 });
420
421 it("restores the saved text color with the DEC cursor, including after resync", () => {
422 const red = { color: TERMINAL_PALETTES.dark[1] };
423 const green = { color: TERMINAL_PALETTES.dark[2] };
424
425 const direct = feed(["\u001b[31mR\u001b7\u001b[32mG\u001b8X"]);
426 expect(terminalOutputSpans(direct.text, TERMINAL_PALETTES.dark)).toEqual([
427 { text: "RX", style: red },
428 ]);
429
430 const source = feed(["\u001b[31mR\u001b7\u001b[32mG"]);
431 const resumed = feed([terminalOutputResumeText(source), "\u001b8X"]);
432 expect(terminalOutputSpans(resumed.text, TERMINAL_PALETTES.dark)).toEqual([
433 { text: "RX", style: red },
434 ]);
435
436 const resetActive = feed(["\u001b[31mR\u001b7\u001b[0m"]);
437 expect(
438 terminalOutputSpans(
439 feed([terminalOutputResumeText(resetActive), "P"]).text,
440 TERMINAL_PALETTES.dark,
441 ),
442 ).toEqual([
443 { text: "R", style: red },
444 { text: "P", style: null },
445 ]);
446
447 const savedDefault = feed(["\u001b7\u001b[32mG\u001b8X"]);
448 expect(terminalOutputSpans(savedDefault.text, TERMINAL_PALETTES.dark)).toEqual([
449 { text: "X", style: null },
450 ]);
451
452 const sco = feed(["\u001b[31mR\u001b[s\u001b[32mG"]);
453 expect(
454 terminalOutputSpans(
455 feed([terminalOutputResumeText(sco), "\u001b[uX"]).text,
456 TERMINAL_PALETTES.dark,
457 ),
458 ).toEqual([
459 { text: "R", style: red },
460 { text: "X", style: green },
461 ]);
462 });
463
464 it("restores a cursor saved on a row erased before the resync", () => {
465 for (const [prefix, next, expected] of [
466 ["\n\u001b7\u001b[A\u001b[J", "A\nB\u001b8Z", "A\nZ"],
467 [
468 "hello\n\n \u001b7\u001b[2A\u001b[6G\u001b[J\u001b[2G",
469 "X\n\n1234\u001b8Z",
470 "hXllo\n\n12Z4",
471 ],
472 ["\u0301\n\u001b7\u001b[A\u001b[2G\u001b[J", "\nB\u001b8Z", "\u0301\nZ"],
473 [
474 `Build started\n${"working\n".repeat(63)}\u001b7\u001b[63A\u001b[J`,
475 `A${"\n".repeat(63)}B\u001b8Z`,
476 `Build started\nA${"\n".repeat(63)}Z`,
477 ],
478 ] as const) {
479 const source = feed([prefix]);
480 const viewer = feed([terminalOutputResumeText(source)]);
481 expect(viewer.text).toBe(source.text);
482 expect(appendTerminalOutput(viewer, next).text).toBe(appendTerminalOutput(source, next).text);
483 expect(appendTerminalOutput(viewer, next).text).toBe(expected);
484 }
485 });
486
487 it("stays on the right line when early output was trimmed away", () => {
488 const lines = Array.from({ length: 40 }, (_, index) => `line ${index}`).join("\n");
489 const source = feed([lines, "\u001b[1A\rLINE"], 120);
490 const viewer = appendTerminalOutput(
491 appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source), 120),
492 " edited",
493 120,
494 );
495 expect(viewer.text).toBe(appendTerminalOutput(source, " edited", 120).text);
496 });
497
498 it("keeps scrollback when a redraw after a cleared screen moves above the live window", () => {
499 const history = "Build started\n";
500 const source = feed([history + "working\n".repeat(63), "\u001b[2J", "Progress: 10%"]);
501 const redraw = "\u001b[1A\r\u001b[2KProgress: 20%";
502 expect(feed([terminalOutputResumeText(source), redraw]).text).toBe(history + "Progress: 20%");
503 });
504
505 it("recovers scrollback hidden by a full output tail once a redraw shortens it", () => {
506 const history = "Build started\n";
507 const progress = "x".repeat(TERMINAL_OUTPUT_TAIL_CHARS);
508 const source = feed([history + "working\n".repeat(63) + "\u001b[2J" + progress]);
509 const viewer = feed([terminalOutputResumeText(source)]);
510 expect(viewer.text).toBe(progress);
511 const redraw = "\r\u001b[2KProgress: done";
512 expect(appendTerminalOutput(viewer, redraw).text).toBe(history + "Progress: done");
513 });
514});
515
516describe("terminalOutputTail", () => {
517 it("drops a detached emoji skin tone at the start of a command tail", () => {
518 const output = normalizeTerminalOutput("before👍🏽done", 6);
519 expect(terminalOutputPlainText(output.text)).toBe("done");
520 });
521
522 it("drops a detached skin tone from serialized output and keeps its active style", () => {
523 const tail = terminalOutputTail("before👍\u001b[0;31m🏽done\u001b[0m", 10);
524 expect(tail).toEqual({ text: "\u001b[0;31mdone\u001b[0m", truncated: true });
525 });
526
527 it.each([
528 ["tone: 🏽", 2, 2, "🏽"],
529 ["\u001b[31m🖐️\u001b[0m🏽X", 3, 3, "X"],
530 ["✌️🏽X", 4, 4, "X"],
531 ["✌️🏽X", 3, 3, "X"],
532 ["✌\u001b[0;31m️\u001b[0m🏽X", 3, 8, "X"],
533 ["xx\u001b[0;31m🇺\u001b[0m🇸🇫🇷", 6, 6, "🇫🇷"],
534 ["xx🇺🇸🇫🇷", 4, 4, "🇫🇷"],
535 ] as const)("keeps complete glyphs in %j", (text, live, saved, expected) => {
536 expect(terminalOutputPlainText(normalizeTerminalOutput(text, live).text)).toBe(expected);
537 expect(terminalOutputPlainText(terminalOutputTail(text, saved).text)).toBe(expected);
538 });
539
540 it("never begins on a lone low surrogate", () => {
541 const text = `${"a".repeat(10)}😀${"b".repeat(4)}`;
542 const tail = terminalOutputTail(text, 5);
543 expect(tail.text).toBe("bbbb");
544 });
545
546 it("never begins a styled tail on a detached combining mark", () => {
547 const red = { color: TERMINAL_PALETTES.dark[1] };
548 const output = normalizeTerminalOutput(`\u001b[31m${"e\u0301".repeat(8)}A`, 16);
549 expect(terminalOutputSpans(output.text, TERMINAL_PALETTES.dark)).toEqual([
550 { text: `${"e\u0301".repeat(5)}A`, style: red },
551 ]);
552
553 const crossStyle = terminalOutputTail("\u001b[0;32me\u001b[0;31m\u0301A\u001b[0m", 13);
554 expect(terminalOutputSpans(crossStyle.text, TERMINAL_PALETTES.dark)).toEqual([
555 { text: "A", style: red },
556 ]);
557 });
558
559 it("keeps the first complete line when the cut falls immediately after a newline", () => {
560 const expected = `abc\n${"x".repeat(30)}`;
561 expect(terminalOutputTail(`junk\n${expected}`, 34)).toEqual({
562 text: expected,
563 truncated: true,
564 });
565 });
566});
567
568describe("terminalOutputSpans", () => {
569 it("collapses the rest into plain text past the span limit", () => {
570 const text = normalizeTerminalOutput("\u001b[31mx\u001b[32my".repeat(50)).text;
571 const spans = terminalOutputSpans(text, TERMINAL_PALETTES.light, 10);
572 expect(spans.length).toBe(11);
573 expect(spans.at(-1)?.style).toBeNull();
574 expect(spans.map((span) => span.text).join("")).toBe("xy".repeat(50));
575 });
576
577 it("gives badge text at least 3:1 on the dark palette's coloured backgrounds", () => {
578 const text = normalizeTerminalOutput("\u001b[42m PASS \u001b[1;37;41m FAIL ").text;
579 const spans = terminalOutputSpans(text, TERMINAL_PALETTES.dark);
580 expect(spans.map((span) => span.style?.color)).toEqual(["#1f2328", "#1f2328"]);
581 });
582});