@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 (152) hide show
  1. package/README.md +18 -12
  2. package/README.zh-CN.md +18 -12
  3. package/package.json +8 -1
  4. package/src/core/AGENTS.md +3 -1
  5. package/src/core/apply.ts +110 -117
  6. package/src/core/character-set.ts +2 -3
  7. package/src/core/copy-span.ts +15 -0
  8. package/src/core/errors.ts +24 -9
  9. package/src/core/lines.ts +71 -17
  10. package/src/core/ranges.ts +5 -4
  11. package/src/core/replace-schema.ts +3 -1
  12. package/src/core/replace.ts +50 -29
  13. package/src/core/search-analysis.ts +21 -22
  14. package/src/core/search.ts +15 -32
  15. package/src/core/types.ts +3 -0
  16. package/src/index.ts +47 -39
  17. package/src/pi/AGENTS.md +30 -19
  18. package/src/pi/action-fusion.ts +284 -211
  19. package/src/pi/argument-diagnostics.ts +50 -37
  20. package/src/pi/argument-error.ts +2 -2
  21. package/src/pi/argument-validation.ts +163 -66
  22. package/src/pi/budgets.ts +12 -0
  23. package/src/pi/diagnostic-buffer.ts +12 -11
  24. package/src/pi/edit-tool.ts +138 -115
  25. package/src/pi/error-text.ts +10 -2
  26. package/src/pi/failure-context.ts +24 -37
  27. package/src/pi/file-commit.ts +131 -164
  28. package/src/pi/file-read.ts +122 -31
  29. package/src/pi/fusion-arguments.ts +214 -0
  30. package/src/pi/fusion-card.ts +102 -60
  31. package/src/pi/grep-candidates.ts +35 -22
  32. package/src/pi/grep-output.ts +197 -190
  33. package/src/pi/grep-render.ts +22 -13
  34. package/src/pi/grep-reread.ts +7 -6
  35. package/src/pi/grep-scope.ts +31 -16
  36. package/src/pi/grep-search.ts +56 -47
  37. package/src/pi/grep-tool.ts +35 -33
  38. package/src/pi/grep-worker.ts +3 -4
  39. package/src/pi/mutation-diff.ts +238 -0
  40. package/src/pi/mutation-result.ts +102 -79
  41. package/src/pi/mutation-runner.ts +107 -84
  42. package/src/pi/native-bash.ts +41 -0
  43. package/src/pi/path.ts +59 -6
  44. package/src/pi/read-tool.ts +173 -138
  45. package/src/pi/regex-worker.ts +14 -8
  46. package/src/pi/render.ts +101 -54
  47. package/src/pi/replace-regex.ts +1 -1
  48. package/src/pi/replace-tool.ts +14 -7
  49. package/src/pi/report-cause.ts +34 -17
  50. package/src/pi/report-schema.ts +184 -85
  51. package/src/pi/report.ts +133 -64
  52. package/src/pi/rg-process.ts +68 -48
  53. package/src/pi/schema.ts +4 -0
  54. package/src/pi/text-stream.ts +81 -87
  55. package/src/pi/workspace-cwd.js +2 -0
  56. package/src/pi/workspace.ts +46 -0
  57. package/src/pi/write-tool.ts +19 -13
  58. package/src/core/apply.test.ts +0 -644
  59. package/src/core/case-fold.test.ts +0 -35
  60. package/src/core/character-set.test.ts +0 -97
  61. package/src/core/errors.test.ts +0 -39
  62. package/src/core/hash.test.ts +0 -64
  63. package/src/core/lines.test.ts +0 -63
  64. package/src/core/ranges.test.ts +0 -123
  65. package/src/core/replace.test.ts +0 -187
  66. package/src/core/search-analysis.test.ts +0 -210
  67. package/src/core/search-matrix.test.ts +0 -64
  68. package/src/core/search-matrix.testing.ts +0 -171
  69. package/src/core/search.test.ts +0 -132
  70. package/src/core/text.test.ts +0 -52
  71. package/src/integration/config-registration.test.ts +0 -291
  72. package/src/integration/fusion-dispatch.test.ts +0 -320
  73. package/src/integration/grep-errors.test.ts +0 -81
  74. package/src/integration/grep-flags.test.ts +0 -76
  75. package/src/integration/grep-js-completeness.test.ts +0 -278
  76. package/src/integration/grep-line-candidates.test.ts +0 -201
  77. package/src/integration/grep-matrix.test.ts +0 -118
  78. package/src/integration/grep-order.test.ts +0 -78
  79. package/src/integration/grep-path-matrix.test.ts +0 -128
  80. package/src/integration/grep-prefilter-coverage.test.ts +0 -204
  81. package/src/integration/grep-process-limits.test.ts +0 -122
  82. package/src/integration/grep-regex.test.ts +0 -412
  83. package/src/integration/grep-rg.test.ts +0 -1659
  84. package/src/integration/grep-scan-budget.test.ts +0 -111
  85. package/src/integration/grep-translation.test.ts +0 -199
  86. package/src/integration/large-text.test.ts +0 -70
  87. package/src/integration/model-budget-lifecycle.test.ts +0 -186
  88. package/src/integration/mutation-file-types.test.ts +0 -30
  89. package/src/integration/mutation-file-types.testing.ts +0 -33
  90. package/src/integration/npm-package.test.ts +0 -19
  91. package/src/integration/npm-package.testing.ts +0 -243
  92. package/src/integration/publication-read-changes.test.ts +0 -454
  93. package/src/integration/read-changes.test.ts +0 -98
  94. package/src/integration/replace-package.test.ts +0 -22
  95. package/src/integration/replace-package.testing.ts +0 -36
  96. package/src/integration/replace-worker-boundary.test.ts +0 -127
  97. package/src/integration/report-replay.test.ts +0 -91
  98. package/src/integration/utf8-errors.test.ts +0 -46
  99. package/src/pi/action-fusion.test.ts +0 -659
  100. package/src/pi/action-fusion.testing.ts +0 -38
  101. package/src/pi/anchor-format.test.ts +0 -124
  102. package/src/pi/argument-error.testing.ts +0 -46
  103. package/src/pi/argument-format.test.ts +0 -196
  104. package/src/pi/argument-validation.test.ts +0 -1007
  105. package/src/pi/codemode.test.ts +0 -101
  106. package/src/pi/config.test.ts +0 -204
  107. package/src/pi/diagnostic-buffer.test.ts +0 -115
  108. package/src/pi/directory-sync.test.ts +0 -57
  109. package/src/pi/execute.test.ts +0 -1257
  110. package/src/pi/extension-loading.test.ts +0 -14
  111. package/src/pi/extension-loading.testing.ts +0 -3
  112. package/src/pi/failure-context.test.ts +0 -370
  113. package/src/pi/failure-path.test.ts +0 -289
  114. package/src/pi/file-commit.test.ts +0 -628
  115. package/src/pi/file-read.test.ts +0 -29
  116. package/src/pi/file-snapshot.test.ts +0 -22
  117. package/src/pi/fusion-card-interaction.test.ts +0 -143
  118. package/src/pi/fusion-card.test.ts +0 -353
  119. package/src/pi/grep-candidates.test.ts +0 -26
  120. package/src/pi/grep-matrix.test.ts +0 -68
  121. package/src/pi/grep-order.test.ts +0 -447
  122. package/src/pi/grep-output.testing.ts +0 -24
  123. package/src/pi/grep-rg.testing.ts +0 -73
  124. package/src/pi/grep.test.ts +0 -1670
  125. package/src/pi/model-budgets.test.ts +0 -96
  126. package/src/pi/mutation-outcome.testing.ts +0 -20
  127. package/src/pi/mutation-result.test.ts +0 -96
  128. package/src/pi/output-budgets.test.ts +0 -221
  129. package/src/pi/pi.test.ts +0 -158
  130. package/src/pi/read-delegation.test.ts +0 -119
  131. package/src/pi/read.test.ts +0 -735
  132. package/src/pi/regex-worker.test.ts +0 -42
  133. package/src/pi/registration.test.ts +0 -549
  134. package/src/pi/render.test.ts +0 -841
  135. package/src/pi/replace.test.ts +0 -634
  136. package/src/pi/report-failure.testing.ts +0 -27
  137. package/src/pi/report.test.ts +0 -533
  138. package/src/pi/result-safety.test.ts +0 -597
  139. package/src/pi/rg-process.test.ts +0 -133
  140. package/src/pi/rg-regex.test.ts +0 -40
  141. package/src/pi/text-stream.test.ts +0 -281
  142. package/src/pi/tool-call.testing.ts +0 -61
  143. package/src/pi/tool-factories.testing.ts +0 -59
  144. package/src/pi/write.test.ts +0 -256
  145. package/src/testing/extension-loader.testing.ts +0 -24
  146. package/src/testing/installation.testing.ts +0 -77
  147. package/src/testing/process.testing.ts +0 -33
  148. package/src/testing/regex-host.testing.ts +0 -20
  149. package/src/testing/regex-translation-corpus.ts +0 -267
  150. package/src/testing/rg-line-listing.fixture.mjs +0 -7
  151. package/src/testing/run-tests.ts +0 -47
  152. package/src/testing/session.testing.ts +0 -127
