@criogaid/pi-hashline-edit 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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +279 -0
  3. package/package.json +50 -0
  4. package/src/core/apply.test.ts +327 -0
  5. package/src/core/apply.ts +264 -0
  6. package/src/core/hash.test.ts +54 -0
  7. package/src/core/hash.ts +68 -0
  8. package/src/core/index.ts +13 -0
  9. package/src/core/lines.test.ts +33 -0
  10. package/src/core/lines.ts +52 -0
  11. package/src/core/types.ts +87 -0
  12. package/src/index.ts +44 -0
  13. package/src/integration/grep-rg.test.ts +390 -0
  14. package/src/pi/action-fusion.test.ts +244 -0
  15. package/src/pi/action-fusion.ts +284 -0
  16. package/src/pi/config.test.ts +22 -0
  17. package/src/pi/config.ts +56 -0
  18. package/src/pi/edit-tool.ts +367 -0
  19. package/src/pi/execute.test.ts +521 -0
  20. package/src/pi/failure-context.test.ts +88 -0
  21. package/src/pi/failure-context.ts +98 -0
  22. package/src/pi/failure-path.test.ts +60 -0
  23. package/src/pi/file-commit.test.ts +200 -0
  24. package/src/pi/file-commit.ts +185 -0
  25. package/src/pi/fusion-card.test.ts +69 -0
  26. package/src/pi/fusion-card.ts +78 -0
  27. package/src/pi/grep-tool.ts +795 -0
  28. package/src/pi/grep.test.ts +515 -0
  29. package/src/pi/pi.test.ts +19 -0
  30. package/src/pi/read-tool.ts +178 -0
  31. package/src/pi/registration.test.ts +41 -0
  32. package/src/pi/render.ts +103 -0
  33. package/src/pi/replace-tool.ts +303 -0
  34. package/src/pi/replace.test.ts +287 -0
  35. package/src/pi/rg-line-filter.test.ts +79 -0
  36. package/src/pi/rg-line-filter.ts +358 -0
  37. package/src/pi/rg-line-ranges.test.ts +30 -0
  38. package/src/pi/rg-line-ranges.ts +106 -0
  39. package/src/pi/state.ts +27 -0
  40. package/src/pi/write-tool.ts +83 -0
  41. package/src/pi/write.test.ts +126 -0
