@criogaid/pi-hashline-edit 0.6.1 → 0.8.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.
- package/README.md +78 -369
- package/README.zh-CN.md +162 -0
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +11 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +328 -161
- package/src/core/apply.ts +129 -95
- package/src/core/case-fold.test.ts +44 -0
- package/src/core/case-fold.ts +77 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +184 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +32 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +51 -0
- package/src/core/replace-schema.ts +41 -0
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +40 -56
- package/src/core/search-analysis.test.ts +208 -0
- package/src/core/search-analysis.ts +379 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +20 -17
- package/src/index.ts +24 -16
- package/src/integration/config-registration.test.ts +42 -28
- package/src/integration/fusion-dispatch.test.ts +62 -127
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +273 -0
- package/src/integration/grep-js-completeness.test.ts +218 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +133 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +407 -0
- package/src/integration/grep-rg.test.ts +294 -436
- package/src/integration/grep-scan-budget.test.ts +111 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +56 -1
- package/src/integration/model-budget-lifecycle.test.ts +137 -0
- package/src/integration/mutation-file-types.test.ts +12 -26
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +97 -0
- package/src/integration/publication-read-changes.test.ts +376 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +36 -0
- package/src/integration/replace-worker-boundary.test.ts +117 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +54 -0
- package/src/pi/action-fusion.test.ts +256 -251
- package/src/pi/action-fusion.ts +121 -332
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +16 -3
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +46 -0
- package/src/pi/argument-error.ts +76 -0
- package/src/pi/argument-format.test.ts +197 -0
- package/src/pi/argument-validation.test.ts +780 -49
- package/src/pi/argument-validation.ts +55 -58
- package/src/pi/budgets.ts +48 -21
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +29 -15
- package/src/pi/config.ts +27 -13
- package/src/pi/diagnostic-buffer.test.ts +51 -16
- package/src/pi/diagnostic-buffer.ts +13 -16
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +103 -99
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +407 -429
- package/src/pi/extension-loading.test.ts +7 -27
- package/src/pi/extension-loading.testing.ts +2 -14
- package/src/pi/failure-context.test.ts +265 -88
- package/src/pi/failure-context.ts +165 -181
- package/src/pi/failure-path.test.ts +179 -117
- package/src/pi/file-commit.test.ts +90 -73
- package/src/pi/file-commit.ts +216 -178
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +233 -51
- package/src/pi/fusion-card.ts +246 -142
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +191 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +187 -151
- package/src/pi/grep-render.ts +31 -25
- package/src/pi/grep-scope.ts +62 -54
- package/src/pi/grep-search.ts +327 -241
- package/src/pi/grep-tool.ts +126 -147
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +813 -713
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +6 -7
- package/src/pi/mutation-result.test.ts +45 -91
- package/src/pi/mutation-result.ts +86 -114
- package/src/pi/mutation-runner.ts +81 -81
- package/src/pi/output-budgets.test.ts +175 -0
- package/src/pi/path.ts +10 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +108 -142
- package/src/pi/read.test.ts +155 -96
- package/src/pi/regex-worker-contract.ts +55 -0
- package/src/pi/regex-worker.ts +95 -0
- package/src/pi/registration.test.ts +148 -124
- package/src/pi/render.test.ts +361 -84
- package/src/pi/render.ts +150 -32
- package/src/pi/replace-regex.ts +13 -48
- package/src/pi/replace-tool.ts +52 -158
- package/src/pi/replace-worker-contract.ts +10 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +191 -340
- package/src/pi/report-cause.ts +103 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +524 -0
- package/src/pi/report.test.ts +343 -0
- package/src/pi/report.ts +285 -0
- package/src/pi/result-safety.test.ts +216 -481
- package/src/pi/rg-process.test.ts +101 -76
- package/src/pi/rg-process.ts +130 -177
- package/src/pi/rg-regex.test.ts +67 -0
- package/src/pi/rg-regex.ts +715 -0
- package/src/pi/text-stream.test.ts +97 -69
- package/src/pi/text-stream.ts +97 -76
- package/src/pi/tool-call.testing.ts +20 -46
- package/src/pi/write-tool.ts +50 -41
- package/src/pi/write.test.ts +71 -95
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +324 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/integration/forget-grep.test.ts +0 -98
- package/src/pi/forget-tool.ts +0 -268
- package/src/pi/forget.test.ts +0 -343
- package/src/pi/forget.testing.ts +0 -273
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { DEFAULT_CONFIG } from "../pi/config.ts";
|
|
7
|
+
import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
|
|
8
|
+
import { reportOf } from "../pi/report.ts";
|
|
9
|
+
import { grepCounts } from "../pi/grep-output.testing.ts";
|
|
10
|
+
import { callTool } from "../pi/tool-call.testing.ts";
|
|
11
|
+
import { rejectsArgument } from "../pi/argument-error.testing.ts";
|
|
12
|
+
|
|
13
|
+
const cases = [
|
|
14
|
+
{
|
|
15
|
+
name: "u counts Unicode code points",
|
|
16
|
+
pattern: "^.$",
|
|
17
|
+
flags: "mu",
|
|
18
|
+
source: "😀\na\nab\n",
|
|
19
|
+
lines: [1, 2],
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
name: "v counts Unicode code points",
|
|
23
|
+
pattern: "^.$",
|
|
24
|
+
flags: "mv",
|
|
25
|
+
source: "😀\na\nab\n",
|
|
26
|
+
lines: [1, 2],
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: "Unicode property escape",
|
|
30
|
+
pattern: "^\\p{Letter}+$",
|
|
31
|
+
flags: "mu",
|
|
32
|
+
source: "é\nabc\n12\n",
|
|
33
|
+
lines: [1, 2],
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "Unicode set intersection",
|
|
37
|
+
pattern: "^[\\p{ASCII}&&\\p{Letter}]+$",
|
|
38
|
+
flags: "mv",
|
|
39
|
+
source: "é\nabc\n12\n",
|
|
40
|
+
lines: [2],
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "Unicode set subtraction",
|
|
44
|
+
pattern: "^[\\p{Letter}--[a-z]]+$",
|
|
45
|
+
flags: "mv",
|
|
46
|
+
source: "é\nabc\nΩ\n",
|
|
47
|
+
lines: [1, 3],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: "Unicode string set",
|
|
51
|
+
pattern: "^[\\q{ab|cd}]$",
|
|
52
|
+
flags: "mv",
|
|
53
|
+
source: "ab\ncd\na\n",
|
|
54
|
+
lines: [1, 2],
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: "Unicode property of strings",
|
|
58
|
+
pattern: "^\\p{RGI_Emoji}$",
|
|
59
|
+
flags: "mv",
|
|
60
|
+
source: "👨👩👧👦\n😀\nx\n",
|
|
61
|
+
lines: [1, 2],
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: "Unicode case folding includes Kelvin",
|
|
65
|
+
pattern: "^k$",
|
|
66
|
+
flags: "miu",
|
|
67
|
+
source: "k\nK\nK\n",
|
|
68
|
+
lines: [1, 2, 3],
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "non-Unicode case folding excludes Kelvin",
|
|
72
|
+
pattern: "^k$",
|
|
73
|
+
flags: "mi",
|
|
74
|
+
source: "k\nK\nK\n",
|
|
75
|
+
lines: [1, 2],
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: "Unicode caseless word set",
|
|
79
|
+
pattern: "^\\w$",
|
|
80
|
+
flags: "miu",
|
|
81
|
+
source: "ſ\nK\nk\né\n",
|
|
82
|
+
lines: [1, 2, 3],
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: "dotAll crosses physical lines when enabled",
|
|
86
|
+
pattern: "^a.b$",
|
|
87
|
+
flags: "s",
|
|
88
|
+
source: "a\nb",
|
|
89
|
+
multiline: true,
|
|
90
|
+
lines: [1, 2],
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: "dotAll does not override line mode",
|
|
94
|
+
pattern: "^a.b$",
|
|
95
|
+
flags: "s",
|
|
96
|
+
source: "a\nb",
|
|
97
|
+
lines: [],
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: "explicit flags can disable m",
|
|
101
|
+
pattern: "^a$",
|
|
102
|
+
flags: "",
|
|
103
|
+
source: "x\na",
|
|
104
|
+
multiline: true,
|
|
105
|
+
lines: [],
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: "explicit m restores line anchors",
|
|
109
|
+
pattern: "^a$",
|
|
110
|
+
flags: "m",
|
|
111
|
+
source: "x\na",
|
|
112
|
+
multiline: true,
|
|
113
|
+
lines: [2],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: "sticky matching starts at each physical line",
|
|
117
|
+
pattern: "a",
|
|
118
|
+
flags: "y",
|
|
119
|
+
source: "ba\na\n",
|
|
120
|
+
lines: [2],
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: "sticky whole-file matching stops at first failure",
|
|
124
|
+
pattern: "a",
|
|
125
|
+
flags: "y",
|
|
126
|
+
source: "ba\na\n",
|
|
127
|
+
multiline: true,
|
|
128
|
+
lines: [],
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: "global sticky matches remain contiguous",
|
|
132
|
+
pattern: "a",
|
|
133
|
+
flags: "gy",
|
|
134
|
+
source: "aa\nba\n",
|
|
135
|
+
multiline: true,
|
|
136
|
+
lines: [1],
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: "global and indices flags reset between physical lines",
|
|
140
|
+
pattern: "a",
|
|
141
|
+
flags: "gd",
|
|
142
|
+
source: "a\na",
|
|
143
|
+
lines: [1, 2],
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: "Unicode zero-width advancement",
|
|
147
|
+
pattern: "(?=.)",
|
|
148
|
+
flags: "u",
|
|
149
|
+
source: "😀\nx",
|
|
150
|
+
multiline: true,
|
|
151
|
+
lines: [1, 2],
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
name: "Unicode mode cannot split a surrogate pair",
|
|
155
|
+
pattern: "\\uDE00",
|
|
156
|
+
flags: "u",
|
|
157
|
+
source: "😀x",
|
|
158
|
+
lines: [],
|
|
159
|
+
},
|
|
160
|
+
...["s", "m"].flatMap((flags) => [
|
|
161
|
+
{
|
|
162
|
+
name: `lowercase pattern with ${flags} → smart-case ignores case`,
|
|
163
|
+
pattern: "^foo$",
|
|
164
|
+
flags,
|
|
165
|
+
source: "FOO\nfoo\n",
|
|
166
|
+
lines: [1, 2],
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
name: `uppercase pattern with ${flags} → smart-case is case-sensitive`,
|
|
170
|
+
pattern: "^FOO$",
|
|
171
|
+
flags,
|
|
172
|
+
source: "FOO\nfoo\n",
|
|
173
|
+
lines: [1],
|
|
174
|
+
},
|
|
175
|
+
]),
|
|
176
|
+
{
|
|
177
|
+
name: "explicit i overrides uppercase smart-case",
|
|
178
|
+
pattern: "^FOO$",
|
|
179
|
+
flags: "i",
|
|
180
|
+
source: "FOO\nfoo\n",
|
|
181
|
+
lines: [1, 2],
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
name: "ignoreCase false overrides i",
|
|
185
|
+
pattern: "^foo$",
|
|
186
|
+
flags: "mi",
|
|
187
|
+
ignoreCase: false,
|
|
188
|
+
source: "FOO\nfoo\n",
|
|
189
|
+
lines: [2],
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
name: "ignoreCase true enables i",
|
|
193
|
+
pattern: "^FOO$",
|
|
194
|
+
flags: "m",
|
|
195
|
+
ignoreCase: true,
|
|
196
|
+
source: "FOO\nfoo\n",
|
|
197
|
+
lines: [1, 2],
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: "literal Unicode case folding",
|
|
201
|
+
pattern: "k",
|
|
202
|
+
literal: true,
|
|
203
|
+
flags: "iu",
|
|
204
|
+
source: "k\nK\nK\n",
|
|
205
|
+
lines: [1, 2, 3],
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: "flag lists deduplicate",
|
|
209
|
+
pattern: "^foo$",
|
|
210
|
+
flags: "mmi",
|
|
211
|
+
source: "FOO\nfoo\n",
|
|
212
|
+
lines: [1, 2],
|
|
213
|
+
},
|
|
214
|
+
];
|
|
215
|
+
|
|
216
|
+
test("grep flags: JavaScript modes preserve native matching through the real search pipeline", async (t) => {
|
|
217
|
+
const directory = await mkdtemp(join(tmpdir(), "hl-grep-flags-"));
|
|
218
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
219
|
+
const path = join(directory, "input.txt");
|
|
220
|
+
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
221
|
+
for (const scenario of cases) {
|
|
222
|
+
await t.test(scenario.name, async () => {
|
|
223
|
+
await writeFile(path, scenario.source);
|
|
224
|
+
const result = await callTool(tool, {
|
|
225
|
+
path,
|
|
226
|
+
pattern: scenario.pattern,
|
|
227
|
+
literal: scenario.literal ?? false,
|
|
228
|
+
flags: scenario.flags,
|
|
229
|
+
multiline: scenario.multiline ?? false,
|
|
230
|
+
...(scenario.ignoreCase === undefined ? {} : { ignoreCase: scenario.ignoreCase }),
|
|
231
|
+
});
|
|
232
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
233
|
+
assert.equal(grepCounts(result).matches, scenario.lines.length);
|
|
234
|
+
const body = result.content[0];
|
|
235
|
+
assert.ok(body.type === "text");
|
|
236
|
+
const lines = [...body.text.matchAll(/^(\d+)#[0-9A-Z]+│/gm)].map((match) => Number(match[1]));
|
|
237
|
+
assert.deepEqual(lines, scenario.lines);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("grep flags: invalid Unicode syntax rejects the pattern argument", async () => {
|
|
243
|
+
const tool = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, {});
|
|
244
|
+
await assert.rejects(
|
|
245
|
+
callTool(tool, {
|
|
246
|
+
path: "missing.txt",
|
|
247
|
+
pattern: "\\p{NotAProperty}",
|
|
248
|
+
literal: false,
|
|
249
|
+
flags: "u",
|
|
250
|
+
}),
|
|
251
|
+
rejectsArgument("pattern"),
|
|
252
|
+
);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("grep flags: valid V8 patterns exceeding translator parser depth still execute", async (t) => {
|
|
256
|
+
const directory = await mkdtemp(join(tmpdir(), "hl-grep-parser-depth-"));
|
|
257
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
258
|
+
const path = join(directory, "input.txt");
|
|
259
|
+
await writeFile(path, "A\na\n");
|
|
260
|
+
const pattern = `${"(?:".repeat(3_000)}A${")".repeat(3_000)}`;
|
|
261
|
+
assert.equal(new RegExp(pattern).test("A"), true);
|
|
262
|
+
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
263
|
+
for (const flags of [undefined, "m"]) {
|
|
264
|
+
const result = await callTool(tool, {
|
|
265
|
+
path,
|
|
266
|
+
pattern,
|
|
267
|
+
literal: false,
|
|
268
|
+
...(flags === undefined ? {} : { flags }),
|
|
269
|
+
});
|
|
270
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
271
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { DEFAULT_CONFIG } from "../pi/config.ts";
|
|
7
|
+
import { MAX_MULTILINE_SEARCH_BYTES } from "../pi/grep-search.ts";
|
|
8
|
+
import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
|
|
9
|
+
import { reportOf } from "../pi/report.ts";
|
|
10
|
+
import { grepCounts } from "../pi/grep-output.testing.ts";
|
|
11
|
+
import { COMMON_RG_ARGS, runRgLines, runRgPaths } from "../pi/rg-process.ts";
|
|
12
|
+
import { searchText } from "../core/search.ts";
|
|
13
|
+
import { callTool } from "../pi/tool-call.testing.ts";
|
|
14
|
+
|
|
15
|
+
const query = { pattern: "^..$", literal: false, ignoreCase: false };
|
|
16
|
+
|
|
17
|
+
test("JS grep: rg discovery retains ignore, hidden, glob, and binary rules", async (t) => {
|
|
18
|
+
const directory = await mkdtemp(join(tmpdir(), "hl-grep-js-scope-"));
|
|
19
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
20
|
+
await mkdir(join(directory, "nested"));
|
|
21
|
+
for (const name of [
|
|
22
|
+
"allowed.txt",
|
|
23
|
+
".hidden.txt",
|
|
24
|
+
"ignored.txt",
|
|
25
|
+
"excluded.txt",
|
|
26
|
+
"nested/child.txt",
|
|
27
|
+
])
|
|
28
|
+
await writeFile(join(directory, name), "😀\n");
|
|
29
|
+
await writeFile(join(directory, ".ignore"), "ignored.txt\n");
|
|
30
|
+
await writeFile(join(directory, "binary.txt"), "😀\n\0");
|
|
31
|
+
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
32
|
+
const result = await callTool(tool, {
|
|
33
|
+
...query,
|
|
34
|
+
path: directory,
|
|
35
|
+
glob: "!excluded.txt",
|
|
36
|
+
outputMode: "files",
|
|
37
|
+
});
|
|
38
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
39
|
+
assert.equal(grepCounts(result).files, 3);
|
|
40
|
+
const body = result.content[0];
|
|
41
|
+
assert.ok(body.type === "text");
|
|
42
|
+
for (const name of ["allowed.txt", ".hidden.txt", "child.txt"])
|
|
43
|
+
assert.ok(body.text.includes(name), name);
|
|
44
|
+
assert.doesNotMatch(body.text, /ignored|excluded|binary/);
|
|
45
|
+
const explicit = await callTool(tool, {
|
|
46
|
+
...query,
|
|
47
|
+
path: join(directory, "ignored.txt"),
|
|
48
|
+
glob: "*.txt",
|
|
49
|
+
});
|
|
50
|
+
assert.equal(grepCounts(explicit).matches, 1);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("JS grep: physical rows, context, and limits agree across output modes", async (t) => {
|
|
54
|
+
const directory = await mkdtemp(join(tmpdir(), "hl-grep-js-output-"));
|
|
55
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
56
|
+
const path = join(directory, "input.txt");
|
|
57
|
+
await writeFile(path, "head\r\n😀\r\nab\ntail\n");
|
|
58
|
+
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
59
|
+
for (const outputMode of ["content", "files", "count"] as const) {
|
|
60
|
+
const result = await callTool(tool, { ...query, path, outputMode, limit: 1, context: 1 });
|
|
61
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
62
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
63
|
+
assert.equal(grepCounts(result).files, 1);
|
|
64
|
+
assert.equal(reportOf(result, "grep")?.matchLimit, 1);
|
|
65
|
+
const body = result.content[0];
|
|
66
|
+
assert.ok(body.type === "text");
|
|
67
|
+
if (outputMode === "content") {
|
|
68
|
+
assert.match(body.text, /1#[0-9A-Z]+│head/);
|
|
69
|
+
assert.match(body.text, /2#[0-9A-Z]+│😀/);
|
|
70
|
+
assert.match(body.text, /3#[0-9A-Z]+│ab/);
|
|
71
|
+
assert.doesNotMatch(body.text, /│tail/);
|
|
72
|
+
} else if (outputMode === "count") assert.match(body.text, /input\.txt: 1/);
|
|
73
|
+
else assert.match(body.text, /input\.txt/);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("JS grep: malformed UTF-8 matches the preview text without editable anchors", async (t) => {
|
|
78
|
+
const directory = await mkdtemp(join(tmpdir(), "hl-grep-js-preview-"));
|
|
79
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
80
|
+
const path = join(directory, "invalid.txt");
|
|
81
|
+
await writeFile(path, Buffer.from([0x61, 0xff, 0x62, 0x0a]));
|
|
82
|
+
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
83
|
+
const result = await callTool(tool, { path, pattern: "a.b", literal: false });
|
|
84
|
+
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
85
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
86
|
+
assert.equal(reportOf(result, "grep")?.invalidUtf8?.length, 1);
|
|
87
|
+
const body = result.content[0];
|
|
88
|
+
assert.ok(body.type === "text");
|
|
89
|
+
assert.match(body.text, /1│a�b/);
|
|
90
|
+
assert.doesNotMatch(body.text, /\d+#[0-9A-Z]+/);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("oversized line or multiline input → explicit incomplete-search diagnostics", async (t) => {
|
|
94
|
+
const directory = await mkdtemp(join(tmpdir(), "hl-grep-js-bound-"));
|
|
95
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
96
|
+
const path = join(directory, "large.txt");
|
|
97
|
+
const good = join(directory, "good.txt");
|
|
98
|
+
const oversized = Buffer.alloc(MAX_MULTILINE_SEARCH_BYTES + 1, 0x61);
|
|
99
|
+
await writeFile(good, "a\n");
|
|
100
|
+
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
101
|
+
for (const finalNewline of [false, true]) {
|
|
102
|
+
await writeFile(path, finalNewline ? Buffer.concat([oversized, Buffer.from("\n")]) : oversized);
|
|
103
|
+
for (const multiline of [false, true]) {
|
|
104
|
+
for (const includeGood of [false, true]) {
|
|
105
|
+
await t.test(
|
|
106
|
+
`finalNewline=${finalNewline}, multiline=${multiline}, includeGood=${includeGood}`,
|
|
107
|
+
async () => {
|
|
108
|
+
const result = await callTool(tool, {
|
|
109
|
+
path: includeGood ? [path, good] : path,
|
|
110
|
+
pattern: "a",
|
|
111
|
+
literal: true,
|
|
112
|
+
multiline,
|
|
113
|
+
});
|
|
114
|
+
const diagnostics = reportOf(result, "grep")?.diagnostics ?? "";
|
|
115
|
+
assert.match(diagnostics, /large\.txt/);
|
|
116
|
+
assert.match(diagnostics, multiline ? /MiB/ : /UTF-16 code units/);
|
|
117
|
+
if (includeGood) {
|
|
118
|
+
assert.equal(result.isError, undefined);
|
|
119
|
+
assert.equal(grepCounts(result).matches, 1);
|
|
120
|
+
} else {
|
|
121
|
+
assert.equal(result.isError, true);
|
|
122
|
+
assert.equal(reportOf(result, "grep")?.error, "SEARCH_INCOMPLETE");
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("real rg candidates → include every JS row and preserve confirmed output in both paths", async (t) => {
|
|
132
|
+
const directory = await mkdtemp(join(tmpdir(), "hl-grep-prefilter-"));
|
|
133
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
134
|
+
const fixtures = [
|
|
135
|
+
["color.txt", Buffer.from("COLOR\r\ncolour\r\ncolouur\n")],
|
|
136
|
+
["invalid.txt", Buffer.from([0x61, 0xff, 0x62, 0x0d, 0x0a])],
|
|
137
|
+
["unicode.txt", Buffer.from("K\nk\nſ\ns\n😀\n")],
|
|
138
|
+
["span.txt", Buffer.from("alpha\r\nbeta\n")],
|
|
139
|
+
["miss.txt", Buffer.from("unrelated\n")],
|
|
140
|
+
] as const;
|
|
141
|
+
for (const [name, bytes] of fixtures) await writeFile(join(directory, name), bytes);
|
|
142
|
+
for (const [pattern, flags, multiline, matches] of [
|
|
143
|
+
["colou?r", "mi", false, 2],
|
|
144
|
+
["a\\uFFFDb", "m", false, 1],
|
|
145
|
+
["k", "miu", false, 2],
|
|
146
|
+
["alpha\\nbeta", "m", true, 2],
|
|
147
|
+
] as const) {
|
|
148
|
+
await t.test(`${pattern} /${flags} → identical confirmed results`, async () => {
|
|
149
|
+
const candidates = new Map<string, readonly number[] | undefined>();
|
|
150
|
+
const filtered = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
|
|
151
|
+
runRgPaths(rg, args, signal, onPath) {
|
|
152
|
+
return runRgPaths(rg, args, signal, (path) => {
|
|
153
|
+
candidates.set(path, undefined);
|
|
154
|
+
return onPath(path);
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
runRgLines(rg, args, signal, maxLines, onFile) {
|
|
158
|
+
return runRgLines(rg, args, signal, maxLines, (path, lines) => {
|
|
159
|
+
candidates.set(path, lines);
|
|
160
|
+
return onFile(path, lines);
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
const full = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
|
|
165
|
+
runRgPaths(rg, args, signal, onPath) {
|
|
166
|
+
const scopeStart = args.indexOf("--null");
|
|
167
|
+
assert.ok(scopeStart >= 0);
|
|
168
|
+
return runRgPaths(
|
|
169
|
+
rg,
|
|
170
|
+
[...COMMON_RG_ARGS, "--files", ...args.slice(scopeStart)],
|
|
171
|
+
signal,
|
|
172
|
+
onPath,
|
|
173
|
+
);
|
|
174
|
+
},
|
|
175
|
+
runRgLines(rg, args, signal, _maxLines, onFile) {
|
|
176
|
+
const scopeStart = args.indexOf("--hidden");
|
|
177
|
+
assert.ok(scopeStart >= 0);
|
|
178
|
+
return runRgPaths(
|
|
179
|
+
rg,
|
|
180
|
+
[...COMMON_RG_ARGS, "--files", "--null", ...args.slice(scopeStart)],
|
|
181
|
+
signal,
|
|
182
|
+
(path) => onFile(path, undefined),
|
|
183
|
+
);
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
for (const outputMode of ["content", "files", "count"] as const) {
|
|
187
|
+
const params = { path: directory, pattern, flags, multiline, literal: false, outputMode };
|
|
188
|
+
const actual = await callTool(filtered, params);
|
|
189
|
+
const expected = await callTool(full, params);
|
|
190
|
+
assert.equal(actual.isError, undefined, JSON.stringify(reportOf(actual, "grep")));
|
|
191
|
+
assert.equal(expected.isError, undefined, JSON.stringify(reportOf(expected, "grep")));
|
|
192
|
+
assert.equal(grepCounts(actual).matches, matches);
|
|
193
|
+
assert.equal(grepCounts(expected).matches, matches);
|
|
194
|
+
const actualText = actual.content[0];
|
|
195
|
+
const expectedText = expected.content[0];
|
|
196
|
+
assert.ok(actualText.type === "text" && expectedText.type === "text");
|
|
197
|
+
assert.deepEqual(actualText.text.split("\n").sort(), expectedText.text.split("\n").sort());
|
|
198
|
+
}
|
|
199
|
+
assert.ok(candidates.size > 0 && candidates.size < fixtures.length);
|
|
200
|
+
for (const [name, bytes] of fixtures) {
|
|
201
|
+
const path = join(directory, name);
|
|
202
|
+
const rows = searchText({
|
|
203
|
+
source: bytes.toString("utf8"),
|
|
204
|
+
query: { patterns: [pattern], literal: false, flags, multiline },
|
|
205
|
+
limit: bytes.length,
|
|
206
|
+
}).lines;
|
|
207
|
+
if (!rows.length) continue;
|
|
208
|
+
assert.ok(candidates.has(path), `rg dropped ${name}`);
|
|
209
|
+
const listed = candidates.get(path);
|
|
210
|
+
for (const row of rows)
|
|
211
|
+
assert.ok(
|
|
212
|
+
listed === undefined || listed.includes(row.lineNumber),
|
|
213
|
+
`${name}:${row.lineNumber}`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
});
|