pi-syntax-highlight 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +81 -40
  2. package/DEVELOPER.md +100 -0
  3. package/LICENSE +21 -0
  4. package/README.md +42 -44
  5. package/extensions/index.ts +5 -10
  6. package/package.json +20 -5
  7. package/src/detect-language.ts +205 -8
  8. package/src/emphasis.ts +82 -1
  9. package/src/highlight.ts +25 -9
  10. package/src/lru-cache.ts +30 -0
  11. package/src/markdown-fence-lang.ts +20 -87
  12. package/src/render-bash.ts +29 -2
  13. package/src/render-edit.ts +0 -6
  14. package/src/render-generic.ts +10 -21
  15. package/src/render-read.ts +8 -149
  16. package/src/render-utils.ts +8 -17
  17. package/src/resolve-renderers.ts +181 -240
  18. package/src/tokens.ts +1 -34
  19. package/src/active-theme.ts +0 -26
  20. package/src/markdown-style.ts +0 -134
  21. package/src/markdown-theme.ts +0 -53
  22. package/src/render-path.ts +0 -54
  23. package/src/render-write.ts +0 -81
  24. package/test/unit/active-theme.test.ts +0 -38
  25. package/test/unit/bash-preview.test.ts +0 -57
  26. package/test/unit/detect-language.test.ts +0 -124
  27. package/test/unit/emphasis.test.ts +0 -113
  28. package/test/unit/extensions-index.test.ts +0 -116
  29. package/test/unit/grep-parse.test.ts +0 -125
  30. package/test/unit/highlight.test.ts +0 -107
  31. package/test/unit/markdown-fence-lang.test.ts +0 -131
  32. package/test/unit/markdown-pipeline.test.ts +0 -153
  33. package/test/unit/markdown-render-parity.test.ts +0 -127
  34. package/test/unit/markdown-style.test.ts +0 -152
  35. package/test/unit/markdown-theme.test.ts +0 -86
  36. package/test/unit/render-bash.test.ts +0 -194
  37. package/test/unit/render-edit.test.ts +0 -214
  38. package/test/unit/render-find.test.ts +0 -139
  39. package/test/unit/render-generic.test.ts +0 -152
  40. package/test/unit/render-grep.test.ts +0 -338
  41. package/test/unit/render-ls.test.ts +0 -132
  42. package/test/unit/render-read.test.ts +0 -375
  43. package/test/unit/render-utils.test.ts +0 -324
  44. package/test/unit/render-write.test.ts +0 -143
  45. package/test/unit/resolve-renderers.test.ts +0 -474
  46. package/test/unit/style-path.test.ts +0 -74
  47. package/tsconfig.json +0 -15
