pi-syntax-highlight 0.0.0-stage → 0.1.0

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 (49) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +68 -2
  3. package/extensions/index.ts +22 -0
  4. package/package.json +35 -4
  5. package/src/active-theme.ts +26 -0
  6. package/src/detect-language.ts +294 -0
  7. package/src/emphasis.ts +160 -0
  8. package/src/grep-parse.ts +42 -0
  9. package/src/highlight.ts +109 -0
  10. package/src/markdown-fence-lang.ts +180 -0
  11. package/src/markdown-style.ts +134 -0
  12. package/src/markdown-theme.ts +53 -0
  13. package/src/render-bash.ts +180 -0
  14. package/src/render-edit.ts +207 -0
  15. package/src/render-find.ts +64 -0
  16. package/src/render-generic.ts +86 -0
  17. package/src/render-grep.ts +142 -0
  18. package/src/render-ls.ts +66 -0
  19. package/src/render-path.ts +54 -0
  20. package/src/render-read.ts +277 -0
  21. package/src/render-utils.ts +219 -0
  22. package/src/render-write.ts +81 -0
  23. package/src/resolve-renderers.ts +406 -0
  24. package/src/style-path.ts +48 -0
  25. package/src/tokens.ts +114 -0
  26. package/test/unit/active-theme.test.ts +38 -0
  27. package/test/unit/bash-preview.test.ts +57 -0
  28. package/test/unit/detect-language.test.ts +124 -0
  29. package/test/unit/emphasis.test.ts +113 -0
  30. package/test/unit/extensions-index.test.ts +116 -0
  31. package/test/unit/grep-parse.test.ts +125 -0
  32. package/test/unit/highlight.test.ts +107 -0
  33. package/test/unit/markdown-fence-lang.test.ts +131 -0
  34. package/test/unit/markdown-pipeline.test.ts +153 -0
  35. package/test/unit/markdown-render-parity.test.ts +127 -0
  36. package/test/unit/markdown-style.test.ts +152 -0
  37. package/test/unit/markdown-theme.test.ts +86 -0
  38. package/test/unit/render-bash.test.ts +194 -0
  39. package/test/unit/render-edit.test.ts +214 -0
  40. package/test/unit/render-find.test.ts +139 -0
  41. package/test/unit/render-generic.test.ts +152 -0
  42. package/test/unit/render-grep.test.ts +338 -0
  43. package/test/unit/render-ls.test.ts +132 -0
  44. package/test/unit/render-read.test.ts +375 -0
  45. package/test/unit/render-utils.test.ts +324 -0
  46. package/test/unit/render-write.test.ts +143 -0
  47. package/test/unit/resolve-renderers.test.ts +474 -0
  48. package/test/unit/style-path.test.ts +74 -0
  49. package/tsconfig.json +15 -0
