chatccc 0.2.199 → 0.2.200
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 +1 -1
- package/agent-prompts/claude_specific.md +45 -45
- package/agent-prompts/codex_specific.md +2 -2
- package/agent-prompts/cursor_specific.md +13 -13
- package/bin/cccagent.mjs +17 -17
- package/config.sample.json +27 -27
- package/im-skills/feishu-skill/receive-send-file.md +63 -63
- package/im-skills/feishu-skill/receive-send-image.md +24 -24
- package/im-skills/feishu-skill/skill.md +3 -3
- package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
- package/im-skills/wechat-file-skill/send-file.mjs +83 -83
- package/im-skills/wechat-file-skill/skill.md +10 -10
- package/im-skills/wechat-image-skill/skill.md +10 -10
- package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
- package/im-skills/wechat-video-skill/send-video.mjs +79 -79
- package/im-skills/wechat-video-skill/skill.md +10 -10
- package/package.json +1 -1
- package/scripts/postinstall-sharp-check.mjs +58 -58
- package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
- package/src/__tests__/agent-reload-config-rpc.test.ts +99 -99
- package/src/__tests__/builtin-chat-session.test.ts +277 -277
- package/src/__tests__/builtin-cli-json.test.ts +39 -39
- package/src/__tests__/builtin-config.test.ts +33 -33
- package/src/__tests__/builtin-context.test.ts +163 -163
- package/src/__tests__/builtin-file-tools.test.ts +224 -224
- package/src/__tests__/builtin-session-select.test.ts +116 -116
- package/src/__tests__/builtin-sigint.test.ts +56 -56
- package/src/__tests__/card-plain-text.test.ts +5 -5
- package/src/__tests__/cardkit.test.ts +60 -60
- package/src/__tests__/cards.test.ts +109 -109
- package/src/__tests__/ccc-adapter.test.ts +114 -114
- package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
- package/src/__tests__/chatgpt-subscription.test.ts +135 -135
- package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
- package/src/__tests__/claude-adapter.test.ts +592 -592
- package/src/__tests__/claude-raw-stream-log.test.ts +87 -87
- package/src/__tests__/codex-raw-stream-log.test.ts +163 -163
- package/src/__tests__/codex-reset-actions.test.ts +146 -146
- package/src/__tests__/config-reload.test.ts +10 -10
- package/src/__tests__/config-sample.test.ts +18 -18
- package/src/__tests__/feishu-api.test.ts +60 -60
- package/src/__tests__/feishu-avatar.test.ts +87 -45
- package/src/__tests__/feishu-platform.test.ts +22 -22
- package/src/__tests__/format-message.test.ts +47 -47
- package/src/__tests__/jsonl-stream.test.ts +79 -79
- package/src/__tests__/orchestrator.test.ts +227 -202
- package/src/__tests__/privacy.test.ts +198 -198
- package/src/__tests__/raw-stream-log.test.ts +106 -106
- package/src/__tests__/session.test.ts +40 -40
- package/src/__tests__/shared-prefix.test.ts +36 -36
- package/src/__tests__/sim-platform.test.ts +12 -12
- package/src/__tests__/stream-state.test.ts +42 -42
- package/src/__tests__/web-ui.test.ts +209 -209
- package/src/adapters/ccc-adapter.ts +121 -121
- package/src/adapters/claude-adapter.ts +603 -603
- package/src/adapters/claude-session-meta-store.ts +120 -120
- package/src/adapters/codex-adapter.ts +380 -380
- package/src/adapters/jsonl-stream.ts +157 -157
- package/src/adapters/raw-stream-log.ts +124 -124
- package/src/adapters/resource-monitor.ts +140 -140
- package/src/agent-delegate-task-rpc.ts +153 -153
- package/src/agent-delegate-task.ts +81 -81
- package/src/agent-reload-config-rpc.ts +34 -34
- package/src/agent-stop-stuck.ts +129 -129
- package/src/builtin/cli.ts +473 -473
- package/src/builtin/context.ts +323 -323
- package/src/builtin/file-tools.ts +1072 -1072
- package/src/builtin/index.ts +404 -404
- package/src/builtin/session-select.ts +48 -48
- package/src/builtin/sigint.ts +50 -50
- package/src/cards.ts +195 -195
- package/src/chatgpt-subscription-rpc.ts +27 -27
- package/src/chatgpt-subscription.ts +299 -299
- package/src/chrome-devtools-guard.ts +318 -318
- package/src/codex-reset-actions.ts +184 -184
- package/src/config.ts +125 -125
- package/src/feishu-api.ts +86 -63
- package/src/feishu-platform.ts +20 -20
- package/src/format-message.ts +293 -293
- package/src/litellm-proxy.ts +374 -374
- package/src/orchestrator.ts +183 -183
- package/src/privacy.ts +118 -118
- package/src/runtime-reload.ts +34 -34
- package/src/session-chat-binding.ts +6 -6
- package/src/session-name.ts +8 -8
- package/src/session.ts +141 -141
- package/src/shared-prefix.ts +29 -29
- package/src/sim-platform.ts +20 -20
- package/src/turn-cards.ts +117 -117
- package/src/web-ui.ts +205 -205
|
@@ -1,224 +1,224 @@
|
|
|
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 { 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
|
-
listDirForTool,
|
|
16
|
-
moveFileForTool,
|
|
17
|
-
readFileForTool,
|
|
18
|
-
runCommandForTool,
|
|
19
|
-
searchCodeForTool,
|
|
20
|
-
} from "../builtin/file-tools.ts";
|
|
21
|
-
|
|
22
|
-
const execFileAsync = promisify(execFile);
|
|
23
|
-
const tempDirs: string[] = [];
|
|
24
|
-
|
|
25
|
-
async function makeTempDir(): Promise<string> {
|
|
26
|
-
const dir = await mkdtemp(join(tmpdir(), "chatccc-builtin-tools-"));
|
|
27
|
-
tempDirs.push(dir);
|
|
28
|
-
return dir;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function hasRg(): Promise<boolean> {
|
|
32
|
-
try {
|
|
33
|
-
await execFileAsync("rg", ["--version"]);
|
|
34
|
-
return true;
|
|
35
|
-
} catch {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function sha256(text: string): string {
|
|
41
|
-
return createHash("sha256").update(text).digest("hex");
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
afterEach(async () => {
|
|
45
|
-
await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
describe("builtin file tools", () => {
|
|
49
|
-
it("reads a text file with line ranges", async () => {
|
|
50
|
-
const dir = await makeTempDir();
|
|
51
|
-
await writeFile(join(dir, ".secret.txt"), "one\ntwo\nthree\n", "utf8");
|
|
52
|
-
|
|
53
|
-
const result = await readFileForTool(dir, { path: ".secret.txt", startLine: 2, endLine: 3 });
|
|
54
|
-
|
|
55
|
-
expect(result).toEqual(expect.objectContaining({
|
|
56
|
-
sha256: sha256("one\ntwo\nthree\n"),
|
|
57
|
-
isBinary: false,
|
|
58
|
-
content: "two\nthree",
|
|
59
|
-
startLine: 2,
|
|
60
|
-
endLine: 3,
|
|
61
|
-
totalLines: 4,
|
|
62
|
-
}));
|
|
63
|
-
expect(result.path).toContain(".secret.txt");
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("lists directory entries including hidden files", async () => {
|
|
67
|
-
const dir = await makeTempDir();
|
|
68
|
-
await writeFile(join(dir, ".env"), "TOKEN=x", "utf8");
|
|
69
|
-
|
|
70
|
-
const result = await listDirForTool(dir);
|
|
71
|
-
|
|
72
|
-
expect(result.entries).toContainEqual(expect.objectContaining({
|
|
73
|
-
name: ".env",
|
|
74
|
-
type: "file",
|
|
75
|
-
}));
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it("searches code with rg without using a shell", async () => {
|
|
79
|
-
if (!await hasRg()) return;
|
|
80
|
-
|
|
81
|
-
const dir = await makeTempDir();
|
|
82
|
-
await writeFile(join(dir, "a.ts"), "const marker = 1;\n", "utf8");
|
|
83
|
-
|
|
84
|
-
const result = await searchCodeForTool(dir, { query: "marker", glob: "*.ts" });
|
|
85
|
-
|
|
86
|
-
expect(result.matches).toEqual([
|
|
87
|
-
expect.objectContaining({
|
|
88
|
-
line: 1,
|
|
89
|
-
text: "const marker = 1;",
|
|
90
|
-
}),
|
|
91
|
-
]);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it("runs non-interactive shell commands in the requested cwd", async () => {
|
|
95
|
-
const dir = await makeTempDir();
|
|
96
|
-
|
|
97
|
-
const result = await runCommandForTool(dir, {
|
|
98
|
-
command: "node -e \"process.stdout.write(process.cwd())\"",
|
|
99
|
-
timeoutMs: 5_000,
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
expect(result.exitCode).toBe(0);
|
|
103
|
-
expect(result.timedOut).toBe(false);
|
|
104
|
-
expect(result.stdout.toLowerCase()).toBe(dir.toLowerCase());
|
|
105
|
-
expect(result.stderr).toBe("");
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it("returns non-zero command exits without throwing", async () => {
|
|
109
|
-
const dir = await makeTempDir();
|
|
110
|
-
|
|
111
|
-
const result = await runCommandForTool(dir, {
|
|
112
|
-
command: "node -e \"process.stderr.write('failed'); process.exit(7)\"",
|
|
113
|
-
timeoutMs: 5_000,
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
expect(result.exitCode).toBe(7);
|
|
117
|
-
expect(result.stderr).toBe("failed");
|
|
118
|
-
expect(result.timedOut).toBe(false);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it("edits a file with exact replacements and a SHA-256 precondition", async () => {
|
|
122
|
-
const dir = await makeTempDir();
|
|
123
|
-
const file = join(dir, "edit.txt");
|
|
124
|
-
await writeFile(file, "alpha\nbeta\ngamma\n", "utf8");
|
|
125
|
-
|
|
126
|
-
const result = await editFileForTool(dir, {
|
|
127
|
-
path: "edit.txt",
|
|
128
|
-
expectedSha256: sha256("alpha\nbeta\ngamma\n"),
|
|
129
|
-
edits: [{ oldText: "beta", newText: "BETA" }],
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
expect(result).toEqual(expect.objectContaining({
|
|
133
|
-
changed: true,
|
|
134
|
-
editsApplied: 1,
|
|
135
|
-
beforeSha256: sha256("alpha\nbeta\ngamma\n"),
|
|
136
|
-
afterSha256: sha256("alpha\nBETA\ngamma\n"),
|
|
137
|
-
}));
|
|
138
|
-
await expect(readFile(file, "utf8")).resolves.toBe("alpha\nBETA\ngamma\n");
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it("rejects edits when the SHA-256 precondition does not match", async () => {
|
|
142
|
-
const dir = await makeTempDir();
|
|
143
|
-
await writeFile(join(dir, "edit.txt"), "current\n", "utf8");
|
|
144
|
-
|
|
145
|
-
await expect(editFileForTool(dir, {
|
|
146
|
-
path: "edit.txt",
|
|
147
|
-
expectedSha256: sha256("stale\n"),
|
|
148
|
-
edits: [{ oldText: "current", newText: "next" }],
|
|
149
|
-
})).rejects.toThrow("SHA-256 mismatch");
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it("creates and deletes files", async () => {
|
|
153
|
-
const dir = await makeTempDir();
|
|
154
|
-
|
|
155
|
-
const created = await createFileForTool(dir, {
|
|
156
|
-
path: "created.txt",
|
|
157
|
-
content: "created\n",
|
|
158
|
-
});
|
|
159
|
-
expect(created).toEqual(expect.objectContaining({
|
|
160
|
-
changed: true,
|
|
161
|
-
afterSha256: sha256("created\n"),
|
|
162
|
-
}));
|
|
163
|
-
await expect(readFile(join(dir, "created.txt"), "utf8")).resolves.toBe("created\n");
|
|
164
|
-
|
|
165
|
-
const deleted = await deleteFileForTool(dir, {
|
|
166
|
-
path: "created.txt",
|
|
167
|
-
expectedSha256: sha256("created\n"),
|
|
168
|
-
});
|
|
169
|
-
expect(deleted).toEqual(expect.objectContaining({
|
|
170
|
-
deleted: true,
|
|
171
|
-
beforeSha256: sha256("created\n"),
|
|
172
|
-
}));
|
|
173
|
-
await expect(stat(join(dir, "created.txt"))).rejects.toThrow();
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it("moves files and creates the destination directory", async () => {
|
|
177
|
-
const dir = await makeTempDir();
|
|
178
|
-
await writeFile(join(dir, "old.txt"), "move me\n", "utf8");
|
|
179
|
-
|
|
180
|
-
const result = await moveFileForTool(dir, {
|
|
181
|
-
sourcePath: "old.txt",
|
|
182
|
-
destinationPath: "nested/new.txt",
|
|
183
|
-
expectedSourceSha256: sha256("move me\n"),
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
expect(result).toEqual(expect.objectContaining({
|
|
187
|
-
moved: true,
|
|
188
|
-
sourceSha256: sha256("move me\n"),
|
|
189
|
-
}));
|
|
190
|
-
await expect(stat(join(dir, "old.txt"))).rejects.toThrow();
|
|
191
|
-
await expect(readFile(join(dir, "nested", "new.txt"), "utf8")).resolves.toBe("move me\n");
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it("applies a unified diff patch", async () => {
|
|
195
|
-
const dir = await makeTempDir();
|
|
196
|
-
await writeFile(join(dir, "patch.txt"), "one\ntwo\nthree\n", "utf8");
|
|
197
|
-
|
|
198
|
-
const result = await applyPatchForTool(dir, {
|
|
199
|
-
patch: [
|
|
200
|
-
"--- a/patch.txt",
|
|
201
|
-
"+++ b/patch.txt",
|
|
202
|
-
"@@ -1,4 +1,4 @@",
|
|
203
|
-
" one",
|
|
204
|
-
"-two",
|
|
205
|
-
"+TWO",
|
|
206
|
-
" three",
|
|
207
|
-
" ",
|
|
208
|
-
"",
|
|
209
|
-
].join("\n"),
|
|
210
|
-
expectedSha256ByPath: {
|
|
211
|
-
"patch.txt": sha256("one\ntwo\nthree\n"),
|
|
212
|
-
},
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
expect(result.changedFiles).toEqual([
|
|
216
|
-
expect.objectContaining({
|
|
217
|
-
action: "edit",
|
|
218
|
-
beforeSha256: sha256("one\ntwo\nthree\n"),
|
|
219
|
-
afterSha256: sha256("one\nTWO\nthree\n"),
|
|
220
|
-
}),
|
|
221
|
-
]);
|
|
222
|
-
await expect(readFile(join(dir, "patch.txt"), "utf8")).resolves.toBe("one\nTWO\nthree\n");
|
|
223
|
-
});
|
|
224
|
-
});
|
|
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 { 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
|
+
listDirForTool,
|
|
16
|
+
moveFileForTool,
|
|
17
|
+
readFileForTool,
|
|
18
|
+
runCommandForTool,
|
|
19
|
+
searchCodeForTool,
|
|
20
|
+
} from "../builtin/file-tools.ts";
|
|
21
|
+
|
|
22
|
+
const execFileAsync = promisify(execFile);
|
|
23
|
+
const tempDirs: string[] = [];
|
|
24
|
+
|
|
25
|
+
async function makeTempDir(): Promise<string> {
|
|
26
|
+
const dir = await mkdtemp(join(tmpdir(), "chatccc-builtin-tools-"));
|
|
27
|
+
tempDirs.push(dir);
|
|
28
|
+
return dir;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function hasRg(): Promise<boolean> {
|
|
32
|
+
try {
|
|
33
|
+
await execFileAsync("rg", ["--version"]);
|
|
34
|
+
return true;
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function sha256(text: string): string {
|
|
41
|
+
return createHash("sha256").update(text).digest("hex");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
afterEach(async () => {
|
|
45
|
+
await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("builtin file tools", () => {
|
|
49
|
+
it("reads a text file with line ranges", async () => {
|
|
50
|
+
const dir = await makeTempDir();
|
|
51
|
+
await writeFile(join(dir, ".secret.txt"), "one\ntwo\nthree\n", "utf8");
|
|
52
|
+
|
|
53
|
+
const result = await readFileForTool(dir, { path: ".secret.txt", startLine: 2, endLine: 3 });
|
|
54
|
+
|
|
55
|
+
expect(result).toEqual(expect.objectContaining({
|
|
56
|
+
sha256: sha256("one\ntwo\nthree\n"),
|
|
57
|
+
isBinary: false,
|
|
58
|
+
content: "two\nthree",
|
|
59
|
+
startLine: 2,
|
|
60
|
+
endLine: 3,
|
|
61
|
+
totalLines: 4,
|
|
62
|
+
}));
|
|
63
|
+
expect(result.path).toContain(".secret.txt");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("lists directory entries including hidden files", async () => {
|
|
67
|
+
const dir = await makeTempDir();
|
|
68
|
+
await writeFile(join(dir, ".env"), "TOKEN=x", "utf8");
|
|
69
|
+
|
|
70
|
+
const result = await listDirForTool(dir);
|
|
71
|
+
|
|
72
|
+
expect(result.entries).toContainEqual(expect.objectContaining({
|
|
73
|
+
name: ".env",
|
|
74
|
+
type: "file",
|
|
75
|
+
}));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("searches code with rg without using a shell", async () => {
|
|
79
|
+
if (!await hasRg()) return;
|
|
80
|
+
|
|
81
|
+
const dir = await makeTempDir();
|
|
82
|
+
await writeFile(join(dir, "a.ts"), "const marker = 1;\n", "utf8");
|
|
83
|
+
|
|
84
|
+
const result = await searchCodeForTool(dir, { query: "marker", glob: "*.ts" });
|
|
85
|
+
|
|
86
|
+
expect(result.matches).toEqual([
|
|
87
|
+
expect.objectContaining({
|
|
88
|
+
line: 1,
|
|
89
|
+
text: "const marker = 1;",
|
|
90
|
+
}),
|
|
91
|
+
]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("runs non-interactive shell commands in the requested cwd", async () => {
|
|
95
|
+
const dir = await makeTempDir();
|
|
96
|
+
|
|
97
|
+
const result = await runCommandForTool(dir, {
|
|
98
|
+
command: "node -e \"process.stdout.write(process.cwd())\"",
|
|
99
|
+
timeoutMs: 5_000,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
expect(result.exitCode).toBe(0);
|
|
103
|
+
expect(result.timedOut).toBe(false);
|
|
104
|
+
expect(result.stdout.toLowerCase()).toBe(dir.toLowerCase());
|
|
105
|
+
expect(result.stderr).toBe("");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("returns non-zero command exits without throwing", async () => {
|
|
109
|
+
const dir = await makeTempDir();
|
|
110
|
+
|
|
111
|
+
const result = await runCommandForTool(dir, {
|
|
112
|
+
command: "node -e \"process.stderr.write('failed'); process.exit(7)\"",
|
|
113
|
+
timeoutMs: 5_000,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
expect(result.exitCode).toBe(7);
|
|
117
|
+
expect(result.stderr).toBe("failed");
|
|
118
|
+
expect(result.timedOut).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("edits a file with exact replacements and a SHA-256 precondition", async () => {
|
|
122
|
+
const dir = await makeTempDir();
|
|
123
|
+
const file = join(dir, "edit.txt");
|
|
124
|
+
await writeFile(file, "alpha\nbeta\ngamma\n", "utf8");
|
|
125
|
+
|
|
126
|
+
const result = await editFileForTool(dir, {
|
|
127
|
+
path: "edit.txt",
|
|
128
|
+
expectedSha256: sha256("alpha\nbeta\ngamma\n"),
|
|
129
|
+
edits: [{ oldText: "beta", newText: "BETA" }],
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
expect(result).toEqual(expect.objectContaining({
|
|
133
|
+
changed: true,
|
|
134
|
+
editsApplied: 1,
|
|
135
|
+
beforeSha256: sha256("alpha\nbeta\ngamma\n"),
|
|
136
|
+
afterSha256: sha256("alpha\nBETA\ngamma\n"),
|
|
137
|
+
}));
|
|
138
|
+
await expect(readFile(file, "utf8")).resolves.toBe("alpha\nBETA\ngamma\n");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("rejects edits when the SHA-256 precondition does not match", async () => {
|
|
142
|
+
const dir = await makeTempDir();
|
|
143
|
+
await writeFile(join(dir, "edit.txt"), "current\n", "utf8");
|
|
144
|
+
|
|
145
|
+
await expect(editFileForTool(dir, {
|
|
146
|
+
path: "edit.txt",
|
|
147
|
+
expectedSha256: sha256("stale\n"),
|
|
148
|
+
edits: [{ oldText: "current", newText: "next" }],
|
|
149
|
+
})).rejects.toThrow("SHA-256 mismatch");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("creates and deletes files", async () => {
|
|
153
|
+
const dir = await makeTempDir();
|
|
154
|
+
|
|
155
|
+
const created = await createFileForTool(dir, {
|
|
156
|
+
path: "created.txt",
|
|
157
|
+
content: "created\n",
|
|
158
|
+
});
|
|
159
|
+
expect(created).toEqual(expect.objectContaining({
|
|
160
|
+
changed: true,
|
|
161
|
+
afterSha256: sha256("created\n"),
|
|
162
|
+
}));
|
|
163
|
+
await expect(readFile(join(dir, "created.txt"), "utf8")).resolves.toBe("created\n");
|
|
164
|
+
|
|
165
|
+
const deleted = await deleteFileForTool(dir, {
|
|
166
|
+
path: "created.txt",
|
|
167
|
+
expectedSha256: sha256("created\n"),
|
|
168
|
+
});
|
|
169
|
+
expect(deleted).toEqual(expect.objectContaining({
|
|
170
|
+
deleted: true,
|
|
171
|
+
beforeSha256: sha256("created\n"),
|
|
172
|
+
}));
|
|
173
|
+
await expect(stat(join(dir, "created.txt"))).rejects.toThrow();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("moves files and creates the destination directory", async () => {
|
|
177
|
+
const dir = await makeTempDir();
|
|
178
|
+
await writeFile(join(dir, "old.txt"), "move me\n", "utf8");
|
|
179
|
+
|
|
180
|
+
const result = await moveFileForTool(dir, {
|
|
181
|
+
sourcePath: "old.txt",
|
|
182
|
+
destinationPath: "nested/new.txt",
|
|
183
|
+
expectedSourceSha256: sha256("move me\n"),
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
expect(result).toEqual(expect.objectContaining({
|
|
187
|
+
moved: true,
|
|
188
|
+
sourceSha256: sha256("move me\n"),
|
|
189
|
+
}));
|
|
190
|
+
await expect(stat(join(dir, "old.txt"))).rejects.toThrow();
|
|
191
|
+
await expect(readFile(join(dir, "nested", "new.txt"), "utf8")).resolves.toBe("move me\n");
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("applies a unified diff patch", async () => {
|
|
195
|
+
const dir = await makeTempDir();
|
|
196
|
+
await writeFile(join(dir, "patch.txt"), "one\ntwo\nthree\n", "utf8");
|
|
197
|
+
|
|
198
|
+
const result = await applyPatchForTool(dir, {
|
|
199
|
+
patch: [
|
|
200
|
+
"--- a/patch.txt",
|
|
201
|
+
"+++ b/patch.txt",
|
|
202
|
+
"@@ -1,4 +1,4 @@",
|
|
203
|
+
" one",
|
|
204
|
+
"-two",
|
|
205
|
+
"+TWO",
|
|
206
|
+
" three",
|
|
207
|
+
" ",
|
|
208
|
+
"",
|
|
209
|
+
].join("\n"),
|
|
210
|
+
expectedSha256ByPath: {
|
|
211
|
+
"patch.txt": sha256("one\ntwo\nthree\n"),
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
expect(result.changedFiles).toEqual([
|
|
216
|
+
expect.objectContaining({
|
|
217
|
+
action: "edit",
|
|
218
|
+
beforeSha256: sha256("one\ntwo\nthree\n"),
|
|
219
|
+
afterSha256: sha256("one\nTWO\nthree\n"),
|
|
220
|
+
}),
|
|
221
|
+
]);
|
|
222
|
+
await expect(readFile(join(dir, "patch.txt"), "utf8")).resolves.toBe("one\nTWO\nthree\n");
|
|
223
|
+
});
|
|
224
|
+
});
|