- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/terminal-text.test.ts?format=json
89 linespinch 100%Last change pinch in version 82, 4 versions changed it
1// 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 });
27 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`);
33 // 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`);
39 });
41 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) =>
79 execFile(process.execPath, [client, `frob${ESC}]52;c;aGk=\x07`], { cwd: dir, env }, (_e, stdout, stderr) =>
80 done({ stdout, stderr }),
81 ),
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});