fix(commands): resolve provider auth choices from plugin runtime

This commit is contained in:
Vincent Koc
2026-04-06 14:38:06 +01:00
parent 029290c8d0
commit a6c854f363
2 changed files with 125 additions and 8 deletions

View File

@@ -0,0 +1,89 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import type { ProviderPlugin } from "../plugins/types.js";
import { normalizeApiKeyTokenProviderAuthChoice } from "./auth-choice.apply.api-providers.js";
const resolvePluginProviders = vi.hoisted(() =>
vi.fn<typeof import("../plugins/provider-auth-choice.runtime.js").resolvePluginProviders>(),
);
vi.mock("../plugins/provider-auth-choice.runtime.js", () => ({
resolvePluginProviders,
}));
function createProvider(params: {
id: string;
aliases?: string[];
auth: Array<{
id: string;
kind: ProviderPlugin["auth"][number]["kind"];
choiceId?: string;
}>;
}): ProviderPlugin {
return {
id: params.id,
label: params.id,
...(params.aliases ? { aliases: params.aliases } : {}),
auth: params.auth.map((method) => ({
id: method.id,
label: method.id,
kind: method.kind,
...(method.choiceId ? { wizard: { choiceId: method.choiceId } } : {}),
run: vi.fn(async () => ({ profiles: [] })),
})),
};
}
describe("normalizeApiKeyTokenProviderAuthChoice", () => {
afterEach(() => {
resolvePluginProviders.mockReset();
});
it("maps token provider auth through plugin token methods", () => {
resolvePluginProviders.mockReturnValue([
createProvider({
id: "anthropic",
auth: [{ id: "setup-token", kind: "token", choiceId: "setup-token" }],
}),
]);
expect(
normalizeApiKeyTokenProviderAuthChoice({
authChoice: "token",
tokenProvider: " anthropic ",
}),
).toBe("setup-token");
});
it("maps apiKey provider auth through plugin api key methods and aliases", () => {
resolvePluginProviders.mockReturnValue([
createProvider({
id: "google",
aliases: ["gemini"],
auth: [{ id: "api-key", kind: "api_key", choiceId: "gemini-api-key" }],
}),
]);
expect(
normalizeApiKeyTokenProviderAuthChoice({
authChoice: "apiKey",
tokenProvider: " GeMiNi ",
}),
).toBe("gemini-api-key");
});
it("leaves the auth choice unchanged when no matching provider method exists", () => {
resolvePluginProviders.mockReturnValue([
createProvider({
id: "openai",
auth: [{ id: "api-key", kind: "api_key", choiceId: "openai-api-key" }],
}),
]);
expect(
normalizeApiKeyTokenProviderAuthChoice({
authChoice: "token",
tokenProvider: "openai",
}),
).toBe("token");
});
});

View File

@@ -1,8 +1,30 @@
import { resolveManifestProviderApiKeyChoice } from "../plugins/provider-auth-choices.js";
import { resolveProviderMatch } from "../plugins/provider-auth-choice-helpers.js";
import { resolvePluginProviders } from "../plugins/provider-auth-choice.runtime.js";
import type { ProviderAuthKind } from "../plugins/types.js";
import { normalizeTokenProviderInput } from "./auth-choice.apply-helpers.js";
import type { ApplyAuthChoiceParams, ApplyAuthChoiceResult } from "./auth-choice.apply.js";
import type { AuthChoice } from "./onboard-types.js";
function resolveProviderAuthChoiceByKind(params: {
providerId: string;
kind: ProviderAuthKind;
config?: ApplyAuthChoiceParams["config"];
workspaceDir?: string;
env?: NodeJS.ProcessEnv;
}): AuthChoice | undefined {
const provider = resolveProviderMatch(
resolvePluginProviders({
config: params.config,
workspaceDir: params.workspaceDir,
env: params.env,
mode: "setup",
}),
params.providerId,
);
const choiceId = provider?.auth.find((method) => method.kind === params.kind)?.wizard?.choiceId;
return choiceId as AuthChoice | undefined;
}
export function normalizeApiKeyTokenProviderAuthChoice(params: {
authChoice: AuthChoice;
tokenProvider?: string;
@@ -17,22 +39,28 @@ export function normalizeApiKeyTokenProviderAuthChoice(params: {
if (!normalizedTokenProvider) {
return params.authChoice;
}
if (
(params.authChoice === "token" || params.authChoice === "setup-token") &&
normalizedTokenProvider === "anthropic"
) {
return "setup-token";
if (params.authChoice === "token" || params.authChoice === "setup-token") {
return (
resolveProviderAuthChoiceByKind({
providerId: normalizedTokenProvider,
kind: "token",
config: params.config,
workspaceDir: params.workspaceDir,
env: params.env,
}) ?? params.authChoice
);
}
if (params.authChoice !== "apiKey") {
return params.authChoice;
}
return (
(resolveManifestProviderApiKeyChoice({
resolveProviderAuthChoiceByKind({
providerId: normalizedTokenProvider,
kind: "api_key",
config: params.config,
workspaceDir: params.workspaceDir,
env: params.env,
})?.choiceId as AuthChoice | undefined) ?? params.authChoice
}) ?? params.authChoice
);
}