refactor(test): centralize auth test env lifecycle cleanup

This commit is contained in:
Peter Steinberger
2026-02-16 16:09:22 +00:00
parent 9a1e168685
commit 110b1cf46f
5 changed files with 48 additions and 49 deletions

View File

@@ -2,7 +2,6 @@ import type { OAuthCredentials } from "@mariozechner/pi-ai";
import fs from "node:fs/promises";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import {
applyAuthProfileConfig,
applyLitellmProviderConfig,
@@ -29,7 +28,11 @@ import {
ZAI_CODING_CN_BASE_URL,
ZAI_GLOBAL_BASE_URL,
} from "./onboard-auth.js";
import { readAuthProfilesForAgent, setupAuthTestEnv } from "./test-wizard-helpers.js";
import {
createAuthTestLifecycle,
readAuthProfilesForAgent,
setupAuthTestEnv,
} from "./test-wizard-helpers.js";
function createLegacyProviderConfig(params: {
providerId: string;
@@ -62,25 +65,20 @@ function createLegacyProviderConfig(params: {
}
describe("writeOAuthCredentials", () => {
const envSnapshot = captureEnv([
const lifecycle = createAuthTestLifecycle([
"OPENCLAW_STATE_DIR",
"OPENCLAW_AGENT_DIR",
"PI_CODING_AGENT_DIR",
"OPENCLAW_OAUTH_DIR",
]);
let tempStateDir: string | null = null;
afterEach(async () => {
if (tempStateDir) {
await fs.rm(tempStateDir, { recursive: true, force: true });
tempStateDir = null;
}
envSnapshot.restore();
await lifecycle.cleanup();
});
it("writes auth-profiles.json under OPENCLAW_AGENT_DIR when set", async () => {
const env = await setupAuthTestEnv("openclaw-oauth-");
tempStateDir = env.stateDir;
lifecycle.setStateDir(env.stateDir);
const creds = {
refresh: "refresh-token",
@@ -100,30 +98,25 @@ describe("writeOAuthCredentials", () => {
});
await expect(
fs.readFile(path.join(tempStateDir, "agents", "main", "agent", "auth-profiles.json"), "utf8"),
fs.readFile(path.join(env.stateDir, "agents", "main", "agent", "auth-profiles.json"), "utf8"),
).rejects.toThrow();
});
});
describe("setMinimaxApiKey", () => {
const envSnapshot = captureEnv([
const lifecycle = createAuthTestLifecycle([
"OPENCLAW_STATE_DIR",
"OPENCLAW_AGENT_DIR",
"PI_CODING_AGENT_DIR",
]);
let tempStateDir: string | null = null;
afterEach(async () => {
if (tempStateDir) {
await fs.rm(tempStateDir, { recursive: true, force: true });
tempStateDir = null;
}
envSnapshot.restore();
await lifecycle.cleanup();
});
it("writes to OPENCLAW_AGENT_DIR when set", async () => {
const env = await setupAuthTestEnv("openclaw-minimax-", { agentSubdir: "custom-agent" });
tempStateDir = env.stateDir;
lifecycle.setStateDir(env.stateDir);
await setMinimaxApiKey("sk-minimax-test");
@@ -137,7 +130,7 @@ describe("setMinimaxApiKey", () => {
});
await expect(
fs.readFile(path.join(tempStateDir, "agents", "main", "agent", "auth-profiles.json"), "utf8"),
fs.readFile(path.join(env.stateDir, "agents", "main", "agent", "auth-profiles.json"), "utf8"),
).rejects.toThrow();
});
});