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