From 85f8fd05331f81a96b961811f04d1da26206f836 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 17 May 2026 16:55:23 +0800 Subject: [PATCH] test: reuse platform spy helper in infra tests --- src/infra/process-respawn.test.ts | 16 ++++------------ src/infra/restart.test.ts | 13 ++----------- src/infra/wsl.test.ts | 12 +++--------- 3 files changed, 9 insertions(+), 32 deletions(-) diff --git a/src/infra/process-respawn.test.ts b/src/infra/process-respawn.test.ts index 3421b9187436..a3f3d5d669b9 100644 --- a/src/infra/process-respawn.test.ts +++ b/src/infra/process-respawn.test.ts @@ -1,5 +1,6 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { captureFullEnv } from "../test-utils/env.js"; +import { mockProcessPlatform } from "../test-utils/vitest-spies.js"; import { SUPERVISOR_HINT_ENV_VARS } from "./supervisor-markers.js"; const spawnMock = vi.hoisted(() => vi.fn()); @@ -30,16 +31,9 @@ import { const originalArgv = [...process.argv]; const originalExecArgv = [...process.execArgv]; const envSnapshot = captureFullEnv(); -const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); -function setPlatform(platform: string) { - if (!originalPlatformDescriptor) { - return; - } - Object.defineProperty(process, "platform", { - ...originalPlatformDescriptor, - value: platform, - }); +function setPlatform(platform: NodeJS.Platform) { + mockProcessPlatform(platform); } afterEach(() => { @@ -50,9 +44,7 @@ afterEach(() => { triggerOpenClawRestartMock.mockClear(); isContainerEnvironmentMock.mockReset(); isContainerEnvironmentMock.mockReturnValue(false); - if (originalPlatformDescriptor) { - Object.defineProperty(process, "platform", originalPlatformDescriptor); - } + vi.restoreAllMocks(); }); function clearSupervisorHints() { diff --git a/src/infra/restart.test.ts b/src/infra/restart.test.ts index e7a92f510157..42bdb5da9006 100644 --- a/src/infra/restart.test.ts +++ b/src/infra/restart.test.ts @@ -1,5 +1,6 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import { captureFullEnv } from "../test-utils/env.js"; +import { mockProcessPlatform } from "../test-utils/vitest-spies.js"; const spawnSyncMock = vi.hoisted(() => vi.fn()); const resolveLsofCommandSyncMock = vi.hoisted(() => vi.fn()); @@ -29,7 +30,6 @@ let triggerOpenClawRestart: typeof import("./restart.js").triggerOpenClawRestart let currentTimeMs = 0; const envSnapshot = captureFullEnv(); -const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); beforeAll(async () => { ({ __testing, cleanStaleGatewayProcessesSync, findGatewayPidsOnPortSync } = @@ -55,20 +55,11 @@ afterEach(() => { envSnapshot.restore(); __testing.setSleepSyncOverride(null); __testing.setDateNowOverride(null); - if (originalPlatformDescriptor) { - Object.defineProperty(process, "platform", originalPlatformDescriptor); - } vi.restoreAllMocks(); }); function setPlatform(platform: NodeJS.Platform): void { - if (!originalPlatformDescriptor) { - return; - } - Object.defineProperty(process, "platform", { - ...originalPlatformDescriptor, - value: platform, - }); + mockProcessPlatform(platform); } function requireFirstSpawnSyncCall(): [unknown, unknown, unknown] { diff --git a/src/infra/wsl.test.ts b/src/infra/wsl.test.ts index eace7eac45ce..dec063cdb54f 100644 --- a/src/infra/wsl.test.ts +++ b/src/infra/wsl.test.ts @@ -1,5 +1,6 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import { captureEnv } from "../test-utils/env.js"; +import { mockProcessPlatform } from "../test-utils/vitest-spies.js"; const readFileSyncMock = vi.hoisted(() => vi.fn()); const readFileMock = vi.hoisted(() => vi.fn()); @@ -30,13 +31,8 @@ let isWSL2Sync: typeof import("./wsl.js").isWSL2Sync; let isWSL: typeof import("./wsl.js").isWSL; let resetWSLStateForTests: typeof import("./wsl.js").resetWSLStateForTests; -const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); - function setPlatform(platform: NodeJS.Platform): void { - Object.defineProperty(process, "platform", { - value: platform, - configurable: true, - }); + mockProcessPlatform(platform); } describe("wsl detection", () => { @@ -60,9 +56,7 @@ describe("wsl detection", () => { afterEach(() => { envSnapshot.restore(); resetWSLStateForTests(); - if (originalPlatformDescriptor) { - Object.defineProperty(process, "platform", originalPlatformDescriptor); - } + vi.restoreAllMocks(); }); it.each([