diff --git a/src/commands/auth-choice.moonshot.e2e.test.ts b/src/commands/auth-choice.moonshot.e2e.test.ts index 2e467ae7f..d215125f3 100644 --- a/src/commands/auth-choice.moonshot.e2e.test.ts +++ b/src/commands/auth-choice.moonshot.e2e.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 { applyAuthChoice } from "./auth-choice.js"; const noopAsync = async () => {}; @@ -42,10 +43,12 @@ function createPrompter(overrides: Partial): WizardPrompter { } describe("applyAuthChoice (moonshot)", () => { - const previousStateDir = process.env.OPENCLAW_STATE_DIR; - const previousAgentDir = process.env.OPENCLAW_AGENT_DIR; - const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR; - const previousMoonshotKey = process.env.MOONSHOT_API_KEY; + const envSnapshot = captureEnv([ + "OPENCLAW_STATE_DIR", + "OPENCLAW_AGENT_DIR", + "PI_CODING_AGENT_DIR", + "MOONSHOT_API_KEY", + ]); let tempStateDir: string | null = null; async function setupTempState() { @@ -61,26 +64,7 @@ describe("applyAuthChoice (moonshot)", () => { await fs.rm(tempStateDir, { recursive: true, force: true }); tempStateDir = null; } - if (previousStateDir === undefined) { - delete process.env.OPENCLAW_STATE_DIR; - } else { - process.env.OPENCLAW_STATE_DIR = previousStateDir; - } - if (previousAgentDir === undefined) { - delete process.env.OPENCLAW_AGENT_DIR; - } else { - process.env.OPENCLAW_AGENT_DIR = previousAgentDir; - } - if (previousPiAgentDir === undefined) { - delete process.env.PI_CODING_AGENT_DIR; - } else { - process.env.PI_CODING_AGENT_DIR = previousPiAgentDir; - } - if (previousMoonshotKey === undefined) { - delete process.env.MOONSHOT_API_KEY; - } else { - process.env.MOONSHOT_API_KEY = previousMoonshotKey; - } + envSnapshot.restore(); }); it("keeps the .cn baseUrl when setDefaultModel is false", async () => { diff --git a/src/commands/onboard-auth.e2e.test.ts b/src/commands/onboard-auth.e2e.test.ts index eb6858f87..a26c544e1 100644 --- a/src/commands/onboard-auth.e2e.test.ts +++ b/src/commands/onboard-auth.e2e.test.ts @@ -3,6 +3,7 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; +import { captureEnv } from "../test-utils/env.js"; import { applyAuthProfileConfig, applyLitellmProviderConfig, @@ -40,9 +41,12 @@ const requireAgentDir = () => { }; describe("writeOAuthCredentials", () => { - const previousStateDir = process.env.OPENCLAW_STATE_DIR; - const previousAgentDir = process.env.OPENCLAW_AGENT_DIR; - const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR; + const envSnapshot = captureEnv([ + "OPENCLAW_STATE_DIR", + "OPENCLAW_AGENT_DIR", + "PI_CODING_AGENT_DIR", + "OPENCLAW_OAUTH_DIR", + ]); let tempStateDir: string | null = null; afterEach(async () => { @@ -50,22 +54,7 @@ describe("writeOAuthCredentials", () => { await fs.rm(tempStateDir, { recursive: true, force: true }); tempStateDir = null; } - if (previousStateDir === undefined) { - delete process.env.OPENCLAW_STATE_DIR; - } else { - process.env.OPENCLAW_STATE_DIR = previousStateDir; - } - if (previousAgentDir === undefined) { - delete process.env.OPENCLAW_AGENT_DIR; - } else { - process.env.OPENCLAW_AGENT_DIR = previousAgentDir; - } - if (previousPiAgentDir === undefined) { - delete process.env.PI_CODING_AGENT_DIR; - } else { - process.env.PI_CODING_AGENT_DIR = previousPiAgentDir; - } - delete process.env.OPENCLAW_OAUTH_DIR; + envSnapshot.restore(); }); it("writes auth-profiles.json under OPENCLAW_AGENT_DIR when set", async () => { @@ -100,9 +89,11 @@ describe("writeOAuthCredentials", () => { }); describe("setMinimaxApiKey", () => { - const previousStateDir = process.env.OPENCLAW_STATE_DIR; - const previousAgentDir = process.env.OPENCLAW_AGENT_DIR; - const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR; + const envSnapshot = captureEnv([ + "OPENCLAW_STATE_DIR", + "OPENCLAW_AGENT_DIR", + "PI_CODING_AGENT_DIR", + ]); let tempStateDir: string | null = null; afterEach(async () => { @@ -110,21 +101,7 @@ describe("setMinimaxApiKey", () => { await fs.rm(tempStateDir, { recursive: true, force: true }); tempStateDir = null; } - if (previousStateDir === undefined) { - delete process.env.OPENCLAW_STATE_DIR; - } else { - process.env.OPENCLAW_STATE_DIR = previousStateDir; - } - if (previousAgentDir === undefined) { - delete process.env.OPENCLAW_AGENT_DIR; - } else { - process.env.OPENCLAW_AGENT_DIR = previousAgentDir; - } - if (previousPiAgentDir === undefined) { - delete process.env.PI_CODING_AGENT_DIR; - } else { - process.env.PI_CODING_AGENT_DIR = previousPiAgentDir; - } + envSnapshot.restore(); }); it("writes to OPENCLAW_AGENT_DIR when set", async () => {