@@ -1,214 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import type { HighlightFn } from "../../src/highlight.ts";
4
- import {
5
- buildEditDiffText,
6
- buildEditErrorText,
7
- classifyEditDiffLine,
8
- type EditDiffInput,
9
- } from "../../src/render-edit.ts";
10
- import { INVERSE_OFF, INVERSE_ON, TOKEN } from "../../src/tokens.ts";
11
-
12
- const fakeTheme = {
13
- fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
14
- bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
15
- bold: (t: string) => `<b>${t}</b>`,
16
- };
17
-
18
- interface HighlightCall {
19
- code: string;
20
- lang: string;
21
- }
22
-
23
- function makeHighlight(calls: HighlightCall[]): HighlightFn {
24
- return (code, lang) => {
25
- calls.push({ code, lang });
26
- return code.split("\n").map((line) => `<${lang}>${line}</${lang}>`);
27
- };
28
- }
29
-
30
- // The intra-line markers are inserted by visible column, so the fakes used with a
31
- // modification must not invent markup: `<typescript>` would count as characters
32
- // on screen.
33
- const plainHighlight: HighlightFn = (code) => code.split("\n");
34
-
35
- function diffInput(overrides: Partial<EditDiffInput> = {}): EditDiffInput {
36
- return {
37
- diff: "",
38
- path: "a.ts",
39
- theme: fakeTheme,
40
- highlight: makeHighlight([]),
41
- ...overrides,
42
- };
43
- }
44
-
45
- // The marker, then the number, then one space, then the content: the shape the
46
- // display diff of the tool has.
47
- function diffLine(token: string, marker: string, number: string, content: string): string {
48
- const painted = /\d/.test(number) ? `<syntaxNumber>${number}</syntaxNumber>` : number;
49
- return `<${token}>${marker}</${token}>${painted} ${content}`;
50
- }
51
-
52
- test("classifyEditDiffLine: the three markers and the right-aligned number are separated", () => {
53
- assert.deepEqual(classifyEditDiffLine("+12 const b = 2;"), {
54
- kind: "added",
55
- prefix: "+",
56
- number: "12",
57
- content: "const b = 2;",
58
- });
59
- assert.deepEqual(classifyEditDiffLine("- 4 y"), { kind: "removed", prefix: "-", number: " 4", content: "y" });
60
- assert.deepEqual(classifyEditDiffLine(" 7 z"), { kind: "context", prefix: " ", number: " 7", content: "z" });
61
- });
62
-
63
- test("classifyEditDiffLine: the elision between two hunks has no number", () => {
64
- assert.deepEqual(classifyEditDiffLine(" ..."), {
65
- kind: "elision",
66
- prefix: " ",
67
- number: " ",
68
- content: "...",
69
- });
70
- });
71
-
72
- test("classifyEditDiffLine: a line outside the grammar is passed through", () => {
73
- assert.deepEqual(classifyEditDiffLine("not a diff line"), { kind: "other", raw: "not a diff line" });
74
- assert.deepEqual(classifyEditDiffLine(""), { kind: "other", raw: "" });
75
- });
76
-
77
- test("buildEditDiffText: an added line carries its marker, its number and the highlighted content", () => {
78
- const text = buildEditDiffText(diffInput({ diff: "+1 const b = 2;" }));
79
- assert.equal(text, diffLine("toolDiffAdded", "+", "1", "<typescript>const b = 2;</typescript>"));
80
- });
81
-
82
- test("buildEditDiffText: a context line keeps the diff context color and its own number", () => {
83
- const text = buildEditDiffText(diffInput({ diff: " 1 unchanged" }));
84
- assert.equal(text, diffLine("toolDiffContext", " ", "1", "<typescript>unchanged</typescript>"));
85
- });
86
-
87
- test("buildEditDiffText: contiguous lines of the same kind are highlighted in one call", () => {
88
- const calls: HighlightCall[] = [];
89
- const text = buildEditDiffText(diffInput({ diff: "+1 one\n+2 two", highlight: makeHighlight(calls) }));
90
- assert.deepEqual(calls, [{ code: "one\ntwo", lang: "typescript" }]);
91
- assert.equal(
92
- text,
93
- [
94
- diffLine("toolDiffAdded", "+", "1", "<typescript>one</typescript>"),
95
- diffLine("toolDiffAdded", "+", "2", "<typescript>two</typescript>"),
96
- ].join("\n"),
97
- );
98
- });
99
-
100
- test("buildEditDiffText: a kind change starts a new run and a new call", () => {
101
- const calls: HighlightCall[] = [];
102
- buildEditDiffText(diffInput({ diff: "+1 one\n 2 two\n+3 three", highlight: makeHighlight(calls) }));
103
- assert.deepEqual(calls, [
104
- { code: "one", lang: "typescript" },
105
- { code: "two", lang: "typescript" },
106
- { code: "three", lang: "typescript" },
107
- ]);
108
- });
109
-
110
- test("buildEditDiffText: without a grammar the content keeps its kind's color", () => {
111
- assert.equal(TOKEN.content, "text");
112
- const calls: HighlightCall[] = [];
113
- const text = buildEditDiffText(diffInput({ diff: "+1 one\n 2 two", path: "notes", highlight: makeHighlight(calls) }));
114
- assert.deepEqual(calls, []);
115
- assert.equal(
116
- text,
117
- [
118
- diffLine("toolDiffAdded", "+", "1", "<toolDiffAdded>one</toolDiffAdded>"),
119
- diffLine("toolDiffContext", " ", "2", "<toolDiffContext>two</toolDiffContext>"),
120
- ].join("\n"),
121
- );
122
- });
123
-
124
- test("buildEditDiffText: the elision is painted as metainformation", () => {
125
- const text = buildEditDiffText(diffInput({ diff: " ..." }));
126
- // The number is the empty slot of the elision, so it is emitted as the spaces
127
- // it is: painting them would be the same pixels with two extra sequences.
128
- assert.equal(text, diffLine("toolDiffContext", " ", " ", "<toolDiffContext>...</toolDiffContext>"));
129
- });
130
-
131
- test("buildEditDiffText: a line outside the grammar keeps the content color", () => {
132
- const text = buildEditDiffText(diffInput({ diff: "not a diff line" }));
133
- assert.equal(text, "<text>not a diff line</text>");
134
- });
135
-
136
- test("buildEditDiffText: an empty diff renders nothing", () => {
137
- assert.equal(buildEditDiffText(diffInput({ diff: "" })), "");
138
- });
139
-
140
- test("buildEditDiffText: one removed and one added line invert only the changed words", () => {
141
- const text = buildEditDiffText(
142
- diffInput({ diff: "-1 const a = 1;\n+1 const b = 2;", highlight: plainHighlight }),
143
- );
144
- assert.equal(
145
- text,
146
- [
147
- diffLine("toolDiffRemoved", "-", "1", `const ${INVERSE_ON}a${INVERSE_OFF} = ${INVERSE_ON}1${INVERSE_OFF};`),
148
- diffLine("toolDiffAdded", "+", "1", `const ${INVERSE_ON}b${INVERSE_OFF} = ${INVERSE_ON}2${INVERSE_OFF};`),
149
- ].join("\n"),
150
- );
151
- });
152
-
153
- test("buildEditDiffText: the indentation of the first changed part is never inverted", () => {
154
- const text = buildEditDiffText(diffInput({ diff: "-1 x\n+1 y", highlight: plainHighlight }));
155
- assert.equal(
156
- text,
157
- [
158
- diffLine("toolDiffRemoved", "-", "1", ` ${INVERSE_ON}x${INVERSE_OFF}`),
159
- diffLine("toolDiffAdded", "+", "1", ` ${INVERSE_ON}y${INVERSE_OFF}`),
160
- ].join("\n"),
161
- );
162
- });
163
-
164
- test("buildEditDiffText: several removed lines are not a modification, so nothing is inverted", () => {
165
- const text = buildEditDiffText(diffInput({ diff: "-1 a\n-2 b\n+1 c", highlight: plainHighlight }));
166
- assert.equal(text.includes(INVERSE_ON), false);
167
- assert.equal(text.split("\n").length, 3);
168
- });
169
-
170
- test("buildEditDiffText: a standalone added line is not a modification", () => {
171
- const text = buildEditDiffText(diffInput({ diff: "+1 b\n-1 a", highlight: plainHighlight }));
172
- assert.equal(text.includes(INVERSE_ON), false);
173
- assert.equal(text.split("\n").length, 2);
174
- });
175
-
176
- test("buildEditDiffText: one removed line followed by several added lines stays flat", () => {
177
- const text = buildEditDiffText(diffInput({ diff: "-1 a\n+1 b\n+2 c", highlight: plainHighlight }));
178
- assert.equal(text.includes(INVERSE_ON), false);
179
- });
180
-
181
- test("buildEditDiffText: the changed span is inverted inside an already colored line", () => {
182
- const ansiHighlight: HighlightFn = (code) => code.split("\n").map((line) => `\x1b[36m${line}\x1b[39m`);
183
- const text = buildEditDiffText(diffInput({ diff: "-1 const a = 1;\n+1 const b = 2;", highlight: ansiHighlight }));
184
- assert.equal(
185
- text,
186
- [
187
- diffLine(
188
- "toolDiffRemoved",
189
- "-",
190
- "1",
191
- `\x1b[36mconst ${INVERSE_ON}a${INVERSE_OFF} = ${INVERSE_ON}1${INVERSE_OFF};\x1b[39m`,
192
- ),
193
- diffLine(
194
- "toolDiffAdded",
195
- "+",
196
- "1",
197
- `\x1b[36mconst ${INVERSE_ON}b${INVERSE_OFF} = ${INVERSE_ON}2${INVERSE_OFF};\x1b[39m`,
198
- ),
199
- ].join("\n"),
200
- );
201
- });
202
-
203
- test("buildEditErrorText: the failure is painted in the error color without a leading newline", () => {
204
- assert.equal(TOKEN.broken, "error");
205
- assert.equal(buildEditErrorText("no such file", fakeTheme), "<error>no such file</error>");
206
- });
207
-
208
- test("buildEditErrorText: several text blocks stay inside the one wrapper", () => {
209
- assert.equal(buildEditErrorText("first\nsecond", fakeTheme), "<error>first\nsecond</error>");
210
- });
211
-
212
- test("buildEditErrorText: an empty failure renders nothing", () => {
213
- assert.equal(buildEditErrorText("", fakeTheme), "");
214
- });
@@ -1,139 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { DEFAULT_MAX_BYTES, formatSize } from "@earendil-works/pi-coding-agent";
4
- import { buildFindResultText, type PathListInput } from "../../src/render-find.ts";
5
- import { TOKEN } from "../../src/tokens.ts";
6
-
7
- const fakeTheme = {
8
- fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
9
- bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
10
- bold: (t: string) => `<b>${t}</b>`,
11
- };
12
-
13
- const EXPAND_HINT = "<hint>";
14
- const FOOTER_1 = "<muted>\n... (1 more lines,</muted> <hint><muted>)</muted>";
15
- const FOOTER_3 = "<muted>\n... (3 more lines,</muted> <hint><muted>)</muted>";
16
-
17
- const PATHS = Array.from({ length: 21 }, (_, index) => `f${index}.ts`);
18
- const NOTICE = "100 results limit reached. Use limit=200 for more, or refine pattern";
19
- // Twenty-one paths, the blank separator find prints, and the notice: twenty-three
20
- // body lines in total.
21
- const PATHS_WITH_NOTICE = `${PATHS.join("\n")}\n\n[${NOTICE}]`;
22
-
23
- function styledPath(path: string): string {
24
- return `<b><syntaxType>${path}</syntaxType></b>`;
25
- }
26
-
27
- function findInput(overrides: Partial<PathListInput> = {}): PathListInput {
28
- return {
29
- output: "",
30
- expanded: false,
31
- isError: false,
32
- details: undefined,
33
- theme: fakeTheme,
34
- expandHint: EXPAND_HINT,
35
- ...overrides,
36
- };
37
- }
38
-
39
- test("buildFindResultText: three paths render in full with no footer", () => {
40
- const text = buildFindResultText(findInput({ output: "a.ts\nb.ts\nc.ts" }));
41
- assert.equal(text, ["", styledPath("a.ts"), styledPath("b.ts"), styledPath("c.ts")].join("\n"));
42
- });
43
-
44
- test("buildFindResultText: twenty-one paths collapse to twenty plus a footer", () => {
45
- const text = buildFindResultText(findInput({ output: PATHS.join("\n") }));
46
- assert.equal(text, ["", ...PATHS.slice(0, 20).map(styledPath)].join("\n") + FOOTER_1);
47
- });
48
-
49
- test("buildFindResultText: expanded shows every path and no footer", () => {
50
- const text = buildFindResultText(findInput({ output: PATHS.join("\n"), expanded: true }));
51
- assert.equal(text, ["", ...PATHS.map(styledPath)].join("\n"));
52
- });
53
-
54
- test("buildFindResultText: a nested path dims the directories and colors the file", () => {
55
- const text = buildFindResultText(findInput({ output: "src/deep/a.ts" }));
56
- assert.equal(text, ["", "<muted>src</muted>/<muted>deep</muted>/<b><syntaxType>a.ts</syntaxType></b>"].join("\n"));
57
- });
58
-
59
- test("buildFindResultText: a directory keeps its trailing slash and is dimmed", () => {
60
- const text = buildFindResultText(findInput({ output: "dir/" }));
61
- assert.equal(text, ["", "<b><dim>dir</dim></b>/"].join("\n"));
62
- });
63
-
64
- test("buildFindResultText: a document is colored like tool output", () => {
65
- const text = buildFindResultText(findInput({ output: "README.md" }));
66
- assert.equal(text, ["", "<b><toolOutput>README.md</toolOutput></b>"].join("\n"));
67
- });
68
-
69
- test("buildFindResultText: the results limit warning sits after the footer", () => {
70
- const text = buildFindResultText(findInput({ output: PATHS.join("\n"), details: { resultLimitReached: 100 } }));
71
- const footerAt = text.indexOf("more lines,");
72
- const warningAt = text.indexOf("[Truncated:");
73
- assert.ok(footerAt !== -1, `expected the footer in: ${text}`);
74
- assert.ok(warningAt > footerAt, `expected the warning after the footer in: ${text}`);
75
- assert.ok(
76
- text.endsWith("\n<warning>[Truncated: 100 results limit]</warning>"),
77
- `expected the results limit warning at the end of: ${text}`,
78
- );
79
- });
80
-
81
- test("buildFindResultText: the size limit is formatted through formatSize, with the default when absent", () => {
82
- const explicit = buildFindResultText(
83
- findInput({ output: "a.ts", details: { truncation: { truncated: true, maxBytes: 1048576 } } }),
84
- );
85
- assert.equal(
86
- explicit,
87
- ["", styledPath("a.ts"), `<warning>[Truncated: ${formatSize(1048576)} limit]</warning>`].join("\n"),
88
- );
89
-
90
- const defaulted = buildFindResultText(findInput({ output: "a.ts", details: { truncation: { truncated: true } } }));
91
- assert.equal(
92
- defaulted,
93
- ["", styledPath("a.ts"), `<warning>[Truncated: ${formatSize(DEFAULT_MAX_BYTES)} limit]</warning>`].join("\n"),
94
- );
95
- });
96
-
97
- test("buildFindResultText: the size warning is absent when the output was not truncated", () => {
98
- const text = buildFindResultText(findInput({ output: "a.ts", details: { truncation: { truncated: false } } }));
99
- assert.equal(text, ["", styledPath("a.ts")].join("\n"));
100
- });
101
-
102
- test("buildFindResultText: the tool's own results notice is a body line, so the footer counts it", () => {
103
- assert.equal(TOKEN.notice, "warning");
104
- assert.equal(
105
- buildFindResultText(findInput({ output: PATHS_WITH_NOTICE })),
106
- ["", ...PATHS.slice(0, 20).map(styledPath)].join("\n") + FOOTER_3,
107
- );
108
-
109
- const expanded = buildFindResultText(findInput({ output: PATHS_WITH_NOTICE, expanded: true }));
110
- assert.equal(expanded.includes("more lines,"), false);
111
- assert.ok(
112
- expanded.endsWith(`\n<warning>[${NOTICE}]</warning>`),
113
- `expected the notice at the end of: ${expanded}`,
114
- );
115
- });
116
-
117
- test("buildFindResultText: an error result is painted flat in the content color", () => {
118
- assert.equal(TOKEN.content, "text");
119
- const text = buildFindResultText(
120
- findInput({ output: "src/a.ts\ndir/\nNo files found matching pattern", isError: true }),
121
- );
122
- // No bold, no file-family color, and no sentinel dimming: the failure is the
123
- // whole message, so every line keeps the one weight.
124
- assert.equal(
125
- text,
126
- ["", "<text>src/a.ts</text>", "<text>dir/</text>", "<text>No files found matching pattern</text>"].join("\n"),
127
- );
128
- });
129
-
130
- test("buildFindResultText: an empty search is dimmed as a sentinel, not styled as a path", () => {
131
- assert.equal(TOKEN.empty, "dim");
132
- const text = buildFindResultText(findInput({ output: "No files found matching pattern" }));
133
- assert.equal(text, ["", "<dim>No files found matching pattern</dim>"].join("\n"));
134
- });
135
-
136
- test("buildFindResultText: empty and blank outputs render nothing", () => {
137
- assert.equal(buildFindResultText(findInput({ output: "" })), "");
138
- assert.equal(buildFindResultText(findInput({ output: " \n " })), "");
139
- });
@@ -1,152 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { buildGenericResultText, type GenericInput } from "../../src/render-generic.ts";
4
- import type { HighlightFn } from "../../src/highlight.ts";
5
- import { TOKEN } from "../../src/tokens.ts";
6
-
7
- const fakeTheme = {
8
- fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
9
- bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
10
- bold: (t: string) => `<b>${t}</b>`,
11
- };
12
-
13
- const EXPAND_HINT = "<hint>";
14
- const FOOTER_1 = "<muted>\n... (1 more lines,</muted> <hint><muted>)</muted>";
15
-
16
- function styled(color: string, text: string): string {
17
- return `<${color}>${text}</${color}>`;
18
- }
19
-
20
- function styledDiffChange(background: string, foreground: string, line: string): string {
21
- return `<bg:${background}>${styled(foreground, line)}</bg:${background}>`;
22
- }
23
-
24
- interface HighlightCall {
25
- code: string;
26
- lang: string;
27
- }
28
-
29
- function makeHighlight(calls: HighlightCall[]): HighlightFn {
30
- return (code, lang) => {
31
- calls.push({ code, lang });
32
- return code.split("\n").map((line) => `<${lang}>${line}</${lang}>`);
33
- };
34
- }
35
-
36
- function genericInput(overrides: Partial<GenericInput> = {}): GenericInput {
37
- return {
38
- output: "",
39
- expanded: false,
40
- isError: false,
41
- theme: fakeTheme,
42
- expandHint: EXPAND_HINT,
43
- ...overrides,
44
- };
45
- }
46
-
47
- const PLAIN_LINES = Array.from({ length: 11 }, (_, index) => `line ${index}`);
48
-
49
- test("buildGenericResultText: a JSON object is highlighted as one block", () => {
50
- const calls: HighlightCall[] = [];
51
- const json = '{"a":1,"b":[2,3]}';
52
- const text = buildGenericResultText(genericInput({ output: json }), makeHighlight(calls));
53
- assert.deepEqual(calls, [{ code: json, lang: "json" }]);
54
- assert.equal(text, ["", styled("json", json)].join("\n"));
55
- });
56
-
57
- test("buildGenericResultText: a multi-line JSON array keeps the surrounding context", () => {
58
- const calls: HighlightCall[] = [];
59
- const json = "[\n 1,\n 2\n]";
60
- const text = buildGenericResultText(genericInput({ output: json }), makeHighlight(calls));
61
- assert.deepEqual(calls, [{ code: json, lang: "json" }]);
62
- assert.equal(
63
- text,
64
- ["", styled("json", "["), styled("json", " 1,"), styled("json", " 2"), styled("json", "]")].join("\n"),
65
- );
66
- });
67
-
68
- test("buildGenericResultText: output that only looks like JSON keeps the flat content color", () => {
69
- const calls: HighlightCall[] = [];
70
- const output = '{"a": }';
71
- const text = buildGenericResultText(genericInput({ output }), makeHighlight(calls));
72
- assert.deepEqual(calls, []);
73
- assert.equal(text, ["", styled("text", output)].join("\n"));
74
- });
75
-
76
- test("buildGenericResultText: a diff is painted per line, changes over their own background", () => {
77
- const calls: HighlightCall[] = [];
78
- const output = [
79
- "diff --git a/x.ts b/x.ts",
80
- "--- a/x.ts",
81
- "+++ b/x.ts",
82
- "@@ -1,2 +1,3 @@",
83
- " const a = 1;",
84
- "+const b = 2;",
85
- "-const c = 3;",
86
- ].join("\n");
87
- const text = buildGenericResultText(genericInput({ output }), makeHighlight(calls));
88
- assert.deepEqual(calls, []);
89
- assert.equal(
90
- text,
91
- [
92
- "",
93
- styled("text", "diff --git a/x.ts b/x.ts"),
94
- styled("syntaxFunction", "--- a/x.ts"),
95
- styled("syntaxFunction", "+++ b/x.ts"),
96
- styled("syntaxFunction", "@@ -1,2 +1,3 @@"),
97
- styled("text", " const a = 1;"),
98
- styledDiffChange("toolSuccessBg", "toolDiffAdded", "+const b = 2;"),
99
- styledDiffChange("toolErrorBg", "toolDiffRemoved", "-const c = 3;"),
100
- ].join("\n"),
101
- );
102
- });
103
-
104
- test("buildGenericResultText: plain text is painted line by line in the content token", () => {
105
- const calls: HighlightCall[] = [];
106
- const text = buildGenericResultText(genericInput({ output: "alpha\nbeta" }), makeHighlight(calls));
107
- assert.deepEqual(calls, []);
108
- assert.equal(text, ["", styled("text", "alpha"), styled("text", "beta")].join("\n"));
109
- });
110
-
111
- test("buildGenericResultText: eleven lines collapse to ten plus the footer", () => {
112
- const text = buildGenericResultText(genericInput({ output: PLAIN_LINES.join("\n") }), makeHighlight([]));
113
- assert.equal(text, ["", ...PLAIN_LINES.slice(0, 10).map((line) => styled("text", line))].join("\n") + FOOTER_1);
114
- });
115
-
116
- test("buildGenericResultText: expanded shows every line and no footer", () => {
117
- const text = buildGenericResultText(
118
- genericInput({ output: PLAIN_LINES.join("\n"), expanded: true }),
119
- makeHighlight([]),
120
- );
121
- assert.equal(text, ["", ...PLAIN_LINES.map((line) => styled("text", line))].join("\n"));
122
- });
123
-
124
- test("buildGenericResultText: a trailing notice is painted in the warning token and is not a line", () => {
125
- const text = buildGenericResultText(
126
- genericInput({ output: `${PLAIN_LINES.join("\n")}\n\n[100 matches limit]` }),
127
- makeHighlight([]),
128
- );
129
- assert.equal(
130
- text,
131
- ["", ...PLAIN_LINES.slice(0, 10).map((line) => styled("text", line))].join("\n") +
132
- FOOTER_1 +
133
- "\n" +
134
- styled("warning", "[100 matches limit]"),
135
- );
136
- });
137
-
138
- test("buildGenericResultText: an error result is painted flat in the content color", () => {
139
- assert.equal(TOKEN.content, "text");
140
- // A JSON body that parses is the case the flat rule exists for: painted by
141
- // kind it would look like a normal result instead of a failure.
142
- const calls: HighlightCall[] = [];
143
- const json = '{"error":"boom"}';
144
- const text = buildGenericResultText(genericInput({ output: json, isError: true }), makeHighlight(calls));
145
- assert.deepEqual(calls, []);
146
- assert.equal(text, ["", styled("text", json)].join("\n"));
147
- });
148
-
149
- test("buildGenericResultText: empty and whitespace-only output render nothing", () => {
150
- assert.equal(buildGenericResultText(genericInput({ output: "" }), makeHighlight([])), "");
151
- assert.equal(buildGenericResultText(genericInput({ output: " \n\n \t " }), makeHighlight([])), "");
152
- });