@@ -1,143 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
- import { initTheme, type AgentSession } from "@earendil-works/pi-coding-agent";
4
- import { ProcessTerminal, TuiMainScreen, type TuiMouseEvent } from "@earendil-works/pi-tui";
5
- import { CustomEntryComponent } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/custom-entry.js";
6
- import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
7
- import { openTestSession } from "../testing/session.testing.ts";
8
- import { registerFusionCards } from "./fusion-card.ts";
9
-
10
- const output = Array.from({ length: 20 }, (_, i) => `command output ${i}`).join("\n");
11
-
12
- async function bindTui(session: AgentSession) {
13
- const tui = new TuiMainScreen(new ProcessTerminal());
14
- tui.requestRender = () => {};
15
- await session.bindExtensions({
16
- mode: "tui",
17
- uiContext: {
18
- ...session.extensionRunner.createContext().ui,
19
- setWidget: ((_, content) => {
20
- if (typeof content === "function") assert.deepEqual(content(tui, theme).render(100), []);
21
- }) satisfies ReturnType<typeof session.extensionRunner.createContext>["ui"]["setWidget"],
22
- },
23
- });
24
- }
25
-
26
- test("command cards share native mouse behavior and preserve independent expansion through updates and redraws", async (t) => {
27
- initTheme("dark");
28
- let report: ReturnType<typeof registerFusionCards> | undefined;
29
- const { session, sessionManager } = await openTestSession(t, {
30
- tools: [],
31
- configure(pi) {
32
- report = registerFusionCards(pi);
33
- },
34
- });
35
- await bindTui(session);
36
- assert.ok(report);
37
- const progress = {
38
- toolCallId: "first",
39
- commandText: "check",
40
- command: "running" as const,
41
- freshness: "unchanged" as const,
42
- result: { content: [{ type: "text" as const, text: output }] },
43
- mutationCompleted: true,
44
- };
45
- report(progress);
46
- report({ ...progress, toolCallId: "second" });
47
- const renderer = session.extensionRunner.getEntryRenderer("hashline-then-run");
48
- assert.ok(renderer);
49
- const hosts = sessionManager
50
- .getBranch()
51
- .filter((entry) => entry.type === "custom" && entry.customType === "hashline-then-run")
52
- .map((entry) => {
53
- assert.ok(entry.type === "custom");
54
- return new CustomEntryComponent(entry, renderer);
55
- });
56
- const [first, second] = hosts;
57
- assert.ok(first && second);
58
- const rows = first.render(100);
59
- const event: TuiMouseEvent = {
60
- type: "click",
61
- button: "left",
62
- x: 4,
63
- y: 2,
64
- screenX: 4,
65
- screenY: 2,
66
- width: 100,
67
- height: rows.length,
68
- shift: false,
69
- alt: false,
70
- ctrl: false,
71
- };
72
- assert.doesNotMatch(rows.join("\n"), /command output 0\b/);
73
- for (const ignored of [
74
- { ...event, button: "right" as const },
75
- ...(["press", "release", "drag", "wheel"] as const).map((type) => ({ ...event, type })),
76
- ]) {
77
- assert.equal(first.handleMouse(ignored), undefined);
78
- assert.deepEqual(first.render(100), rows);
79
- }
80
- assert.equal(first.handleMouse(event)?.handled, true);
81
- assert.match(first.render(100).join("\n"), /command output 0\b/);
82
- assert.doesNotMatch(second.render(100).join("\n"), /command output 0\b/);
83
- report({
84
- ...progress,
85
- command: "succeeded",
86
- result: { content: [{ type: "text", text: `${output}\nfinished` }] },
87
- });
88
- first.invalidate();
89
- assert.match(first.render(40).join("\n"), /command output 0\b/);
90
- assert.match(first.render(100).join("\n"), /finished/);
91
- first.setExpanded(true);
92
- first.setExpanded(false);
93
- assert.doesNotMatch(first.render(100).join("\n"), /command output 0\b/);
94
- first.setExpanded(true);
95
- assert.match(first.render(100).join("\n"), /command output 0\b/);
96
- });
97
-
98
- test("command entries rebuilt before session_start → remain visible and become interactive after UI binding", async (t) => {
99
- initTheme("dark");
100
- let report: ReturnType<typeof registerFusionCards> | undefined;
101
- const { session, sessionManager } = await openTestSession(t, {
102
- tools: [],
103
- configure(pi) {
104
- report = registerFusionCards(pi);
105
- },
106
- });
107
- assert.ok(report);
108
- report({
109
- toolCallId: "restored",
110
- commandText: "check",
111
- command: "succeeded",
112
- freshness: "unchanged",
113
- result: { content: [{ type: "text", text: output }] },
114
- mutationCompleted: true,
115
- });
116
- const entry = sessionManager
117
- .getBranch()
118
- .find((entry) => entry.type === "custom" && entry.customType === "hashline-then-run");
119
- const renderer = session.extensionRunner.getEntryRenderer("hashline-then-run");
120
- assert.ok(entry?.type === "custom" && renderer);
121
- const host = new CustomEntryComponent(entry, renderer);
122
- assert.equal(host.hasContent(), true, "Pi discards entries without a component during reload");
123
- await bindTui(session);
124
- const rows = host.render(100);
125
- assert.match(rows.join("\n"), /succeeded[\s\S]*command output 19/);
126
- assert.equal(
127
- host.handleMouse({
128
- type: "click",
129
- button: "left",
130
- x: 4,
131
- y: 2,
132
- screenX: 4,
133
- screenY: 2,
134
- width: 100,
135
- height: rows.length,
136
- shift: false,
137
- alt: false,
138
- ctrl: false,
139
- })?.handled,
140
- true,
141
- );
142
- assert.match(host.render(100).join("\n"), /command output 0\b/);
143
- });
@@ -1,353 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { after, before, test } from "node:test";
3
- import { mkdtemp, rm } from "node:fs/promises";
4
- import { tmpdir } from "node:os";
5
- import { join } from "node:path";
6
- import { initTheme } from "@earendil-works/pi-coding-agent";
7
- import { registerFusionCards } from "./fusion-card.ts";
8
- import { createActionFusionExecutor } from "./action-fusion.ts";
9
- import type { ActionFusionProgress } from "./report-schema.ts";
10
- import type { CommandResult } from "./report-schema.ts";
11
- import { commandText } from "./mutation-result.ts";
12
- import { reportOf } from "./report.ts";
13
- import { makeWriteOverride } from "./tool-factories.testing.ts";
14
- import { commandCompletion, createFusionContext } from "./action-fusion.testing.ts";
15
- import { callTool } from "./tool-call.testing.ts";
16
- import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
17
- import { ProcessTerminal, TuiMainScreen } from "@earendil-works/pi-tui";
18
- import { theme as activeTheme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
19
-
20
- const themeKey = Symbol.for("@earendil-works/pi-coding-agent:theme");
21
- const originalTheme = Object.getOwnPropertyDescriptor(globalThis, themeKey);
22
- before(() => initTheme("dark"));
23
- after(() => {
24
- if (originalTheme) Object.defineProperty(globalThis, themeKey, originalTheme);
25
- else Reflect.deleteProperty(globalThis, themeKey);
26
- });
27
-
28
- function harness(entries: any[] = []) {
29
- const renderers = new Map<string, any>();
30
- const handlers = new Map<string, any>();
31
- const tui = new TuiMainScreen(new ProcessTerminal());
32
- tui.requestRender = () => {};
33
- const report = registerFusionCards({
34
- getSettings() {
35
- return {};
36
- },
37
- on(event: string, handler: any) {
38
- handlers.set(event, handler);
39
- },
40
- registerEntryRenderer(type: string, renderer: any) {
41
- renderers.set(type, renderer);
42
- },
43
- appendEntry(customType: string, data: unknown) {
44
- entries.push({ type: "custom", customType, data: structuredClone(data) });
45
- },
46
- } as any);
47
- const restore = (event = "session_start", branch = entries, extra = {}) =>
48
- handlers.get(event)(
49
- {},
50
- {
51
- cwd: process.cwd(),
52
- mode: "tui",
53
- ui: {
54
- setWidget(_key: string, content: Parameters<ExtensionContext["ui"]["setWidget"]>[1]) {
55
- if (typeof content === "function") content(tui, activeTheme);
56
- },
57
- },
58
- sessionManager: { getBranch: () => branch },
59
- ...extra,
60
- },
61
- );
62
- const card = (entry: any, expanded = false) =>
63
- renderers.get(entry.customType)(entry, { expanded }, activeTheme);
64
- restore();
65
- return { entries, report, restore, card };
66
- }
67
-
68
- const waiting: ActionFusionProgress = {
69
- toolCallId: "call-1",
70
- commandText: "npm test",
71
- command: "waiting",
72
- freshness: "unknown",
73
- mutationCompleted: false,
74
- };
75
-
76
- test("one independent card updates live and only persists its endpoints", () => {
77
- const h = harness();
78
- h.report(waiting);
79
- const card = h.card(h.entries[0]);
80
- assert.match(card.render(100).join("\n"), /waiting/);
81
- assert.ok(card.render(100).join("\n").includes(activeTheme.getBgAnsi("toolPendingBg")));
82
- h.report({
83
- ...waiting,
84
- command: "running",
85
- result: { content: [{ type: "text", text: "first test passed" }] },
86
- });
87
- assert.match(card.render(100).join("\n"), /running[\s\S]*first test passed/);
88
- assert.equal(h.entries.length, 1);
89
- const output = Array.from({ length: 20 }, (_, i) => `output line ${i}`).join("\n");
90
- h.report({
91
- ...waiting,
92
- command: "succeeded",
93
- freshness: "changed",
94
- result: { content: [{ type: "text", text: output }] },
95
- });
96
- assert.equal(h.entries.length, 2);
97
- assert.equal(h.entries[0].data.command, "waiting");
98
- const collapsed = card.render(100).join("\n");
99
- assert.match(collapsed, /succeeded/);
100
- assert.ok(collapsed.includes(activeTheme.getBgAnsi("toolSuccessBg")));
101
- assert.match(collapsed, /output line 19/);
102
- assert.doesNotMatch(collapsed, /output line 0\b/);
103
- assert.match(h.card(h.entries[0], true).render(100).join("\n"), /output line 0\b/);
104
- });
105
-
106
- test("Fusion command outcomes → progress, cards, and reports preserve command results", async (t) => {
107
- const success: CommandResult = {
108
- details: {},
109
- content: [
110
- { type: "text", text: "first" },
111
- { type: "text", text: "second" },
112
- ],
113
- };
114
- const failure: CommandResult = { content: [{ type: "text", text: "boom" }], details: {} };
115
- const cases: readonly {
116
- scenario:
117
- | "succeeded"
118
- | "failed"
119
- | "cancelled-error"
120
- | "cancelled-empty"
121
- | "skipped"
122
- | "cancelled";
123
- command: ActionFusionProgress["command"];
124
- result?: CommandResult;
125
- text: RegExp;
126
- }[] = [
127
- { scenario: "succeeded", command: "succeeded", result: success, text: /first[\s\S]*second/ },
128
- { scenario: "failed", command: "failed", result: failure, text: /boom/ },
129
- { scenario: "cancelled-error", command: "cancelled", result: failure, text: /boom/ },
130
- {
131
- scenario: "cancelled-empty",
132
- command: "cancelled",
133
- result: { content: [], details: {} },
134
- text: /Not run: cancelled\./,
135
- },
136
- { scenario: "skipped", command: "skipped", text: /Not run: the mutation did not complete/ },
137
- { scenario: "cancelled", command: "cancelled", text: /Not run: cancelled\./ },
138
- ];
139
- for (const { scenario, command, result: expected, text } of cases) {
140
- await t.test(scenario, async (t) => {
141
- const dir = await mkdtemp(join(tmpdir(), "hashline-fusion-card-"));
142
- t.after(() => rm(dir, { recursive: true, force: true }));
143
- const h = harness();
144
- h.restore("session_start", h.entries, { cwd: dir });
145
- const events: ActionFusionProgress[] = [];
146
- const controller = new AbortController();
147
- if (scenario === "cancelled") controller.abort();
148
- let commands = 0;
149
- const context = await createFusionContext(dir, t, async () => {
150
- commands++;
151
- if (scenario === "cancelled-error" || scenario === "cancelled-empty") controller.abort();
152
- if (scenario === "failed" || scenario === "cancelled-error")
153
- return commandCompletion(failure, true);
154
- return scenario === "cancelled-empty"
155
- ? commandCompletion({ content: [] }, true)
156
- : commandCompletion(success);
157
- });
158
- const fusion = createActionFusionExecutor((progress) => {
159
- events.push(progress);
160
- h.report(progress);
161
- });
162
- const result = await callTool(
163
- makeWriteOverride(dir, fusion),
164
- {
165
- path: "file.txt",
166
- mode: scenario === "skipped" ? "overwrite" : "create",
167
- content: "saved\n",
168
- then_run: { command: "check" },
169
- },
170
- { ctx: context, signal: controller.signal },
171
- );
172
- assert.equal(commands, expected ? 1 : 0);
173
- const final = events.at(-1);
174
- assert.ok(final);
175
- assert.equal(final.command, command);
176
- assert.equal(Object.hasOwn(final, "result"), expected !== undefined);
177
- assert.deepEqual(final.result, expected);
178
- for (const progress of events) {
179
- if (progress.command === "waiting") assert.equal(Object.hasOwn(progress, "result"), false);
180
- }
181
- const saved = h.entries.find((entry) => entry.customType === "hashline-then-run-result");
182
- assert.ok(saved);
183
- assert.equal(Object.hasOwn(saved.data, "result"), expected !== undefined);
184
- assert.deepEqual(saved.data.result, expected);
185
- assert.equal(Object.hasOwn(h.entries[0].data, "result"), false);
186
- const block = result.content.at(-1);
187
- assert.ok(block?.type === "text");
188
- const report = reportOf({ content: [block] }, "write");
189
- assert.ok(report);
190
- const output = expected ? commandText(expected.content) : "";
191
- assert.deepEqual(report.then_run, { status: command, ...(output ? { output } : {}) });
192
- const rendered = h.card(h.entries[0], true).render(160).join("\n");
193
- assert.match(rendered, text);
194
- if (command === "cancelled") assert.equal(rendered.includes("Not run: cancelled."), !output);
195
- });
196
- }
197
- });
198
-
199
- test("session restore uses final snapshots and marks unfinished calls unknown", () => {
200
- const h = harness();
201
- h.report(waiting);
202
- h.report({
203
- ...waiting,
204
- command: "failed",
205
- result: {
206
- content: [{ type: "text", text: "Command exited with code 7" }],
207
- details: { fullOutputPath: "/tmp/command-output.log" },
208
- },
209
- });
210
- const resumed = harness(h.entries);
211
- resumed.restore();
212
- assert.match(
213
- resumed.card(h.entries[0]).render(100).join("\n"),
214
- /failed[\s\S]*Command exited with code 7[\s\S]*command-output\.log/,
215
- );
216
- assert.ok(
217
- resumed
218
- .card(h.entries[0])
219
- .render(100)
220
- .join("\n")
221
- .includes(activeTheme.getBgAnsi("toolErrorBg")),
222
- );
223
- resumed.restore("session_tree", [h.entries[0]]);
224
- assert.match(
225
- resumed.card(h.entries[0]).render(100).join("\n"),
226
- /interrupted \(final status unknown\)/,
227
- );
228
- });
229
-
230
- test("session restore accepts optional command results", async (t) => {
231
- const cases: readonly {
232
- name: string;
233
- fields: { result?: CommandResult };
234
- command: ActionFusionProgress["command"];
235
- text: RegExp;
236
- }[] = [
237
- {
238
- name: "result is present → restore result text",
239
- fields: { result: { content: [{ type: "text", text: "result text" }] } },
240
- command: "succeeded",
241
- text: /result text/,
242
- },
243
- {
244
- name: "result is absent → restore the command status",
245
- fields: {},
246
- command: "cancelled",
247
- text: /Not run: cancelled\./,
248
- },
249
- ];
250
- for (const { name, fields, command, text } of cases) {
251
- await t.test(name, () => {
252
- for (const event of ["session_start", "session_tree"]) {
253
- const original = harness();
254
- original.report(waiting);
255
- const initial = original.entries[0];
256
- const snapshot = {
257
- ...initial,
258
- data: {
259
- toolCallId: waiting.toolCallId,
260
- commandText: waiting.commandText,
261
- command,
262
- mutationCompleted: true,
263
- freshness: "unchanged",
264
- ...fields,
265
- },
266
- };
267
- const resumed = harness();
268
- resumed.restore(event, [initial, { ...snapshot, customType: "hashline-then-run-result" }]);
269
- const shown = resumed.card(initial, true).render(160).join("\n");
270
- assert.match(shown, text, event);
271
- }
272
- });
273
- }
274
- });
275
-
276
- test("skips and cancellations stay neutral and restore their own reason", () => {
277
- for (const command of ["skipped", "cancelled"] as const) {
278
- const h = harness();
279
- h.report(waiting);
280
- h.report({ ...waiting, command, mutationCompleted: false });
281
- for (const restored of [false, true]) {
282
- const view = restored ? harness(h.entries) : h;
283
- if (restored) view.restore();
284
- const output = view.card(h.entries[0], true).render(160).join("\n");
285
- assert.match(
286
- output,
287
- command === "skipped" ? /Not run: the mutation did not complete/ : /Not run: cancelled/,
288
- );
289
- assert.doesNotMatch(output, /\(no output\)/);
290
- assert.ok(!output.includes(activeTheme.getBgAnsi("toolErrorBg")));
291
- assert.ok(!output.includes(activeTheme.getBgAnsi("toolSuccessBg")));
292
- assert.ok(output.includes(activeTheme.getBgAnsi("customMessageBg")));
293
- }
294
- }
295
- });
296
-
297
- test("timed command cards show remaining seconds only while actively running", () => {
298
- const h = harness();
299
- h.report(waiting);
300
- const card = h.card(h.entries[0]);
301
- assert.doesNotMatch(card.render(160).join("\n"), /remaining/);
302
- const running: ActionFusionProgress = {
303
- ...waiting,
304
- command: "running",
305
- mutationCompleted: true,
306
- timing: { timeoutSeconds: 10, remainingSeconds: 10 },
307
- };
308
- h.report(running);
309
- assert.match(card.render(160).join("\n"), /10s remaining/);
310
- h.report({ ...running, timing: { timeoutSeconds: 10, remainingSeconds: 7 } });
311
- assert.match(card.render(160).join("\n"), /7s remaining/);
312
- assert.equal(h.entries.length, 1);
313
- h.report({ ...running, timing: { timeoutSeconds: 10, remainingSeconds: 0 } });
314
- assert.match(card.render(160).join("\n"), /0s remaining/);
315
- h.report({
316
- ...running,
317
- command: "timeout",
318
- result: { content: [{ type: "text", text: "Command timed out after 10 seconds" }] },
319
- });
320
- assert.doesNotMatch(card.render(160).join("\n"), /remaining/);
321
- const restored = harness(h.entries);
322
- restored.restore();
323
- assert.doesNotMatch(restored.card(h.entries[0]).render(160).join("\n"), /remaining/);
324
- });
325
-
326
- test("invalid persisted command snapshots → do not replace valid progress or become executable cards", () => {
327
- const original = harness();
328
- original.report(waiting);
329
- const initial = original.entries[0];
330
- for (const invalid of [
331
- ...["mutationCompleted", "freshness"].map((missing) =>
332
- Object.fromEntries(
333
- Object.entries({ ...initial.data, command: "succeeded" }).filter(
334
- ([key]) => key !== missing,
335
- ),
336
- ),
337
- ),
338
- { ...initial.data, command: "not-a-command-status" },
339
- { ...initial.data, freshness: "fresh" },
340
- { ...initial.data, mutationCompleted: "yes" },
341
- { ...initial.data, timing: { timeoutSeconds: -1, remainingSeconds: 2 } },
342
- { ...initial.data, timing: { timeoutSeconds: 3, remainingSeconds: "2" } },
343
- { ...initial.data, foreign: true },
344
- ]) {
345
- const malformed = { ...initial, data: invalid };
346
- const resumed = harness([initial, { ...malformed, customType: "hashline-then-run-result" }]);
347
- resumed.restore();
348
- assert.equal(resumed.card(malformed), undefined);
349
- const shown = resumed.card(initial).render(160).join("\n");
350
- assert.match(shown, /interrupted \(final status unknown\)/);
351
- assert.doesNotMatch(shown, /not-a-command-status|remaining/);
352
- }
353
- });
@@ -1,26 +0,0 @@
1
- import { fileCandidateArgs } from "./grep-rg.testing.ts";
2
- import { test } from "node:test";
3
- import assert from "node:assert/strict";
4
- import { COMMON_RG_ARGS } from "./rg-process.ts";
5
-
6
- test("candidate literals → rg lists files with escaped byte-preserving alternatives", async () => {
7
- const query = { patterns: ["."], literal: false, ignoreCase: false, multiline: true };
8
- assert.deepEqual(await fileCandidateArgs(query), [...COMMON_RG_ARGS, "--files"]);
9
- for (const [patterns, literal, ignoreCase, pattern] of [
10
- [["a.b", "-é"], true, false, "a\\x{2e}b|\\x{2d}\\x{e9}"],
11
- [["k_9"], true, true, "[Kk]_9"],
12
- ] as const) {
13
- assert.deepEqual(
14
- await fileCandidateArgs({ patterns: [...patterns], literal, ignoreCase, multiline: true }),
15
- [
16
- ...COMMON_RG_ARGS,
17
- "--files-with-matches",
18
- "--engine=default",
19
- "--case-sensitive",
20
- "--encoding=none",
21
- "-e",
22
- pattern,
23
- ],
24
- );
25
- }
26
- });
@@ -1,68 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
- import { MATRIX_SYNTAX } from "../core/search-matrix.testing.ts";
4
- import { GREP_FLAGS } from "../core/search.ts";
5
- import { makeMockGrep } from "./grep-rg.testing.ts";
6
- const { DEFAULT_CONFIG } = await import("./tool-factories.testing.ts");
7
- import { reportOf } from "./report.ts";
8
- import { rejectsArgument } from "./argument-error.testing.ts";
9
- import { callTool } from "./tool-call.testing.ts";
10
-
11
- test("Cartesian JS syntax × case override → tool entry agrees with V8 before discovery", async (t) => {
12
- let discoveries = 0;
13
- const discover = async () => {
14
- discoveries++;
15
- return { code: 1, stderr: "", stopped: false };
16
- };
17
- const tool = makeMockGrep(process.cwd(), DEFAULT_CONFIG, {
18
- runRgPaths: discover,
19
- runRgLines: discover,
20
- });
21
- const patterns = [
22
- ...MATRIX_SYNTAX.map(([, pattern]) => pattern),
23
- "(",
24
- "(?i)a",
25
- "a++",
26
- "(?P<name>a)",
27
- ];
28
- let accepted = 0;
29
- let rejected = 0;
30
- for (const pattern of patterns) {
31
- for (const ignoreCase of [undefined, false, true]) {
32
- let field: "pattern[0]" | undefined;
33
- try {
34
- new RegExp(pattern, GREP_FLAGS);
35
- } catch (error) {
36
- if (!(error instanceof SyntaxError)) throw error;
37
- field = "pattern[0]";
38
- }
39
- const before = discoveries;
40
- const params = {
41
- pattern,
42
- literal: false,
43
- ...(ignoreCase === undefined ? {} : { ignoreCase }),
44
- };
45
- const result = callTool(tool, params);
46
- const context = JSON.stringify(params);
47
- if (field) {
48
- rejected++;
49
- await assert.rejects(result, rejectsArgument(field), context);
50
- assert.equal(discoveries, before, context);
51
- } else {
52
- accepted++;
53
- const value = await result;
54
- assert.equal(value.isError, undefined, context);
55
- assert.equal(reportOf(value, "grep")?.matches, 0, context);
56
- assert.equal(discoveries, before + 1, context);
57
- }
58
- }
59
- }
60
- t.diagnostic(
61
- JSON.stringify({
62
- syntaxInputs: patterns.length,
63
- accepted,
64
- rejected,
65
- }),
66
- );
67
- assert.ok(accepted > 0 && rejected > 0);
68
- });