|
| 1 | +import type { ChildProcess, SpawnOptions } from "node:child_process"; |
| 2 | +import { EventEmitter } from "node:events"; |
| 3 | +import os from "node:os"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js"; |
| 6 | +import { captureEnv, setTestEnvValue } from "../test-utils/env.js"; |
| 7 | +import { |
| 8 | + maybeWrapCommandWithShellSnapshot, |
| 9 | + resetShellSnapshotCacheForTests, |
| 10 | +} from "./shell-snapshot.js"; |
| 11 | + |
| 12 | +const { killProcessTreeMock, spawnMock } = vi.hoisted(() => ({ |
| 13 | + killProcessTreeMock: vi.fn(), |
| 14 | + spawnMock: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("node:child_process", async () => { |
| 18 | + const { mockNodeBuiltinModule } = await import("openclaw/plugin-sdk/test-node-mocks"); |
| 19 | + return mockNodeBuiltinModule( |
| 20 | + () => vi.importActual<typeof import("node:child_process")>("node:child_process"), |
| 21 | + { spawn: spawnMock }, |
| 22 | + ); |
| 23 | +}); |
| 24 | + |
| 25 | +vi.mock("../process/kill-tree.js", () => ({ |
| 26 | + killProcessTree: killProcessTreeMock, |
| 27 | +})); |
| 28 | + |
| 29 | +describe.skipIf(process.platform === "win32")("shell snapshot subprocesses", () => { |
| 30 | + const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 31 | + let envSnapshot: ReturnType<typeof captureEnv>; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + envSnapshot = captureEnv(["HOME", "OPENCLAW_EXEC_SHELL_SNAPSHOT", "OPENCLAW_STATE_DIR"]); |
| 35 | + spawnMock.mockReset(); |
| 36 | + killProcessTreeMock.mockReset(); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + resetShellSnapshotCacheForTests(); |
| 41 | + envSnapshot.restore(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("does not create output pipes for status-only shell commands", async () => { |
| 45 | + const child = Object.assign(new EventEmitter(), { pid: 4242 }) as ChildProcess; |
| 46 | + spawnMock.mockImplementation(() => { |
| 47 | + queueMicrotask(() => child.emit("close", 1)); |
| 48 | + return child; |
| 49 | + }); |
| 50 | + |
| 51 | + const home = tempDirs.make("openclaw-snapshot-spawn-home-"); |
| 52 | + const stateDir = tempDirs.make("openclaw-snapshot-spawn-state-"); |
| 53 | + setTestEnvValue("HOME", home); |
| 54 | + setTestEnvValue("OPENCLAW_STATE_DIR", stateDir); |
| 55 | + |
| 56 | + const command = "echo unchanged"; |
| 57 | + await expect( |
| 58 | + maybeWrapCommandWithShellSnapshot({ |
| 59 | + command, |
| 60 | + shell: "/bin/bash", |
| 61 | + shellArgs: ["-c"], |
| 62 | + cwd: os.tmpdir(), |
| 63 | + env: { ...process.env }, |
| 64 | + }), |
| 65 | + ).resolves.toBe(command); |
| 66 | + |
| 67 | + const options = spawnMock.mock.calls[0]?.[2] as SpawnOptions | undefined; |
| 68 | + expect(options?.stdio).toBe("ignore"); |
| 69 | + expect(killProcessTreeMock).toHaveBeenCalledWith(4242, { graceMs: 0 }); |
| 70 | + }); |
| 71 | +}); |
0 commit comments