diff --git a/src/commands/auth-choice.moonshot.e2e.test.ts b/src/commands/auth-choice.moonshot.e2e.test.ts index 8bddbd7a6..2e467ae7f 100644 --- a/src/commands/auth-choice.moonshot.e2e.test.ts +++ b/src/commands/auth-choice.moonshot.e2e.test.ts @@ -17,6 +17,30 @@ const requireAgentDir = () => { return agentDir; }; +function createRuntime(): RuntimeEnv { + return { + log: vi.fn(), + error: vi.fn(), + exit: vi.fn((code: number) => { + throw new Error(`exit:${code}`); + }), + }; +} + +function createPrompter(overrides: Partial): WizardPrompter { + return { + intro: vi.fn(noopAsync), + outro: vi.fn(noopAsync), + note: vi.fn(noopAsync), + select: vi.fn(async () => "" as never), + multiselect: vi.fn(async () => []), + text: vi.fn(async () => "") as unknown as WizardPrompter["text"], + confirm: vi.fn(async () => false), + progress: vi.fn(() => ({ update: noop, stop: noop })), + ...overrides, + }; +} + describe("applyAuthChoice (moonshot)", () => { const previousStateDir = process.env.OPENCLAW_STATE_DIR; const previousAgentDir = process.env.OPENCLAW_AGENT_DIR; @@ -24,6 +48,14 @@ describe("applyAuthChoice (moonshot)", () => { const previousMoonshotKey = process.env.MOONSHOT_API_KEY; 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; + delete process.env.MOONSHOT_API_KEY; + } + afterEach(async () => { if (tempStateDir) { await fs.rm(tempStateDir, { recursive: true, force: true }); @@ -52,30 +84,11 @@ describe("applyAuthChoice (moonshot)", () => { }); it("keeps the .cn baseUrl when setDefaultModel is false", async () => { - 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; - delete process.env.MOONSHOT_API_KEY; + await setupTempState(); const text = vi.fn().mockResolvedValue("sk-moonshot-cn-test"); - const prompter: WizardPrompter = { - intro: vi.fn(noopAsync), - outro: vi.fn(noopAsync), - note: vi.fn(noopAsync), - select: vi.fn(async () => "" as never), - multiselect: vi.fn(async () => []), - text, - confirm: vi.fn(async () => false), - progress: vi.fn(() => ({ update: noop, stop: noop })), - }; - const runtime: RuntimeEnv = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn((code: number) => { - throw new Error(`exit:${code}`); - }), - }; + const prompter = createPrompter({ text: text as unknown as WizardPrompter["text"] }); + const runtime = createRuntime(); const result = await applyAuthChoice({ authChoice: "moonshot-api-key-cn", @@ -107,30 +120,11 @@ describe("applyAuthChoice (moonshot)", () => { }); it("sets the default model when setDefaultModel is true", async () => { - 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; - delete process.env.MOONSHOT_API_KEY; + await setupTempState(); const text = vi.fn().mockResolvedValue("sk-moonshot-cn-test"); - const prompter: WizardPrompter = { - intro: vi.fn(noopAsync), - outro: vi.fn(noopAsync), - note: vi.fn(noopAsync), - select: vi.fn(async () => "" as never), - multiselect: vi.fn(async () => []), - text, - confirm: vi.fn(async () => false), - progress: vi.fn(() => ({ update: noop, stop: noop })), - }; - const runtime: RuntimeEnv = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn((code: number) => { - throw new Error(`exit:${code}`); - }), - }; + const prompter = createPrompter({ text: text as unknown as WizardPrompter["text"] }); + const runtime = createRuntime(); const result = await applyAuthChoice({ authChoice: "moonshot-api-key-cn", diff --git a/src/commands/onboard-channels.e2e.test.ts b/src/commands/onboard-channels.e2e.test.ts index 25c1c6fc2..210ef5b7a 100644 --- a/src/commands/onboard-channels.e2e.test.ts +++ b/src/commands/onboard-channels.e2e.test.ts @@ -12,6 +12,32 @@ import { setActivePluginRegistry } from "../plugins/runtime.js"; import { createTestRegistry } from "../test-utils/channel-plugins.js"; import { setupChannels } from "./onboard-channels.js"; +const noopAsync = async () => {}; + +function createRuntime(): RuntimeEnv { + return { + log: vi.fn(), + error: vi.fn(), + exit: vi.fn((code: number) => { + throw new Error(`exit:${code}`); + }), + }; +} + +function createPrompter(overrides: Partial): WizardPrompter { + return { + intro: vi.fn(noopAsync), + outro: vi.fn(noopAsync), + note: vi.fn(noopAsync), + select: vi.fn(async () => "__done__" as never), + multiselect: vi.fn(async () => []), + text: vi.fn(async () => "") as unknown as WizardPrompter["text"], + confirm: vi.fn(async () => false), + progress: vi.fn(() => ({ update: vi.fn(), stop: vi.fn() })), + ...overrides, + }; +} + vi.mock("node:fs/promises", () => ({ default: { access: vi.fn(async () => { @@ -56,24 +82,13 @@ describe("setupChannels", () => { throw new Error(`unexpected text prompt: ${message}`); }); - const prompter: WizardPrompter = { - intro: vi.fn(async () => {}), - outro: vi.fn(async () => {}), - note: vi.fn(async () => {}), + const prompter = createPrompter({ select, multiselect, text: text as unknown as WizardPrompter["text"], - confirm: vi.fn(async () => false), - progress: vi.fn(() => ({ update: vi.fn(), stop: vi.fn() })), - }; + }); - const runtime: RuntimeEnv = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn((code: number) => { - throw new Error(`exit:${code}`); - }), - }; + const runtime = createRuntime(); await setupChannels({} as OpenClawConfig, runtime, prompter, { skipConfirm: true, @@ -97,24 +112,14 @@ describe("setupChannels", () => { throw new Error(`unexpected text prompt: ${message}`); }); - const prompter: WizardPrompter = { - intro: vi.fn(async () => {}), - outro: vi.fn(async () => {}), + const prompter = createPrompter({ note, select, multiselect, text: text as unknown as WizardPrompter["text"], - confirm: vi.fn(async () => false), - progress: vi.fn(() => ({ update: vi.fn(), stop: vi.fn() })), - }; + }); - const runtime: RuntimeEnv = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn((code: number) => { - throw new Error(`exit:${code}`); - }), - }; + const runtime = createRuntime(); await setupChannels({} as OpenClawConfig, runtime, prompter, { skipConfirm: true, @@ -146,24 +151,13 @@ describe("setupChannels", () => { throw new Error(`unexpected text prompt: ${message}`); }); - const prompter: WizardPrompter = { - intro: vi.fn(async () => {}), - outro: vi.fn(async () => {}), - note: vi.fn(async () => {}), + const prompter = createPrompter({ select, multiselect, text: text as unknown as WizardPrompter["text"], - confirm: vi.fn(async () => false), - progress: vi.fn(() => ({ update: vi.fn(), stop: vi.fn() })), - }; + }); - const runtime: RuntimeEnv = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn((code: number) => { - throw new Error(`exit:${code}`); - }), - }; + const runtime = createRuntime(); await setupChannels( { @@ -209,24 +203,13 @@ describe("setupChannels", () => { const multiselect = vi.fn(async () => { throw new Error("unexpected multiselect"); }); - const prompter: WizardPrompter = { - intro: vi.fn(async () => {}), - outro: vi.fn(async () => {}), - note: vi.fn(async () => {}), + const prompter = createPrompter({ select, multiselect, - text: vi.fn(async () => ""), - confirm: vi.fn(async () => false), - progress: vi.fn(() => ({ update: vi.fn(), stop: vi.fn() })), - }; + text: vi.fn(async () => "") as unknown as WizardPrompter["text"], + }); - const runtime: RuntimeEnv = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn((code: number) => { - throw new Error(`exit:${code}`); - }), - }; + const runtime = createRuntime(); await setupChannels( {