@aria-cli/tools 1.0.9 → 1.0.11

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 (241) hide show
  1. package/package.json +9 -5
  2. package/src/__tests__/web-fetch-download.test.ts +0 -433
  3. package/src/__tests__/web-tools.test.ts +0 -619
  4. package/src/ask-user-interaction.ts +0 -33
  5. package/src/cache/web-cache.ts +0 -110
  6. package/src/definitions/arion.ts +0 -118
  7. package/src/definitions/browser/browser.ts +0 -502
  8. package/src/definitions/browser/index.ts +0 -5
  9. package/src/definitions/browser/pw-downloads.ts +0 -142
  10. package/src/definitions/browser/pw-interactions.ts +0 -282
  11. package/src/definitions/browser/pw-responses.ts +0 -98
  12. package/src/definitions/browser/pw-session.ts +0 -405
  13. package/src/definitions/browser/pw-shared.ts +0 -85
  14. package/src/definitions/browser/pw-snapshot.ts +0 -383
  15. package/src/definitions/browser/pw-state.ts +0 -101
  16. package/src/definitions/browser/types.ts +0 -203
  17. package/src/definitions/code-intelligence.ts +0 -526
  18. package/src/definitions/core.ts +0 -118
  19. package/src/definitions/delegation.ts +0 -567
  20. package/src/definitions/deploy.ts +0 -73
  21. package/src/definitions/filesystem.ts +0 -217
  22. package/src/definitions/frg.ts +0 -67
  23. package/src/definitions/index.ts +0 -28
  24. package/src/definitions/memory.ts +0 -150
  25. package/src/definitions/messaging.ts +0 -734
  26. package/src/definitions/meta.ts +0 -392
  27. package/src/definitions/network.ts +0 -179
  28. package/src/definitions/outlook.ts +0 -318
  29. package/src/definitions/patch/apply-patch.ts +0 -235
  30. package/src/definitions/patch/fuzzy-match.ts +0 -217
  31. package/src/definitions/patch/index.ts +0 -1
  32. package/src/definitions/patch/patch-parser.ts +0 -297
  33. package/src/definitions/patch/sandbox-paths.ts +0 -129
  34. package/src/definitions/process/index.ts +0 -5
  35. package/src/definitions/process/process-registry.ts +0 -303
  36. package/src/definitions/process/process.ts +0 -456
  37. package/src/definitions/process/pty-keys.ts +0 -298
  38. package/src/definitions/process/session-slug.ts +0 -147
  39. package/src/definitions/quip.ts +0 -225
  40. package/src/definitions/search.ts +0 -67
  41. package/src/definitions/session-history.ts +0 -79
  42. package/src/definitions/shell.ts +0 -202
  43. package/src/definitions/slack.ts +0 -211
  44. package/src/definitions/web.ts +0 -119
  45. package/src/executors/apply-patch.ts +0 -1035
  46. package/src/executors/arion.ts +0 -199
  47. package/src/executors/code-intelligence.ts +0 -1179
  48. package/src/executors/deploy.ts +0 -1066
  49. package/src/executors/filesystem.ts +0 -1428
  50. package/src/executors/frg-freshness.ts +0 -743
  51. package/src/executors/frg.ts +0 -394
  52. package/src/executors/index.ts +0 -280
  53. package/src/executors/learning-meta.ts +0 -1367
  54. package/src/executors/lsp-client.ts +0 -355
  55. package/src/executors/memory.ts +0 -978
  56. package/src/executors/meta.ts +0 -293
  57. package/src/executors/process-registry.ts +0 -570
  58. package/src/executors/pty-session-store.ts +0 -43
  59. package/src/executors/pty.ts +0 -342
  60. package/src/executors/restart.ts +0 -133
  61. package/src/executors/search-freshness.ts +0 -249
  62. package/src/executors/search-types.ts +0 -98
  63. package/src/executors/search.ts +0 -89
  64. package/src/executors/self-diagnose.ts +0 -552
  65. package/src/executors/session-history.ts +0 -435
  66. package/src/executors/shell-safety.ts +0 -519
  67. package/src/executors/shell.ts +0 -1243
  68. package/src/executors/utils.ts +0 -40
  69. package/src/executors/web.ts +0 -786
  70. package/src/extraction/content-extraction.ts +0 -281
  71. package/src/extraction/index.ts +0 -5
  72. package/src/headless-control-contract.ts +0 -1149
  73. package/src/index.ts +0 -788
  74. package/src/local-control-http-auth.ts +0 -2
  75. package/src/mcp/client.ts +0 -218
  76. package/src/mcp/connection.ts +0 -568
  77. package/src/mcp/index.ts +0 -11
  78. package/src/mcp/jsonrpc.ts +0 -195
  79. package/src/mcp/types.ts +0 -199
  80. package/src/network-control-adapter.ts +0 -88
  81. package/src/network-runtime/address-types.ts +0 -218
  82. package/src/network-runtime/db-owner-fencing.ts +0 -91
  83. package/src/network-runtime/delivery-receipts.ts +0 -372
  84. package/src/network-runtime/direct-endpoint-authority.ts +0 -35
  85. package/src/network-runtime/index.ts +0 -316
  86. package/src/network-runtime/local-control-contract.ts +0 -784
  87. package/src/network-runtime/node-store-contract.ts +0 -46
  88. package/src/network-runtime/pair-route-contract.ts +0 -97
  89. package/src/network-runtime/peer-capabilities.ts +0 -48
  90. package/src/network-runtime/peer-principal-ref.ts +0 -20
  91. package/src/network-runtime/peer-state-machine.ts +0 -160
  92. package/src/network-runtime/protocol-schemas.ts +0 -265
  93. package/src/network-runtime/runtime-bootstrap-contract.ts +0 -83
  94. package/src/outlook/desktop-session.ts +0 -409
  95. package/src/policy.ts +0 -171
  96. package/src/providers/brave.ts +0 -80
  97. package/src/providers/duckduckgo.ts +0 -199
  98. package/src/providers/exa.ts +0 -85
  99. package/src/providers/firecrawl.ts +0 -77
  100. package/src/providers/index.ts +0 -8
  101. package/src/providers/jina.ts +0 -70
  102. package/src/providers/router.ts +0 -121
  103. package/src/providers/search-provider.ts +0 -74
  104. package/src/providers/tavily.ts +0 -74
  105. package/src/quip/desktop-session.ts +0 -435
  106. package/src/registry/index.ts +0 -1
  107. package/src/registry/registry.ts +0 -905
  108. package/src/runtime-socket-local-control-client.ts +0 -632
  109. package/src/security/dns-normalization.ts +0 -34
  110. package/src/security/dns-pinning.ts +0 -138
  111. package/src/security/external-content.ts +0 -129
  112. package/src/security/ssrf.ts +0 -207
  113. package/src/slack/desktop-session.ts +0 -493
  114. package/src/tool-factory.ts +0 -91
  115. package/src/types.ts +0 -1341
  116. package/src/utils/retry.ts +0 -163
  117. package/src/utils/safe-parse-json.ts +0 -176
  118. package/src/utils/url.ts +0 -20
  119. package/tests/benchmarks/registry.bench.ts +0 -57
  120. package/tests/cache/web-cache.test.ts +0 -147
  121. package/tests/critical-integration.test.ts +0 -1465
  122. package/tests/definitions/apply-patch.test.ts +0 -586
  123. package/tests/definitions/browser.test.ts +0 -495
  124. package/tests/definitions/delegation-pause-resume.test.ts +0 -758
  125. package/tests/definitions/execution.test.ts +0 -671
  126. package/tests/definitions/messaging-inbox-scope.test.ts +0 -229
  127. package/tests/definitions/messaging.test.ts +0 -1468
  128. package/tests/definitions/outlook.test.ts +0 -30
  129. package/tests/definitions/process.test.ts +0 -469
  130. package/tests/definitions/slack.test.ts +0 -28
  131. package/tests/definitions/tool-inventory.test.ts +0 -218
  132. package/tests/e2e/delegation-quest-orchestration.e2e.test.ts +0 -433
  133. package/tests/e2e/memory-tool-discovery-contract.e2e.test.ts +0 -81
  134. package/tests/executors/apply-patch.test.ts +0 -538
  135. package/tests/executors/arion.test.ts +0 -309
  136. package/tests/executors/conversation-primitives.test.ts +0 -250
  137. package/tests/executors/deploy.test.ts +0 -746
  138. package/tests/executors/filesystem-tools.test.ts +0 -357
  139. package/tests/executors/filesystem.test.ts +0 -959
  140. package/tests/executors/frg-freshness.test.ts +0 -136
  141. package/tests/executors/frg-merge.test.ts +0 -70
  142. package/tests/executors/frg-session-content.test.ts +0 -40
  143. package/tests/executors/frg.test.ts +0 -56
  144. package/tests/executors/memory-bugfixes.test.ts +0 -257
  145. package/tests/executors/memory-real-memoria.integration.test.ts +0 -316
  146. package/tests/executors/memory.test.ts +0 -853
  147. package/tests/executors/meta-tools.test.ts +0 -411
  148. package/tests/executors/meta.test.ts +0 -683
  149. package/tests/executors/path-containment.test.ts +0 -51
  150. package/tests/executors/process-registry.test.ts +0 -505
  151. package/tests/executors/pty.test.ts +0 -664
  152. package/tests/executors/quest-security.test.ts +0 -249
  153. package/tests/executors/read-file-media.test.ts +0 -230
  154. package/tests/executors/recall-knowledge-schema.test.ts +0 -209
  155. package/tests/executors/recall-tags.test.ts +0 -278
  156. package/tests/executors/remember-null-safety.contract.test.ts +0 -41
  157. package/tests/executors/restart.test.ts +0 -67
  158. package/tests/executors/search-unified.test.ts +0 -381
  159. package/tests/executors/session-history.test.ts +0 -340
  160. package/tests/executors/session-transcript.test.ts +0 -561
  161. package/tests/executors/shell-abort.test.ts +0 -416
  162. package/tests/executors/shell-env-blocklist.test.ts +0 -648
  163. package/tests/executors/shell-env-process.test.ts +0 -245
  164. package/tests/executors/shell-process-registry.test.ts +0 -334
  165. package/tests/executors/shell-tools.test.ts +0 -393
  166. package/tests/executors/shell.test.ts +0 -690
  167. package/tests/executors/web-abort-vs-timeout.test.ts +0 -213
  168. package/tests/executors/web-integration.test.ts +0 -633
  169. package/tests/executors/web-symlink.test.ts +0 -18
  170. package/tests/executors/web.test.ts +0 -1400
  171. package/tests/executors/write-stdin.test.ts +0 -145
  172. package/tests/extraction/content-extraction.test.ts +0 -153
  173. package/tests/guards/tools-default-test-lane.integration.test.ts +0 -21
  174. package/tests/guards/tools-package-test-commands.e2e.test.ts +0 -43
  175. package/tests/guards/tools-test-lane-manifest.contract.test.ts +0 -76
  176. package/tests/guards/tools-vitest-workspace-alias.contract.test.ts +0 -63
  177. package/tests/helpers/async-waits.ts +0 -53
  178. package/tests/integration/headless-control-contract.integration.test.ts +0 -153
  179. package/tests/integration/memory-tool-schema-parity.integration.test.ts +0 -67
  180. package/tests/integration/meta-tools-round-trip.integration.test.ts +0 -506
  181. package/tests/integration/quest-round-trip.test.ts +0 -303
  182. package/tests/integration/registry-executor-flow.test.ts +0 -85
  183. package/tests/integration.test.ts +0 -177
  184. package/tests/loading-tier.test.ts +0 -126
  185. package/tests/mcp/client-reconnect.test.ts +0 -267
  186. package/tests/mcp/connection.test.ts +0 -846
  187. package/tests/mcp/injectable-logger.test.ts +0 -83
  188. package/tests/mcp/jsonrpc.test.ts +0 -109
  189. package/tests/mcp/lifecycle.test.ts +0 -879
  190. package/tests/network-runtime/address-types.contract.test.ts +0 -143
  191. package/tests/network-runtime/continuity-bind-schema.contract.test.ts +0 -203
  192. package/tests/network-runtime/local-control-contract.test.ts +0 -869
  193. package/tests/network-runtime/local-control-invite-token.contract.test.ts +0 -146
  194. package/tests/network-runtime/node-store-contract.test.ts +0 -11
  195. package/tests/network-runtime/pair-protocol-nodeid.contract.test.ts +0 -15
  196. package/tests/network-runtime/peer-state-machine.contract.test.ts +0 -148
  197. package/tests/network-runtime/protocol-schemas.contract.test.ts +0 -512
  198. package/tests/network-runtime/relay-pending-nodeid.contract.test.ts +0 -62
  199. package/tests/network-runtime/runtime-bootstrap-contract.test.ts +0 -227
  200. package/tests/network-runtime/runtime-socket-local-control-client.test.ts +0 -621
  201. package/tests/network-runtime/wait-for-message-script.test.ts +0 -288
  202. package/tests/parallel.test.ts +0 -71
  203. package/tests/policy.test.ts +0 -184
  204. package/tests/print-default-test-lane.ts +0 -14
  205. package/tests/print-test-lane-manifest.ts +0 -22
  206. package/tests/providers/brave.test.ts +0 -159
  207. package/tests/providers/duckduckgo.test.ts +0 -207
  208. package/tests/providers/exa.test.ts +0 -175
  209. package/tests/providers/firecrawl.test.ts +0 -168
  210. package/tests/providers/jina.test.ts +0 -144
  211. package/tests/providers/router.test.ts +0 -328
  212. package/tests/providers/tavily.test.ts +0 -165
  213. package/tests/registry/discovery.test.ts +0 -154
  214. package/tests/registry/injectable-logger.test.ts +0 -230
  215. package/tests/registry/input-validation.test.ts +0 -361
  216. package/tests/registry/interface-completeness.test.ts +0 -85
  217. package/tests/registry/mcp-integration.test.ts +0 -103
  218. package/tests/registry/mcp-read-only-hint.test.ts +0 -60
  219. package/tests/registry/memoria-discovery.test.ts +0 -390
  220. package/tests/registry/nested-validation.test.ts +0 -283
  221. package/tests/registry/pseudo-tool-filtering.test.ts +0 -258
  222. package/tests/registry/registration-lifecycle.test.ts +0 -133
  223. package/tests/registry-validation.test.ts +0 -424
  224. package/tests/registry.test.ts +0 -460
  225. package/tests/security/dns-pinning.test.ts +0 -162
  226. package/tests/security/external-content.test.ts +0 -144
  227. package/tests/security/ssrf.test.ts +0 -118
  228. package/tests/shell-safety-integration.test.ts +0 -32
  229. package/tests/shell-safety.test.ts +0 -365
  230. package/tests/slack/desktop-session.test.ts +0 -50
  231. package/tests/test-lane-manifest.ts +0 -440
  232. package/tests/test-utils.ts +0 -27
  233. package/tests/tool-factory.test.ts +0 -188
  234. package/tests/utils/retry.test.ts +0 -231
  235. package/tests/utils/url.test.ts +0 -63
  236. package/tsconfig.cjs.json +0 -24
  237. package/tsconfig.json +0 -12
  238. package/vitest.config.ts +0 -55
  239. package/vitest.e2e.config.ts +0 -24
  240. package/vitest.integration.config.ts +0 -24
  241. package/vitest.native.config.ts +0 -24
