chatccc 0.2.228 → 0.2.230

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.
@@ -1,275 +1,240 @@
1
- import { createHash } from "node:crypto";
2
- import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
-
6
- import { afterEach, describe, expect, it } from "vitest";
7
-
8
- import {
9
- applyPatchForTool,
10
- createFileForTool,
11
- deleteFileForTool,
12
- editFileForTool,
13
- listDirForTool,
14
- moveFileForTool,
15
- readFileForTool,
16
- runCommandForTool,
17
- searchCodeForTool,
18
- } from "../builtin/file-tools.ts";
19
-
20
- const tempDirs: string[] = [];
21
-
22
- async function makeTempDir(): Promise<string> {
23
- const dir = await mkdtemp(join(tmpdir(), "chatccc-builtin-tools-"));
24
- tempDirs.push(dir);
25
- return dir;
26
- }
27
-
28
- function sha256(text: string): string {
29
- return createHash("sha256").update(text).digest("hex");
30
- }
31
-
32
- afterEach(async () => {
33
- await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
34
- });
35
-
36
- describe("builtin file tools", () => {
37
- it("reads a text file with line ranges", async () => {
38
- const dir = await makeTempDir();
39
- await writeFile(join(dir, ".secret.txt"), "one\ntwo\nthree\n", "utf8");
40
-
41
- const result = await readFileForTool(dir, { path: ".secret.txt", startLine: 2, endLine: 3 });
42
-
43
- expect(result).toEqual(expect.objectContaining({
44
- sha256: sha256("one\ntwo\nthree\n"),
45
- isBinary: false,
46
- content: "two\nthree",
47
- startLine: 2,
48
- endLine: 3,
49
- totalLines: 4,
50
- }));
51
- expect(result.path).toContain(".secret.txt");
52
- });
53
-
54
- it("lists directory entries including hidden files", async () => {
55
- const dir = await makeTempDir();
56
- await writeFile(join(dir, ".env"), "TOKEN=x", "utf8");
57
-
58
- const result = await listDirForTool(dir);
59
-
60
- expect(result.entries).toContainEqual(expect.objectContaining({
61
- name: ".env",
62
- type: "file",
63
- }));
64
- });
65
-
66
- it("searches with the project-bundled ripgrep even when rg is absent from PATH", async () => {
67
- const dir = await makeTempDir();
68
- await writeFile(join(dir, "a.ts"), "const marker = 1;\n", "utf8");
69
- const originalPath = process.env.PATH;
70
- process.env.PATH = "";
71
-
72
- try {
73
- const result = await searchCodeForTool(dir, { query: "marker", glob: "*.ts" });
74
-
75
- expect(result.matches).toEqual([
76
- expect.objectContaining({
77
- line: 1,
78
- column: 7,
79
- text: "const marker = 1;",
80
- }),
81
- ]);
82
- } finally {
83
- process.env.PATH = originalPath;
84
- }
85
- });
86
-
87
- it("falls back to Node search when no ripgrep executable can be used", async () => {
88
- const dir = await makeTempDir();
89
- await writeFile(join(dir, "a.ts"), "const marker = 1;\n", "utf8");
90
- await mkdir(join(dir, "nested"));
91
- await writeFile(join(dir, "nested", "b.md"), "xx marker = 2\n", "utf8");
92
- await writeFile(join(dir, "ignored.txt"), "marker = 3\n", "utf8");
93
- const originalPath = process.env.PATH;
94
- process.env.PATH = "";
95
-
96
- try {
97
- const result = await searchCodeForTool(
98
- dir,
99
- { query: "marker\\s*=\\s*\\d", glob: "**/*.{ts,md}", maxResults: 10 },
100
- undefined,
101
- { ripgrepCommands: [join(dir, "missing-rg")] },
102
- );
103
-
104
- expect(result.matches).toEqual([
105
- expect.objectContaining({ path: join(dir, "a.ts"), line: 1, column: 7 }),
106
- expect.objectContaining({ path: join(dir, "nested", "b.md"), line: 1, column: 4 }),
107
- ]);
108
- expect(result.truncated).toBe(false);
109
- } finally {
110
- process.env.PATH = originalPath;
111
- }
112
- });
113
-
114
- it("runs non-interactive shell commands in the requested cwd", async () => {
115
- const dir = await makeTempDir();
116
-
117
- const result = await runCommandForTool(dir, {
118
- command: "node -e \"process.stdout.write(process.cwd())\"",
119
- timeoutMs: 5_000,
120
- });
121
-
122
- expect(result.exitCode).toBe(0);
123
- expect(result.timedOut).toBe(false);
124
- expect(result.stdout.toLowerCase()).toBe(dir.toLowerCase());
125
- expect(result.stderr).toBe("");
126
- });
127
-
128
- it("returns non-zero command exits without throwing", async () => {
129
- const dir = await makeTempDir();
130
-
131
- const result = await runCommandForTool(dir, {
132
- command: "node -e \"process.stderr.write('failed'); process.exit(7)\"",
133
- timeoutMs: 5_000,
134
- });
135
-
136
- expect(result.exitCode).toBe(7);
137
- expect(result.stderr).toBe("failed");
138
- expect(result.timedOut).toBe(false);
139
- });
140
-
141
- it("edits a file with exact replacements and a SHA-256 precondition", async () => {
142
- const dir = await makeTempDir();
143
- const file = join(dir, "edit.txt");
144
- await writeFile(file, "alpha\nbeta\ngamma\n", "utf8");
145
-
146
- const result = await editFileForTool(dir, {
147
- path: "edit.txt",
148
- expectedSha256: sha256("alpha\nbeta\ngamma\n"),
149
- edits: [{ oldText: "beta", newText: "BETA" }],
150
- });
151
-
152
- expect(result).toEqual(expect.objectContaining({
153
- changed: true,
154
- editsApplied: 1,
155
- beforeSha256: sha256("alpha\nbeta\ngamma\n"),
156
- afterSha256: sha256("alpha\nBETA\ngamma\n"),
157
- }));
158
- await expect(readFile(file, "utf8")).resolves.toBe("alpha\nBETA\ngamma\n");
159
- });
160
-
161
- it("edits a CRLF file with LF oldText by normalizing line endings", async () => {
162
- const dir = await makeTempDir();
163
- const file = join(dir, "crlf.txt");
164
- const crlfContent = "alpha\r\nbeta\r\ngamma\r\n";
165
- await writeFile(file, crlfContent, "utf8");
166
-
167
- const result = await editFileForTool(dir, {
168
- path: "crlf.txt",
169
- expectedSha256: sha256(crlfContent),
170
- edits: [{ oldText: "alpha\nbeta", newText: "ALPHA\nBETA" }],
171
- });
172
-
173
- expect(result).toEqual(expect.objectContaining({
174
- changed: true,
175
- editsApplied: 1,
176
- beforeSha256: sha256(crlfContent),
177
- afterSha256: sha256("ALPHA\r\nBETA\r\ngamma\r\n"),
178
- }));
179
- await expect(readFile(file, "utf8")).resolves.toBe("ALPHA\r\nBETA\r\ngamma\r\n");
180
- });
181
-
182
- it("rejects a multi-line oldText that still does not match after EOL normalization", async () => {
183
- const dir = await makeTempDir();
184
- await writeFile(join(dir, "crlf.txt"), "alpha\r\nbeta\r\n", "utf8");
185
-
186
- await expect(editFileForTool(dir, {
187
- path: "crlf.txt",
188
- edits: [{ oldText: "alpha\ngamma", newText: "x" }],
189
- })).rejects.toThrow("oldText was not found");
190
- });
191
-
192
- it("rejects edits when the SHA-256 precondition does not match", async () => {
193
- const dir = await makeTempDir();
194
- await writeFile(join(dir, "edit.txt"), "current\n", "utf8");
195
-
196
- await expect(editFileForTool(dir, {
197
- path: "edit.txt",
198
- expectedSha256: sha256("stale\n"),
199
- edits: [{ oldText: "current", newText: "next" }],
200
- })).rejects.toThrow("SHA-256 mismatch");
201
- });
202
-
203
- it("creates and deletes files", async () => {
204
- const dir = await makeTempDir();
205
-
206
- const created = await createFileForTool(dir, {
207
- path: "created.txt",
208
- content: "created\n",
209
- });
210
- expect(created).toEqual(expect.objectContaining({
211
- changed: true,
212
- afterSha256: sha256("created\n"),
213
- }));
214
- await expect(readFile(join(dir, "created.txt"), "utf8")).resolves.toBe("created\n");
215
-
216
- const deleted = await deleteFileForTool(dir, {
217
- path: "created.txt",
218
- expectedSha256: sha256("created\n"),
219
- });
220
- expect(deleted).toEqual(expect.objectContaining({
221
- deleted: true,
222
- beforeSha256: sha256("created\n"),
223
- }));
224
- await expect(stat(join(dir, "created.txt"))).rejects.toThrow();
225
- });
226
-
227
- it("moves files and creates the destination directory", async () => {
228
- const dir = await makeTempDir();
229
- await writeFile(join(dir, "old.txt"), "move me\n", "utf8");
230
-
231
- const result = await moveFileForTool(dir, {
232
- sourcePath: "old.txt",
233
- destinationPath: "nested/new.txt",
234
- expectedSourceSha256: sha256("move me\n"),
235
- });
236
-
237
- expect(result).toEqual(expect.objectContaining({
238
- moved: true,
239
- sourceSha256: sha256("move me\n"),
240
- }));
241
- await expect(stat(join(dir, "old.txt"))).rejects.toThrow();
242
- await expect(readFile(join(dir, "nested", "new.txt"), "utf8")).resolves.toBe("move me\n");
243
- });
244
-
245
- it("applies a unified diff patch", async () => {
246
- const dir = await makeTempDir();
247
- await writeFile(join(dir, "patch.txt"), "one\ntwo\nthree\n", "utf8");
248
-
249
- const result = await applyPatchForTool(dir, {
250
- patch: [
251
- "--- a/patch.txt",
252
- "+++ b/patch.txt",
253
- "@@ -1,4 +1,4 @@",
254
- " one",
255
- "-two",
256
- "+TWO",
257
- " three",
258
- " ",
259
- "",
260
- ].join("\n"),
261
- expectedSha256ByPath: {
262
- "patch.txt": sha256("one\ntwo\nthree\n"),
263
- },
264
- });
265
-
266
- expect(result.changedFiles).toEqual([
267
- expect.objectContaining({
268
- action: "edit",
269
- beforeSha256: sha256("one\ntwo\nthree\n"),
270
- afterSha256: sha256("one\nTWO\nthree\n"),
271
- }),
272
- ]);
273
- await expect(readFile(join(dir, "patch.txt"), "utf8")).resolves.toBe("one\nTWO\nthree\n");
274
- });
275
- });
1
+ import { execFile } from "node:child_process";
2
+ import { createHash } from "node:crypto";
3
+ import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
4
+ import { homedir, tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { promisify } from "node:util";
7
+
8
+ import { afterEach, describe, expect, it } from "vitest";
9
+
10
+ import {
11
+ applyPatchForTool,
12
+ createFileForTool,
13
+ deleteFileForTool,
14
+ editFileForTool,
15
+ expandHomePath,
16
+ listDirForTool,
17
+ moveFileForTool,
18
+ readFileForTool,
19
+ runCommandForTool,
20
+ searchCodeForTool,
21
+ } from "../builtin/file-tools.ts";
22
+
23
+ const execFileAsync = promisify(execFile);
24
+ const tempDirs: string[] = [];
25
+
26
+ async function makeTempDir(): Promise<string> {
27
+ const dir = await mkdtemp(join(tmpdir(), "deepccc-tools-"));
28
+ tempDirs.push(dir);
29
+ return dir;
30
+ }
31
+
32
+ describe("expandHomePath", () => {
33
+ it("expands ~ and ~/ (both separators) to the user home directory", () => {
34
+ const home = homedir();
35
+ expect(expandHomePath("~")).toBe(home);
36
+ expect(expandHomePath("~/x/y.txt")).toBe(join(home, "x", "y.txt"));
37
+ expect(expandHomePath("~\\x\\y.txt")).toBe(join(home, "x", "y.txt"));
38
+ });
39
+
40
+ it("leaves absolute paths and other inputs unchanged", () => {
41
+ expect(expandHomePath("C:/a/b")).toBe("C:/a/b");
42
+ expect(expandHomePath("~other/x")).toBe("~other/x");
43
+ expect(expandHomePath("")).toBe("");
44
+ });
45
+ });
46
+
47
+ async function hasRg(): Promise<boolean> {
48
+ try {
49
+ await execFileAsync("rg", ["--version"]);
50
+ return true;
51
+ } catch {
52
+ return false;
53
+ }
54
+ }
55
+
56
+ function sha256(text: string): string {
57
+ return createHash("sha256").update(text).digest("hex");
58
+ }
59
+
60
+ afterEach(async () => {
61
+ await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
62
+ });
63
+
64
+ describe("DeepCCC file tools", () => {
65
+ it("reads a text file with line ranges", async () => {
66
+ const dir = await makeTempDir();
67
+ await writeFile(join(dir, ".secret.txt"), "one\ntwo\nthree\n", "utf8");
68
+
69
+ const result = await readFileForTool(dir, { path: ".secret.txt", startLine: 2, endLine: 3 });
70
+
71
+ expect(result).toEqual(expect.objectContaining({
72
+ sha256: sha256("one\ntwo\nthree\n"),
73
+ isBinary: false,
74
+ content: "two\nthree",
75
+ startLine: 2,
76
+ endLine: 3,
77
+ totalLines: 4,
78
+ }));
79
+ expect(result.path).toContain(".secret.txt");
80
+ });
81
+
82
+ it("lists directory entries including hidden files", async () => {
83
+ const dir = await makeTempDir();
84
+ await writeFile(join(dir, ".env"), "TOKEN=x", "utf8");
85
+
86
+ const result = await listDirForTool(dir);
87
+
88
+ expect(result.entries).toContainEqual(expect.objectContaining({
89
+ name: ".env",
90
+ type: "file",
91
+ }));
92
+ });
93
+
94
+ it("searches code with rg without using a shell", async () => {
95
+ if (!await hasRg()) return;
96
+
97
+ const dir = await makeTempDir();
98
+ await writeFile(join(dir, "a.ts"), "const marker = 1;\n", "utf8");
99
+
100
+ const result = await searchCodeForTool(dir, { query: "marker", glob: "*.ts" });
101
+
102
+ expect(result.matches).toEqual([
103
+ expect.objectContaining({
104
+ line: 1,
105
+ text: "const marker = 1;",
106
+ }),
107
+ ]);
108
+ });
109
+
110
+ it("runs non-interactive shell commands in the requested cwd", async () => {
111
+ const dir = await makeTempDir();
112
+
113
+ const result = await runCommandForTool(dir, {
114
+ command: "node -e \"process.stdout.write(process.cwd())\"",
115
+ timeoutMs: 5_000,
116
+ });
117
+
118
+ expect(result.exitCode).toBe(0);
119
+ expect(result.timedOut).toBe(false);
120
+ expect(result.stdout.toLowerCase()).toBe(dir.toLowerCase());
121
+ expect(result.stderr).toBe("");
122
+ });
123
+
124
+ it("returns non-zero command exits without throwing", async () => {
125
+ const dir = await makeTempDir();
126
+
127
+ const result = await runCommandForTool(dir, {
128
+ command: "node -e \"process.stderr.write('failed'); process.exit(7)\"",
129
+ timeoutMs: 5_000,
130
+ });
131
+
132
+ expect(result.exitCode).toBe(7);
133
+ expect(result.stderr).toBe("failed");
134
+ expect(result.timedOut).toBe(false);
135
+ });
136
+
137
+ it("edits a file with exact replacements and a SHA-256 precondition", async () => {
138
+ const dir = await makeTempDir();
139
+ const file = join(dir, "edit.txt");
140
+ await writeFile(file, "alpha\nbeta\ngamma\n", "utf8");
141
+
142
+ const result = await editFileForTool(dir, {
143
+ path: "edit.txt",
144
+ expectedSha256: sha256("alpha\nbeta\ngamma\n"),
145
+ edits: [{ oldText: "beta", newText: "BETA" }],
146
+ });
147
+
148
+ expect(result).toEqual(expect.objectContaining({
149
+ changed: true,
150
+ editsApplied: 1,
151
+ beforeSha256: sha256("alpha\nbeta\ngamma\n"),
152
+ afterSha256: sha256("alpha\nBETA\ngamma\n"),
153
+ }));
154
+ await expect(readFile(file, "utf8")).resolves.toBe("alpha\nBETA\ngamma\n");
155
+ });
156
+
157
+ it("rejects edits when the SHA-256 precondition does not match", async () => {
158
+ const dir = await makeTempDir();
159
+ await writeFile(join(dir, "edit.txt"), "current\n", "utf8");
160
+
161
+ await expect(editFileForTool(dir, {
162
+ path: "edit.txt",
163
+ expectedSha256: sha256("stale\n"),
164
+ edits: [{ oldText: "current", newText: "next" }],
165
+ })).rejects.toThrow("SHA-256 mismatch");
166
+ });
167
+
168
+ it("creates and deletes files", async () => {
169
+ const dir = await makeTempDir();
170
+
171
+ const created = await createFileForTool(dir, {
172
+ path: "created.txt",
173
+ content: "created\n",
174
+ });
175
+ expect(created).toEqual(expect.objectContaining({
176
+ changed: true,
177
+ afterSha256: sha256("created\n"),
178
+ }));
179
+ await expect(readFile(join(dir, "created.txt"), "utf8")).resolves.toBe("created\n");
180
+
181
+ const deleted = await deleteFileForTool(dir, {
182
+ path: "created.txt",
183
+ expectedSha256: sha256("created\n"),
184
+ });
185
+ expect(deleted).toEqual(expect.objectContaining({
186
+ deleted: true,
187
+ beforeSha256: sha256("created\n"),
188
+ }));
189
+ await expect(stat(join(dir, "created.txt"))).rejects.toThrow();
190
+ });
191
+
192
+ it("moves files and creates the destination directory", async () => {
193
+ const dir = await makeTempDir();
194
+ await writeFile(join(dir, "old.txt"), "move me\n", "utf8");
195
+
196
+ const result = await moveFileForTool(dir, {
197
+ sourcePath: "old.txt",
198
+ destinationPath: "nested/new.txt",
199
+ expectedSourceSha256: sha256("move me\n"),
200
+ });
201
+
202
+ expect(result).toEqual(expect.objectContaining({
203
+ moved: true,
204
+ sourceSha256: sha256("move me\n"),
205
+ }));
206
+ await expect(stat(join(dir, "old.txt"))).rejects.toThrow();
207
+ await expect(readFile(join(dir, "nested", "new.txt"), "utf8")).resolves.toBe("move me\n");
208
+ });
209
+
210
+ it("applies a unified diff patch", async () => {
211
+ const dir = await makeTempDir();
212
+ await writeFile(join(dir, "patch.txt"), "one\ntwo\nthree\n", "utf8");
213
+
214
+ const result = await applyPatchForTool(dir, {
215
+ patch: [
216
+ "--- a/patch.txt",
217
+ "+++ b/patch.txt",
218
+ "@@ -1,4 +1,4 @@",
219
+ " one",
220
+ "-two",
221
+ "+TWO",
222
+ " three",
223
+ " ",
224
+ "",
225
+ ].join("\n"),
226
+ expectedSha256ByPath: {
227
+ "patch.txt": sha256("one\ntwo\nthree\n"),
228
+ },
229
+ });
230
+
231
+ expect(result.changedFiles).toEqual([
232
+ expect.objectContaining({
233
+ action: "edit",
234
+ beforeSha256: sha256("one\ntwo\nthree\n"),
235
+ afterSha256: sha256("one\nTWO\nthree\n"),
236
+ }),
237
+ ]);
238
+ await expect(readFile(join(dir, "patch.txt"), "utf8")).resolves.toBe("one\nTWO\nthree\n");
239
+ });
240
+ });