refactor(test): simplify oauth/profile env restore

This commit is contained in:
Peter Steinberger
2026-02-16 00:03:45 +00:00
parent c07036e813
commit e075a33ca3
2 changed files with 8 additions and 14 deletions

View File

@@ -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<typeof captureEnv>;
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(() => ({

View File

@@ -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<typeof captureEnv>;
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;