@@ -1,959 +0,0 @@
1
- /**
2
- * @aria/tools - Filesystem executor tests
3
- */
4
-
5
- import { describe, it, expect, beforeEach, afterEach } from "vitest";
6
- import * as fs from "node:fs/promises";
7
- import * as path from "node:path";
8
- import * as os from "node:os";
9
- import { createHash } from "node:crypto";
10
- import type { ToolContext } from "../../src/types.js";
11
- import {
12
- executeReadFile,
13
- executeWriteFile,
14
- executeEditFile,
15
- executeLs,
16
- executeGlob,
17
- executeGrep,
18
- FILESYSTEM_OUTPUT_LIMITS,
19
- LS_INTERNALS,
20
- } from "../../src/executors/filesystem.js";
21
-
22
- // Helper to create a unique temp directory for each test
23
- const createTempDir = async (): Promise<string> => {
24
- const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "aria-test-"));
25
- return tempDir;
26
- };
27
-
28
- // Helper to clean up temp directory
29
- const cleanupTempDir = async (dir: string): Promise<void> => {
30
- await fs.rm(dir, { recursive: true, force: true });
31
- };
32
-
33
- // Helper to create a context
34
- const createContext = (workingDir: string): ToolContext => ({
35
- workingDir,
36
- env: {},
37
- confirm: async () => true,
38
- });
39
-
40
- describe("Filesystem Executors", () => {
41
- let tempDir: string;
42
- let ctx: ToolContext;
43
-
44
- beforeEach(async () => {
45
- tempDir = await createTempDir();
46
- ctx = createContext(tempDir);
47
- });
48
-
49
- afterEach(async () => {
50
- await cleanupTempDir(tempDir);
51
- });
52
-
53
- describe("executeReadFile", () => {
54
- it("should read a file successfully", async () => {
55
- const filePath = path.join(tempDir, "test.txt");
56
- await fs.writeFile(filePath, "Hello, World!");
57
-
58
- const result = await executeReadFile({ path: filePath }, ctx);
59
-
60
- expect(result.success).toBe(true);
61
- expect(result.data).toBe("Hello, World!");
62
- });
63
-
64
- it("should read a file with relative path", async () => {
65
- const filePath = path.join(tempDir, "test.txt");
66
- await fs.writeFile(filePath, "Relative test");
67
-
68
- const result = await executeReadFile({ path: "test.txt" }, ctx);
69
-
70
- expect(result.success).toBe(true);
71
- expect(result.data).toBe("Relative test");
72
- });
73
-
74
- it("should support offset and limit", async () => {
75
- const filePath = path.join(tempDir, "lines.txt");
76
- await fs.writeFile(filePath, "line1\nline2\nline3\nline4\nline5");
77
-
78
- const result = await executeReadFile({ path: filePath, offset: 1, limit: 2 }, ctx);
79
-
80
- expect(result.success).toBe(true);
81
- expect(result.data).toBe("line2\nline3");
82
- });
83
-
84
- it("should handle file with trailing newline correctly with offset/limit", async () => {
85
- const filePath = path.join(tempDir, "trailing.txt");
86
- // File ends with newline - common for text files
87
- await fs.writeFile(filePath, "line1\nline2\nline3\n");
88
-
89
- const result = await executeReadFile({ path: filePath, offset: 0, limit: 3 }, ctx);
90
-
91
- expect(result.success).toBe(true);
92
- // Should return 3 actual lines, not include empty line from trailing newline
93
- expect(result.data).toBe("line1\nline2\nline3");
94
- });
95
-
96
- it("should handle offset beyond file length", async () => {
97
- const filePath = path.join(tempDir, "short.txt");
98
- await fs.writeFile(filePath, "line1\nline2");
99
-
100
- const result = await executeReadFile({ path: filePath, offset: 10, limit: 5 }, ctx);
101
-
102
- expect(result.success).toBe(true);
103
- expect(result.data).toBe("");
104
- });
105
-
106
- it("should truncate large reads by default and include resume offset guidance", async () => {
107
- const filePath = path.join(tempDir, "large.txt");
108
- const lines = Array.from({ length: 2500 }, (_, i) => `line-${i + 1}`);
109
- await fs.writeFile(filePath, lines.join("\n"));
110
-
111
- const result = await executeReadFile({ path: filePath }, ctx);
112
-
113
- expect(result.success).toBe(true);
114
- const data = result.data as string;
115
- expect(data.split("\n").length).toBe(2000);
116
- expect(result.message).toContain("truncated");
117
- expect(result.message).toContain("offset=2000");
118
- });
119
-
120
- it("should reject invalid offset/limit values", async () => {
121
- const filePath = path.join(tempDir, "invalid.txt");
122
- await fs.writeFile(filePath, "x");
123
-
124
- const badOffset = await executeReadFile({ path: filePath, offset: -1 }, ctx);
125
- expect(badOffset.success).toBe(false);
126
- expect(badOffset.message).toContain("offset");
127
-
128
- const badLimit = await executeReadFile({ path: filePath, limit: 0 }, ctx);
129
- expect(badLimit.success).toBe(false);
130
- expect(badLimit.message).toContain("limit");
131
- });
132
-
133
- it("should return error for non-existent file", async () => {
134
- const result = await executeReadFile({ path: "nonexistent.txt" }, ctx);
135
-
136
- expect(result.success).toBe(false);
137
- expect(result.message).toContain("ENOENT");
138
- });
139
-
140
- it("should return error for directory", async () => {
141
- const result = await executeReadFile({ path: tempDir }, ctx);
142
-
143
- expect(result.success).toBe(false);
144
- expect(result.message).toBeDefined();
145
- });
146
-
147
- it("should reject absolute paths outside the working directory", async () => {
148
- const outsideDir = await createTempDir();
149
- try {
150
- const outsideFile = path.join(outsideDir, "outside.txt");
151
- await fs.writeFile(outsideFile, "outside");
152
-
153
- const result = await executeReadFile({ path: outsideFile }, ctx);
154
- expect(result.success).toBe(false);
155
- expect(result.message).toContain("outside working directory");
156
- } finally {
157
- await cleanupTempDir(outsideDir);
158
- }
159
- });
160
-
161
- it("should reject prefix-collision absolute paths outside the working directory", async () => {
162
- const siblingDir = `${tempDir}-evil`;
163
- await fs.mkdir(siblingDir, { recursive: true });
164
- const siblingFile = path.join(siblingDir, "outside.txt");
165
- await fs.writeFile(siblingFile, "outside");
166
-
167
- const result = await executeReadFile({ path: siblingFile }, ctx);
168
- expect(result.success).toBe(false);
169
- expect(result.message).toContain("outside working directory");
170
- });
171
-
172
- describe("home directory access", () => {
173
- let fakeHome: string;
174
- let originalHome: string | undefined;
175
-
176
- beforeEach(async () => {
177
- fakeHome = await createTempDir();
178
- originalHome = process.env.HOME;
179
- process.env.HOME = fakeHome;
180
- });
181
-
182
- afterEach(async () => {
183
- process.env.HOME = originalHome;
184
- await cleanupTempDir(fakeHome);
185
- });
186
-
187
- it("should allow reading files under home directory", async () => {
188
- const homeFile = path.join(fakeHome, "notes.txt");
189
- await fs.writeFile(homeFile, "hello from home");
190
-
191
- // workingDir is tempDir (different from fakeHome), so this tests the home fallback
192
- const result = await executeReadFile({ path: homeFile }, ctx);
193
- expect(result.success).toBe(true);
194
- expect(result.message).toContain("notes.txt");
195
- });
196
-
197
- it("should block access to ~/.ssh", async () => {
198
- const sshDir = path.join(fakeHome, ".ssh");
199
- await fs.mkdir(sshDir, { recursive: true });
200
- await fs.writeFile(path.join(sshDir, "id_rsa"), "secret-key");
201
-
202
- const result = await executeReadFile({ path: path.join(sshDir, "id_rsa") }, ctx);
203
- expect(result.success).toBe(false);
204
- expect(result.message).toContain("sensitive home directory");
205
- });
206
-
207
- it("should block access to ~/.aws", async () => {
208
- const awsDir = path.join(fakeHome, ".aws");
209
- await fs.mkdir(awsDir, { recursive: true });
210
- await fs.writeFile(path.join(awsDir, "credentials"), "secret");
211
-
212
- const result = await executeReadFile({ path: path.join(awsDir, "credentials") }, ctx);
213
- expect(result.success).toBe(false);
214
- expect(result.message).toContain("sensitive home directory");
215
- });
216
-
217
- it("should block access to ~/.gnupg", async () => {
218
- const gpgDir = path.join(fakeHome, ".gnupg");
219
- await fs.mkdir(gpgDir, { recursive: true });
220
- await fs.writeFile(path.join(gpgDir, "private-keys"), "secret");
221
-
222
- const result = await executeReadFile({ path: path.join(gpgDir, "private-keys") }, ctx);
223
- expect(result.success).toBe(false);
224
- expect(result.message).toContain("sensitive home directory");
225
- });
226
-
227
- it("should block access to ~/.git-credentials", async () => {
228
- const credFile = path.join(fakeHome, ".git-credentials");
229
- await fs.writeFile(credFile, "https://user:token@github.com");
230
-
231
- const result = await executeReadFile({ path: credFile }, ctx);
232
- expect(result.success).toBe(false);
233
- expect(result.message).toContain("sensitive home directory");
234
- });
235
-
236
- it("should allow access to ~/.aria subdirectories", async () => {
237
- const ariaDir = path.join(fakeHome, ".aria", "tools");
238
- await fs.mkdir(ariaDir, { recursive: true });
239
- await fs.writeFile(path.join(ariaDir, "my-tool.sh"), "#!/bin/bash\necho hi");
240
-
241
- const result = await executeReadFile({ path: path.join(ariaDir, "my-tool.sh") }, ctx);
242
- expect(result.success).toBe(true);
243
- });
244
-
245
- it("should allow listing home subdirectories that are not blocked", async () => {
246
- const projectsDir = path.join(fakeHome, "projects");
247
- await fs.mkdir(projectsDir, { recursive: true });
248
- await fs.writeFile(path.join(projectsDir, "file.ts"), "code");
249
-
250
- const result = await executeLs({ path: projectsDir }, ctx);
251
- expect(result.success).toBe(true);
252
- });
253
-
254
- it("should block listing ~/.ssh", async () => {
255
- const sshDir = path.join(fakeHome, ".ssh");
256
- await fs.mkdir(sshDir, { recursive: true });
257
- await fs.writeFile(path.join(sshDir, "config"), "Host *");
258
-
259
- const result = await executeLs({ path: sshDir }, ctx);
260
- expect(result.success).toBe(false);
261
- expect(result.message).toContain("sensitive home directory");
262
- });
263
-
264
- it("should allow access to ~/.ssh when autonomy is 'full'", async () => {
265
- const fullCtx = createContext(tempDir);
266
- fullCtx.autonomy = "full";
267
-
268
- const sshDir = path.join(fakeHome, ".ssh");
269
- await fs.mkdir(sshDir, { recursive: true });
270
- await fs.writeFile(path.join(sshDir, "config"), "Host *");
271
-
272
- const result = await executeReadFile({ path: path.join(sshDir, "config") }, fullCtx);
273
- expect(result.success).toBe(true);
274
- });
275
-
276
- it("should still block ~/.ssh when autonomy is 'high'", async () => {
277
- const highCtx = createContext(tempDir);
278
- highCtx.autonomy = "high";
279
-
280
- const sshDir = path.join(fakeHome, ".ssh");
281
- await fs.mkdir(sshDir, { recursive: true });
282
- await fs.writeFile(path.join(sshDir, "id_rsa"), "secret-key");
283
-
284
- const result = await executeReadFile({ path: path.join(sshDir, "id_rsa") }, highCtx);
285
- expect(result.success).toBe(false);
286
- expect(result.message).toContain("sensitive home directory");
287
- });
288
- });
289
- });
290
-
291
- describe("executeWriteFile", () => {
292
- it("should write a new file", async () => {
293
- const filePath = path.join(tempDir, "new.txt");
294
-
295
- const result = await executeWriteFile({ path: filePath, content: "New content" }, ctx);
296
-
297
- expect(result.success).toBe(true);
298
- const content = await fs.readFile(filePath, "utf-8");
299
- expect(content).toBe("New content");
300
- });
301
-
302
- it("should write with relative path", async () => {
303
- const result = await executeWriteFile(
304
- { path: "relative.txt", content: "Relative content" },
305
- ctx,
306
- );
307
-
308
- expect(result.success).toBe(true);
309
- const content = await fs.readFile(path.join(tempDir, "relative.txt"), "utf-8");
310
- expect(content).toBe("Relative content");
311
- });
312
-
313
- it("should overwrite existing file by default", async () => {
314
- const filePath = path.join(tempDir, "existing.txt");
315
- await fs.writeFile(filePath, "Original");
316
-
317
- const result = await executeWriteFile({ path: filePath, content: "Overwritten" }, ctx);
318
-
319
- expect(result.success).toBe(true);
320
- const content = await fs.readFile(filePath, "utf-8");
321
- expect(content).toBe("Overwritten");
322
- });
323
-
324
- it("should include structuredPatch for overwrites", async () => {
325
- const filePath = path.join(tempDir, "diff-write.txt");
326
- await fs.writeFile(filePath, "line 1\nline 2\nline 3\n");
327
-
328
- const result = await executeWriteFile(
329
- { path: filePath, content: "line 1\nLINE TWO\nline 3\n" },
330
- ctx,
331
- );
332
-
333
- expect(result.success).toBe(true);
334
- const data = result.data as Record<string, unknown>;
335
- expect(data.structuredPatch).toBeDefined();
336
- const hunks = data.structuredPatch as Array<{ lines: string[] }>;
337
- expect(hunks.length).toBeGreaterThan(0);
338
- const allLines = hunks.flatMap((h) => h.lines);
339
- expect(allLines).toContain("-line 2");
340
- expect(allLines).toContain("+LINE TWO");
341
- });
342
-
343
- it("should not include structuredPatch for new files", async () => {
344
- const filePath = path.join(tempDir, "brand-new.txt");
345
-
346
- const result = await executeWriteFile({ path: filePath, content: "new content" }, ctx);
347
-
348
- expect(result.success).toBe(true);
349
- const data = result.data as Record<string, unknown>;
350
- expect(data.structuredPatch).toBeUndefined();
351
- });
352
-
353
- it("should append when append option is true", async () => {
354
- const filePath = path.join(tempDir, "append.txt");
355
- await fs.writeFile(filePath, "Original");
356
-
357
- const result = await executeWriteFile(
358
- { path: filePath, content: " appended", append: true },
359
- ctx,
360
- );
361
-
362
- expect(result.success).toBe(true);
363
- const content = await fs.readFile(filePath, "utf-8");
364
- expect(content).toBe("Original appended");
365
- });
366
-
367
- it("should create parent directories if needed", async () => {
368
- const filePath = path.join(tempDir, "nested", "dir", "file.txt");
369
-
370
- const result = await executeWriteFile({ path: filePath, content: "Nested content" }, ctx);
371
-
372
- expect(result.success).toBe(true);
373
- const content = await fs.readFile(filePath, "utf-8");
374
- expect(content).toBe("Nested content");
375
- });
376
-
377
- it("should enforce expectedHash optimistic concurrency", async () => {
378
- const filePath = path.join(tempDir, "guarded.txt");
379
- await fs.writeFile(filePath, "original");
380
-
381
- const wrongHash = "a".repeat(64);
382
- const rejected = await executeWriteFile(
383
- { path: filePath, content: "new", expectedHash: wrongHash },
384
- ctx,
385
- );
386
- expect(rejected.success).toBe(false);
387
- expect(rejected.message).toContain("Hash mismatch");
388
-
389
- const sha256 = createHash("sha256").update("original", "utf8").digest("hex");
390
- const accepted = await executeWriteFile(
391
- { path: filePath, content: "new", expectedHash: sha256 },
392
- ctx,
393
- );
394
- expect(accepted.success).toBe(true);
395
- expect((accepted.data as { currentHash: string }).currentHash).toHaveLength(64);
396
- });
397
- });
398
-
399
- describe("executeEditFile", () => {
400
- it("should replace content in a file", async () => {
401
- const filePath = path.join(tempDir, "edit.txt");
402
- await fs.writeFile(filePath, "Hello World");
403
-
404
- const result = await executeEditFile(
405
- { path: filePath, oldText: "World", newText: "Universe" },
406
- ctx,
407
- );
408
-
409
- expect(result.success).toBe(true);
410
- const content = await fs.readFile(filePath, "utf-8");
411
- expect(content).toBe("Hello Universe");
412
- });
413
-
414
- it("should include structuredPatch in result data", async () => {
415
- const filePath = path.join(tempDir, "diff.txt");
416
- await fs.writeFile(filePath, "line 1\nline 2\nline 3\nline 4\nline 5\n");
417
-
418
- const result = await executeEditFile(
419
- { path: filePath, oldText: "line 3", newText: "LINE THREE" },
420
- ctx,
421
- );
422
-
423
- expect(result.success).toBe(true);
424
- const data = result.data as Record<string, unknown>;
425
- expect(data.structuredPatch).toBeDefined();
426
- expect(Array.isArray(data.structuredPatch)).toBe(true);
427
- const hunks = data.structuredPatch as Array<{
428
- oldStart: number;
429
- newStart: number;
430
- lines: string[];
431
- }>;
432
- expect(hunks.length).toBeGreaterThan(0);
433
- // Verify the hunk contains the change
434
- const allLines = hunks.flatMap((h) => h.lines);
435
- expect(allLines).toContain("-line 3");
436
- expect(allLines).toContain("+LINE THREE");
437
- });
438
-
439
- it("should replace all occurrences when replaceAll is true", async () => {
440
- const filePath = path.join(tempDir, "multi.txt");
441
- await fs.writeFile(filePath, "foo bar foo baz foo");
442
-
443
- const result = await executeEditFile(
444
- { path: filePath, oldText: "foo", newText: "qux", replaceAll: true },
445
- ctx,
446
- );
447
-
448
- expect(result.success).toBe(true);
449
- const content = await fs.readFile(filePath, "utf-8");
450
- expect(content).toBe("qux bar qux baz qux");
451
- });
452
-
453
- it("should replace only first occurrence by default", async () => {
454
- const filePath = path.join(tempDir, "first.txt");
455
- await fs.writeFile(filePath, "foo bar foo");
456
-
457
- const result = await executeEditFile({ path: filePath, oldText: "foo", newText: "qux" }, ctx);
458
-
459
- expect(result.success).toBe(true);
460
- const content = await fs.readFile(filePath, "utf-8");
461
- expect(content).toBe("qux bar foo");
462
- });
463
-
464
- it("should return error if oldText not found", async () => {
465
- const filePath = path.join(tempDir, "notfound.txt");
466
- await fs.writeFile(filePath, "Hello World");
467
-
468
- const result = await executeEditFile(
469
- { path: filePath, oldText: "notexist", newText: "new" },
470
- ctx,
471
- );
472
-
473
- expect(result.success).toBe(false);
474
- expect(result.message).toContain("not found");
475
- });
476
-
477
- it("should return error for non-existent file", async () => {
478
- const result = await executeEditFile(
479
- { path: "nonexistent.txt", oldText: "a", newText: "b" },
480
- ctx,
481
- );
482
-
483
- expect(result.success).toBe(false);
484
- expect(result.message).toBeDefined();
485
- });
486
-
487
- it("should return error for empty oldText", async () => {
488
- const filePath = path.join(tempDir, "edit.txt");
489
- await fs.writeFile(filePath, "Hello World");
490
-
491
- const result = await executeEditFile({ path: filePath, oldText: "", newText: "new" }, ctx);
492
-
493
- expect(result.success).toBe(false);
494
- expect(result.message).toContain("empty");
495
- });
496
-
497
- it("should enforce expectedReplacements when replaceAll is true", async () => {
498
- const filePath = path.join(tempDir, "counts.txt");
499
- await fs.writeFile(filePath, "foo bar foo");
500
-
501
- const mismatch = await executeEditFile(
502
- {
503
- path: filePath,
504
- oldText: "foo",
505
- newText: "qux",
506
- replaceAll: true,
507
- expectedReplacements: 3,
508
- },
509
- ctx,
510
- );
511
- expect(mismatch.success).toBe(false);
512
- expect(mismatch.message).toContain("expected 3");
513
-
514
- const accepted = await executeEditFile(
515
- {
516
- path: filePath,
517
- oldText: "foo",
518
- newText: "qux",
519
- replaceAll: true,
520
- expectedReplacements: 2,
521
- },
522
- ctx,
523
- );
524
- expect(accepted.success).toBe(true);
525
- });
526
-
527
- it("should reject expectedReplacements > 1 without replaceAll", async () => {
528
- const filePath = path.join(tempDir, "strict.txt");
529
- await fs.writeFile(filePath, "foo foo");
530
-
531
- const result = await executeEditFile(
532
- { path: filePath, oldText: "foo", newText: "bar", expectedReplacements: 2 },
533
- ctx,
534
- );
535
- expect(result.success).toBe(false);
536
- expect(result.message).toContain("replaceAll=true");
537
- });
538
-
539
- it("should enforce expectedHash before applying edit", async () => {
540
- const filePath = path.join(tempDir, "guarded-edit.txt");
541
- await fs.writeFile(filePath, "const x = 1;\n");
542
-
543
- const wrongHash = "b".repeat(64);
544
- const rejected = await executeEditFile(
545
- {
546
- path: filePath,
547
- oldText: "1",
548
- newText: "2",
549
- expectedHash: wrongHash,
550
- },
551
- ctx,
552
- );
553
- expect(rejected.success).toBe(false);
554
- expect(rejected.message).toContain("Hash mismatch");
555
-
556
- const hash = createHash("sha256").update("const x = 1;\n", "utf8").digest("hex");
557
- const accepted = await executeEditFile(
558
- {
559
- path: filePath,
560
- oldText: "1",
561
- newText: "2",
562
- expectedHash: hash,
563
- },
564
- ctx,
565
- );
566
- expect(accepted.success).toBe(true);
567
- });
568
-
569
- it("should use fallback strategy when exact match fails due whitespace drift", async () => {
570
- const filePath = path.join(tempDir, "fallback.ts");
571
- await fs.writeFile(filePath, ["function run() {", " return 42;", "}", ""].join("\n"));
572
-
573
- const result = await executeEditFile(
574
- {
575
- path: filePath,
576
- oldText: "function run() {\nreturn 42;\n}",
577
- newText: "function run() {\n return 7;\n}",
578
- replaceAll: false,
579
- },
580
- ctx,
581
- );
582
-
583
- expect(result.success).toBe(true);
584
- const data = result.data as { strategy: string };
585
- expect(data.strategy).not.toBe("exact");
586
- const content = await fs.readFile(filePath, "utf-8");
587
- expect(content).toContain("return 7;");
588
- });
589
-
590
- it("should return canonical shape with filePath (not path)", async () => {
591
- const filePath = path.join(tempDir, "canonical.txt");
592
- await fs.writeFile(filePath, "line 1\nline 2\n");
593
- const result = await executeEditFile(
594
- { path: filePath, oldText: "line 1", newText: "LINE ONE" },
595
- ctx,
596
- );
597
- expect(result.success).toBe(true);
598
- const data = result.data as Record<string, unknown>;
599
- // Use realpath to handle macOS /var → /private/var symlink
600
- const realFilePath = await fs.realpath(filePath);
601
- expect(data.filePath).toBe(realFilePath);
602
- expect(data).not.toHaveProperty("path");
603
- expect(data.structuredPatch).toBeDefined();
604
- });
605
- });
606
-
607
- describe("executeLs", () => {
608
- it("should list directory contents", async () => {
609
- await fs.writeFile(path.join(tempDir, "file1.txt"), "content1");
610
- await fs.writeFile(path.join(tempDir, "file2.txt"), "content2");
611
- await fs.mkdir(path.join(tempDir, "subdir"));
612
-
613
- const result = await executeLs({ path: tempDir }, ctx);
614
-
615
- expect(result.success).toBe(true);
616
- const data = result.data as { entries: Array<{ name: string; type: string }> };
617
- expect(data.entries.map((e) => e.name).sort()).toEqual(["file1.txt", "file2.txt", "subdir"]);
618
- });
619
-
620
- it("should list with relative path", async () => {
621
- await fs.writeFile(path.join(tempDir, "file.txt"), "content");
622
-
623
- const result = await executeLs({ path: "." }, ctx);
624
-
625
- expect(result.success).toBe(true);
626
- const data = result.data as { entries: Array<{ name: string }> };
627
- expect(data.entries.some((e) => e.name === "file.txt")).toBe(true);
628
- });
629
-
630
- it("should include file type information", async () => {
631
- await fs.writeFile(path.join(tempDir, "file.txt"), "content");
632
- await fs.mkdir(path.join(tempDir, "dir"));
633
-
634
- const result = await executeLs({ path: tempDir }, ctx);
635
-
636
- expect(result.success).toBe(true);
637
- const data = result.data as { entries: Array<{ name: string; type: string }> };
638
- const file = data.entries.find((e) => e.name === "file.txt");
639
- const dir = data.entries.find((e) => e.name === "dir");
640
- expect(file?.type).toBe("file");
641
- expect(dir?.type).toBe("directory");
642
- });
643
-
644
- it("should return error for non-existent directory", async () => {
645
- const result = await executeLs({ path: "/nonexistent/dir" }, ctx);
646
-
647
- expect(result.success).toBe(false);
648
- expect(result.message).toBeDefined();
649
- });
650
- });
651
-
652
- describe("executeLs — pagination", () => {
653
- it("should return structured data with pagination metadata", async () => {
654
- await fs.writeFile(path.join(tempDir, "alpha.txt"), "a");
655
- await fs.mkdir(path.join(tempDir, "nested"), { recursive: true });
656
- await fs.writeFile(path.join(tempDir, "nested", "beta.txt"), "b");
657
- await fs.writeFile(path.join(tempDir, "nested", "skip.tmp"), "tmp");
658
-
659
- const result = await executeLs(
660
- {
661
- path: ".",
662
- depth: 2,
663
- offset: 1,
664
- limit: 1,
665
- ignore: ["**/*.tmp"],
666
- },
667
- ctx,
668
- );
669
-
670
- expect(result.success).toBe(true);
671
- expect(result.data).toMatchObject({
672
- depth: 2,
673
- offset: 1,
674
- limit: 1,
675
- ignore: ["**/*.tmp"],
676
- truncated: true,
677
- total: expect.any(Number),
678
- entries: expect.any(Array),
679
- });
680
- const data = result.data as {
681
- entries: Array<{ path: string; depth: number }>;
682
- };
683
- expect(data.entries).toHaveLength(1);
684
- expect(data.entries[0]).toMatchObject({
685
- path: expect.any(String),
686
- depth: expect.any(Number),
687
- });
688
- });
689
-
690
- it("should only resolve types for paged entries", async () => {
691
- for (let i = 0; i < 12; i++) {
692
- await fs.writeFile(path.join(tempDir, `file-${i}.txt`), String(i));
693
- }
694
-
695
- const originalReadType = LS_INTERNALS.readType;
696
- let readTypeCalls = 0;
697
- LS_INTERNALS.readType = async (entryPath: string) => {
698
- readTypeCalls++;
699
- return originalReadType(entryPath);
700
- };
701
-
702
- try {
703
- const result = await executeLs({ path: ".", offset: 5, limit: 2 }, ctx);
704
- expect(result.success).toBe(true);
705
- expect(result.data).toMatchObject({
706
- offset: 5,
707
- limit: 2,
708
- });
709
- expect(readTypeCalls).toBeLessThanOrEqual(2);
710
- } finally {
711
- LS_INTERNALS.readType = originalReadType;
712
- }
713
- });
714
- });
715
-
716
- // executeMkdir, executeRm, executeMv, executeCp tests removed — tools deleted in aria-c3x
717
-
718
- describe("executeGlob", () => {
719
- it("should match files with glob pattern", async () => {
720
- await fs.writeFile(path.join(tempDir, "file1.ts"), "content1");
721
- await fs.writeFile(path.join(tempDir, "file2.ts"), "content2");
722
- await fs.writeFile(path.join(tempDir, "file3.txt"), "content3");
723
-
724
- const result = await executeGlob({ pattern: "*.ts" }, ctx);
725
-
726
- expect(result.success).toBe(true);
727
- const files = result.data as string[];
728
- expect(files).toHaveLength(2);
729
- expect(files.every((f) => f.endsWith(".ts"))).toBe(true);
730
- });
731
-
732
- it("should match nested files with ** pattern", async () => {
733
- await fs.mkdir(path.join(tempDir, "nested", "deep"), { recursive: true });
734
- await fs.writeFile(path.join(tempDir, "root.ts"), "content");
735
- await fs.writeFile(path.join(tempDir, "nested", "nested.ts"), "content");
736
- await fs.writeFile(path.join(tempDir, "nested", "deep", "deep.ts"), "content");
737
-
738
- const result = await executeGlob({ pattern: "**/*.ts" }, ctx);
739
-
740
- expect(result.success).toBe(true);
741
- const files = result.data as string[];
742
- expect(files.length).toBeGreaterThanOrEqual(3);
743
- });
744
-
745
- it("should work with absolute path pattern", async () => {
746
- await fs.writeFile(path.join(tempDir, "file.txt"), "content");
747
-
748
- const result = await executeGlob({ pattern: path.join(tempDir, "*.txt") }, ctx);
749
-
750
- expect(result.success).toBe(true);
751
- const files = result.data as string[];
752
- expect(files).toHaveLength(1);
753
- });
754
-
755
- it("should return empty array for no matches", async () => {
756
- const result = await executeGlob({ pattern: "*.nonexistent" }, ctx);
757
-
758
- expect(result.success).toBe(true);
759
- expect(result.data).toEqual([]);
760
- });
761
-
762
- it("should support cwd option", async () => {
763
- const subdir = path.join(tempDir, "subdir");
764
- await fs.mkdir(subdir);
765
- await fs.writeFile(path.join(subdir, "file.ts"), "content");
766
-
767
- const result = await executeGlob({ pattern: "*.ts", cwd: subdir }, ctx);
768
-
769
- expect(result.success).toBe(true);
770
- const files = result.data as string[];
771
- expect(files).toHaveLength(1);
772
- });
773
-
774
- it("should reject cwd prefix-collision paths outside working directory", async () => {
775
- const siblingDir = `${tempDir}-evil`;
776
- await fs.mkdir(siblingDir, { recursive: true });
777
- await fs.writeFile(path.join(siblingDir, "outside.ts"), "content");
778
-
779
- const result = await executeGlob({ pattern: "*.ts", cwd: siblingDir }, ctx);
780
-
781
- expect(result.success).toBe(false);
782
- expect(result.message).toContain("outside working directory");
783
- });
784
-
785
- it("should cap glob output to hard limit", async () => {
786
- const total = FILESYSTEM_OUTPUT_LIMITS.MAX_GLOB_RESULTS + 25;
787
- const writes = Array.from({ length: total }, (_, i) =>
788
- fs.writeFile(path.join(tempDir, `cap-${i}.txt`), "x"),
789
- );
790
- await Promise.all(writes);
791
-
792
- const result = await executeGlob({ pattern: "cap-*.txt" }, ctx);
793
-
794
- expect(result.success).toBe(true);
795
- const files = result.data as string[];
796
- expect(files).toHaveLength(FILESYSTEM_OUTPUT_LIMITS.MAX_GLOB_RESULTS);
797
- expect(result.message).toContain("truncated");
798
- });
799
- });
800
-
801
- describe("executeGrep", () => {
802
- beforeEach(async () => {
803
- await fs.writeFile(
804
- path.join(tempDir, "file1.txt"),
805
- "Hello World\nThis is a test\nHello again",
806
- );
807
- await fs.writeFile(path.join(tempDir, "file2.txt"), "Another file\nNo matches here");
808
- await fs.writeFile(
809
- path.join(tempDir, "file3.ts"),
810
- 'const hello = "Hello";\nfunction test() {}',
811
- );
812
- });
813
-
814
- it("should find pattern in single file", async () => {
815
- const filePath = path.join(tempDir, "file1.txt");
816
-
817
- const result = await executeGrep({ pattern: "Hello", path: filePath }, ctx);
818
-
819
- expect(result.success).toBe(true);
820
- const data = result.data as {
821
- matches: Array<{ file: string; line: number; content: string }>;
822
- errors?: string[];
823
- };
824
- expect(data.matches.length).toBe(2);
825
- expect(data.matches[0].content).toContain("Hello");
826
- });
827
-
828
- it("should search directory recursively", async () => {
829
- const result = await executeGrep({ pattern: "Hello", path: tempDir }, ctx);
830
-
831
- expect(result.success).toBe(true);
832
- const data = result.data as { matches: Array<{ file: string }>; errors?: string[] };
833
- expect(data.matches.length).toBeGreaterThanOrEqual(3);
834
- });
835
-
836
- it("should support glob patterns", async () => {
837
- const result = await executeGrep({ pattern: "Hello", path: tempDir, glob: "*.txt" }, ctx);
838
-
839
- expect(result.success).toBe(true);
840
- const data = result.data as { matches: Array<{ file: string }>; errors?: string[] };
841
- expect(data.matches.every((m) => m.file.endsWith(".txt"))).toBe(true);
842
- });
843
-
844
- it("should support case-insensitive search", async () => {
845
- const result = await executeGrep({ pattern: "hello", path: tempDir, ignoreCase: true }, ctx);
846
-
847
- expect(result.success).toBe(true);
848
- const data = result.data as { matches: Array<{ content: string }>; errors?: string[] };
849
- expect(data.matches.length).toBeGreaterThan(0);
850
- });
851
-
852
- it("should return empty array for no matches", async () => {
853
- const result = await executeGrep({ pattern: "nomatchpattern123", path: tempDir }, ctx);
854
-
855
- expect(result.success).toBe(true);
856
- const data = result.data as { matches: Array<unknown>; errors?: string[] };
857
- expect(data.matches).toEqual([]);
858
- });
859
-
860
- it("should return error for invalid regex", async () => {
861
- const result = await executeGrep({ pattern: "[invalid(regex", path: tempDir }, ctx);
862
-
863
- expect(result.success).toBe(false);
864
- expect(result.message).toBeDefined();
865
- });
866
-
867
- it("should reject potentially catastrophic regex patterns", async () => {
868
- const result = await executeGrep({ pattern: "(a+)+$", path: tempDir }, ctx);
869
-
870
- expect(result.success).toBe(false);
871
- expect((result.message ?? "").toLowerCase()).toContain("unsafe regular expression");
872
- });
873
-
874
- it("should reject ambiguous repeated alternation patterns prone to backtracking", async () => {
875
- const result = await executeGrep({ pattern: "(a|aa)+$", path: tempDir }, ctx);
876
-
877
- expect(result.success).toBe(false);
878
- expect((result.message ?? "").toLowerCase()).toContain("unsafe regular expression");
879
- });
880
-
881
- it("should reject lookaround assertions for grep regex safety", async () => {
882
- const result = await executeGrep({ pattern: "(?=a)a+", path: tempDir }, ctx);
883
-
884
- expect(result.success).toBe(false);
885
- expect((result.message ?? "").toLowerCase()).toContain("unsafe regular expression");
886
- });
887
-
888
- it("should work with relative path", async () => {
889
- const result = await executeGrep({ pattern: "Hello", path: "." }, ctx);
890
-
891
- expect(result.success).toBe(true);
892
- const data = result.data as { matches: Array<{ file: string }>; errors?: string[] };
893
- expect(data.matches.length).toBeGreaterThan(0);
894
- });
895
-
896
- it("should default path to current working directory when omitted", async () => {
897
- const result = await executeGrep({ pattern: "Hello" }, ctx);
898
-
899
- expect(result.success).toBe(true);
900
- const data = result.data as { matches: Array<{ file: string }>; errors?: string[] };
901
- expect(data.matches.length).toBeGreaterThan(0);
902
- });
903
-
904
- it("should cap grep matches to hard limit and mark truncated", async () => {
905
- const hits = FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_MATCHES + 20;
906
- await fs.writeFile(path.join(tempDir, "many-hits.txt"), Array(hits).fill("HIT").join("\n"));
907
-
908
- const result = await executeGrep(
909
- { pattern: "HIT", path: path.join(tempDir, "many-hits.txt") },
910
- ctx,
911
- );
912
-
913
- expect(result.success).toBe(true);
914
- const data = result.data as {
915
- matches: Array<{ file: string; line: number; content: string }>;
916
- truncated?: boolean;
917
- };
918
- expect(data.matches).toHaveLength(FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_MATCHES);
919
- expect(data.truncated).toBe(true);
920
- expect(result.message).toContain("truncated");
921
- });
922
-
923
- it("should skip oversized files instead of reading them fully", async () => {
924
- const oversizedPath = path.join(tempDir, "oversized.txt");
925
- await fs.writeFile(
926
- oversizedPath,
927
- "a".repeat(FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_FILE_BYTES + 1024),
928
- );
929
-
930
- const result = await executeGrep({ pattern: "a+", path: oversizedPath }, ctx);
931
-
932
- expect(result.success).toBe(true);
933
- const data = result.data as {
934
- matches: Array<{ file: string; line: number; content: string }>;
935
- errors?: string[];
936
- };
937
- expect(data.matches).toHaveLength(0);
938
- expect(data.errors?.[0]).toContain("exceeds");
939
- });
940
-
941
- it("should skip extremely long lines to cap regex evaluation risk", async () => {
942
- const longLinePath = path.join(tempDir, "long-line.txt");
943
- await fs.writeFile(
944
- longLinePath,
945
- `${"a".repeat(FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_LINE_BYTES + 1)}\nshort line`,
946
- );
947
-
948
- const result = await executeGrep({ pattern: "a+", path: longLinePath }, ctx);
949
-
950
- expect(result.success).toBe(true);
951
- const data = result.data as {
952
- matches: Array<{ file: string; line: number; content: string }>;
953
- errors?: string[];
954
- };
955
- expect(data.matches).toHaveLength(0);
956
- expect(data.errors?.[0]).toContain("Skipped lines longer than");
957
- });
958
- });
959
- });