chatccc 0.2.196 → 0.2.198

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 (53) hide show
  1. package/agent-prompts/cursor_specific.md +13 -13
  2. package/bin/cccagent.mjs +17 -17
  3. package/config.sample.json +27 -27
  4. package/package.json +2 -1
  5. package/src/__tests__/agent-reload-config-rpc.test.ts +99 -0
  6. package/src/__tests__/builtin-chat-session.test.ts +277 -181
  7. package/src/__tests__/builtin-cli-json.test.ts +39 -39
  8. package/src/__tests__/builtin-config.test.ts +33 -33
  9. package/src/__tests__/builtin-context.test.ts +163 -163
  10. package/src/__tests__/builtin-file-tools.test.ts +224 -196
  11. package/src/__tests__/builtin-session-select.test.ts +116 -116
  12. package/src/__tests__/builtin-sigint.test.ts +56 -56
  13. package/src/__tests__/cards.test.ts +109 -109
  14. package/src/__tests__/ccc-adapter.test.ts +114 -113
  15. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  16. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  17. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  18. package/src/__tests__/claude-raw-stream-log.test.ts +87 -0
  19. package/src/__tests__/codex-raw-stream-log.test.ts +163 -0
  20. package/src/__tests__/config-reload.test.ts +10 -10
  21. package/src/__tests__/config-sample.test.ts +18 -18
  22. package/src/__tests__/cursor-adapter.test.ts +167 -113
  23. package/src/__tests__/feishu-avatar.test.ts +40 -40
  24. package/src/__tests__/jsonl-stream.test.ts +79 -0
  25. package/src/__tests__/orchestrator.test.ts +181 -154
  26. package/src/__tests__/raw-stream-log.test.ts +106 -106
  27. package/src/__tests__/session.test.ts +40 -40
  28. package/src/__tests__/sim-platform.test.ts +12 -12
  29. package/src/__tests__/web-ui.test.ts +209 -209
  30. package/src/adapters/ccc-adapter.ts +121 -119
  31. package/src/adapters/claude-adapter.ts +603 -566
  32. package/src/adapters/codex-adapter.ts +65 -52
  33. package/src/adapters/cursor-adapter.ts +269 -276
  34. package/src/adapters/jsonl-stream.ts +157 -0
  35. package/src/adapters/raw-stream-log.ts +124 -124
  36. package/src/agent-reload-config-rpc.ts +34 -0
  37. package/src/builtin/cli.ts +473 -461
  38. package/src/builtin/context.ts +323 -323
  39. package/src/builtin/file-tools.ts +1072 -915
  40. package/src/builtin/index.ts +404 -353
  41. package/src/builtin/session-select.ts +48 -48
  42. package/src/builtin/sigint.ts +50 -50
  43. package/src/cards.ts +195 -195
  44. package/src/chatgpt-subscription-rpc.ts +27 -27
  45. package/src/chatgpt-subscription.ts +299 -299
  46. package/src/chrome-devtools-guard.ts +318 -318
  47. package/src/config.ts +125 -125
  48. package/src/feishu-api.ts +49 -49
  49. package/src/index.ts +8 -13
  50. package/src/orchestrator.ts +166 -145
  51. package/src/runtime-reload.ts +34 -0
  52. package/src/session.ts +141 -141
  53. package/src/web-ui.ts +205 -205
