diff --git a/webview-ui/src/utils/__tests__/validate.provider-validation.spec.ts b/webview-ui/src/utils/__tests__/validate.provider-validation.spec.ts new file mode 100644 index 00000000000..08b7f3f84fb --- /dev/null +++ b/webview-ui/src/utils/__tests__/validate.provider-validation.spec.ts @@ -0,0 +1,273 @@ +import { + providerIdentifiers, + type OrganizationAllowList, + type ProviderSettings, + type RouterModels, +} from "@roo-code/types" +import type { TFunction } from "i18next" + +import { getModelValidationError, validateApiConfigurationExcludingModelErrors, validateBedrockArn } from "../validate" + +// `TFunction` is branded; this double only answers a key plus its interpolation values, which is +// what these cases assert on: the key a provider earns and the values handed to the translator. +const t = ((key: string, options?: Record) => { + if (options) { + return Object.entries(options).reduce((acc, [k, v]) => `${acc} ${k}=${v}`, key) + } + return key +}) as unknown as TFunction + +const routerModels: RouterModels = { + openrouter: { + "valid-model": { + maxTokens: 8192, + contextWindow: 200000, + supportsImages: true, + supportsPromptCache: false, + inputPrice: 3, + outputPrice: 15, + }, + "another-valid-model": { + maxTokens: 4096, + contextWindow: 100000, + supportsImages: false, + supportsPromptCache: false, + inputPrice: 1, + outputPrice: 5, + }, + }, + requesty: {}, + unbound: {}, + litellm: {}, + ollama: {}, + lmstudio: {}, + "vercel-ai-gateway": {}, + poe: {}, + deepseek: {}, + "opencode-go": {}, + kenari: {}, + nanogpt: {}, + "zoo-gateway": {}, + "kimi-code": {}, + friendli: {}, + moonshot: {}, +} + +const restrictiveOrganization: OrganizationAllowList = { allowAll: false, providers: {} } + +describe("validateApiConfigurationExcludingModelErrors - the credential each provider asks for", () => { + // Each row of this matrix only proves a rejection: a provider branch that kept returning the + // same error after the value that branch asks for was supplied would still pass every row + // below. Each row therefore carries its pair - the same provider with that value present - + // asserted through the same entry point, so the accept path is pinned next to the reject path. + const cases: { + name: string + config: ProviderSettings + expected: string + pair: { name: string; config: ProviderSettings } + }[] = [ + { + name: "requesty without its key", + config: { apiProvider: providerIdentifiers.requesty }, + expected: "settings:validation.apiKey", + pair: { + name: "requesty with its key", + config: { apiProvider: providerIdentifiers.requesty, requestyApiKey: "mock-key" }, + }, + }, + { + name: "unbound without its key", + config: { apiProvider: providerIdentifiers.unbound }, + expected: "settings:validation.apiKey", + pair: { + name: "unbound with its key", + config: { apiProvider: providerIdentifiers.unbound, unboundApiKey: "mock-key" }, + }, + }, + { + name: "litellm without its key", + config: { apiProvider: providerIdentifiers.litellm }, + expected: "settings:validation.apiKey", + pair: { + name: "litellm with its key", + config: { apiProvider: providerIdentifiers.litellm, litellmApiKey: "mock-key" }, + }, + }, + { + name: "gemini without its key", + config: { apiProvider: providerIdentifiers.gemini }, + expected: "settings:validation.apiKey", + pair: { + name: "gemini with its key", + config: { apiProvider: providerIdentifiers.gemini, geminiApiKey: "mock-key" }, + }, + }, + { + name: "openai-native without its key", + config: { apiProvider: providerIdentifiers.openaiNative }, + expected: "settings:validation.apiKey", + pair: { + name: "openai-native with its key", + config: { apiProvider: providerIdentifiers.openaiNative, openAiNativeApiKey: "mock-key" }, + }, + }, + { + name: "mistral without its key", + config: { apiProvider: providerIdentifiers.mistral }, + expected: "settings:validation.apiKey", + pair: { + name: "mistral with its key", + config: { apiProvider: providerIdentifiers.mistral, mistralApiKey: "mock-key" }, + }, + }, + { + name: "fireworks without its key", + config: { apiProvider: providerIdentifiers.fireworks }, + expected: "settings:validation.apiKey", + pair: { + name: "fireworks with its key", + config: { apiProvider: providerIdentifiers.fireworks, fireworksApiKey: "mock-key" }, + }, + }, + { + name: "vercel-ai-gateway without its key", + config: { apiProvider: providerIdentifiers.vercelAiGateway }, + expected: "settings:validation.apiKey", + pair: { + name: "vercel-ai-gateway with its key", + config: { apiProvider: providerIdentifiers.vercelAiGateway, vercelAiGatewayApiKey: "mock-key" }, + }, + }, + { + name: "baseten without its key", + config: { apiProvider: providerIdentifiers.baseten }, + expected: "settings:validation.apiKey", + pair: { + name: "baseten with its key", + config: { apiProvider: providerIdentifiers.baseten, basetenApiKey: "mock-key" }, + }, + }, + { + name: "bedrock without a region", + config: { apiProvider: providerIdentifiers.bedrock }, + expected: "settings:validation.awsRegion", + pair: { + name: "bedrock with a region", + config: { apiProvider: providerIdentifiers.bedrock, awsRegion: "us-east-1" }, + }, + }, + { + name: "vertex with a project but no region", + config: { apiProvider: providerIdentifiers.vertex, vertexProjectId: "mock-project" }, + expected: "settings:validation.googleCloud", + pair: { + name: "vertex with its project and region", + config: { + apiProvider: providerIdentifiers.vertex, + vertexProjectId: "mock-project", + vertexRegion: "us-east1", + }, + }, + }, + { + name: "ollama without a model id", + config: { apiProvider: providerIdentifiers.ollama }, + expected: "settings:validation.modelId", + pair: { + name: "ollama with a model id", + config: { apiProvider: providerIdentifiers.ollama, ollamaModelId: "mock-model" }, + }, + }, + { + name: "lmstudio without a model id", + config: { apiProvider: providerIdentifiers.lmstudio }, + expected: "settings:validation.modelId", + pair: { + name: "lmstudio with a model id", + config: { apiProvider: providerIdentifiers.lmstudio, lmStudioModelId: "mock-model" }, + }, + }, + { + name: "vscode-lm without a model selector", + config: { apiProvider: providerIdentifiers.vscodeLm }, + expected: "settings:validation.modelSelector", + pair: { + name: "vscode-lm with a model selector", + config: { + apiProvider: providerIdentifiers.vscodeLm, + vsCodeLmModelSelector: { vendor: "mock-vendor", family: "mock-family" }, + }, + }, + }, + { + name: "qwen-code without an oauth path", + config: { apiProvider: providerIdentifiers.qwenCode }, + expected: "settings:validation.qwenCodeOauthPath", + pair: { + name: "qwen-code with an oauth path", + config: { apiProvider: providerIdentifiers.qwenCode, qwenCodeOauthPath: "mock-oauth-path" }, + }, + }, + ] + + cases.forEach(({ name, config, expected, pair }) => { + it(`returns ${expected} for ${name}`, () => { + expect(validateApiConfigurationExcludingModelErrors(config, t)).toBe(expected) + }) + + // The pair of the row above: the same provider once the value that branch asks for is in + // the configuration. This is the half the rejection matrix cannot show on its own - the + // successful return value, which is no error key at all. + it(`returns no error for ${pair.name}`, () => { + expect(validateApiConfigurationExcludingModelErrors(pair.config, t)).toBeUndefined() + }) + }) + + it("names the provider the organization allow-list refused", () => { + const config: ProviderSettings = { + apiProvider: providerIdentifiers.openrouter, + openRouterApiKey: "mock-key", + openRouterModelId: "valid-model", + } + + expect(validateApiConfigurationExcludingModelErrors(config, t, undefined, restrictiveOrganization)).toBe( + `settings:validation.providerNotAllowed provider=${providerIdentifiers.openrouter}`, + ) + }) +}) + +describe("validateBedrockArn - the pair a region mismatch reports", () => { + it("reports both the ARN region and the configured region", () => { + const arn = + "arn:aws:bedrock:eu-west-1:123456789012:inference-profile/eu.anthropic.claude-3-7-sonnet-20250219-v1:0" + + const result = validateBedrockArn(arn, t, "us-east-1") + + expect(result.isValid).toBe(true) + expect(result.arnRegion).toBe("eu-west-1") + expect(result.errorMessage).toBe("settings:validation.arn.regionMismatch arnRegion=eu-west-1 region=us-east-1") + }) +}) + +describe("getModelValidationError - the model a dynamic provider rejected", () => { + it("names the model id that is not in the fetched list", () => { + const config: ProviderSettings = { + apiProvider: providerIdentifiers.openrouter, + openRouterApiKey: "mock-key", + openRouterModelId: "not-in-the-list", + } + + expect(getModelValidationError(config, t, routerModels)).toBe( + "settings:validation.modelAvailability modelId=not-in-the-list", + ) + }) + it("accepts the model that is in the fetched list", () => { + const config: ProviderSettings = { + apiProvider: providerIdentifiers.openrouter, + openRouterApiKey: "mock-key", + openRouterModelId: "valid-model", + } + + expect(getModelValidationError(config, t, routerModels)).toBeUndefined() + }) +})