@@ -0,0 +1,86 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { markdownPalette } from "../../src/markdown-theme.ts";
4
+ import { DIM_OFF, DIM_ON, FG_DEFAULT, type AnsiTheme } from "../../src/tokens.ts";
5
+
6
+ const ACCENT = "\x1b[38;5;5m";
7
+ const QUOTE_COLOR = "\x1b[38;5;8m";
8
+ const OTHER_QUOTE_COLOR = "\x1b[38;5;1m";
9
+
10
+ // A theme whose sequences live in a mutable map, like the host's: a theme switch
11
+ // changes the values under the same object, so a fake that returned fixed literals
12
+ // could not show the signature moving.
13
+ function fakeTheme(overrides: Record<string, string> = {}): { theme: AnsiTheme; ansi: Record<string, string> } {
14
+ const ansi: Record<string, string> = { accent: ACCENT, mdQuote: FG_DEFAULT + DIM_ON, ...overrides };
15
+ return {
16
+ ansi,
17
+ theme: {
18
+ getFgAnsi: (color) => ansi[color] ?? "",
19
+ getBgAnsi: () => "",
20
+ },
21
+ };
22
+ }
23
+
24
+ test("markdownPalette: the heading accent is the theme's foreground for the accent token", () => {
25
+ assert.equal(markdownPalette(fakeTheme().theme).accent, ACCENT);
26
+ });
27
+
28
+ test("markdownPalette: a faint quote turns dim off while it opens and restores it when it closes", () => {
29
+ const palette = markdownPalette(fakeTheme().theme);
30
+ assert.equal(palette.quoteOpen, DIM_OFF + ACCENT);
31
+ // `\x1b[22m` closes bold along with dim, which is why the close puts the theme's
32
+ // own faint back instead of leaving the line brighter than the host painted it.
33
+ assert.equal(palette.quoteClose, FG_DEFAULT + DIM_ON);
34
+ });
35
+
36
+ test("markdownPalette: a quote that is not faint opens with the color alone", () => {
37
+ const palette = markdownPalette(fakeTheme({ mdQuote: QUOTE_COLOR }).theme);
38
+ assert.equal(palette.quoteOpen, ACCENT);
39
+ assert.equal(palette.quoteClose, FG_DEFAULT);
40
+ });
41
+
42
+ test("markdownPalette: a token that resolves to the terminal default adds no color", () => {
43
+ // The host maps a token with no color of its own to 39, and wrapping text in that
44
+ // sequence only cancels the style the surrounding line already has.
45
+ assert.equal(markdownPalette(fakeTheme({ accent: "" }).theme).accent, "");
46
+ assert.equal(markdownPalette(fakeTheme({ accent: FG_DEFAULT }).theme).accent, "");
47
+ });
48
+
49
+ test("markdownPalette: a colorless quote injects no pair at all", () => {
50
+ const palette = markdownPalette(fakeTheme({ accent: FG_DEFAULT }).theme);
51
+ assert.equal(palette.quoteOpen, "");
52
+ assert.equal(palette.quoteClose, "");
53
+ });
54
+
55
+ test("markdownPalette: the signature is stable while the sequences are", () => {
56
+ const { theme } = fakeTheme();
57
+ const first = markdownPalette(theme);
58
+ assert.equal(markdownPalette(theme).signature, first.signature);
59
+ // The signature is the concatenation of what the pass injects, so two palettes
60
+ // with the same one cannot produce different bytes.
61
+ assert.equal(first.signature, [first.accent, first.quoteOpen, first.quoteClose].join("\u0000"));
62
+ });
63
+
64
+ test("markdownPalette: the signature follows the accent when the theme changes", () => {
65
+ const { theme, ansi } = fakeTheme();
66
+ const before = markdownPalette(theme).signature;
67
+ ansi.accent = "\x1b[38;5;6m";
68
+ assert.notEqual(markdownPalette(theme).signature, before);
69
+ });
70
+
71
+ test("markdownPalette: the signature follows the faint flag of the quote", () => {
72
+ const { theme, ansi } = fakeTheme();
73
+ const before = markdownPalette(theme).signature;
74
+ ansi.mdQuote = QUOTE_COLOR;
75
+ assert.notEqual(markdownPalette(theme).signature, before);
76
+ });
77
+
78
+ test("markdownPalette: a change the pass cannot see leaves the signature alone", () => {
79
+ const { theme, ansi } = fakeTheme({ mdQuote: QUOTE_COLOR });
80
+ const before = markdownPalette(theme).signature;
81
+ // Another color, same faint flag: the injected sequences are byte identical, so
82
+ // invalidating a memoized render over it would be wasted work.
83
+ ansi.mdQuote = OTHER_QUOTE_COLOR;
84
+ ansi.warning = "\x1b[38;5;3m";
85
+ assert.equal(markdownPalette(theme).signature, before);
86
+ });
@@ -0,0 +1,194 @@
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 { buildBashBody, formatDuration, type BashInput } from "../../src/render-bash.ts";
5
+ import type { HighlightFn } from "../../src/highlight.ts";
6
+ import { TOKEN } from "../../src/tokens.ts";
7
+
8
+ const fakeTheme = {
9
+ fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
10
+ bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
11
+ bold: (t: string) => `<b>${t}</b>`,
12
+ };
13
+
14
+ interface HighlightCall {
15
+ code: string;
16
+ lang: string;
17
+ }
18
+
19
+ function makeHighlight(calls: HighlightCall[]): HighlightFn {
20
+ return (code, lang) => {
21
+ calls.push({ code, lang });
22
+ return code.split("\n").map((line) => `<${lang}>${line}</${lang}>`);
23
+ };
24
+ }
25
+
26
+ function styled(color: string, text: string): string {
27
+ return `<${color}>${text}</${color}>`;
28
+ }
29
+
30
+ function styledDiffChange(background: string, foreground: string, line: string): string {
31
+ return `<bg:${background}>${styled(foreground, line)}</bg:${background}>`;
32
+ }
33
+
34
+ function bashInput(overrides: Partial<BashInput> = {}): BashInput {
35
+ return {
36
+ output: "",
37
+ details: undefined,
38
+ durationMs: undefined,
39
+ theme: fakeTheme,
40
+ ...overrides,
41
+ };
42
+ }
43
+
44
+ test("buildBashBody: a diff is painted per line, headers in the function color and changes over their own background", () => {
45
+ const output = [
46
+ "diff --git a/x.ts b/x.ts",
47
+ "--- a/x.ts",
48
+ "+++ b/x.ts",
49
+ "@@ -1,2 +1,3 @@",
50
+ " const a = 1;",
51
+ "+const b = 2;",
52
+ "-const c = 3;",
53
+ ].join("\n");
54
+ const { body, warnings, timer } = buildBashBody(bashInput({ output }));
55
+ assert.equal(
56
+ body,
57
+ [
58
+ styled("text", "diff --git a/x.ts b/x.ts"),
59
+ styled("syntaxFunction", "--- a/x.ts"),
60
+ styled("syntaxFunction", "+++ b/x.ts"),
61
+ styled("syntaxFunction", "@@ -1,2 +1,3 @@"),
62
+ styled("text", " const a = 1;"),
63
+ styledDiffChange("toolSuccessBg", "toolDiffAdded", "+const b = 2;"),
64
+ styledDiffChange("toolErrorBg", "toolDiffRemoved", "-const c = 3;"),
65
+ ].join("\n"),
66
+ );
67
+ assert.equal(warnings, undefined);
68
+ assert.equal(timer, undefined);
69
+ });
70
+
71
+ test("buildBashBody: JSON output is highlighted as one block", () => {
72
+ const calls: HighlightCall[] = [];
73
+ const json = '{"a":1,"b":[2,3]}';
74
+ const { body } = buildBashBody(bashInput({ output: json, highlight: makeHighlight(calls) }));
75
+ assert.deepEqual(calls, [{ code: json, lang: "json" }]);
76
+ assert.equal(body, "<json>{\"a\":1,\"b\":[2,3]}</json>");
77
+ });
78
+
79
+ test("buildBashBody: output that starts like JSON but does not parse is painted per line", () => {
80
+ const { body } = buildBashBody(bashInput({ output: '{"a": }', highlight: makeHighlight([]) }));
81
+ assert.equal(body, styled("text", '{"a": }'));
82
+ });
83
+
84
+ test("buildBashBody: error and warning lines get their own colors", () => {
85
+ assert.equal(TOKEN.broken, "error");
86
+ assert.equal(TOKEN.notice, "warning");
87
+ const { body } = buildBashBody(bashInput({ output: "error: boom\nwarning: careful\nplain text" }));
88
+ assert.equal(
89
+ body,
90
+ [
91
+ styled("error", "error: boom"),
92
+ styled("warning", "warning: careful"),
93
+ styled("text", "plain text"),
94
+ ].join("\n"),
95
+ );
96
+ });
97
+
98
+ test("buildBashBody: a path, line and text line is split into its three colors", () => {
99
+ const { body } = buildBashBody(bashInput({ output: "src/app.ts:12: some text" }));
100
+ assert.equal(
101
+ body,
102
+ [
103
+ styled("accent", "src/app.ts"),
104
+ styled("dim", ":"),
105
+ styled("syntaxNumber", "12"),
106
+ styled("dim", ":"),
107
+ " ",
108
+ styled("text", "some text"),
109
+ ].join(""),
110
+ );
111
+ });
112
+
113
+ test("buildBashBody: line truncation is reported with the shown and total line counts", () => {
114
+ const { body, warnings } = buildBashBody(
115
+ bashInput({
116
+ output: "line1\nline2",
117
+ details: { truncation: { truncated: true, truncatedBy: "lines", outputLines: 10, totalLines: 500 } },
118
+ }),
119
+ );
120
+ assert.equal(warnings, "Truncated: showing 10 of 500 lines");
121
+ assert.equal(body, [styled("text", "line1"), styled("text", "line2")].join("\n"));
122
+ });
123
+
124
+ test("buildBashBody: byte truncation reports the limit through formatSize", () => {
125
+ const recorded = buildBashBody(
126
+ bashInput({
127
+ output: "x",
128
+ details: { truncation: { truncated: true, truncatedBy: "bytes", outputLines: 7, maxBytes: 1024 } },
129
+ }),
130
+ );
131
+ assert.equal(recorded.warnings, `Truncated: 7 lines shown (${formatSize(1024)} limit)`);
132
+
133
+ const implicit = buildBashBody(
134
+ bashInput({ output: "x", details: { truncation: { truncated: true, truncatedBy: "bytes", outputLines: 7 } } }),
135
+ );
136
+ assert.equal(implicit.warnings, `Truncated: 7 lines shown (${formatSize(DEFAULT_MAX_BYTES)} limit)`);
137
+ });
138
+
139
+ test("buildBashBody: the full output block moves from the body to the warnings", () => {
140
+ const output = "line1\nline2\n\n[Full output: /tmp/pi-bash-1.log]";
141
+ const { body, warnings } = buildBashBody(
142
+ bashInput({
143
+ output,
144
+ details: {
145
+ truncation: { truncated: true, truncatedBy: "lines", outputLines: 2, totalLines: 900 },
146
+ fullOutputPath: "/tmp/pi-bash-1.log",
147
+ },
148
+ }),
149
+ );
150
+ assert.equal(body, [styled("text", "line1"), styled("text", "line2")].join("\n"));
151
+ assert.equal(warnings, "Full output: /tmp/pi-bash-1.log. Truncated: showing 2 of 900 lines");
152
+ });
153
+
154
+ test("buildBashBody: a full output path without truncation is warned and stays in the body", () => {
155
+ const output = "line1\n\n[Full output: /tmp/x.log]";
156
+ const { body, warnings } = buildBashBody(bashInput({ output, details: { fullOutputPath: "/tmp/x.log" } }));
157
+ assert.equal(warnings, "Full output: /tmp/x.log");
158
+ assert.equal(
159
+ body,
160
+ [
161
+ styled("text", "line1"),
162
+ styled("text", ""),
163
+ styled("text", "[Full output: /tmp/x.log]"),
164
+ ].join("\n"),
165
+ );
166
+ });
167
+
168
+ test("buildBashBody: an untruncated result carries no warning", () => {
169
+ const { warnings } = buildBashBody(bashInput({ output: "line1" }));
170
+ assert.equal(warnings, undefined);
171
+ });
172
+
173
+ test("buildBashBody: durationMs becomes the timer text without the prefix", () => {
174
+ assert.equal(buildBashBody(bashInput({ output: "x", durationMs: 4200 })).timer, "4.2s");
175
+ assert.equal(buildBashBody(bashInput({ output: "x" })).timer, undefined);
176
+ });
177
+
178
+ test("buildBashBody: empty and blank outputs have no body, warnings or timer", () => {
179
+ for (const output of ["", " \n "]) {
180
+ assert.deepEqual(buildBashBody(bashInput({ output })), {
181
+ body: "",
182
+ warnings: undefined,
183
+ timer: undefined,
184
+ });
185
+ }
186
+ });
187
+
188
+ test("formatDuration: identical to the built-in formatter", () => {
189
+ assert.equal(formatDuration(4200), "4.2s");
190
+ assert.equal(formatDuration(65000), "1m 5s");
191
+ assert.equal(formatDuration(7324000), "2h 2m 4s");
192
+ // toFixed rounds 59.999 up to 60, which is what stock pi prints.
193
+ assert.equal(formatDuration(59999), "60.0s");
194
+ });
@@ -0,0 +1,214 @@
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
+ });
@@ -0,0 +1,139 @@
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
+ });