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

terminalOutput.test.ts

544 lines21.8 KB

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