@@ -1,196 +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
- searchCodeForTool,
19
- } from "../builtin/file-tools.ts";
20
-
21
- const execFileAsync = promisify(execFile);
22
- const tempDirs: string[] = [];
23
-
24
- async function makeTempDir(): Promise<string> {
25
- const dir = await mkdtemp(join(tmpdir(), "chatccc-builtin-tools-"));
26
- tempDirs.push(dir);
27
- return dir;
28
- }
29
-
30
- async function hasRg(): Promise<boolean> {
31
- try {
32
- await execFileAsync("rg", ["--version"]);
33
- return true;
34
- } catch {
35
- return false;
36
- }
37
- }
38
-
39
- function sha256(text: string): string {
40
- return createHash("sha256").update(text).digest("hex");
41
- }
42
-
43
- afterEach(async () => {
44
- await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
45
- });
46
-
47
- describe("builtin file tools", () => {
48
- it("reads a text file with line ranges", async () => {
49
- const dir = await makeTempDir();
50
- await writeFile(join(dir, ".secret.txt"), "one\ntwo\nthree\n", "utf8");
51
-
52
- const result = await readFileForTool(dir, { path: ".secret.txt", startLine: 2, endLine: 3 });
53
-
54
- expect(result).toEqual(expect.objectContaining({
55
- sha256: sha256("one\ntwo\nthree\n"),
56
- isBinary: false,
57
- content: "two\nthree",
58
- startLine: 2,
59
- endLine: 3,
60
- totalLines: 4,
61
- }));
62
- expect(result.path).toContain(".secret.txt");
63
- });
64
-
65
- it("lists directory entries including hidden files", async () => {
66
- const dir = await makeTempDir();
67
- await writeFile(join(dir, ".env"), "TOKEN=x", "utf8");
68
-
69
- const result = await listDirForTool(dir);
70
-
71
- expect(result.entries).toContainEqual(expect.objectContaining({
72
- name: ".env",
73
- type: "file",
74
- }));
75
- });
76
-
77
- it("searches code with rg without using a shell", async () => {
78
- if (!await hasRg()) return;
79
-
80
- const dir = await makeTempDir();
81
- await writeFile(join(dir, "a.ts"), "const marker = 1;\n", "utf8");
82
-
83
- const result = await searchCodeForTool(dir, { query: "marker", glob: "*.ts" });
84
-
85
- expect(result.matches).toEqual([
86
- expect.objectContaining({
87
- line: 1,
88
- text: "const marker = 1;",
89
- }),
90
- ]);
91
- });
92
-
93
- it("edits a file with exact replacements and a SHA-256 precondition", async () => {
94
- const dir = await makeTempDir();
95
- const file = join(dir, "edit.txt");
96
- await writeFile(file, "alpha\nbeta\ngamma\n", "utf8");
97
-
98
- const result = await editFileForTool(dir, {
99
- path: "edit.txt",
100
- expectedSha256: sha256("alpha\nbeta\ngamma\n"),
101
- edits: [{ oldText: "beta", newText: "BETA" }],
102
- });
103
-
104
- expect(result).toEqual(expect.objectContaining({
105
- changed: true,
106
- editsApplied: 1,
107
- beforeSha256: sha256("alpha\nbeta\ngamma\n"),
108
- afterSha256: sha256("alpha\nBETA\ngamma\n"),
109
- }));
110
- await expect(readFile(file, "utf8")).resolves.toBe("alpha\nBETA\ngamma\n");
111
- });
112
-
113
- it("rejects edits when the SHA-256 precondition does not match", async () => {
114
- const dir = await makeTempDir();
115
- await writeFile(join(dir, "edit.txt"), "current\n", "utf8");
116
-
117
- await expect(editFileForTool(dir, {
118
- path: "edit.txt",
119
- expectedSha256: sha256("stale\n"),
120
- edits: [{ oldText: "current", newText: "next" }],
121
- })).rejects.toThrow("SHA-256 mismatch");
122
- });
123
-
124
- it("creates and deletes files", async () => {
125
- const dir = await makeTempDir();
126
-
127
- const created = await createFileForTool(dir, {
128
- path: "created.txt",
129
- content: "created\n",
130
- });
131
- expect(created).toEqual(expect.objectContaining({
132
- changed: true,
133
- afterSha256: sha256("created\n"),
134
- }));
135
- await expect(readFile(join(dir, "created.txt"), "utf8")).resolves.toBe("created\n");
136
-
137
- const deleted = await deleteFileForTool(dir, {
138
- path: "created.txt",
139
- expectedSha256: sha256("created\n"),
140
- });
141
- expect(deleted).toEqual(expect.objectContaining({
142
- deleted: true,
143
- beforeSha256: sha256("created\n"),
144
- }));
145
- await expect(stat(join(dir, "created.txt"))).rejects.toThrow();
146
- });
147
-
148
- it("moves files and creates the destination directory", async () => {
149
- const dir = await makeTempDir();
150
- await writeFile(join(dir, "old.txt"), "move me\n", "utf8");
151
-
152
- const result = await moveFileForTool(dir, {
153
- sourcePath: "old.txt",
154
- destinationPath: "nested/new.txt",
155
- expectedSourceSha256: sha256("move me\n"),
156
- });
157
-
158
- expect(result).toEqual(expect.objectContaining({
159
- moved: true,
160
- sourceSha256: sha256("move me\n"),
161
- }));
162
- await expect(stat(join(dir, "old.txt"))).rejects.toThrow();
163
- await expect(readFile(join(dir, "nested", "new.txt"), "utf8")).resolves.toBe("move me\n");
164
- });
165
-
166
- it("applies a unified diff patch", async () => {
167
- const dir = await makeTempDir();
168
- await writeFile(join(dir, "patch.txt"), "one\ntwo\nthree\n", "utf8");
169
-
170
- const result = await applyPatchForTool(dir, {
171
- patch: [
172
- "--- a/patch.txt",
173
- "+++ b/patch.txt",
174
- "@@ -1,4 +1,4 @@",
175
- " one",
176
- "-two",
177
- "+TWO",
178
- " three",
179
- " ",
180
- "",
181
- ].join("\n"),
182
- expectedSha256ByPath: {
183
- "patch.txt": sha256("one\ntwo\nthree\n"),
184
- },
185
- });
186
-
187
- expect(result.changedFiles).toEqual([
188
- expect.objectContaining({
189
- action: "edit",
190
- beforeSha256: sha256("one\ntwo\nthree\n"),
191
- afterSha256: sha256("one\nTWO\nthree\n"),
192
- }),
193
- ]);
194
- await expect(readFile(join(dir, "patch.txt"), "utf8")).resolves.toBe("one\nTWO\nthree\n");
195
- });
196
- });
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,116 +1,116 @@
1
- import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
2
- import { tmpdir } from "node:os";
3
- import { join } from "node:path";
4
-
5
- import { describe, expect, it } from "vitest";
6
-
7
- import { defaultBuiltinSessionId } from "../builtin/context.ts";
8
- import { resolveBuiltinSession } from "../builtin/session-select.ts";
9
-
10
- async function writeSession(
11
- contextDir: string,
12
- sessionId: string,
13
- options: { cwd?: string; updatedAt: number; totalMessages?: number },
14
- ): Promise<void> {
15
- const dir = join(contextDir, sessionId);
16
- await mkdir(dir, { recursive: true });
17
- await writeFile(
18
- join(dir, "context.json"),
19
- JSON.stringify({
20
- version: 1,
21
- createdAt: options.updatedAt,
22
- updatedAt: options.updatedAt,
23
- sessionId,
24
- ...(options.cwd ? { cwd: options.cwd } : {}),
25
- summary: "",
26
- messages: [],
27
- totalMessages: options.totalMessages ?? 0,
28
- compactedMessages: 0,
29
- }),
30
- "utf8",
31
- );
32
- }
33
-
34
- describe("resolveBuiltinSession", () => {
35
- it("creates a fresh timestamp session by default", async () => {
36
- const contextDir = await mkdtemp(join(tmpdir(), "chatccc-session-select-new-"));
37
-
38
- const result = resolveBuiltinSession({
39
- cwd: "C:\\repo",
40
- contextDir,
41
- now: new Date(2026, 6, 2, 12, 15, 30),
42
- randomSuffix: "a1b2c3",
43
- });
44
-
45
- expect(result).toEqual({
46
- mode: "new",
47
- sessionId: "session-20260702-121530-a1b2c3",
48
- });
49
- });
50
-
51
- it("resumes an explicit existing session id", async () => {
52
- const contextDir = join(tmpdir(), `chatccc-session-select-explicit-${Date.now()}`);
53
- await writeSession(contextDir, "manual-session", { updatedAt: 1 });
54
-
55
- expect(resolveBuiltinSession({
56
- cwd: "C:\\repo",
57
- contextDir,
58
- resume: "manual-session",
59
- })).toEqual({
60
- mode: "resumed",
61
- sessionId: "manual-session",
62
- });
63
- });
64
-
65
- it("fails when an explicit session id does not exist", async () => {
66
- const contextDir = join(tmpdir(), `chatccc-session-select-missing-${Date.now()}`);
67
-
68
- expect(() => resolveBuiltinSession({
69
- cwd: "C:\\repo",
70
- contextDir,
71
- resume: "missing",
72
- })).toThrow("未找到 ccc 会话: missing");
73
- });
74
-
75
- it("resumes the newest session for cwd when resume has no id", async () => {
76
- const contextDir = join(tmpdir(), `chatccc-session-select-cwd-${Date.now()}`);
77
- await writeSession(contextDir, "old-match", { cwd: "C:\\repo", updatedAt: 1_000 });
78
- await writeSession(contextDir, "new-match", { cwd: "C:\\repo", updatedAt: 2_000 });
79
- await writeSession(contextDir, "other-cwd", { cwd: "C:\\other", updatedAt: 3_000 });
80
-
81
- expect(resolveBuiltinSession({
82
- cwd: "C:\\repo",
83
- contextDir,
84
- resume: true,
85
- })).toEqual({
86
- mode: "resumed",
87
- sessionId: "new-match",
88
- });
89
- });
90
-
91
- it("resumes legacy cwd-hash sessions when resume has no id", async () => {
92
- const contextDir = join(tmpdir(), `chatccc-session-select-legacy-${Date.now()}`);
93
- const cwd = "C:\\repo";
94
- const legacySessionId = defaultBuiltinSessionId(cwd);
95
- await writeSession(contextDir, legacySessionId, { updatedAt: 1_000 });
96
-
97
- expect(resolveBuiltinSession({
98
- cwd,
99
- contextDir,
100
- resume: true,
101
- })).toEqual({
102
- mode: "resumed",
103
- sessionId: legacySessionId,
104
- });
105
- });
106
-
107
- it("fails when there is no cwd session to resume", async () => {
108
- const contextDir = join(tmpdir(), `chatccc-session-select-empty-${Date.now()}`);
109
-
110
- expect(() => resolveBuiltinSession({
111
- cwd: "C:\\repo",
112
- contextDir,
113
- resume: true,
114
- })).toThrow("未找到当前目录可恢复的 ccc 会话");
115
- });
116
- });
1
+ import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+
5
+ import { describe, expect, it } from "vitest";
6
+
7
+ import { defaultBuiltinSessionId } from "../builtin/context.ts";
8
+ import { resolveBuiltinSession } from "../builtin/session-select.ts";
9
+
10
+ async function writeSession(
11
+ contextDir: string,
12
+ sessionId: string,
13
+ options: { cwd?: string; updatedAt: number; totalMessages?: number },
14
+ ): Promise<void> {
15
+ const dir = join(contextDir, sessionId);
16
+ await mkdir(dir, { recursive: true });
17
+ await writeFile(
18
+ join(dir, "context.json"),
19
+ JSON.stringify({
20
+ version: 1,
21
+ createdAt: options.updatedAt,
22
+ updatedAt: options.updatedAt,
23
+ sessionId,
24
+ ...(options.cwd ? { cwd: options.cwd } : {}),
25
+ summary: "",
26
+ messages: [],
27
+ totalMessages: options.totalMessages ?? 0,
28
+ compactedMessages: 0,
29
+ }),
30
+ "utf8",
31
+ );
32
+ }
33
+
34
+ describe("resolveBuiltinSession", () => {
35
+ it("creates a fresh timestamp session by default", async () => {
36
+ const contextDir = await mkdtemp(join(tmpdir(), "chatccc-session-select-new-"));
37
+
38
+ const result = resolveBuiltinSession({
39
+ cwd: "C:\\repo",
40
+ contextDir,
41
+ now: new Date(2026, 6, 2, 12, 15, 30),
42
+ randomSuffix: "a1b2c3",
43
+ });
44
+
45
+ expect(result).toEqual({
46
+ mode: "new",
47
+ sessionId: "session-20260702-121530-a1b2c3",
48
+ });
49
+ });
50
+
51
+ it("resumes an explicit existing session id", async () => {
52
+ const contextDir = join(tmpdir(), `chatccc-session-select-explicit-${Date.now()}`);
53
+ await writeSession(contextDir, "manual-session", { updatedAt: 1 });
54
+
55
+ expect(resolveBuiltinSession({
56
+ cwd: "C:\\repo",
57
+ contextDir,
58
+ resume: "manual-session",
59
+ })).toEqual({
60
+ mode: "resumed",
61
+ sessionId: "manual-session",
62
+ });
63
+ });
64
+
65
+ it("fails when an explicit session id does not exist", async () => {
66
+ const contextDir = join(tmpdir(), `chatccc-session-select-missing-${Date.now()}`);
67
+
68
+ expect(() => resolveBuiltinSession({
69
+ cwd: "C:\\repo",
70
+ contextDir,
71
+ resume: "missing",
72
+ })).toThrow("未找到 ccc 会话: missing");
73
+ });
74
+
75
+ it("resumes the newest session for cwd when resume has no id", async () => {
76
+ const contextDir = join(tmpdir(), `chatccc-session-select-cwd-${Date.now()}`);
77
+ await writeSession(contextDir, "old-match", { cwd: "C:\\repo", updatedAt: 1_000 });
78
+ await writeSession(contextDir, "new-match", { cwd: "C:\\repo", updatedAt: 2_000 });
79
+ await writeSession(contextDir, "other-cwd", { cwd: "C:\\other", updatedAt: 3_000 });
80
+
81
+ expect(resolveBuiltinSession({
82
+ cwd: "C:\\repo",
83
+ contextDir,
84
+ resume: true,
85
+ })).toEqual({
86
+ mode: "resumed",
87
+ sessionId: "new-match",
88
+ });
89
+ });
90
+
91
+ it("resumes legacy cwd-hash sessions when resume has no id", async () => {
92
+ const contextDir = join(tmpdir(), `chatccc-session-select-legacy-${Date.now()}`);
93
+ const cwd = "C:\\repo";
94
+ const legacySessionId = defaultBuiltinSessionId(cwd);
95
+ await writeSession(contextDir, legacySessionId, { updatedAt: 1_000 });
96
+
97
+ expect(resolveBuiltinSession({
98
+ cwd,
99
+ contextDir,
100
+ resume: true,
101
+ })).toEqual({
102
+ mode: "resumed",
103
+ sessionId: legacySessionId,
104
+ });
105
+ });
106
+
107
+ it("fails when there is no cwd session to resume", async () => {
108
+ const contextDir = join(tmpdir(), `chatccc-session-select-empty-${Date.now()}`);
109
+
110
+ expect(() => resolveBuiltinSession({
111
+ cwd: "C:\\repo",
112
+ contextDir,
113
+ resume: true,
114
+ })).toThrow("未找到当前目录可恢复的 ccc 会话");
115
+ });
116
+ });