refactor(test): share auth test env/profile helpers

This commit is contained in:
Peter Steinberger
2026-02-16 15:25:23 +00:00
parent 1d37389490
commit 94f455c693
4 changed files with 79 additions and 69 deletions

View File

@@ -1,6 +1,5 @@
import type { OAuthCredentials } from "@mariozechner/pi-ai";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { captureEnv } from "../test-utils/env.js";
@@ -30,15 +29,7 @@ import {
ZAI_CODING_CN_BASE_URL,
ZAI_GLOBAL_BASE_URL,
} from "./onboard-auth.js";
const authProfilePathFor = (agentDir: string) => path.join(agentDir, "auth-profiles.json");
const requireAgentDir = () => {
const agentDir = process.env.OPENCLAW_AGENT_DIR;
if (!agentDir) {
throw new Error("OPENCLAW_AGENT_DIR not set");
}
return agentDir;
};
import { readAuthProfilesForAgent, setupAuthTestEnv } from "./test-wizard-helpers.js";
function createLegacyProviderConfig(params: {
providerId: string;
@@ -88,10 +79,8 @@ describe("writeOAuthCredentials", () => {
});
it("writes auth-profiles.json under OPENCLAW_AGENT_DIR when set", async () => {
tempStateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-oauth-"));
process.env.OPENCLAW_STATE_DIR = tempStateDir;
process.env.OPENCLAW_AGENT_DIR = path.join(tempStateDir, "agent");
process.env.PI_CODING_AGENT_DIR = process.env.OPENCLAW_AGENT_DIR;
const env = await setupAuthTestEnv("openclaw-oauth-");
tempStateDir = env.stateDir;
const creds = {
refresh: "refresh-token",
@@ -101,11 +90,9 @@ describe("writeOAuthCredentials", () => {
await writeOAuthCredentials("openai-codex", creds);
const authProfilePath = authProfilePathFor(requireAgentDir());
const raw = await fs.readFile(authProfilePath, "utf8");
const parsed = JSON.parse(raw) as {
const parsed = await readAuthProfilesForAgent<{
profiles?: Record<string, OAuthCredentials & { type?: string }>;
};
}>(env.agentDir);
expect(parsed.profiles?.["openai-codex:default"]).toMatchObject({
refresh: "refresh-token",
access: "access-token",
@@ -135,18 +122,14 @@ describe("setMinimaxApiKey", () => {
});
it("writes to OPENCLAW_AGENT_DIR when set", async () => {
tempStateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-minimax-"));
process.env.OPENCLAW_STATE_DIR = tempStateDir;
process.env.OPENCLAW_AGENT_DIR = path.join(tempStateDir, "custom-agent");
process.env.PI_CODING_AGENT_DIR = process.env.OPENCLAW_AGENT_DIR;
const env = await setupAuthTestEnv("openclaw-minimax-", { agentSubdir: "custom-agent" });
tempStateDir = env.stateDir;
await setMinimaxApiKey("sk-minimax-test");
const customAuthPath = authProfilePathFor(requireAgentDir());
const raw = await fs.readFile(customAuthPath, "utf8");
const parsed = JSON.parse(raw) as {
const parsed = await readAuthProfilesForAgent<{
profiles?: Record<string, { type?: string; provider?: string; key?: string }>;
};
}>(env.agentDir);
expect(parsed.profiles?.["minimax:default"]).toMatchObject({
type: "api_key",
provider: "minimax",