From fc43a16d43b217bde93f8b70aac7ecd370151b84 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 18:28:49 +0000 Subject: [PATCH] refactor(test): replace ad-hoc env restore blocks with helpers --- src/commands/doctor-gateway-services.test.ts | 13 +++---------- src/infra/provider-usage.test.ts | 13 +++---------- src/infra/update-runner.test.ts | 14 +++----------- 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/commands/doctor-gateway-services.test.ts b/src/commands/doctor-gateway-services.test.ts index e80954a63..a09550fe0 100644 --- a/src/commands/doctor-gateway-services.test.ts +++ b/src/commands/doctor-gateway-services.test.ts @@ -1,5 +1,6 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import type { OpenClawConfig } from "../config/config.js"; +import { withEnvAsync } from "../test-utils/env.js"; const mocks = vi.hoisted(() => ({ readCommand: vi.fn(), @@ -139,9 +140,7 @@ describe("maybeRepairGatewayServiceConfig", () => { }); it("uses OPENCLAW_GATEWAY_TOKEN when config token is missing", async () => { - const previousToken = process.env.OPENCLAW_GATEWAY_TOKEN; - process.env.OPENCLAW_GATEWAY_TOKEN = "env-token"; - try { + await withEnvAsync({ OPENCLAW_GATEWAY_TOKEN: "env-token" }, async () => { setupGatewayTokenRepairScenario("env-token"); const cfg: OpenClawConfig = { @@ -161,12 +160,6 @@ describe("maybeRepairGatewayServiceConfig", () => { }), ); expect(mocks.install).toHaveBeenCalledTimes(1); - } finally { - if (previousToken === undefined) { - delete process.env.OPENCLAW_GATEWAY_TOKEN; - } else { - process.env.OPENCLAW_GATEWAY_TOKEN = previousToken; - } - } + }); }); }); diff --git a/src/infra/provider-usage.test.ts b/src/infra/provider-usage.test.ts index 0a3282f22..17ce3754c 100644 --- a/src/infra/provider-usage.test.ts +++ b/src/infra/provider-usage.test.ts @@ -3,6 +3,7 @@ import path from "node:path"; import { describe, expect, it, vi } from "vitest"; import { withTempHome } from "../../test/helpers/temp-home.js"; import { ensureAuthProfileStore, listProfilesForProvider } from "../agents/auth-profiles.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { createProviderUsageFetch, makeResponse } from "../test-utils/provider-usage-fetch.js"; import { formatUsageReportLines, @@ -302,9 +303,7 @@ describe("provider usage loading", () => { }); it("falls back to claude.ai web usage when OAuth scope is missing", async () => { - const cookieSnapshot = process.env.CLAUDE_AI_SESSION_KEY; - process.env.CLAUDE_AI_SESSION_KEY = "sk-ant-web-1"; - try { + await withEnvAsync({ CLAUDE_AI_SESSION_KEY: "sk-ant-web-1" }, async () => { const mockFetch = createProviderUsageFetch(async (url) => { if (url.includes("api.anthropic.com/api/oauth/usage")) { return makeResponse(403, { @@ -336,13 +335,7 @@ describe("provider usage loading", () => { const claude = expectSingleAnthropicProvider(summary); expect(claude?.windows.some((w) => w.label === "5h")).toBe(true); expect(claude?.windows.some((w) => w.label === "Week")).toBe(true); - } finally { - if (cookieSnapshot === undefined) { - delete process.env.CLAUDE_AI_SESSION_KEY; - } else { - process.env.CLAUDE_AI_SESSION_KEY = cookieSnapshot; - } - } + }); }); it("loads snapshots for copilot antigravity gemini codex and xiaomi", async () => { diff --git a/src/infra/update-runner.test.ts b/src/infra/update-runner.test.ts index df6bdc13e..bb301c563 100644 --- a/src/infra/update-runner.test.ts +++ b/src/infra/update-runner.test.ts @@ -2,6 +2,7 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { withEnvAsync } from "../test-utils/env.js"; import { pathExists } from "../utils.js"; import { runGatewayUpdate } from "./update-runner.js"; @@ -421,11 +422,8 @@ describe("runGatewayUpdate", () => { }); it("updates global bun installs when detected", async () => { - const oldBunInstall = process.env.BUN_INSTALL; const bunInstall = path.join(tempDir, "bun-install"); - process.env.BUN_INSTALL = bunInstall; - - try { + await withEnvAsync({ BUN_INSTALL: bunInstall }, async () => { const bunGlobalRoot = path.join(bunInstall, "install", "global", "node_modules"); const pkgRoot = path.join(bunGlobalRoot, "openclaw"); await seedGlobalPackageRoot(pkgRoot); @@ -449,13 +447,7 @@ describe("runGatewayUpdate", () => { expect(result.before?.version).toBe("1.0.0"); expect(result.after?.version).toBe("2.0.0"); expect(calls.some((call) => call === "bun add -g openclaw@latest")).toBe(true); - } finally { - if (oldBunInstall === undefined) { - delete process.env.BUN_INSTALL; - } else { - process.env.BUN_INSTALL = oldBunInstall; - } - } + }); }); it("rejects git roots that are not a openclaw checkout", async () => {