The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 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 | |
| 15 | function 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 | |
| 22 | describe("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.each([ |
| 28 | ["😀\u001b[1Gx", "x "], |
| 29 | ["界a\u001b[3GZ", "界Z"], |
| 30 | ["e\u0301a\u001b[2GZ", "e\u0301Z"], |
| 31 | ["𐐀a\u001b[2GZ", "𐐀Z"], |
| 32 | ["界a\bZ", "界Z"], |
| 33 | ["界a\u001b[2GZ", " Za"], |
| 34 | ["a界b\rXY", "XY b"], |
| 35 | ["e\u0301a\rZ", "Za"], |
| 36 | ["界a\u001b[2G\u001b[K", " "], |
| 37 | ["界a\u001b[1G\u001b[1K", " a"], |
| 38 | ["\u001b[31m界\u001b[32ma\u001b[3GZ", "界Z"], |
| 39 | ["e\u001b[31m\u0301a\u001b[1GZ", "Za"], |
| 40 | ])("overwrites Unicode at terminal columns: %j", (input, expected) => { |
| 41 | expect(terminalOutputPlainText(feed([input]).text)).toBe(expected); |
| 42 | for (let cut = 1; cut < input.length; cut += 1) { |
| 43 | const source = feed([input.slice(0, cut)]); |
| 44 | const chunk = input.slice(cut); |
| 45 | expect(terminalOutputPlainText(appendTerminalOutput(source, chunk).text)).toBe(expected); |
| 46 | expect(terminalOutputPlainText(feed([terminalOutputResumeText(source), chunk]).text)).toBe( |
| 47 | expected, |
| 48 | ); |
| 49 | } |
| 50 | }); |
| 51 | |
| 52 | it("redraws several lines in place when the cursor moves up", () => { |
| 53 | const frames = [ |
| 54 | "layer a: 10%\nlayer b: 10%\n", |
| 55 | "\u001b[2A\u001b[2Klayer a: 60%\n\u001b[2Klayer b: 40%\n", |
| 56 | "\u001b[2A\u001b[2Klayer a: done\n\u001b[2Klayer b: done\n", |
| 57 | ]; |
| 58 | expect(feed(frames).text).toBe("layer a: done\nlayer b: done\n"); |
| 59 | }); |
| 60 | |
| 61 | it("treats CRLF as one newline even when the pair is split across chunks", () => { |
| 62 | const state = feed(["one\r", "\ntwo\r\n"]); |
| 63 | expect(state.text).toBe("one\ntwo\n"); |
| 64 | expect(state.pending).toBe(""); |
| 65 | }); |
| 66 | |
| 67 | it("holds an escape sequence split across chunks until it finishes", () => { |
| 68 | const first = appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, "red: \u001b[3"); |
| 69 | expect(first.text).toBe("red: "); |
| 70 | expect(feed(["red: \u001b[3", "1mR\u001b[0m"]).text).toBe("red: \u001b[0;31mR\u001b[0m"); |
| 71 | expect( |
| 72 | terminalOutputSpans(feed(["\u001b[38:2::12:", "34:56mR"]).text, TERMINAL_PALETTES.dark), |
| 73 | ).toEqual([{ text: "R", style: { color: "rgb(12,34,56)" } }]); |
| 74 | }); |
| 75 | |
| 76 | it("keeps a bounded tail that starts on a fresh line", () => { |
| 77 | const lines = Array.from({ length: 2_000 }, (_, index) => `line ${index}`).join("\n"); |
| 78 | const state = feed([lines], 1_000); |
| 79 | expect(state.truncated).toBe(true); |
| 80 | expect(state.text.length).toBeLessThanOrEqual(1_000); |
| 81 | expect(state.text.startsWith("line ")).toBe(true); |
| 82 | expect(state.text.endsWith("line 1999")).toBe(true); |
| 83 | // A newline a quarter of the budget past the cut is too far to start on. |
| 84 | expect(feed(["abcdefgh\nij\nklmnop"], 8).text).toBe("klmnop"); |
| 85 | expect(feed(["abcdefgh\nijk\nlmnop"], 8).text).toBe("jk\nlmnop"); |
| 86 | }); |
| 87 | |
| 88 | it("keeps Unicode tails bounded without shifting saved cursor columns", () => { |
| 89 | const source = feed(["界界e\u0301\u001b7ab"], 4); |
| 90 | expect(source.text).toBe("e\u0301ab"); |
| 91 | expect(source.screen.col).toBe(3); |
| 92 | expect(source.screen.saved?.col).toBe(1); |
| 93 | expect(appendTerminalOutput(source, "\u001b8Z", 4).text).toBe("e\u0301Zb"); |
| 94 | expect(feed([terminalOutputResumeText(source), "\u001b8Z"], 4).text).toBe("e\u0301Zb"); |
| 95 | expect(feed(["𐐀a"], 2).text).toBe("a"); |
| 96 | expect(feed(["e\u0301ab"], 3).text).toBe("ab"); |
| 97 | const wide = feed(["界".repeat(40_000), "\u001b7"]); |
| 98 | expect(feed([terminalOutputResumeText(wide)]).screen.col).toBe(wide.screen.col); |
| 99 | }); |
| 100 | |
| 101 | it.each([".", "界", "😀", "\u001b[31m界\u001b[0m"])( |
| 102 | "stays bounded across many appends of huge output, including one endless line of %s", |
| 103 | (character) => { |
| 104 | let state = EMPTY_TERMINAL_OUTPUT; |
| 105 | for (let index = 0; index < 5_000; index += 1) { |
| 106 | state = appendTerminalOutput(state, "y\n".repeat(200), 4_096); |
| 107 | } |
| 108 | expect(state.text.length).toBeLessThanOrEqual(4_096); |
| 109 | expect(state.truncated).toBe(true); |
| 110 | for (let index = 0; index < 1_000; index += 1) { |
| 111 | state = appendTerminalOutput(state, character.repeat(100), 4_096); |
| 112 | } |
| 113 | expect(terminalOutputPlainText(state.text).length).toBeLessThanOrEqual(4_096); |
| 114 | expect(terminalOutputPlainText(state.text).endsWith(terminalOutputPlainText(character))).toBe( |
| 115 | true, |
| 116 | ); |
| 117 | }, |
| 118 | ); |
| 119 | |
| 120 | it("hides a long string sequence's payload however it is split into chunks", () => { |
| 121 | const input = `a\u001b]0;${"t".repeat(100)}\u0007b`; |
| 122 | expect(feed([input]).text).toBe("ab"); |
| 123 | expect(feed(["a\u001b]0;", "t".repeat(100), "\u0007b"]).text).toBe("ab"); |
| 124 | expect(feed([`a\u001b]0;${"t".repeat(80)}`, "\u001b", "\\b"]).text).toBe("ab"); |
| 125 | }); |
| 126 | |
| 127 | it("drops an escape sequence that never terminates and resumes after it", () => { |
| 128 | expect(feed(["ok\u001b]", "x".repeat(200), "\nafter"]).text).toBe("ok\nafter"); |
| 129 | expect(feed(["ok\u001b]0;title\u001b", "\u001b[31mred"]).text).toBe( |
| 130 | "ok\u001b[0;31mred\u001b[0m", |
| 131 | ); |
| 132 | }); |
| 133 | }); |
| 134 | |
| 135 | describe("appendTerminalOutput edge cases from review", () => { |
| 136 | const boldRed = { color: TERMINAL_PALETTES.dark[1], fontWeight: "600" } as const; |
| 137 | it.each([ |
| 138 | [ |
| 139 | "a curly underline", |
| 140 | "\u001b[1;31m\u001b[4:3mX", |
| 141 | { ...boldRed, textDecorationLine: "underline" }, |
| 142 | ], |
| 143 | ["underline off in colon form", "\u001b[1;31m\u001b[4:0mX", boldRed], |
| 144 | ["a true-colour underline colour", "\u001b[1;31m\u001b[58;2;255;0;0mX", boldRed], |
| 145 | ["a 256-colour underline colour", "\u001b[58;5;31mX", null], |
| 146 | [ |
| 147 | "a colon-form underline colour", |
| 148 | "\u001b[4m\u001b[58:2::255:0:0mX", |
| 149 | { textDecorationLine: "underline" }, |
| 150 | ], |
| 151 | ["extra fields in a colon-form indexed underline colour", "\u001b[58:5:0:32mX", null], |
| 152 | [ |
| 153 | "extra fields in a colon-form indexed colour", |
| 154 | "\u001b[38:5:1:4mX", |
| 155 | { color: TERMINAL_PALETTES.dark[1] }, |
| 156 | ], |
| 157 | [ |
| 158 | "a true colour with its fields after colons", |
| 159 | "\u001b[38;2:255:0:0;1mX", |
| 160 | { color: "rgb(255,0,0)", fontWeight: "600" }, |
| 161 | ], |
| 162 | [ |
| 163 | "an indexed colour with its index after a colon", |
| 164 | "\u001b[38;5:1;4mX", |
| 165 | { color: TERMINAL_PALETTES.dark[1], textDecorationLine: "underline" }, |
| 166 | ], |
| 167 | [ |
| 168 | "a semicolon-form colour value that looks like a colour code", |
| 169 | "\u001b[38;5;38;2:1:2:3mX", |
| 170 | { color: "rgb(0,175,215)", opacity: 0.65 }, |
| 171 | ], |
| 172 | ])("reads %s without disturbing the other attributes", (_, input, style) => { |
| 173 | const text = normalizeTerminalOutput(input).text; |
| 174 | expect(terminalOutputSpans(text, TERMINAL_PALETTES.dark)).toEqual([{ text: "X", style }]); |
| 175 | for (let cut = 1; cut < input.length; cut += 1) { |
| 176 | expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe(text); |
| 177 | } |
| 178 | }); |
| 179 | |
| 180 | it("leaves the screen and cursor alone for unsupported erase modes", () => { |
| 181 | for (const sequence of ["3K", "4J", "99K", "99J"]) { |
| 182 | const input = `a\nb\nc\u001b[${sequence}X`; |
| 183 | expect(feed([input]).text).toBe("a\nb\ncX"); |
| 184 | for (let cut = 1; cut < input.length; cut += 1) { |
| 185 | expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("a\nb\ncX"); |
| 186 | } |
| 187 | } |
| 188 | }); |
| 189 | |
| 190 | it("clears only scrollback on ESC[3J", () => { |
| 191 | const lines = Array.from({ length: 70 }, (_, index) => `line ${index}`).join("\n"); |
| 192 | const state = feed([lines, "\u001b[3J"]); |
| 193 | expect(state.text.startsWith("line 6\n")).toBe(true); |
| 194 | expect(state.text.endsWith("line 69")).toBe(true); |
| 195 | expect(state.truncated).toBe(true); |
| 196 | }); |
| 197 | |
| 198 | it("keeps the whole window within the tail when every line is long", () => { |
| 199 | const long = `${"x".repeat(900)}\n`; |
| 200 | const state = feed([long.repeat(64)], 4_096); |
| 201 | const windowChars = state.screen.lines.reduce( |
| 202 | (total, line) => total + line.reduce((sum, run) => sum + run.text.length, 0), |
| 203 | 0, |
| 204 | ); |
| 205 | expect(windowChars).toBeLessThanOrEqual(4_096); |
| 206 | expect(state.text.length).toBeLessThanOrEqual(4_096); |
| 207 | for (const prefix of ["😀", "e\u0301"]) { |
| 208 | expect(feed([`a\n${prefix}bx\nZ\u001b[2A`], 4).text).toBe("bx\nZ"); |
| 209 | } |
| 210 | }); |
| 211 | }); |
| 212 | |
| 213 | describe("appendTerminalOutput under adversarial output", () => { |
| 214 | it("ignores unsupported cursor-control parameters without corrupting the screen", () => { |
| 215 | for (const sequence of ["1.5A", "1.5G", "1$K", "1$J", ">1A", "?25l", "1:2A", "1:2K", "1:2J"]) { |
| 216 | const input = `a\nb\nc\u001b[${sequence}X`; |
| 217 | expect(feed([input]).text).toBe("a\nb\ncX"); |
| 218 | for (let cut = 1; cut < input.length; cut += 1) { |
| 219 | expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("a\nb\ncX"); |
| 220 | } |
| 221 | } |
| 222 | }); |
| 223 | |
| 224 | it("drops escape sequences with intermediate bytes without printing their final byte", () => { |
| 225 | for (const sequence of ["#8", "#6", "%G", " F", "-A", ".A", "(%5", "(B"]) { |
| 226 | const input = `a\u001b${sequence}b`; |
| 227 | expect(feed([input]).text).toBe("ab"); |
| 228 | for (let cut = 1; cut < input.length; cut += 1) { |
| 229 | expect(feed([input.slice(0, cut), input.slice(cut)]).text).toBe("ab"); |
| 230 | } |
| 231 | } |
| 232 | for (const [interrupted, expected] of [ |
| 233 | ["a\u001b#\u001b[31mb", feed(["a\u001b[31mb"]).text], |
| 234 | ["a\u001b#\n8b", "a\n8b"], |
| 235 | ] as const) { |
| 236 | for (let cut = 0; cut < interrupted.length; cut += 1) { |
| 237 | expect(feed([interrupted.slice(0, cut), interrupted.slice(cut)]).text).toBe(expected); |
| 238 | } |
| 239 | } |
| 240 | }); |
| 241 | |
| 242 | it("gives the same result however the output is split into chunks", () => { |
| 243 | const input = |
| 244 | "abcdef\rX\u001b[31mred\u001b[0m\nnext line\r\u001b[2Kdone\n" + |
| 245 | "\u001b[1;31mbold red\u001b[mplain\u001b[38:2::12:34:56mrgb\u001b[0m\n" + |
| 246 | "\u001b[1;31mbold red again\u001b[mplain again\u001b[38:2::12:34:56mrgb again\u001b[0m"; |
| 247 | for (const maxChars of [3, 1_024]) { |
| 248 | const whole = feed([input], maxChars).text; |
| 249 | for (let cut = 1; cut < input.length; cut += 1) { |
| 250 | expect(feed([input.slice(0, cut), input.slice(cut)], maxChars).text).toBe(whole); |
| 251 | } |
| 252 | } |
| 253 | expect(feed(["abcdef", "\rX"], 3).text).toBe(feed(["abcdef\rX"], 3).text); |
| 254 | }); |
| 255 | }); |
| 256 | |
| 257 | describe("terminalOutputResumeText", () => { |
| 258 | it("lets a viewer that starts from it apply later chunks exactly as the source does", () => { |
| 259 | const chunks = [ |
| 260 | "a: 1%\nb: 1%\n", |
| 261 | "\u001b[2A\u001b[2K\u001b[33ma: 5", |
| 262 | "0%\u001b[0", |
| 263 | "m\n\u001b[2Kb: 50%\n", |
| 264 | ]; |
| 265 | const source = feed(chunks.slice(0, 3)); |
| 266 | const viewer = appendTerminalOutput( |
| 267 | appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source)), |
| 268 | chunks[3]!, |
| 269 | ); |
| 270 | expect(viewer.text).toBe(feed(chunks).text); |
| 271 | expect(terminalOutputPlainText(viewer.text)).toBe("a: 50%\nb: 50%\n"); |
| 272 | }); |
| 273 | }); |
| 274 | |
| 275 | describe("terminalOutputResumeText with a saved cursor or trimmed output", () => { |
| 276 | it("restores a cursor saved before the resync", () => { |
| 277 | const source = feed(["界e\u0301\u001b7def"]); |
| 278 | const viewer = appendTerminalOutput( |
| 279 | appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source)), |
| 280 | "\u001b8X", |
| 281 | ); |
| 282 | expect(viewer.text).toBe("界e\u0301Xef"); |
| 283 | }); |
| 284 | |
| 285 | it("stays on the right line when early output was trimmed away", () => { |
| 286 | const lines = Array.from({ length: 40 }, (_, index) => `line ${index}`).join("\n"); |
| 287 | const source = feed([lines, "\u001b[1A\rLINE"], 120); |
| 288 | const viewer = appendTerminalOutput( |
| 289 | appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source), 120), |
| 290 | " edited", |
| 291 | 120, |
| 292 | ); |
| 293 | expect(viewer.text).toBe(appendTerminalOutput(source, " edited", 120).text); |
| 294 | }); |
| 295 | }); |
| 296 | |
| 297 | describe("terminalOutputTail", () => { |
| 298 | it("never begins on a lone low surrogate", () => { |
| 299 | const text = `${"a".repeat(10)}😀${"b".repeat(4)}`; |
| 300 | const tail = terminalOutputTail(text, 5); |
| 301 | expect(tail.text).toBe("bbbb"); |
| 302 | }); |
| 303 | }); |
| 304 | |
| 305 | describe("terminalOutputSpans", () => { |
| 306 | it("collapses the rest into plain text past the span limit", () => { |
| 307 | const text = normalizeTerminalOutput("\u001b[31mx\u001b[32my".repeat(50)).text; |
| 308 | const spans = terminalOutputSpans(text, TERMINAL_PALETTES.light, 10); |
| 309 | expect(spans.length).toBe(11); |
| 310 | expect(spans.at(-1)?.style).toBeNull(); |
| 311 | expect(spans.map((span) => span.text).join("")).toBe("xy".repeat(50)); |
| 312 | }); |
| 313 | |
| 314 | it("gives badge text at least 3:1 on the dark palette's coloured backgrounds", () => { |
| 315 | const text = normalizeTerminalOutput("\u001b[42m PASS \u001b[1;37;41m FAIL ").text; |
| 316 | const spans = terminalOutputSpans(text, TERMINAL_PALETTES.dark); |
| 317 | expect(spans.map((span) => span.style?.color)).toEqual(["#1f2328", "#1f2328"]); |
| 318 | }); |
| 319 | }); |