@@ -0,0 +1,515 @@
1
+ /**
2
+ * Deterministic grep override tests. Ripgrep and built-in grep are injected;
3
+ * fixture files live only in a per-test system temporary directory.
4
+ */
5
+ import { test } from "node:test";
6
+ import assert from "node:assert/strict";
7
+ import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
8
+ import { tmpdir } from "node:os";
9
+ import { join } from "node:path";
10
+ import { rgPath } from "@vscode/ripgrep";
11
+ import { computeLineHash } from "../core/hash.ts";
12
+ import { makeGrepOverrideWithBackend, type GrepBackend } from "./grep-tool.ts";
13
+ import { makeEditOverride } from "./edit-tool.ts";
14
+ import type { LinePredicate, SearchModes } from "./rg-line-filter.ts";
15
+ import { getState } from "./state.ts";
16
+
17
+ type FakeOptions = {
18
+ lines?: string[];
19
+ code?: number | null;
20
+ stderr?: string;
21
+ validation?: { code: number | null; stderr: string };
22
+ error?: Error;
23
+ onRun?: () => void;
24
+ smartCase?: boolean;
25
+ };
26
+
27
+ async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
28
+ const dir = await mkdtemp(join(tmpdir(), "hl-grep-"));
29
+ try {
30
+ return await fn(dir);
31
+ } finally {
32
+ await rm(dir, { recursive: true, force: true });
33
+ }
34
+ }
35
+
36
+ function rgMatch(filePath: string, lineNumber: number, text: string): string {
37
+ return JSON.stringify({
38
+ type: "match",
39
+ data: { path: { text: filePath }, line_number: lineNumber, lines: { text } },
40
+ });
41
+ }
42
+
43
+ function fakeSmartCase(patterns: readonly string[]): boolean {
44
+ return patterns.every((pattern) => {
45
+ const syntaxStripped = pattern
46
+ .replace(/\\[pP]\{[^}]*\}/g, "")
47
+ .replace(/\\[A-Z]/g, "")
48
+ .replace(/\(\?P<[^>]*>/g, "");
49
+ return syntaxStripped === syntaxStripped.toLowerCase();
50
+ });
51
+ }
52
+
53
+ function fakePredicate(patterns: readonly string[], modes: SearchModes, word: boolean): LinePredicate {
54
+ const matchers = patterns.map((pattern) => {
55
+ let source = modes.literal ? pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") : pattern;
56
+ if (word) source = `\\b(?:${source})\\b`;
57
+ return new RegExp(source, modes.ignoreCase ? "iu" : "u");
58
+ });
59
+ return async (lines) => lines.map((line) => {
60
+ const text = line.toString("utf8").replace(/\r?\n$/, "");
61
+ return matchers.some((matcher) => matcher.test(text));
62
+ });
63
+ }
64
+
65
+ function fakeBackend(options: FakeOptions = {}) {
66
+ const calls: { path: string; args: string[] }[] = [];
67
+ const modeCalls: { patterns: readonly string[]; literal: boolean; explicit: boolean | undefined }[] = [];
68
+ const backend: GrepBackend = {
69
+ async runRg(path, args, _signal, onLine) {
70
+ calls.push({ path, args });
71
+ options.onRun?.();
72
+ if (options.error) throw options.error;
73
+ if (args.includes("--quiet")) {
74
+ return { code: 1, stderr: "", ...options.validation, stopped: false };
75
+ }
76
+ for (const line of options.lines ?? []) {
77
+ if (!await onLine(line)) return { code: null, stderr: options.stderr ?? "", stopped: true };
78
+ }
79
+ return {
80
+ code: options.code === undefined ? 0 : options.code,
81
+ stderr: options.stderr ?? "",
82
+ stopped: false,
83
+ };
84
+ },
85
+ async runRgPaths() {
86
+ return { code: 1, stderr: "", stopped: false };
87
+ },
88
+ async resolveIgnoreCase(_path, patterns, modes, explicit) {
89
+ modeCalls.push({ patterns, literal: modes.literal, explicit });
90
+ return explicit ?? options.smartCase ?? fakeSmartCase(patterns);
91
+ },
92
+ async validatePatterns() {},
93
+ createLinePredicate(_path, patterns, modes, word) {
94
+ return fakePredicate(patterns, modes, word);
95
+ },
96
+ };
97
+ return { backend, calls, modeCalls };
98
+ }
99
+
100
+ const text = (result: any): string => result.content[0].text;
101
+ const call = (tool: any, params: any, signal?: AbortSignal) =>
102
+ tool.execute("0", params, signal, undefined);
103
+
104
+ async function withEnabled<T>(enabled: boolean, fn: () => Promise<T>): Promise<T> {
105
+ const state = getState();
106
+ const previous = state.config.enabled;
107
+ state.config.enabled = enabled;
108
+ try {
109
+ return await fn();
110
+ } finally {
111
+ state.config.enabled = previous;
112
+ }
113
+ }
114
+
115
+ test("formats parsed rg matches with full-line hash anchors", async () => {
116
+ await withDir(async (dir) =>
117
+ withEnabled(true, async () => {
118
+ const a = join(dir, "a.ts");
119
+ const b = join(dir, "b.ts");
120
+ await writeFile(a, "alpha beta\ngamma\nalpha only\n");
121
+ await writeFile(b, "alpha here\n");
122
+ const fake = fakeBackend({
123
+ lines: [
124
+ "not json",
125
+ JSON.stringify({ type: "begin" }),
126
+ rgMatch(a, 1, "alpha beta\n"),
127
+ rgMatch(a, 3, "alpha only\n"),
128
+ rgMatch(b, 1, "alpha here\n"),
129
+ ],
130
+ });
131
+
132
+ const tool = makeGrepOverrideWithBackend(dir, fake.backend);
133
+ assert.deepEqual(tool.parameters.required, ["pattern"]);
134
+ for (const option of ["noIgnore", "follow", "pcre2", "multiline"] as const) {
135
+ assert.equal(tool.parameters.properties[option].type, "boolean");
136
+ }
137
+ const result = await call(tool, {
138
+ pattern: "alpha",
139
+ });
140
+ const output = text(result);
141
+ assert.match(output, /a\.ts · 2 matches/);
142
+ assert.match(output, /b\.ts · 1 match/);
143
+ assert.match(output, new RegExp(`1#${computeLineHash(1, "alpha beta")}│alpha beta`));
144
+ assert.match(output, /3#[0-9A-Z]+│alpha only/);
145
+ assert.deepEqual(fake.calls[0], {
146
+ path: rgPath,
147
+ args: ["--no-config", "--color=never", "--crlf", "--engine=default", "--no-multiline", "--ignore-case", "--fixed-strings", "--json", "--line-number", "--hidden", "-e", "alpha", "--", dir],
148
+ });
149
+ }),
150
+ );
151
+ });
152
+
153
+ test("grep in a subdirectory returns a path that edits the matching file", async () => {
154
+ await withDir(async (dir) =>
155
+ withEnabled(true, async () => {
156
+ await mkdir(join(dir, "src"));
157
+ const original = "export const status = 1;\n";
158
+ const rootFile = join(dir, "status.ts");
159
+ const matchedFile = join(dir, "src", "status.ts");
160
+ await writeFile(rootFile, original);
161
+ await writeFile(matchedFile, original);
162
+ const fake = fakeBackend({ lines: [rgMatch(matchedFile, 1, original)] });
163
+ const result = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
164
+ pattern: "status",
165
+ path: "src",
166
+ });
167
+ const output = text(result);
168
+ const displayPath = output.split(" · ")[0];
169
+ const edit: any = makeEditOverride(dir);
170
+ await call(edit, {
171
+ path: displayPath,
172
+ edits: [
173
+ {
174
+ op: "replace",
175
+ anchor: `1#${computeLineHash(1, original.trimEnd())}`,
176
+ body: ["export const status = 2;"],
177
+ },
178
+ ],
179
+ });
180
+ assert.equal(await readFile(matchedFile, "utf-8"), "export const status = 2;\n");
181
+ assert.equal(await readFile(rootFile, "utf-8"), original);
182
+ }),
183
+ );
184
+ });
185
+
186
+ test("applies all, exclude, context, and CRLF filtering after rg output", async () => {
187
+ await withDir(async (dir) =>
188
+ withEnabled(true, async () => {
189
+ const file = join(dir, "a.ts");
190
+ await writeFile(
191
+ file,
192
+ "outside-before\r\nalpha beta drop\r\nbefore survivor\r\nalpha beta\r\nafter survivor\r\nalpha only\r\noutside-after\r\n",
193
+ );
194
+ const fake = fakeBackend({
195
+ lines: [
196
+ rgMatch(file, 2, "alpha beta drop\r\n"),
197
+ rgMatch(file, 4, "alpha beta\r\n"),
198
+ rgMatch(file, 6, "alpha only\r\n"),
199
+ ],
200
+ });
201
+
202
+ const tool = makeGrepOverrideWithBackend(dir, fake.backend);
203
+ const contextSchema: any = tool.parameters.properties.context;
204
+ assert.equal(contextSchema.type, "integer");
205
+ assert.equal(contextSchema.minimum, 0);
206
+ assert.equal(contextSchema.maximum, 20);
207
+ const result = await call(tool, {
208
+ pattern: ["alpha", "beta$"],
209
+ matchMode: "all",
210
+ excludePattern: "drop",
211
+ context: 1.9,
212
+ });
213
+ assert.equal(
214
+ text(result),
215
+ [
216
+ "a.ts · 1 match",
217
+ `3#${computeLineHash(3, "before survivor")}│before survivor`,
218
+ `4#${computeLineHash(4, "alpha beta")}│alpha beta`,
219
+ `5#${computeLineHash(5, "after survivor")}│after survivor`,
220
+ ].join("\n"),
221
+ );
222
+ }),
223
+ );
224
+ });
225
+
226
+ test("passes output flags and formats files and counts", async () => {
227
+ await withDir(async (dir) =>
228
+ withEnabled(true, async () => {
229
+ const a = join(dir, "a.ts");
230
+ const b = join(dir, "b.ts");
231
+ await writeFile(a, "Foo a.b\n");
232
+ await writeFile(b, "foo a.b\n");
233
+ const fake = fakeBackend({ lines: [rgMatch(a, 1, "Foo a.b\n"), rgMatch(b, 1, "foo a.b\n")] });
234
+
235
+ const files = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
236
+ pattern: ["Foo", "a.b"],
237
+ path: ["a.ts", "b.ts"],
238
+ glob: ["*.ts", "!**/*.test.ts"],
239
+ ignoreCase: true,
240
+ literal: true,
241
+ wordMatch: true,
242
+ outputMode: "files",
243
+ });
244
+ assert.equal(text(files), "a.ts\nb.ts");
245
+ assert.deepEqual(fake.calls[0].args, [
246
+ "--no-config",
247
+ "--color=never",
248
+ "--crlf",
249
+ "--engine=default",
250
+ "--no-multiline",
251
+ "--ignore-case",
252
+ "--fixed-strings",
253
+ "--word-regexp",
254
+ "--json",
255
+ "--line-number",
256
+ "--hidden",
257
+ "--glob",
258
+ "*.ts",
259
+ "--glob",
260
+ "!**/*.test.ts",
261
+ "-e",
262
+ "Foo",
263
+ "-e",
264
+ "a.b",
265
+ "--",
266
+ a,
267
+ b,
268
+ ]);
269
+
270
+ const count = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
271
+ pattern: "foo",
272
+ outputMode: "count",
273
+ });
274
+ assert.equal(text(count), "a.ts: 1\nb.ts: 1\nTotal: 2 matches in 2 files");
275
+ }),
276
+ );
277
+ });
278
+
279
+ test("counts only surviving matches toward the limit and stops the fake runner", async () => {
280
+ await withDir(async (dir) =>
281
+ withEnabled(true, async () => {
282
+ const file = join(dir, "a.ts");
283
+ await writeFile(file, "alpha beta\nalpha only\nalpha later\n");
284
+ const fake = fakeBackend({
285
+ lines: [
286
+ rgMatch(file, 1, "alpha beta\n"),
287
+ rgMatch(file, 2, "alpha only\n"),
288
+ rgMatch(file, 3, "alpha later\n"),
289
+ ],
290
+ });
291
+
292
+ const result = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
293
+ pattern: "alpha",
294
+ excludePattern: "beta",
295
+ limit: 1,
296
+ });
297
+ assert.match(text(result), /2#[0-9A-Z]+│alpha only/);
298
+ assert.match(
299
+ text(result),
300
+ /\[1 matches limit reached\. Use limit=2 for more, or refine pattern\]/,
301
+ );
302
+ }),
303
+ );
304
+ });
305
+
306
+ test("auto-detects modes with rg validation while preserving explicit overrides", async () => {
307
+ await withDir(async (dir) => {
308
+ const valid = fakeBackend();
309
+ const invalid = fakeBackend({ validation: { code: 2, stderr: "regex parse error:\nerror: unclosed group" } });
310
+ const tool = makeGrepOverrideWithBackend(dir, valid.backend);
311
+ const fallback = makeGrepOverrideWithBackend(dir, invalid.backend);
312
+
313
+ const result = await call(fallback, { pattern: "queueTool(" });
314
+ assert.match(text(result), /Invalid regex; searched all patterns as literal text/);
315
+ await call(fallback, { pattern: ["plain", "broken("] });
316
+ await call(tool, { pattern: "value.*" });
317
+ await call(tool, { pattern: "plain", literal: false });
318
+ await call(tool, { pattern: "value.*", literal: true });
319
+
320
+ assert.deepEqual(
321
+ [...invalid.calls, ...valid.calls].filter(({ args }) => !args.includes("--quiet"))
322
+ .map(({ args }) => args.includes("--fixed-strings")),
323
+ [true, true, false, false, true],
324
+ );
325
+ assert.equal(valid.calls.filter(({ args }) => args.includes("--quiet")).length, 2);
326
+ assert.deepEqual(invalid.calls[0].args, [
327
+ "--no-config", "--color=never", "--crlf", "--engine=default", "--no-multiline",
328
+ "--quiet", "-e", "queueTool(", "--", "-",
329
+ ]);
330
+
331
+ const failed = fakeBackend({ validation: { code: 2, stderr: "Permission denied" } });
332
+ await assert.rejects(
333
+ call(makeGrepOverrideWithBackend(dir, failed.backend), { pattern: "value.*" }),
334
+ /Permission denied/,
335
+ );
336
+ assert.equal(failed.calls.length, 1);
337
+ });
338
+ });
339
+
340
+ test("uses smart-case by default and preserves explicit case overrides", async () => {
341
+ await withDir(async (dir) => {
342
+ const target = join(dir, "case.ts");
343
+ await writeFile(target, "FOO alpha\n");
344
+
345
+ const lower = fakeBackend({ lines: [rgMatch(target, 1, "FOO alpha\n")] });
346
+ const lowerResult = await call(makeGrepOverrideWithBackend(dir, lower.backend), {
347
+ pattern: ["foo", "alpha"],
348
+ matchMode: "all",
349
+ });
350
+ assert.match(text(lowerResult), /FOO alpha/);
351
+
352
+ const mixed = fakeBackend();
353
+ const mixedResult = await call(makeGrepOverrideWithBackend(dir, mixed.backend), {
354
+ pattern: ["Foo", "alpha"],
355
+ matchMode: "all",
356
+ });
357
+ assert.equal(text(mixedResult), "No matches found");
358
+
359
+ const flags = fakeBackend();
360
+ const tool = makeGrepOverrideWithBackend(dir, flags.backend);
361
+ await call(tool, { pattern: "lower" });
362
+ await call(tool, { pattern: "Upper" });
363
+ await call(tool, { pattern: "lower", ignoreCase: true });
364
+ await call(tool, { pattern: "lower", ignoreCase: false });
365
+ await call(tool, { pattern: "foo\\S*" });
366
+ await call(tool, { pattern: "foo\\S*", ignoreCase: true });
367
+ assert.deepEqual(
368
+ flags.calls.filter(({ args }) => !args.includes("--quiet"))
369
+ .map(({ args }) => [args.includes("--ignore-case"), args.includes("--case-sensitive")]),
370
+ [[true, false], [false, true], [true, false], [false, true], [true, false], [true, false]],
371
+ );
372
+ });
373
+ });
374
+
375
+ test("bounds rg predicate fanout for matchMode all", async () => {
376
+ await withDir(async (dir) => {
377
+ const fake = fakeBackend();
378
+ const tool = makeGrepOverrideWithBackend(dir, fake.backend);
379
+ await assert.rejects(
380
+ call(tool, {
381
+ pattern: Array.from({ length: 17 }, (_, index) => `pattern${index}`),
382
+ matchMode: "all",
383
+ }),
384
+ /supports at most 16 patterns/,
385
+ );
386
+ assert.equal(fake.calls.length, 0);
387
+ });
388
+ });
389
+
390
+ test("rejects empty and wildcard-only regexes without blocking literal or empty-line searches", async () => {
391
+ await withDir(async (dir) => {
392
+ const fake = fakeBackend();
393
+ const tool = makeGrepOverrideWithBackend(dir, fake.backend);
394
+
395
+ for (const pattern of ["", " ", []]) {
396
+ await assert.rejects(call(tool, { pattern }), /pattern (?:is required|must not be empty)/);
397
+ }
398
+ for (const pattern of [".*", "^.+$", ".?", "*"]) {
399
+ await assert.rejects(call(tool, { pattern }), /is wildcard-only/);
400
+ }
401
+ assert.equal(fake.calls.length, 0);
402
+
403
+ assert.equal(text(await call(tool, { pattern: ".*", literal: true })), "No matches found");
404
+ assert.equal(text(await call(tool, { pattern: "^$" })), "No matches found");
405
+ assert.equal(fake.calls.filter(({ args }) => !args.includes("--quiet")).length, 2);
406
+ assert.equal(fake.calls.filter(({ args }) => args.includes("--quiet")).length, 1);
407
+ });
408
+ });
409
+
410
+ test("reports empty output and ripgrep execution failures", async () => {
411
+ await withDir(async (dir) =>
412
+ withEnabled(true, async () => {
413
+ const empty = fakeBackend({ code: 1 });
414
+ assert.equal(
415
+ text(await call(makeGrepOverrideWithBackend(dir, empty.backend), { pattern: "missing" })),
416
+ "No matches found",
417
+ );
418
+
419
+ const failed = fakeBackend({ code: 2, stderr: "bad regex" });
420
+ await assert.rejects(
421
+ call(makeGrepOverrideWithBackend(dir, failed.backend), { pattern: "[", literal: false }),
422
+ /bad regex/,
423
+ );
424
+
425
+ const rejected = fakeBackend({ error: new Error("spawn failed") });
426
+ await assert.rejects(
427
+ call(makeGrepOverrideWithBackend(dir, rejected.backend), { pattern: "x" }),
428
+ /spawn failed/,
429
+ );
430
+ }),
431
+ );
432
+ });
433
+
434
+
435
+ test("rejects calls aborted before or during rg execution", async () => {
436
+ await withDir(async (dir) => {
437
+ const alreadyAborted = fakeBackend();
438
+ const first = new AbortController();
439
+ first.abort();
440
+ await assert.rejects(
441
+ call(
442
+ makeGrepOverrideWithBackend(dir, alreadyAborted.backend),
443
+ { pattern: ["x", "y"] },
444
+ first.signal,
445
+ ),
446
+ /Operation aborted/,
447
+ );
448
+
449
+ const controller = new AbortController();
450
+ const interrupted = fakeBackend({ onRun: () => controller.abort() });
451
+ await assert.rejects(
452
+ call(
453
+ makeGrepOverrideWithBackend(dir, interrupted.backend),
454
+ { pattern: ["x", "y"], matchMode: "all" },
455
+ controller.signal,
456
+ ),
457
+ /Operation aborted/,
458
+ );
459
+ });
460
+ });
461
+
462
+
463
+ test("AND searches gate on the first pattern and amortize filtering across bounded batches", async () => {
464
+ await withDir(async (dir) => {
465
+ const file = join(dir, "fixture.ts");
466
+ const fake = fakeBackend({
467
+ lines: Array.from({ length: 4097 }, (_, index) => rgMatch(file, index + 1, "foo bar\n")),
468
+ });
469
+ const batches: number[][] = [];
470
+ const filteredPatterns: string[][] = [];
471
+ fake.backend.createLinePredicate = (_path, patterns) => {
472
+ filteredPatterns.push([...patterns]);
473
+ const sizes: number[] = [];
474
+ batches.push(sizes);
475
+ return async (candidates) => {
476
+ sizes.push(candidates.length);
477
+ return candidates.map(() => patterns[0] === "bar");
478
+ };
479
+ };
480
+
481
+ const result = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
482
+ pattern: ["foo", "bar"],
483
+ matchMode: "all",
484
+ excludePattern: "skip",
485
+ literal: true,
486
+ ignoreCase: false,
487
+ outputMode: "count",
488
+ limit: 5000,
489
+ });
490
+ assert.match(text(result), /fixture\.ts: 4097/);
491
+ assert.deepEqual(batches, [[4096, 1], [4096, 1]]);
492
+ assert.deepEqual(filteredPatterns, [["bar"], ["skip"]]);
493
+ const args = fake.calls[0].args;
494
+ assert.deepEqual(args.flatMap((arg, index) => args[index - 1] === "-e" ? [arg] : []), ["foo"]);
495
+ });
496
+ });
497
+
498
+ test("large candidate lines flush before the batch line-count limit", async () => {
499
+ await withDir(async (dir) => {
500
+ const line = `foo${"x".repeat(600_000)}\n`;
501
+ const fake = fakeBackend({
502
+ lines: Array.from({ length: 3 }, (_, index) => rgMatch(join(dir, "large.ts"), index + 1, line)),
503
+ });
504
+ const sizes: number[] = [];
505
+ fake.backend.createLinePredicate = () => async (candidates) => {
506
+ sizes.push(candidates.length);
507
+ return candidates.map(() => true);
508
+ };
509
+ const result = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
510
+ pattern: "foo", excludePattern: "foo", literal: true, ignoreCase: false,
511
+ });
512
+ assert.equal(text(result), "No matches found");
513
+ assert.deepEqual(sizes, [2, 1]);
514
+ });
515
+ });
@@ -0,0 +1,19 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { canonicalPath } from "./read-tool.ts";
4
+ import { homedir } from "node:os";
5
+ import { join, resolve } from "node:path";
6
+
7
+ test("canonicalPath resolves relative and absolute", () => {
8
+ const cwd = resolve("/cwd");
9
+ const absolute = resolve("/abs/x.ts");
10
+ assert.equal(canonicalPath(cwd, "foo.ts"), join(cwd, "foo.ts"));
11
+ assert.equal(canonicalPath(cwd, "./foo.ts"), join(cwd, "foo.ts"));
12
+ assert.equal(canonicalPath(cwd, absolute), absolute);
13
+ });
14
+
15
+ test("canonicalPath expands ~ to home directory", () => {
16
+ const home = homedir();
17
+ assert.equal(canonicalPath("/cwd", "~"), home);
18
+ assert.equal(canonicalPath("/cwd", "~/foo.ts"), join(home, "foo.ts"));
19
+ });