diff --git a/src/commands/auth-choice.e2e.test.ts b/src/commands/auth-choice.e2e.test.ts index 2d61c858e4..a886f01d27 100644 --- a/src/commands/auth-choice.e2e.test.ts +++ b/src/commands/auth-choice.e2e.test.ts @@ -399,54 +399,47 @@ describe("applyAuthChoice", () => { expect(result.agentModelOverride).toBe("opencode/claude-opus-4-6"); }); - it("does not persist literal 'undefined' when Anthropic API key prompt returns undefined", async () => { - await setupTempState(); - delete process.env.ANTHROPIC_API_KEY; + it("does not persist literal 'undefined' when API key prompts return undefined", async () => { + const scenarios = [ + { + authChoice: "apiKey" as const, + envKey: "ANTHROPIC_API_KEY", + profileId: "anthropic:default", + provider: "anthropic", + }, + { + authChoice: "openrouter-api-key" as const, + envKey: "OPENROUTER_API_KEY", + profileId: "openrouter:default", + provider: "openrouter", + }, + ]; - const text = vi.fn(async () => undefined as unknown as string); - const prompter = createPrompter({ text }); - const runtime = createExitThrowingRuntime(); + for (const scenario of scenarios) { + await setupTempState(); + delete process.env[scenario.envKey]; - const result = await applyAuthChoice({ - authChoice: "apiKey", - config: {}, - prompter, - runtime, - setDefaultModel: false, - }); + const text = vi.fn(async () => undefined as unknown as string); + const prompter = createPrompter({ text }); + const runtime = createExitThrowingRuntime(); - expect(result.config.auth?.profiles?.["anthropic:default"]).toMatchObject({ - provider: "anthropic", - mode: "api_key", - }); + const result = await applyAuthChoice({ + authChoice: scenario.authChoice, + config: {}, + prompter, + runtime, + setDefaultModel: false, + }); - expect((await readAuthProfile("anthropic:default"))?.key).toBe(""); - expect((await readAuthProfile("anthropic:default"))?.key).not.toBe("undefined"); - }); + expect(result.config.auth?.profiles?.[scenario.profileId]).toMatchObject({ + provider: scenario.provider, + mode: "api_key", + }); - it("does not persist literal 'undefined' when OpenRouter API key prompt returns undefined", async () => { - await setupTempState(); - delete process.env.OPENROUTER_API_KEY; - - const text = vi.fn(async () => undefined as unknown as string); - const prompter = createPrompter({ text }); - const runtime = createExitThrowingRuntime(); - - const result = await applyAuthChoice({ - authChoice: "openrouter-api-key", - config: {}, - prompter, - runtime, - setDefaultModel: false, - }); - - expect(result.config.auth?.profiles?.["openrouter:default"]).toMatchObject({ - provider: "openrouter", - mode: "api_key", - }); - - expect((await readAuthProfile("openrouter:default"))?.key).toBe(""); - expect((await readAuthProfile("openrouter:default"))?.key).not.toBe("undefined"); + const profile = await readAuthProfile(scenario.profileId); + expect(profile?.key).toBe(""); + expect(profile?.key).not.toBe("undefined"); + } }); it("uses existing OPENROUTER_API_KEY when selecting openrouter-api-key", async () => {