chatccc 0.2.235 → 0.2.236

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,177 +1,232 @@
1
- import { EventEmitter } from "node:events";
2
- import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
-
6
- import { describe, expect, it, vi, afterEach } from "vitest";
7
-
8
- import {
9
- buildRestartSpawnSpec,
10
- decideRestartParentExit,
11
- spawnRestartChild,
12
- RESTART_CHILD_READY_MS,
13
- } from "../orchestrator.ts";
14
- import { INTERNAL_RESTART_ENV_VAR } from "../startup-lifecycle.ts";
15
-
16
- describe("buildRestartSpawnSpec", () => {
17
- it("spawns node directly with the local tsx CLI (never via npx/npm)", () => {
18
- const spec = buildRestartSpawnSpec("F:/proj");
19
- expect(spec.command).toBe(process.execPath);
20
- expect(spec.args[0]).toBe(join("F:/proj", "node_modules", "tsx", "dist", "cli.mjs"));
21
- expect(spec.args[1]).toBe("src/index.ts");
22
- expect([spec.command, ...spec.args].join(" ")).not.toMatch(/npx|npm/i);
23
- });
24
-
25
- it("accepts the default project root", () => {
26
- const spec = buildRestartSpawnSpec();
27
- expect(spec.command).toBe(process.execPath);
28
- expect(spec.args[0]).toContain("tsx");
29
- });
30
- });
31
-
32
- describe("spawnRestartChild", () => {
33
- class FakeChild extends EventEmitter {
34
- pid = 12345;
35
- exitCode: number | null = null;
36
- signalCode: number | string | null = null;
37
- unref = vi.fn();
38
- stderr = new EventEmitter();
39
- }
40
-
41
- let tmpDirs: string[] = [];
42
- afterEach(async () => {
43
- for (const dir of tmpDirs) await rm(dir, { recursive: true, force: true });
44
- tmpDirs = [];
45
- });
46
-
47
- async function tmpLogDir(): Promise<string> {
48
- const dir = await mkdtemp(join(tmpdir(), "chatccc-restart-"));
49
- tmpDirs.push(dir);
50
- return dir;
51
- }
52
-
53
- it("spawns without a shell, redirects stderr to a restart log file, and marks the internal restart env", async () => {
54
- const logDir = await tmpLogDir();
55
- const fake = new FakeChild();
56
- const spawnImpl = vi.fn(() => fake as never);
57
- const trace = vi.fn();
58
-
59
- const child = spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace, restartLogDir: logDir });
60
-
61
- expect(child).toBe(fake);
62
- expect(spawnImpl).toHaveBeenCalledWith(
63
- process.execPath,
64
- [join("F:/proj", "node_modules", "tsx", "dist", "cli.mjs"), "src/index.ts"],
65
- expect.objectContaining({
66
- detached: true,
67
- env: expect.objectContaining({ [INTERNAL_RESTART_ENV_VAR]: "1" }),
68
- // 不使用 shell:避免 npm/npx 的 PATH 注入问题
69
- shell: false,
70
- }),
71
- );
72
- // spawnImpl 收到的 command/args 中不得出现 npx/npm(env 里的 npm_* 变量不算)
73
- const firstCall = spawnImpl.mock.calls[0] as unknown as [string, string[]];
74
- const [cmd, args] = firstCall;
75
- expect([cmd, ...args].join(" ")).not.toMatch(/npx|npm/i);
76
-
77
- // stderr 指向磁盘日志文件(fd),而不是 pipe:pipe 读端随父进程退出关闭后,
78
- // 子进程写 stderr 会 EPIPE 崩溃(飞书 SDK console.warn 崩溃根因)。
79
- const callArgs = spawnImpl.mock.calls[0] as unknown as Array<unknown>;
80
- const stdio = (callArgs[2] as { stdio: unknown[] }).stdio;
81
- expect(stdio[0]).toBe("ignore");
82
- expect(stdio[1]).toBe("ignore");
83
- expect(typeof stdio[2]).toBe("number");
84
- expect(stdio[2] as number).toBeGreaterThan(2);
85
-
86
- const files = await readdir(logDir);
87
- expect(files.some((f) => f.startsWith("restart-") && f.endsWith(".log"))).toBe(true);
88
- });
89
-
90
- it("records child exit code/signal in trace without pipe capture", async () => {
91
- const logDir = await tmpLogDir();
92
- const fake = new FakeChild();
93
- const spawnImpl = vi.fn(() => fake as never);
94
- const trace = vi.fn();
95
-
96
- spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace, restartLogDir: logDir });
97
-
98
- fake.exitCode = 1;
99
- fake.emit("exit", 1, null);
100
-
101
- const exitCall = trace.mock.calls.find(([name]) => name === "restart: child exit");
102
- expect(exitCall).toBeTruthy();
103
- expect(exitCall![1]).toEqual(expect.objectContaining({
104
- childPid: 12345,
105
- code: 1,
106
- signal: null,
107
- }));
108
- // 不再用 pipe 收集 stderr,trace 里不再有 stderr 字段
109
- expect(exitCall![1]).not.toHaveProperty("stderr");
110
- });
111
-
112
- it("falls back to pipe capture when the stderr log file cannot be opened", async () => {
113
- const dir = await tmpLogDir();
114
- // 用普通文件顶替目录:mkdir/open 日志文件必然失败
115
- const blocker = join(dir, "blocker");
116
- await writeFile(blocker, "x");
117
- const fake = new FakeChild();
118
- const spawnImpl = vi.fn(() => fake as never);
119
- const trace = vi.fn();
120
-
121
- spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace, restartLogDir: blocker });
122
-
123
- const callArgs = spawnImpl.mock.calls[0] as unknown as Array<unknown>;
124
- expect((callArgs[2] as { stdio: unknown[] }).stdio[2]).toBe("pipe");
125
- const failCall = trace.mock.calls.find(
126
- ([name]) => name === "restart: stderr log open failed, falling back to pipe",
127
- );
128
- expect(failCall).toBeTruthy();
129
- expect(typeof (failCall![1] as { error: unknown }).error).toBe("string");
130
- });
131
- });
132
-
133
- describe("decideRestartParentExit", () => {
134
- it("returns false (parent stays alive) when the child dies during the window", async () => {
135
- const child = { exitCode: 1, signalCode: null, pid: 42 } as never;
136
- const trace = vi.fn();
137
-
138
- const shouldExit = await decideRestartParentExit(child, 200, 50, trace);
139
-
140
- expect(shouldExit).toBe(false);
141
- expect(trace).toHaveBeenCalledWith(
142
- "restart: child died during window, keeping parent",
143
- expect.objectContaining({ childPid: 42, exitCode: 1 }),
144
- );
145
- });
146
-
147
- it("returns true (parent exits) when the child stays alive through the window", async () => {
148
- const child = { exitCode: null, signalCode: null, pid: 42 } as never;
149
- const trace = vi.fn();
150
-
151
- const shouldExit = await decideRestartParentExit(child, 150, 30, trace);
152
-
153
- expect(shouldExit).toBe(true);
154
- expect(trace).toHaveBeenCalledWith(
155
- "restart: child alive after window, parent exiting",
156
- expect.objectContaining({ childPid: 42 }),
157
- );
158
- });
159
-
160
- it("surfaces a child that died with a signal", async () => {
161
- const child = { exitCode: null, signalCode: "SIGKILL", pid: 42 } as never;
162
- const trace = vi.fn();
163
-
164
- const shouldExit = await decideRestartParentExit(child, 100, 25, trace);
165
-
166
- expect(shouldExit).toBe(false);
167
- expect(trace).toHaveBeenCalledWith(
168
- "restart: child died during window, keeping parent",
169
- expect.objectContaining({ signalCode: "SIGKILL" }),
170
- );
171
- });
172
-
173
- it("exports a sane default readiness window", () => {
174
- expect(RESTART_CHILD_READY_MS).toBeGreaterThan(0);
175
- expect(RESTART_CHILD_READY_MS).toBeLessThan(60_000);
176
- });
177
- });
1
+ import { EventEmitter } from "node:events";
2
+ import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+
6
+ import { describe, expect, it, vi, afterEach } from "vitest";
7
+
8
+ import {
9
+ buildRestartSpawnSpec,
10
+ decideRestartParentExit,
11
+ spawnRestartChild,
12
+ RESTART_CHILD_READY_MS,
13
+ } from "../orchestrator.ts";
14
+ import { INTERNAL_RESTART_ENV_VAR } from "../startup-lifecycle.ts";
15
+
16
+ describe("buildRestartSpawnSpec", () => {
17
+ it("spawns node directly with the local tsx CLI (never via npx/npm)", () => {
18
+ const spec = buildRestartSpawnSpec("F:/proj");
19
+ expect(spec.command).toBe(process.execPath);
20
+ expect(spec.args[0]).toBe(join("F:/proj", "node_modules", "tsx", "dist", "cli.mjs"));
21
+ expect(spec.args[1]).toBe("src/index.ts");
22
+ expect([spec.command, ...spec.args].join(" ")).not.toMatch(/npx|npm/i);
23
+ });
24
+
25
+ it("accepts the default project root", () => {
26
+ const spec = buildRestartSpawnSpec();
27
+ expect(spec.command).toBe(process.execPath);
28
+ expect(spec.args[0]).toContain("tsx");
29
+ });
30
+ });
31
+
32
+ describe("spawnRestartChild", () => {
33
+ class FakeChild extends EventEmitter {
34
+ pid = 12345;
35
+ exitCode: number | null = null;
36
+ signalCode: number | string | null = null;
37
+ unref = vi.fn();
38
+ stderr = new EventEmitter();
39
+ }
40
+
41
+ let tmpDirs: string[] = [];
42
+ afterEach(async () => {
43
+ for (const dir of tmpDirs) await rm(dir, { recursive: true, force: true });
44
+ tmpDirs = [];
45
+ });
46
+
47
+ async function tmpLogDir(): Promise<string> {
48
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-restart-"));
49
+ tmpDirs.push(dir);
50
+ return dir;
51
+ }
52
+
53
+ it("spawns without a shell, redirects stderr to a restart log file, and marks the internal restart env", async () => {
54
+ const logDir = await tmpLogDir();
55
+ const fake = new FakeChild();
56
+ const spawnImpl = vi.fn(() => fake as never);
57
+ const trace = vi.fn();
58
+
59
+ const child = spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace, restartLogDir: logDir });
60
+
61
+ expect(child).toBe(fake);
62
+ expect(spawnImpl).toHaveBeenCalledWith(
63
+ process.execPath,
64
+ [join("F:/proj", "node_modules", "tsx", "dist", "cli.mjs"), "src/index.ts"],
65
+ expect.objectContaining({
66
+ detached: true,
67
+ env: expect.objectContaining({ [INTERNAL_RESTART_ENV_VAR]: "1" }),
68
+ // 不使用 shell:避免 npm/npx 的 PATH 注入问题
69
+ shell: false,
70
+ }),
71
+ );
72
+ // spawnImpl 收到的 command/args 中不得出现 npx/npm(env 里的 npm_* 变量不算)
73
+ const firstCall = spawnImpl.mock.calls[0] as unknown as [string, string[]];
74
+ const [cmd, args] = firstCall;
75
+ expect([cmd, ...args].join(" ")).not.toMatch(/npx|npm/i);
76
+
77
+ // stderr 指向磁盘日志文件(fd),而不是 pipe:pipe 读端随父进程退出关闭后,
78
+ // 子进程写 stderr 会 EPIPE 崩溃(飞书 SDK console.warn 崩溃根因)。
79
+ const callArgs = spawnImpl.mock.calls[0] as unknown as Array<unknown>;
80
+ const stdio = (callArgs[2] as { stdio: unknown[] }).stdio;
81
+ expect(stdio[0]).toBe("ignore");
82
+ expect(stdio[1]).toBe("ignore");
83
+ expect(typeof stdio[2]).toBe("number");
84
+ expect(stdio[2] as number).toBeGreaterThan(2);
85
+
86
+ const files = await readdir(logDir);
87
+ expect(files.some((f) => f.startsWith("restart-") && f.endsWith(".log"))).toBe(true);
88
+ });
89
+
90
+ it("records child exit code/signal in trace without pipe capture", async () => {
91
+ const logDir = await tmpLogDir();
92
+ const fake = new FakeChild();
93
+ const spawnImpl = vi.fn(() => fake as never);
94
+ const trace = vi.fn();
95
+
96
+ spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace, restartLogDir: logDir });
97
+
98
+ fake.exitCode = 1;
99
+ fake.emit("exit", 1, null);
100
+
101
+ const exitCall = trace.mock.calls.find(([name]) => name === "restart: child exit");
102
+ expect(exitCall).toBeTruthy();
103
+ expect(exitCall![1]).toEqual(expect.objectContaining({
104
+ childPid: 12345,
105
+ code: 1,
106
+ signal: null,
107
+ }));
108
+ // 不再用 pipe 收集 stderr,trace 里不再有 stderr 字段
109
+ expect(exitCall![1]).not.toHaveProperty("stderr");
110
+ });
111
+
112
+ it("falls back to pipe capture when the stderr log file cannot be opened", async () => {
113
+ const dir = await tmpLogDir();
114
+ // 用普通文件顶替目录:mkdir/open 日志文件必然失败
115
+ const blocker = join(dir, "blocker");
116
+ await writeFile(blocker, "x");
117
+ const fake = new FakeChild();
118
+ const spawnImpl = vi.fn(() => fake as never);
119
+ const trace = vi.fn();
120
+
121
+ spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace, restartLogDir: blocker });
122
+
123
+ const callArgs = spawnImpl.mock.calls[0] as unknown as Array<unknown>;
124
+ expect((callArgs[2] as { stdio: unknown[] }).stdio[2]).toBe("pipe");
125
+ const failCall = trace.mock.calls.find(
126
+ ([name]) => name === "restart: stderr log open failed, falling back to pipe",
127
+ );
128
+ expect(failCall).toBeTruthy();
129
+ expect(typeof (failCall![1] as { error: unknown }).error).toBe("string");
130
+ });
131
+
132
+ it("inherits the full terminal stdio when launched from a TTY (visible window logs, no EPIPE)", async () => {
133
+ const logDir = await tmpLogDir();
134
+ const fake = new FakeChild();
135
+ const spawnImpl = vi.fn(() => fake as never);
136
+ const trace = vi.fn();
137
+
138
+ spawnRestartChild({
139
+ projectRoot: "F:/proj",
140
+ spawnImpl,
141
+ trace,
142
+ restartLogDir: logDir,
143
+ isTty: () => true,
144
+ });
145
+
146
+ const callArgs = spawnImpl.mock.calls[0] as unknown as Array<unknown>;
147
+ const stdio = (callArgs[2] as { stdio: unknown[] }).stdio;
148
+ // 全部 inherit(含 stdin):避免 detached + stdin=ignore Windows
149
+ // 弹新控制台/丢失控制台关联,日志必须留在用户当前窗口
150
+ expect(stdio).toEqual(["inherit", "inherit", "inherit"]);
151
+ // TTY 场景 stderr 走终端,不再生成 restart-*.log 文件
152
+ const files = await readdir(logDir);
153
+ expect(files.filter((f) => f.startsWith("restart-") && f.endsWith(".log"))).toHaveLength(0);
154
+ // 运行时自检 trace:记录 isTty 判定与最终 stdio
155
+ const spawnCall = trace.mock.calls.find(([name]) => name === "restart: spawn child");
156
+ expect(spawnCall).toBeTruthy();
157
+ expect(spawnCall![1]).toEqual({
158
+ isTty: true,
159
+ stdio: JSON.stringify(["inherit", "inherit", "inherit"]),
160
+ });
161
+ });
162
+
163
+ it("still redirects stderr to a file when explicitly not a TTY", async () => {
164
+ const logDir = await tmpLogDir();
165
+ const fake = new FakeChild();
166
+ const spawnImpl = vi.fn(() => fake as never);
167
+ const trace = vi.fn();
168
+
169
+ spawnRestartChild({
170
+ projectRoot: "F:/proj",
171
+ spawnImpl,
172
+ trace,
173
+ restartLogDir: logDir,
174
+ isTty: () => false,
175
+ });
176
+
177
+ const callArgs = spawnImpl.mock.calls[0] as unknown as Array<unknown>;
178
+ const stdio = (callArgs[2] as { stdio: unknown[] }).stdio;
179
+ expect(stdio[0]).toBe("ignore");
180
+ expect(stdio[1]).toBe("ignore");
181
+ expect(typeof stdio[2]).toBe("number");
182
+ expect(stdio[2] as number).toBeGreaterThan(2);
183
+ const files = await readdir(logDir);
184
+ expect(files.some((f) => f.startsWith("restart-") && f.endsWith(".log"))).toBe(true);
185
+ });
186
+ });
187
+
188
+ describe("decideRestartParentExit", () => {
189
+ it("returns false (parent stays alive) when the child dies during the window", async () => {
190
+ const child = { exitCode: 1, signalCode: null, pid: 42 } as never;
191
+ const trace = vi.fn();
192
+
193
+ const shouldExit = await decideRestartParentExit(child, 200, 50, trace);
194
+
195
+ expect(shouldExit).toBe(false);
196
+ expect(trace).toHaveBeenCalledWith(
197
+ "restart: child died during window, keeping parent",
198
+ expect.objectContaining({ childPid: 42, exitCode: 1 }),
199
+ );
200
+ });
201
+
202
+ it("returns true (parent exits) when the child stays alive through the window", async () => {
203
+ const child = { exitCode: null, signalCode: null, pid: 42 } as never;
204
+ const trace = vi.fn();
205
+
206
+ const shouldExit = await decideRestartParentExit(child, 150, 30, trace);
207
+
208
+ expect(shouldExit).toBe(true);
209
+ expect(trace).toHaveBeenCalledWith(
210
+ "restart: child alive after window, parent exiting",
211
+ expect.objectContaining({ childPid: 42 }),
212
+ );
213
+ });
214
+
215
+ it("surfaces a child that died with a signal", async () => {
216
+ const child = { exitCode: null, signalCode: "SIGKILL", pid: 42 } as never;
217
+ const trace = vi.fn();
218
+
219
+ const shouldExit = await decideRestartParentExit(child, 100, 25, trace);
220
+
221
+ expect(shouldExit).toBe(false);
222
+ expect(trace).toHaveBeenCalledWith(
223
+ "restart: child died during window, keeping parent",
224
+ expect.objectContaining({ signalCode: "SIGKILL" }),
225
+ );
226
+ });
227
+
228
+ it("exports a sane default readiness window", () => {
229
+ expect(RESTART_CHILD_READY_MS).toBeGreaterThan(0);
230
+ expect(RESTART_CHILD_READY_MS).toBeLessThan(60_000);
231
+ });
232
+ });
@@ -362,8 +362,13 @@ describe("Claude SDK install routes", () => {
362
362
  const body = await response.json();
363
363
  expect(typeof body.installed).toBe("boolean");
364
364
  expect(typeof body.running).toBe("boolean");
365
+ // 契约护栏:前端轮询读顶层 phase/message(若只嵌套在 progress 里,
366
+ // 前端 s.phase 恒为 undefined,进度条永不渲染——历史回归点)
365
367
  expect(body.progress).toBeDefined();
366
368
  expect(body.progress.phase).toBeDefined();
369
+ expect(body.phase).toBeDefined();
370
+ expect(typeof body.message).toBe("string");
371
+ expect(typeof body.percent).toBe("number");
367
372
  } finally {
368
373
  await new Promise<void>((resolve, reject) => server.close((err) => err ? reject(err) : resolve()));
369
374
  }
@@ -389,6 +394,26 @@ describe("Claude SDK install routes", () => {
389
394
  expect(PAGE_HTML).toContain("claude-engine-progress-bar");
390
395
  expect(PAGE_HTML).toContain("installClaudeEngine()");
391
396
  expect(PAGE_HTML).toContain("claudeEngineRefreshStatus");
397
+ // 开关接入弹窗确认(SDK 必装才能使用)
398
+ expect(PAGE_HTML).toContain("onClaudeToggle(this)");
399
+ // 按钮文案:安装 Claude Code SDK,且不再显示体积提示
400
+ expect(PAGE_HTML).toContain("安装 Claude Code SDK");
401
+ expect(PAGE_HTML).not.toContain("约 220MB");
402
+ });
403
+
404
+ it("全新用户(四个 Agent 均未启用)时 wizard 只默认勾选 DeepCCC(ccc)", () => {
405
+ // 护栏:renderStep2() 必须包含「全未启用 → 只勾 ccc」的逻辑片段
406
+ expect(PAGE_HTML).toContain("// 全新用户:四个 Agent 均无启用/配置痕迹时,只默认勾选 DeepCCC(ccc),其余不勾");
407
+ expect(PAGE_HTML).toContain("if (!claudeOn && !cursorOn && !codexOn && !cccOn)");
408
+ expect(PAGE_HTML).toContain("cccOn = true;");
409
+ });
410
+
411
+ it("Claude 开关打开时实时检测 SDK 安装状态(已装不重复安装)", () => {
412
+ // 护栏:onClaudeToggle() 必须实时查询 /api/claude-sdk/status 而非仅依赖缓存
413
+ expect(PAGE_HTML).toContain("// 实时查询后端安装状态");
414
+ expect(PAGE_HTML).toContain("var sdkReady = s.installed === true || s.phase === 'done'");
415
+ // 已安装/安装中直接打开,不触发 installClaudeEngine()
416
+ expect(PAGE_HTML).toContain("// 已安装 / 正在安装 / 正在检测:直接打开开关,不再触发安装");
392
417
  });
393
418
 
394
419
  it("设置页卡片顺序:CCC 置顶于 Claude 之前", () => {