From 4752e9a67d4f7e0bd9b6d21214bda28e42a7e5e6 Mon Sep 17 00:00:00 2001 From: Shakker Date: Fri, 5 Jun 2026 17:09:55 +0100 Subject: [PATCH] test: bracket provider worker env --- src/agents/model-provider-auth.worker.test.ts | 96 ++++++++----------- 1 file changed, 41 insertions(+), 55 deletions(-) diff --git a/src/agents/model-provider-auth.worker.test.ts b/src/agents/model-provider-auth.worker.test.ts index b4639bc85404..3341e26618d9 100644 --- a/src/agents/model-provider-auth.worker.test.ts +++ b/src/agents/model-provider-auth.worker.test.ts @@ -4,24 +4,12 @@ import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import type { OpenClawConfig } from "../config/types.openclaw.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { clearRuntimeAuthProfileStoreSnapshots } from "./auth-profiles.js"; import { clearCurrentProviderAuthState } from "./model-provider-auth.js"; import { runProviderAuthWarmWorkerInput } from "./model-provider-auth.worker.js"; const tempDirs: string[] = []; -const envKeys = ["OPENCLAW_DISABLE_PERSISTED_PLUGIN_REGISTRY", "OPENCLAW_STATE_DIR"] as const; - -function restoreEnv(previous: Record<(typeof envKeys)[number], string | undefined>): void { - // Worker tests mutate process env to isolate state/plugin registry paths; - // restore exact previous values after each case. - for (const key of envKeys) { - if (previous[key] === undefined) { - delete process.env[key]; - continue; - } - process.env[key] = previous[key]; - } -} describe("provider auth warm worker", () => { afterEach(() => { @@ -37,52 +25,50 @@ describe("provider auth warm worker", () => { // carry them explicitly or warming loses provider availability. const root = mkdtempSync(path.join(tmpdir(), "openclaw-provider-auth-worker-")); tempDirs.push(root); - const previousEnv = Object.fromEntries(envKeys.map((key) => [key, process.env[key]])) as Record< - (typeof envKeys)[number], - string | undefined - >; - process.env.OPENCLAW_DISABLE_PERSISTED_PLUGIN_REGISTRY = "1"; - process.env.OPENCLAW_STATE_DIR = path.join(root, "state"); - try { - const agentDir = path.join(root, "agent"); - const cfg = { - agents: { list: [{ id: "main", agentDir }] }, - models: { - providers: { - "runtime-only": { - baseUrl: "https://example.com/v1", - api: "openai", - models: [{ id: "runtime-model", name: "Runtime Model" }], - }, - }, - }, - } as unknown as OpenClawConfig; - const result = await runProviderAuthWarmWorkerInput({ - cfg, - runtimeAuthStores: [ - { - agentDir, - store: { - version: 1, - profiles: { - "runtime-only:default": { - type: "api_key", - provider: "runtime-only", - }, + await withEnvAsync( + { + OPENCLAW_DISABLE_PERSISTED_PLUGIN_REGISTRY: "1", + OPENCLAW_STATE_DIR: path.join(root, "state"), + }, + async () => { + const agentDir = path.join(root, "agent"); + const cfg = { + agents: { list: [{ id: "main", agentDir }] }, + models: { + providers: { + "runtime-only": { + baseUrl: "https://example.com/v1", + api: "openai", + models: [{ id: "runtime-model", name: "Runtime Model" }], }, }, }, - ], - }); + } as unknown as OpenClawConfig; + const result = await runProviderAuthWarmWorkerInput({ + cfg, + runtimeAuthStores: [ + { + agentDir, + store: { + version: 1, + profiles: { + "runtime-only:default": { + type: "api_key", + provider: "runtime-only", + }, + }, + }, + }, + ], + }); - expect(result.status).toBe("ok"); - if (result.status !== "ok") { - return; - } - expect(result.snapshot.agents[0]?.providers).toContainEqual(["runtime-only", true]); - } finally { - restoreEnv(previousEnv); - } + expect(result.status).toBe("ok"); + if (result.status !== "ok") { + return; + } + expect(result.snapshot.agents[0]?.providers).toContainEqual(["runtime-only", true]); + }, + ); }, 30_000); });