From e075a33ca3d408b06ae86654aa4c34f820d91bb9 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 16 Feb 2026 00:03:45 +0000 Subject: [PATCH] refactor(test): simplify oauth/profile env restore --- src/commands/status.e2e.test.ts | 11 ++++------- src/web/accounts.whatsapp-auth.test.ts | 11 ++++------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/commands/status.e2e.test.ts b/src/commands/status.e2e.test.ts index f3957a41c..ae866bbd2 100644 --- a/src/commands/status.e2e.test.ts +++ b/src/commands/status.e2e.test.ts @@ -1,18 +1,15 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { captureEnv } from "../test-utils/env.js"; -let previousProfile: string | undefined; +let envSnapshot: ReturnType; beforeAll(() => { - previousProfile = process.env.OPENCLAW_PROFILE; + envSnapshot = captureEnv(["OPENCLAW_PROFILE"]); process.env.OPENCLAW_PROFILE = "isolated"; }); afterAll(() => { - if (previousProfile === undefined) { - delete process.env.OPENCLAW_PROFILE; - } else { - process.env.OPENCLAW_PROFILE = previousProfile; - } + envSnapshot.restore(); }); const mocks = vi.hoisted(() => ({ diff --git a/src/web/accounts.whatsapp-auth.test.ts b/src/web/accounts.whatsapp-auth.test.ts index c63ae12e5..89dac3977 100644 --- a/src/web/accounts.whatsapp-auth.test.ts +++ b/src/web/accounts.whatsapp-auth.test.ts @@ -2,10 +2,11 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { captureEnv } from "../test-utils/env.js"; import { hasAnyWhatsAppAuth, listWhatsAppAuthDirs } from "./accounts.js"; describe("hasAnyWhatsAppAuth", () => { - let previousOauthDir: string | undefined; + let envSnapshot: ReturnType; let tempOauthDir: string | undefined; const writeCreds = (dir: string) => { @@ -14,17 +15,13 @@ describe("hasAnyWhatsAppAuth", () => { }; beforeEach(() => { - previousOauthDir = process.env.OPENCLAW_OAUTH_DIR; + envSnapshot = captureEnv(["OPENCLAW_OAUTH_DIR"]); tempOauthDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-oauth-")); process.env.OPENCLAW_OAUTH_DIR = tempOauthDir; }); afterEach(() => { - if (previousOauthDir === undefined) { - delete process.env.OPENCLAW_OAUTH_DIR; - } else { - process.env.OPENCLAW_OAUTH_DIR = previousOauthDir; - } + envSnapshot.restore(); if (tempOauthDir) { fs.rmSync(tempOauthDir, { recursive: true, force: true }); tempOauthDir = undefined;