From f1351fc545303915135b0f832df1c2b8ed0be8de Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 16 Feb 2026 15:44:33 +0000 Subject: [PATCH] refactor(test): centralize auth test agent-dir helpers --- src/commands/auth-choice.e2e.test.ts | 16 ++++------------ src/commands/auth-choice.moonshot.e2e.test.ts | 11 ++--------- src/commands/test-wizard-helpers.ts | 14 +++++++++++++- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/commands/auth-choice.e2e.test.ts b/src/commands/auth-choice.e2e.test.ts index 57c2485a290..b2344e11654 100644 --- a/src/commands/auth-choice.e2e.test.ts +++ b/src/commands/auth-choice.e2e.test.ts @@ -1,5 +1,4 @@ import fs from "node:fs/promises"; -import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { WizardPrompter } from "../wizard/prompts.js"; import type { AuthChoice } from "./onboard-types.js"; @@ -11,9 +10,11 @@ import { ZAI_CODING_GLOBAL_BASE_URL, } from "./onboard-auth.js"; import { + authProfilePathForAgent, createExitThrowingRuntime, createWizardPrompter, readAuthProfilesForAgent, + requireOpenClawAgentDir, setupAuthTestEnv, } from "./test-wizard-helpers.js"; @@ -31,14 +32,6 @@ vi.mock("../plugins/providers.js", () => ({ resolvePluginProviders, })); -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; -}; type StoredAuthProfile = { key?: string; access?: string; @@ -75,7 +68,7 @@ describe("applyAuthChoice", () => { async function readAuthProfiles() { return await readAuthProfilesForAgent<{ profiles?: Record; - }>(requireAgentDir()); + }>(requireOpenClawAgentDir()); } async function readAuthProfile(profileId: string) { return (await readAuthProfiles()).profiles?.[profileId]; @@ -521,8 +514,7 @@ describe("applyAuthChoice", () => { await setupTempState(); process.env.LITELLM_API_KEY = "sk-litellm-test"; - const authProfilePath = authProfilePathFor(requireAgentDir()); - await fs.mkdir(path.dirname(authProfilePath), { recursive: true }); + const authProfilePath = authProfilePathForAgent(requireOpenClawAgentDir()); await fs.writeFile( authProfilePath, JSON.stringify( diff --git a/src/commands/auth-choice.moonshot.e2e.test.ts b/src/commands/auth-choice.moonshot.e2e.test.ts index 1dfc392a7ba..d28148838aa 100644 --- a/src/commands/auth-choice.moonshot.e2e.test.ts +++ b/src/commands/auth-choice.moonshot.e2e.test.ts @@ -7,17 +7,10 @@ import { createExitThrowingRuntime, createWizardPrompter, readAuthProfilesForAgent, + requireOpenClawAgentDir, setupAuthTestEnv, } from "./test-wizard-helpers.js"; -const requireAgentDir = () => { - const agentDir = process.env.OPENCLAW_AGENT_DIR; - if (!agentDir) { - throw new Error("OPENCLAW_AGENT_DIR not set"); - } - return agentDir; -}; - function createPrompter(overrides: Partial): WizardPrompter { return createWizardPrompter(overrides, { defaultSelect: "" }); } @@ -40,7 +33,7 @@ describe("applyAuthChoice (moonshot)", () => { async function readAuthProfiles() { return await readAuthProfilesForAgent<{ profiles?: Record; - }>(requireAgentDir()); + }>(requireOpenClawAgentDir()); } afterEach(async () => { diff --git a/src/commands/test-wizard-helpers.ts b/src/commands/test-wizard-helpers.ts index 6394ec99898..1fc1f09cbc3 100644 --- a/src/commands/test-wizard-helpers.ts +++ b/src/commands/test-wizard-helpers.ts @@ -51,7 +51,19 @@ export async function setupAuthTestEnv( return { stateDir, agentDir }; } +export function requireOpenClawAgentDir(): string { + const agentDir = process.env.OPENCLAW_AGENT_DIR; + if (!agentDir) { + throw new Error("OPENCLAW_AGENT_DIR not set"); + } + return agentDir; +} + +export function authProfilePathForAgent(agentDir: string): string { + return path.join(agentDir, "auth-profiles.json"); +} + export async function readAuthProfilesForAgent(agentDir: string): Promise { - const raw = await fs.readFile(path.join(agentDir, "auth-profiles.json"), "utf8"); + const raw = await fs.readFile(authProfilePathForAgent(agentDir), "utf8"); return JSON.parse(raw) as T; }