chatccc 0.2.230 → 0.2.232

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,132 +1,132 @@
1
- import { EventEmitter } from "node:events";
2
- import { join } from "node:path";
3
-
4
- import { describe, expect, it, vi } from "vitest";
5
-
6
- import {
7
- buildRestartSpawnSpec,
8
- decideRestartParentExit,
9
- spawnRestartChild,
10
- RESTART_CHILD_READY_MS,
11
- } from "../orchestrator.ts";
12
- import { INTERNAL_RESTART_ENV_VAR } from "../startup-lifecycle.ts";
13
-
14
- describe("buildRestartSpawnSpec", () => {
15
- it("spawns node directly with the local tsx CLI (never via npx/npm)", () => {
16
- const spec = buildRestartSpawnSpec("F:/proj");
17
- expect(spec.command).toBe(process.execPath);
18
- expect(spec.args[0]).toBe(join("F:/proj", "node_modules", "tsx", "dist", "cli.mjs"));
19
- expect(spec.args[1]).toBe("src/index.ts");
20
- expect([spec.command, ...spec.args].join(" ")).not.toMatch(/npx|npm/i);
21
- });
22
-
23
- it("accepts the default project root", () => {
24
- const spec = buildRestartSpawnSpec();
25
- expect(spec.command).toBe(process.execPath);
26
- expect(spec.args[0]).toContain("tsx");
27
- });
28
- });
29
-
30
- describe("spawnRestartChild", () => {
31
- class FakeChild extends EventEmitter {
32
- pid = 12345;
33
- exitCode: number | null = null;
34
- signalCode: number | string | null = null;
35
- unref = vi.fn();
36
- stderr = new EventEmitter();
37
- }
38
-
39
- it("spawns without a shell, captures stderr, and marks the internal restart env", () => {
40
- const fake = new FakeChild();
41
- const spawnImpl = vi.fn(() => fake as never);
42
- const trace = vi.fn();
43
-
44
- const child = spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace });
45
-
46
- expect(child).toBe(fake);
47
- expect(spawnImpl).toHaveBeenCalledWith(
48
- process.execPath,
49
- [join("F:/proj", "node_modules", "tsx", "dist", "cli.mjs"), "src/index.ts"],
50
- expect.objectContaining({
51
- detached: true,
52
- stdio: ["ignore", "ignore", "pipe"],
53
- env: expect.objectContaining({ [INTERNAL_RESTART_ENV_VAR]: "1" }),
54
- // 不使用 shell:避免 npm/npx 的 PATH 注入问题
55
- shell: false,
56
- }),
57
- );
58
- // spawnImpl 收到的 command/args 中不得出现 npx/npm(env 里的 npm_* 变量不算)
59
- const firstCall = spawnImpl.mock.calls[0] as unknown as [string, string[]];
60
- const [cmd, args] = firstCall;
61
- expect([cmd, ...args].join(" ")).not.toMatch(/npx|npm/i);
62
- });
63
-
64
- it("writes captured stderr into the trace on child exit", () => {
65
- const fake = new FakeChild();
66
- const spawnImpl = vi.fn(() => fake as never);
67
- const trace = vi.fn();
68
-
69
- spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace });
70
-
71
- fake.stderr.emit("data", Buffer.from("tsx error line 1\n"));
72
- fake.stderr.emit("data", Buffer.from("tsx error line 2\n"));
73
- fake.exitCode = 1;
74
- fake.emit("exit", 1, null);
75
-
76
- const exitCall = trace.mock.calls.find(([name]) => name === "restart: child exit");
77
- expect(exitCall).toBeTruthy();
78
- expect(exitCall![1]).toEqual(expect.objectContaining({
79
- childPid: 12345,
80
- code: 1,
81
- signal: null,
82
- }));
83
- expect(exitCall![1].stderr).toContain("tsx error line 1");
84
- expect(exitCall![1].stderr).toContain("tsx error line 2");
85
- });
86
- });
87
-
88
- describe("decideRestartParentExit", () => {
89
- it("returns false (parent stays alive) when the child dies during the window", async () => {
90
- const child = { exitCode: 1, signalCode: null, pid: 42 } as never;
91
- const trace = vi.fn();
92
-
93
- const shouldExit = await decideRestartParentExit(child, 200, 50, trace);
94
-
95
- expect(shouldExit).toBe(false);
96
- expect(trace).toHaveBeenCalledWith(
97
- "restart: child died during window, keeping parent",
98
- expect.objectContaining({ childPid: 42, exitCode: 1 }),
99
- );
100
- });
101
-
102
- it("returns true (parent exits) when the child stays alive through the window", async () => {
103
- const child = { exitCode: null, signalCode: null, pid: 42 } as never;
104
- const trace = vi.fn();
105
-
106
- const shouldExit = await decideRestartParentExit(child, 150, 30, trace);
107
-
108
- expect(shouldExit).toBe(true);
109
- expect(trace).toHaveBeenCalledWith(
110
- "restart: child alive after window, parent exiting",
111
- expect.objectContaining({ childPid: 42 }),
112
- );
113
- });
114
-
115
- it("surfaces a child that died with a signal", async () => {
116
- const child = { exitCode: null, signalCode: "SIGKILL", pid: 42 } as never;
117
- const trace = vi.fn();
118
-
119
- const shouldExit = await decideRestartParentExit(child, 100, 25, trace);
120
-
121
- expect(shouldExit).toBe(false);
122
- expect(trace).toHaveBeenCalledWith(
123
- "restart: child died during window, keeping parent",
124
- expect.objectContaining({ signalCode: "SIGKILL" }),
125
- );
126
- });
127
-
128
- it("exports a sane default readiness window", () => {
129
- expect(RESTART_CHILD_READY_MS).toBeGreaterThan(0);
130
- expect(RESTART_CHILD_READY_MS).toBeLessThan(60_000);
131
- });
132
- });
1
+ import { EventEmitter } from "node:events";
2
+ import { join } from "node:path";
3
+
4
+ import { describe, expect, it, vi } from "vitest";
5
+
6
+ import {
7
+ buildRestartSpawnSpec,
8
+ decideRestartParentExit,
9
+ spawnRestartChild,
10
+ RESTART_CHILD_READY_MS,
11
+ } from "../orchestrator.ts";
12
+ import { INTERNAL_RESTART_ENV_VAR } from "../startup-lifecycle.ts";
13
+
14
+ describe("buildRestartSpawnSpec", () => {
15
+ it("spawns node directly with the local tsx CLI (never via npx/npm)", () => {
16
+ const spec = buildRestartSpawnSpec("F:/proj");
17
+ expect(spec.command).toBe(process.execPath);
18
+ expect(spec.args[0]).toBe(join("F:/proj", "node_modules", "tsx", "dist", "cli.mjs"));
19
+ expect(spec.args[1]).toBe("src/index.ts");
20
+ expect([spec.command, ...spec.args].join(" ")).not.toMatch(/npx|npm/i);
21
+ });
22
+
23
+ it("accepts the default project root", () => {
24
+ const spec = buildRestartSpawnSpec();
25
+ expect(spec.command).toBe(process.execPath);
26
+ expect(spec.args[0]).toContain("tsx");
27
+ });
28
+ });
29
+
30
+ describe("spawnRestartChild", () => {
31
+ class FakeChild extends EventEmitter {
32
+ pid = 12345;
33
+ exitCode: number | null = null;
34
+ signalCode: number | string | null = null;
35
+ unref = vi.fn();
36
+ stderr = new EventEmitter();
37
+ }
38
+
39
+ it("spawns without a shell, captures stderr, and marks the internal restart env", () => {
40
+ const fake = new FakeChild();
41
+ const spawnImpl = vi.fn(() => fake as never);
42
+ const trace = vi.fn();
43
+
44
+ const child = spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace });
45
+
46
+ expect(child).toBe(fake);
47
+ expect(spawnImpl).toHaveBeenCalledWith(
48
+ process.execPath,
49
+ [join("F:/proj", "node_modules", "tsx", "dist", "cli.mjs"), "src/index.ts"],
50
+ expect.objectContaining({
51
+ detached: true,
52
+ stdio: ["ignore", "ignore", "pipe"],
53
+ env: expect.objectContaining({ [INTERNAL_RESTART_ENV_VAR]: "1" }),
54
+ // 不使用 shell:避免 npm/npx 的 PATH 注入问题
55
+ shell: false,
56
+ }),
57
+ );
58
+ // spawnImpl 收到的 command/args 中不得出现 npx/npm(env 里的 npm_* 变量不算)
59
+ const firstCall = spawnImpl.mock.calls[0] as unknown as [string, string[]];
60
+ const [cmd, args] = firstCall;
61
+ expect([cmd, ...args].join(" ")).not.toMatch(/npx|npm/i);
62
+ });
63
+
64
+ it("writes captured stderr into the trace on child exit", () => {
65
+ const fake = new FakeChild();
66
+ const spawnImpl = vi.fn(() => fake as never);
67
+ const trace = vi.fn();
68
+
69
+ spawnRestartChild({ projectRoot: "F:/proj", spawnImpl, trace });
70
+
71
+ fake.stderr.emit("data", Buffer.from("tsx error line 1\n"));
72
+ fake.stderr.emit("data", Buffer.from("tsx error line 2\n"));
73
+ fake.exitCode = 1;
74
+ fake.emit("exit", 1, null);
75
+
76
+ const exitCall = trace.mock.calls.find(([name]) => name === "restart: child exit");
77
+ expect(exitCall).toBeTruthy();
78
+ expect(exitCall![1]).toEqual(expect.objectContaining({
79
+ childPid: 12345,
80
+ code: 1,
81
+ signal: null,
82
+ }));
83
+ expect(exitCall![1].stderr).toContain("tsx error line 1");
84
+ expect(exitCall![1].stderr).toContain("tsx error line 2");
85
+ });
86
+ });
87
+
88
+ describe("decideRestartParentExit", () => {
89
+ it("returns false (parent stays alive) when the child dies during the window", async () => {
90
+ const child = { exitCode: 1, signalCode: null, pid: 42 } as never;
91
+ const trace = vi.fn();
92
+
93
+ const shouldExit = await decideRestartParentExit(child, 200, 50, trace);
94
+
95
+ expect(shouldExit).toBe(false);
96
+ expect(trace).toHaveBeenCalledWith(
97
+ "restart: child died during window, keeping parent",
98
+ expect.objectContaining({ childPid: 42, exitCode: 1 }),
99
+ );
100
+ });
101
+
102
+ it("returns true (parent exits) when the child stays alive through the window", async () => {
103
+ const child = { exitCode: null, signalCode: null, pid: 42 } as never;
104
+ const trace = vi.fn();
105
+
106
+ const shouldExit = await decideRestartParentExit(child, 150, 30, trace);
107
+
108
+ expect(shouldExit).toBe(true);
109
+ expect(trace).toHaveBeenCalledWith(
110
+ "restart: child alive after window, parent exiting",
111
+ expect.objectContaining({ childPid: 42 }),
112
+ );
113
+ });
114
+
115
+ it("surfaces a child that died with a signal", async () => {
116
+ const child = { exitCode: null, signalCode: "SIGKILL", pid: 42 } as never;
117
+ const trace = vi.fn();
118
+
119
+ const shouldExit = await decideRestartParentExit(child, 100, 25, trace);
120
+
121
+ expect(shouldExit).toBe(false);
122
+ expect(trace).toHaveBeenCalledWith(
123
+ "restart: child died during window, keeping parent",
124
+ expect.objectContaining({ signalCode: "SIGKILL" }),
125
+ );
126
+ });
127
+
128
+ it("exports a sane default readiness window", () => {
129
+ expect(RESTART_CHILD_READY_MS).toBeGreaterThan(0);
130
+ expect(RESTART_CHILD_READY_MS).toBeLessThan(60_000);
131
+ });
132
+ });