- MCP
- blame on this site's /mcp
- CLI
- abso blame client/test/terminal-text.test.ts --json
- JSON
- /christopher/abso-core/blame/client/test/terminal-text.test.ts?format=json
89 linespinch 100%Last change pinch in version 82,
89 lines from 4 changes in 4 versions.
pinch74Server text can't drive the terminal: everything the client prints goes through one filter. Tabs, newlines and colors (SGR) pass, colors only to a terminal with NO_COLOR unset. Every other control character, and so every other escape sequence (OSC clipboard writes and links, cursor moves, DCS), prints as visible text such as \x1b, so nothing vanishes silently. It covers news, version messages, CI logs, file paths and agent names, from any server.Compare1// What the client prints can't drive the terminal: colors pass to a terminal
2// with NO_COLOR unset, and every other control character shows as text.
4import assert from "node:assert/strict";
5import { execFile } from "node:child_process";
6import { mkdtempSync, rmSync } from "node:fs";
7import { tmpdir } from "node:os";
8import { join, resolve } from "node:path";
9import { describe, it } from "node:test";
10import { colorsPass, terminalText } from "../src/terminal-text.ts";
12const ESC = "\x1b";
14describe("terminalText", () => {
15 it("leaves plain text, tabs, newlines and CRLF alone", () => {
16 const plain = "abso 72\tpublished\nnext line: ü, 中文, 🙂\r\n";
17 assert.equal(terminalText(plain, true), plain);
18 assert.equal(terminalText(plain, false), plain);
19 });
21 it("keeps colors only when they pass, and drops them quietly otherwise", () => {
22 const red = `${ESC}[31mfailed${ESC}[0m and ${ESC}[1;38;5;208mbold${ESC}[m`;
23 assert.equal(terminalText(red, true), red);
24 assert.equal(terminalText(red, false), "failed and bold");
25 });
pinch79abso help --json: every command help shows, with its usage, about and flags, as data, so a command reference can be built from the client itself. And the terminal filter drops SGR 8 (conceal), which could hide text, keeping 8 where it's a color (38;5;8).Compare27 it("drops conceal (SGR 8), which would hide text, but keeps 8 as a color", () => {
28 assert.equal(terminalText(`${ESC}[8mhidden${ESC}[0m`, true), `hidden${ESC}[0m`);
29 assert.equal(terminalText(`${ESC}[1;8;31mx`, true), `${ESC}[1;31mx`);
30 assert.equal(terminalText(`${ESC}[38;5;8;48;2;8;8;8mx`, true), `${ESC}[38;5;8;48;2;8;8;8mx`);
31 assert.equal(terminalText(`${ESC}[38;5;8;8mx`, true), `${ESC}[38;5;8mx`);
32 assert.equal(terminalText(`${ESC}[mx${ESC}[28m`, true), `${ESC}[mx${ESC}[28m`);
pinch82Terminal text: conceal (SGR 8) is found by value, as a terminal reads it. 08, 008 and 8:1 are 8 too and are dropped. 038;05;8 is still a color and is kept.Compare33 // By value, as a terminal reads it (Security 2): leading zeros and a sub-parameter are still 8.
34 assert.equal(terminalText(`${ESC}[08mx`, true), "x");
35 assert.equal(terminalText(`${ESC}[1;008mx`, true), `${ESC}[1mx`);
36 assert.equal(terminalText(`${ESC}[8:1mx`, true), "x");
37 assert.equal(terminalText(`${ESC}[038;05;8mx`, true), `${ESC}[038;05;8mx`);
38 assert.equal(terminalText(`${ESC}[38:5:8mx`, true), `${ESC}[38:5:8mx`);
pinch79abso help --json: every command help shows, with its usage, about and flags, as data, so a command reference can be built from the client itself. And the terminal filter drops SGR 8 (conceal), which could hide text, keeping 8 where it's a color (38;5;8).Compare39 });
pinch74Server text can't drive the terminal: everything the client prints goes through one filter. Tabs, newlines and colors (SGR) pass, colors only to a terminal with NO_COLOR unset. Every other control character, and so every other escape sequence (OSC clipboard writes and links, cursor moves, DCS), prints as visible text such as \x1b, so nothing vanishes silently. It covers news, version messages, CI logs, file paths and agent names, from any server.Compare41 it("shows OSC 52 (the clipboard) and OSC 8 (links) as text, with or without colors", () => {
42 const clip = `${ESC}]52;c;ZWNobyBoaQ==\x07`;
43 assert.equal(terminalText(clip, true), "\\x1b]52;c;ZWNobyBoaQ==\\x07");
44 const link = `${ESC}]8;;https://evil.example${ESC}\\click${ESC}]8;;${ESC}\\`;
45 assert.equal(terminalText(link, true), "\\x1b]8;;https://evil.example\\x1b\\click\\x1b]8;;\\x1b\\");
46 });
48 it("shows other CSI, DCS and the rest of C0 and C1 as text", () => {
49 assert.equal(terminalText(`${ESC}[2J${ESC}[H`, true), "\\x1b[2J\\x1b[H");
50 assert.equal(terminalText(`${ESC}P1$r${ESC}\\`, true), "\\x1bP1$r\\x1b\\");
51 assert.equal(terminalText("over\rwrite\b\x00\x7f", true), "over\\x0dwrite\\x08\\x00\\x7f");
52 assert.equal(terminalText("\u009b31m\u0090x\u009c", true), "\\u009b31m\\u0090x\\u009c");
53 // An SGR with anything but digits, ; and : isn't one.
54 assert.equal(terminalText(`${ESC}[31;"m`, true), '\\x1b[31;"m');
55 });
57 it("colors pass to a terminal only, and never with NO_COLOR set", () => {
58 assert.equal(colorsPass({ isTTY: true }, {}), true);
59 assert.equal(colorsPass({ isTTY: true }, { NO_COLOR: "1" }), false);
60 assert.equal(colorsPass({ isTTY: true }, { NO_COLOR: "" }), true);
61 assert.equal(colorsPass({ isTTY: false }, {}), false);
62 assert.equal(colorsPass({}, {}), false);
63 });
65 it("the client's own output goes through it: an error naming text with escapes prints them as text", async () => {
66 const home = mkdtempSync(join(tmpdir(), "abso-test-terminal-"));
67 try {
68 // An unknown command: the error names it, escape and all.
69 const dir = mkdtempSync(join(home, "x"));
70 const env: Record<string, string> = {
71 PATH: process.env.PATH ?? "",
72 HOME: home,
73 USERPROFILE: home,
74 AVCS_SERVERS_FILE: "no-servers.json",
75 };
76 if (process.env.SystemRoot) env.SystemRoot = process.env.SystemRoot;
77 const client = resolve(import.meta.dirname, "..", "abso.ts");
78 const run = await new Promise<{ stdout: string; stderr: string }>((done) =>
pinch77Format terminal-text.test.ts: one line Biome wanted joined, which turned the check red from version 74 on.Compare79 execFile(process.execPath, [client, `frob${ESC}]52;c;aGk=\x07`], { cwd: dir, env }, (_e, stdout, stderr) =>
80 done({ stdout, stderr }),
pinch74Server text can't drive the terminal: everything the client prints goes through one filter. Tabs, newlines and colors (SGR) pass, colors only to a terminal with NO_COLOR unset. Every other control character, and so every other escape sequence (OSC clipboard writes and links, cursor moves, DCS), prints as visible text such as \x1b, so nothing vanishes silently. It covers news, version messages, CI logs, file paths and agent names, from any server.Compare81 ),
82 );
83 assert.doesNotMatch(run.stdout + run.stderr, /\x1b/);
84 assert.match(run.stderr, /frob\\x1b\]52;c;aGk=\\x07/);
85 } finally {
86 rmSync(home, { recursive: true, force: true });
87 }
88 });
89});