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,13 +1,15 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { WizardPrompter } from "../wizard/prompts.js";
import { captureEnv } from "../test-utils/env.js";
import { applyAuthChoice } from "./auth-choice.js";
import { createExitThrowingRuntime, createWizardPrompter } from "./test-wizard-helpers.js";
import {
createExitThrowingRuntime,
createWizardPrompter,
readAuthProfilesForAgent,
setupAuthTestEnv,
} from "./test-wizard-helpers.js";
const authProfilePathFor = (agentDir: string) => path.join(agentDir, "auth-profiles.json");
const requireAgentDir = () => {
const agentDir = process.env.OPENCLAW_AGENT_DIR;
if (!agentDir) {
@@ -30,13 +32,17 @@ describe("applyAuthChoice (moonshot)", () => {
let tempStateDir: string | null = null;
async function setupTempState() {
tempStateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-auth-"));
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-auth-");
tempStateDir = env.stateDir;
delete process.env.MOONSHOT_API_KEY;
}
async function readAuthProfiles() {
return await readAuthProfilesForAgent<{
profiles?: Record<string, { key?: string }>;
}>(requireAgentDir());
}
afterEach(async () => {
if (tempStateDir) {
await fs.rm(tempStateDir, { recursive: true, force: true });
@@ -73,11 +79,7 @@ describe("applyAuthChoice (moonshot)", () => {
expect(result.config.models?.providers?.moonshot?.baseUrl).toBe("https://api.moonshot.cn/v1");
expect(result.agentModelOverride).toBe("moonshot/kimi-k2.5");
const authProfilePath = authProfilePathFor(requireAgentDir());
const raw = await fs.readFile(authProfilePath, "utf8");
const parsed = JSON.parse(raw) as {
profiles?: Record<string, { key?: string }>;
};
const parsed = await readAuthProfiles();
expect(parsed.profiles?.["moonshot:default"]?.key).toBe("sk-moonshot-cn-test");
});
@@ -100,11 +102,7 @@ describe("applyAuthChoice (moonshot)", () => {
expect(result.config.models?.providers?.moonshot?.baseUrl).toBe("https://api.moonshot.cn/v1");
expect(result.agentModelOverride).toBeUndefined();
const authProfilePath = authProfilePathFor(requireAgentDir());
const raw = await fs.readFile(authProfilePath, "utf8");
const parsed = JSON.parse(raw) as {
profiles?: Record<string, { key?: string }>;
};
const parsed = await readAuthProfiles();
expect(parsed.profiles?.["moonshot:default"]?.key).toBe("sk-moonshot-cn-test");
});
});