
























@@ -76,6 +76,36 @@ async function createAdapterHarness(params?: {
7676return { adapter, killMock };
7777}
787879+type SpawnWithFallbackParams = {
80+argv?: string[];
81+options?: {
82+detached?: boolean;
83+env?: NodeJS.ProcessEnv | Record<string, string>;
84+stdio?: string[];
85+};
86+fallbacks?: Array<{ options?: { detached?: boolean } }>;
87+};
88+89+function firstSpawnWithFallbackParams(): SpawnWithFallbackParams {
90+const [call] = spawnWithFallbackMock.mock.calls;
91+if (!call) {
92+throw new Error("expected spawnWithFallback call");
93+}
94+const [params] = call;
95+if (typeof params !== "object" || params === null || Array.isArray(params)) {
96+throw new Error("expected spawnWithFallback params to be an object");
97+}
98+return params;
99+}
100+101+function firstMockArg(mock: { mock: { calls: readonly unknown[][] } }, label: string): unknown {
102+const [call] = mock.mock.calls;
103+if (!call) {
104+throw new Error(`expected ${label} call`);
105+}
106+return call[0];
107+}
108+79109describe("createChildAdapter", () => {
80110const originalServiceMarker = process.env.OPENCLAW_SERVICE_MARKER;
81111const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
@@ -121,10 +151,7 @@ describe("createChildAdapter", () => {
121151it("uses process-tree kill for default SIGKILL", async () => {
122152const { adapter, killMock } = await createAdapterHarness({ pid: 4321 });
123153124-const spawnArgs = spawnWithFallbackMock.mock.calls[0]?.[0] as {
125-options?: { detached?: boolean };
126-fallbacks?: Array<{ options?: { detached?: boolean } }>;
127-};
154+const spawnArgs = firstSpawnWithFallbackParams();
128155// On Windows, detached defaults to false (headless Scheduled Task compat);
129156// on POSIX, detached is true with a no-detach fallback.
130157if (process.platform === "win32") {
@@ -198,9 +225,7 @@ describe("createChildAdapter", () => {
198225argv: ["node", "-e", "setTimeout(() => {}, 1000)"],
199226});
200227201-const spawnArgs = spawnWithFallbackMock.mock.calls[0]?.[0] as {
202-options?: { stdio?: Array<string> };
203-};
228+const spawnArgs = firstSpawnWithFallbackParams();
204229expect(spawnArgs.options?.stdio?.[0]).toBe("inherit");
205230expect(adapter.stdin).toBeUndefined();
206231});
@@ -217,7 +242,7 @@ describe("createChildAdapter", () => {
217242218243const writeCallback = vi.fn();
219244adapter.stdin?.write("late", writeCallback);
220-expect(writeCallback.mock.calls[0]?.[0]).toBeInstanceOf(Error);
245+expect(firstMockArg(writeCallback, "write callback")).toBeInstanceOf(Error);
221246222247adapter.stdin?.destroy?.();
223248expect(adapter.stdin?.destroyed).toBe(true);
@@ -312,10 +337,7 @@ describe("createChildAdapter", () => {
312337313338await createAdapterHarness({ pid: 7777 });
314339315-const spawnArgs = spawnWithFallbackMock.mock.calls[0]?.[0] as {
316-options?: { detached?: boolean };
317-fallbacks?: Array<{ options?: { detached?: boolean } }>;
318-};
340+const spawnArgs = firstSpawnWithFallbackParams();
319341expect(spawnArgs.options?.detached).toBe(false);
320342expect(spawnArgs.fallbacks ?? []).toStrictEqual([]);
321343});
@@ -328,10 +350,7 @@ describe("createChildAdapter", () => {
328350argv: ["node", "-e", "process.exit(0)"],
329351});
330352331-const spawnArgs = spawnWithFallbackMock.mock.calls[0]?.[0] as {
332-argv?: string[];
333-options?: { env?: NodeJS.ProcessEnv };
334-};
353+const spawnArgs = firstSpawnWithFallbackParams();
335354expect(spawnArgs.argv).toEqual(["node", "-e", "process.exit(0)"]);
336355expect(spawnArgs.options?.env).toBeUndefined();
337356});
@@ -367,10 +386,7 @@ describe("createChildAdapter", () => {
367386}
368387}
369388370-const spawnArgs = spawnWithFallbackMock.mock.calls[0]?.[0] as {
371-argv?: string[];
372-options?: { env?: NodeJS.ProcessEnv };
373-};
389+const spawnArgs = firstSpawnWithFallbackParams();
374390expect(spawnArgs.argv?.slice(0, 4)).toEqual([
375391"/bin/sh",
376392"-c",
@@ -393,9 +409,7 @@ describe("createChildAdapter", () => {
393409env: { FOO: "bar", COUNT: "12", DROP_ME: undefined },
394410});
395411396-const spawnArgs = spawnWithFallbackMock.mock.calls[0]?.[0] as {
397-options?: { env?: Record<string, string> };
398-};
412+const spawnArgs = firstSpawnWithFallbackParams();
399413expect(spawnArgs.options?.env).toEqual({ FOO: "bar", COUNT: "12" });
400414});
401415此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。