mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-01 18:48:16 +00:00
refactor(test): simplify model auth env restore
This commit is contained in:
@@ -3,6 +3,7 @@ import fs from "node:fs/promises";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { captureEnv } from "../test-utils/env.js";
|
||||||
import { ensureAuthProfileStore } from "./auth-profiles.js";
|
import { ensureAuthProfileStore } from "./auth-profiles.js";
|
||||||
import { getApiKeyForModel, resolveApiKeyForProvider, resolveEnvApiKey } from "./model-auth.js";
|
import { getApiKeyForModel, resolveApiKeyForProvider, resolveEnvApiKey } from "./model-auth.js";
|
||||||
|
|
||||||
@@ -15,9 +16,11 @@ const oauthFixture = {
|
|||||||
|
|
||||||
describe("getApiKeyForModel", () => {
|
describe("getApiKeyForModel", () => {
|
||||||
it("migrates legacy oauth.json into auth-profiles.json", async () => {
|
it("migrates legacy oauth.json into auth-profiles.json", async () => {
|
||||||
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
|
const envSnapshot = captureEnv([
|
||||||
const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
|
"OPENCLAW_STATE_DIR",
|
||||||
const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
|
"OPENCLAW_AGENT_DIR",
|
||||||
|
"PI_CODING_AGENT_DIR",
|
||||||
|
]);
|
||||||
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-oauth-"));
|
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-oauth-"));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -73,30 +76,18 @@ describe("getApiKeyForModel", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
if (previousStateDir === undefined) {
|
envSnapshot.restore();
|
||||||
delete process.env.OPENCLAW_STATE_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.OPENCLAW_STATE_DIR = previousStateDir;
|
|
||||||
}
|
|
||||||
if (previousAgentDir === undefined) {
|
|
||||||
delete process.env.OPENCLAW_AGENT_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.OPENCLAW_AGENT_DIR = previousAgentDir;
|
|
||||||
}
|
|
||||||
if (previousPiAgentDir === undefined) {
|
|
||||||
delete process.env.PI_CODING_AGENT_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.PI_CODING_AGENT_DIR = previousPiAgentDir;
|
|
||||||
}
|
|
||||||
await fs.rm(tempDir, { recursive: true, force: true });
|
await fs.rm(tempDir, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("suggests openai-codex when only Codex OAuth is configured", async () => {
|
it("suggests openai-codex when only Codex OAuth is configured", async () => {
|
||||||
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
|
const envSnapshot = captureEnv([
|
||||||
const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
|
"OPENAI_API_KEY",
|
||||||
const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
|
"OPENCLAW_STATE_DIR",
|
||||||
const previousOpenAiKey = process.env.OPENAI_API_KEY;
|
"OPENCLAW_AGENT_DIR",
|
||||||
|
"PI_CODING_AGENT_DIR",
|
||||||
|
]);
|
||||||
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-auth-"));
|
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-auth-"));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -137,26 +128,7 @@ describe("getApiKeyForModel", () => {
|
|||||||
}
|
}
|
||||||
expect(String(error)).toContain("openai-codex/gpt-5.3-codex");
|
expect(String(error)).toContain("openai-codex/gpt-5.3-codex");
|
||||||
} finally {
|
} finally {
|
||||||
if (previousOpenAiKey === undefined) {
|
envSnapshot.restore();
|
||||||
delete process.env.OPENAI_API_KEY;
|
|
||||||
} else {
|
|
||||||
process.env.OPENAI_API_KEY = previousOpenAiKey;
|
|
||||||
}
|
|
||||||
if (previousStateDir === undefined) {
|
|
||||||
delete process.env.OPENCLAW_STATE_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.OPENCLAW_STATE_DIR = previousStateDir;
|
|
||||||
}
|
|
||||||
if (previousAgentDir === undefined) {
|
|
||||||
delete process.env.OPENCLAW_AGENT_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.OPENCLAW_AGENT_DIR = previousAgentDir;
|
|
||||||
}
|
|
||||||
if (previousPiAgentDir === undefined) {
|
|
||||||
delete process.env.PI_CODING_AGENT_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.PI_CODING_AGENT_DIR = previousPiAgentDir;
|
|
||||||
}
|
|
||||||
await fs.rm(tempDir, { recursive: true, force: true });
|
await fs.rm(tempDir, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user