From ee2fa5f4114cf12e8173c1eae18c0847a355653e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 16 Feb 2026 00:02:32 +0000 Subject: [PATCH] refactor(test): reuse env snapshots in unit suites --- .../auth-choice.apply.huggingface.test.ts | 21 +++---------------- src/pairing/pairing-messages.test.ts | 11 ++++------ src/pairing/pairing-store.test.ts | 9 +++----- src/process/exec.test.ts | 9 +++----- 4 files changed, 13 insertions(+), 37 deletions(-) diff --git a/src/commands/auth-choice.apply.huggingface.test.ts b/src/commands/auth-choice.apply.huggingface.test.ts index 3f6d995a90..aa0e211523 100644 --- a/src/commands/auth-choice.apply.huggingface.test.ts +++ b/src/commands/auth-choice.apply.huggingface.test.ts @@ -4,6 +4,7 @@ import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { RuntimeEnv } from "../runtime.js"; import type { WizardPrompter } from "../wizard/prompts.js"; +import { captureEnv } from "../test-utils/env.js"; import { applyAuthChoiceHuggingface } from "./auth-choice.apply.huggingface.js"; const noopAsync = async () => {}; @@ -11,9 +12,7 @@ const noop = () => {}; const authProfilePathFor = (agentDir: string) => path.join(agentDir, "auth-profiles.json"); describe("applyAuthChoiceHuggingface", () => { - const previousAgentDir = process.env.OPENCLAW_AGENT_DIR; - const previousHfToken = process.env.HF_TOKEN; - const previousHubToken = process.env.HUGGINGFACE_HUB_TOKEN; + const envSnapshot = captureEnv(["OPENCLAW_AGENT_DIR", "HF_TOKEN", "HUGGINGFACE_HUB_TOKEN"]); let tempStateDir: string | null = null; afterEach(async () => { @@ -21,21 +20,7 @@ describe("applyAuthChoiceHuggingface", () => { await fs.rm(tempStateDir, { recursive: true, force: true }); tempStateDir = null; } - if (previousAgentDir === undefined) { - delete process.env.OPENCLAW_AGENT_DIR; - } else { - process.env.OPENCLAW_AGENT_DIR = previousAgentDir; - } - if (previousHfToken === undefined) { - delete process.env.HF_TOKEN; - } else { - process.env.HF_TOKEN = previousHfToken; - } - if (previousHubToken === undefined) { - delete process.env.HUGGINGFACE_HUB_TOKEN; - } else { - process.env.HUGGINGFACE_HUB_TOKEN = previousHubToken; - } + envSnapshot.restore(); }); it("returns null when authChoice is not huggingface-api-key", async () => { diff --git a/src/pairing/pairing-messages.test.ts b/src/pairing/pairing-messages.test.ts index e63083560a..5480d333c5 100644 --- a/src/pairing/pairing-messages.test.ts +++ b/src/pairing/pairing-messages.test.ts @@ -1,20 +1,17 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { captureEnv } from "../test-utils/env.js"; import { buildPairingReply } from "./pairing-messages.js"; describe("buildPairingReply", () => { - let previousProfile: string | undefined; + let envSnapshot: ReturnType; beforeEach(() => { - previousProfile = process.env.OPENCLAW_PROFILE; + envSnapshot = captureEnv(["OPENCLAW_PROFILE"]); process.env.OPENCLAW_PROFILE = "isolated"; }); afterEach(() => { - if (previousProfile === undefined) { - delete process.env.OPENCLAW_PROFILE; - return; - } - process.env.OPENCLAW_PROFILE = previousProfile; + envSnapshot.restore(); }); const cases = [ diff --git a/src/pairing/pairing-store.test.ts b/src/pairing/pairing-store.test.ts index ab66744381..c0fb933f9e 100644 --- a/src/pairing/pairing-store.test.ts +++ b/src/pairing/pairing-store.test.ts @@ -4,6 +4,7 @@ import os from "node:os"; import path from "node:path"; import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; import { resolveOAuthDir } from "../config/paths.js"; +import { captureEnv } from "../test-utils/env.js"; import { listChannelPairingRequests, upsertChannelPairingRequest } from "./pairing-store.js"; let fixtureRoot = ""; @@ -20,18 +21,14 @@ afterAll(async () => { }); async function withTempStateDir(fn: (stateDir: string) => Promise) { - const previous = process.env.OPENCLAW_STATE_DIR; + const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]); const dir = path.join(fixtureRoot, `case-${caseId++}`); await fs.mkdir(dir, { recursive: true }); process.env.OPENCLAW_STATE_DIR = dir; try { return await fn(dir); } finally { - if (previous === undefined) { - delete process.env.OPENCLAW_STATE_DIR; - } else { - process.env.OPENCLAW_STATE_DIR = previous; - } + envSnapshot.restore(); } } diff --git a/src/process/exec.test.ts b/src/process/exec.test.ts index 7e1dd1f023..0af0979558 100644 --- a/src/process/exec.test.ts +++ b/src/process/exec.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { captureEnv } from "../test-utils/env.js"; import { runCommandWithTimeout, shouldSpawnWithShell } from "./exec.js"; describe("runCommandWithTimeout", () => { @@ -25,7 +26,7 @@ describe("runCommandWithTimeout", () => { }); it("merges custom env with process.env", async () => { - const previous = process.env.OPENCLAW_BASE_ENV; + const envSnapshot = captureEnv(["OPENCLAW_BASE_ENV"]); process.env.OPENCLAW_BASE_ENV = "base"; try { const result = await runCommandWithTimeout( @@ -43,11 +44,7 @@ describe("runCommandWithTimeout", () => { expect(result.code).toBe(0); expect(result.stdout).toBe("base|ok"); } finally { - if (previous === undefined) { - delete process.env.OPENCLAW_BASE_ENV; - } else { - process.env.OPENCLAW_BASE_ENV = previous; - } + envSnapshot.restore(); } }); });