Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

27.1 KB
Raw Blame
package providers_test

import (
	"bytes"
	"fmt"
	"strings"
	"testing"

	"github.com/Tencent/WeKnora/internal/models"
	"github.com/Tencent/WeKnora/internal/models/api"
	"github.com/Tencent/WeKnora/internal/models/api/anthropicmessages"
	"github.com/Tencent/WeKnora/internal/models/providers"
	modelruntime "github.com/Tencent/WeKnora/internal/models/runtime"
	"github.com/Tencent/WeKnora/internal/types"
)

// expectedIDs is every built-in vendor this package links.
var expectedIDs = []string{
	"generic", "weknoracloud", "huggingface_tei",
	"aliyun", "zhipu", "volcengine", "hunyuan", "siliconflow", "deepseek",
	"minimax", "moonshot", "mimo", "modelscope", "qianfan", "qiniu", "longcat", "lkeap",
	"openai", "azure_openai", "anthropic", "gemini",
	"openrouter", "litellm", "requesty", "pinecone",
	"jina", "nvidia", "novita", "gpustack", "cohere",
}

func TestMinimaxOfficialDefaultURLs(t *testing.T) {
	v, ok := modelruntime.Get(providers.MinimaxID)
	if !ok {
		t.Fatal("MiniMax provider missing")
	}
	for _, kind := range []types.ModelType{types.ModelTypeKnowledgeQA, types.ModelTypeASR} {
		if got := v.GetDefaultURL(kind); got != "https://api.minimax.cn/v1" {
			t.Errorf("%s default = %q, want current mainland endpoint", kind, got)
		}
	}
	if providers.MinimaxAnthropicBaseURL != "https://api.minimax.cn/anthropic" {
		t.Errorf("unexpected mainland Anthropic URL: %s", providers.MinimaxAnthropicBaseURL)
	}
	if v.Website != "https://platform.minimax.cn" {
		t.Errorf("unexpected mainland website: %s", v.Website)
	}
	for _, base := range []string{
		"https://api.minimax.cn/v1", "https://api.minimax.cn/anthropic",
		"https://api.minimaxi.com/v1", "https://api.minimaxi.com/anthropic",
		"https://api.minimax.io/v1", "https://api.minimax.io/anthropic",
	} {
		if got := modelruntime.DetectByURL(base); got != providers.MinimaxID {
			t.Errorf("DetectByURL(%q) = %q", base, got)
		}
		resolved, err := modelruntime.Resolve(modelruntime.Ref{BaseURL: base, Model: "MiniMax-M2.7"})
		if err != nil {
			t.Fatal(err)
		}
		if resolved.BaseURL != base {
			t.Errorf("explicit address was rewritten: %q -> %q", base, resolved.BaseURL)
		}
		wantAPI := api.APIOpenAICompletions
		if strings.HasSuffix(base, "/anthropic") {
			wantAPI = api.APIAnthropicMessages
		}
		if resolved.API != wantAPI {
			t.Errorf("%s resolved API = %s, want %s", base, resolved.API, wantAPI)
		}
	}
}

func TestBuiltinDefaultURLsAcrossModelTypes(t *testing.T) {
	for _, vendor := range modelruntime.List() {
		for _, kind := range vendor.ModelTypes {
			base := vendor.GetDefaultURL(kind)
			// These connections have no hosted service to prefill.
			if vendor.ID == "generic" || vendor.ID == "huggingface_tei" {
				if base != "" {
					t.Errorf("%s/%s should require an operator URL", vendor.ID, kind)
				}
				continue
			}
			if !strings.HasPrefix(base, "https://") && !strings.HasPrefix(base, "http://") {
				t.Errorf("%s/%s has no absolute default URL: %q", vendor.ID, kind, base)
			}
		}
	}
}

func TestAllVendorsRegistered(t *testing.T) {
	if len(expectedIDs) != 30 {
		t.Fatalf("expected 30 vendor ids in the spec, got %d", len(expectedIDs))
	}
	for _, id := range expectedIDs {
		v, ok := modelruntime.Get(id)
		if !ok {
			t.Errorf("vendor %q is not registered", id)
			continue
		}
		if v.ID != strings.ToLower(v.ID) {
			t.Errorf("vendor %q: id is not lowercase", id)
		}
		if v.Name == "" {
			t.Errorf("vendor %q: empty Name", id)
		}
		if len(v.Icon) == 0 || !bytes.HasPrefix(bytes.TrimSpace(v.Icon), []byte("<svg")) {
			t.Errorf("vendor %q: icon is empty or not an <svg> document", id)
		}
		if len(v.Icon) > 6*1024 {
			t.Errorf("vendor %q: icon is %d bytes, over the 6 KB budget", id, len(v.Icon))
		}
		if v.RequiresAuth && v.Auth == "" {
			t.Errorf("vendor %q: RequiresAuth without Auth style", id)
		}
		if v.Auth == providers.AuthSigned && v.Signer == nil {
			t.Errorf("vendor %q: AuthSigned without Signer", id)
		}
		if !v.API.Known() {
			t.Errorf("vendor %q: unknown default API %q", id, v.API)
		}
		if len(v.ModelTypes) == 0 {
			t.Errorf("vendor %q: no ModelTypes", id)
		}
	}
	for _, v := range modelruntime.List() {
		found := false
		for _, id := range expectedIDs {
			if id == v.ID {
				found = true
				break
			}
		}
		if !found {
			t.Errorf("unexpected vendor %q registered", v.ID)
		}
	}
}

func TestHuggingFaceTEIRequiresURLButNotAPIKey(t *testing.T) {
	v, ok := modelruntime.Get(providers.HuggingFaceTEIID)
	if !ok {
		t.Fatal("Hugging Face TEI provider not registered")
	}
	if err := v.ValidateConfig(&providers.Config{
		BaseURL: "http://tei.internal:8080", ModelName: "BAAI/bge-reranker-large",
	}); err != nil {
		t.Fatalf("a self-hosted TEI server should not require an API key: %v", err)
	}
	if err := v.ValidateConfig(&providers.Config{ModelName: "BAAI/bge-reranker-large"}); err == nil {
		t.Fatal("TEI provider accepted a missing base URL")
	}
}

func TestEveryCatalogEntryResolves(t *testing.T) {
	for _, id := range expectedIDs {
		v, ok := modelruntime.Get(id)
		if !ok {
			t.Fatalf("vendor %q missing", id)
		}
		for _, m := range v.Models() {
			if m.ID == "" && m.Match == "" {
				t.Errorf("%s: model entry %q has neither id nor match", id, m.Name)
				continue
			}
			name := m.ID
			if name == "" {
				// Exercise pattern entries with a name that matches the glob.
				name = strings.ReplaceAll(m.Match, "*", "x")
			}
			modelType := m.Type
			if modelType == "" {
				modelType = types.ModelTypeKnowledgeQA
			}
			r, err := modelruntime.Resolve(modelruntime.Ref{Provider: v.ID, Model: name, ModelType: modelType})
			// An entry may declare that this build cannot serve it — a vendor
			// whose second rerank dialect has no protocol package. Refusing is
			// the point: the alternative is a request shaped for the wrong
			// protocol. Such an entry must refuse, and must do it with a reason.
			if bytes.Contains(m.Compat, []byte("unsupported_reason")) {
				if err == nil {
					t.Errorf("%s/%s: declares unsupported_reason but still resolves", id, name)
				}
				continue
			}
			if err != nil {
				t.Errorf("%s/%s: resolve: %v", id, name, err)
				continue
			}
			if !r.Cataloged {
				t.Errorf("%s/%s: did not match its own catalog entry", id, name)
			}
			if modelType == types.ModelTypeKnowledgeQA && !r.API.Known() {
				t.Errorf("%s/%s: resolved to unknown API %q", id, name, r.API)
			}
			if modelType == types.ModelTypeASR && !r.TranscriptionAPI.Known() {
				t.Errorf("%s/%s: resolved to unknown transcription API %q", id, name, r.TranscriptionAPI)
			}
			if modelType == types.ModelTypeEmbedding && !r.EmbeddingAPI.Known() {
				t.Errorf("%s/%s: resolved to unknown embedding API %q", id, name, r.EmbeddingAPI)
			}
			if modelType == types.ModelTypeEmbedding && m.ID != "" && m.Dimension <= 0 {
				t.Logf("%s/%s: embedding entry without dimension", id, name)
			}
		}
	}
}

// Model names that fall inside a chat family's glob must still resolve as
// embedding and rerank rows. The families that caught them carry chat compat
// (thinking_always_send on Aliyun's qwen3*, max_tokens_field and
// supports_temperature on gpt-5*), which the embedding and rerank overlays
// reject, so an untyped lookup made these rows impossible to build — new ids
// and dated snapshots first of all, since they are never in models.json yet.
func TestNamesInsideChatGlobsResolveForOtherTypes(t *testing.T) {
	names := []struct{ provider, model string }{
		{"aliyun", "qwen3.8-text-embedding"},
		{"aliyun", "qwen3-embedding"},
		{"aliyun", "qwen3.7-text-embedding-20260601"},
		{"generic", "gpt-5-embed"},
		{"openai", "gpt-5-embed"},
	}
	for _, n := range names {
		for _, modelType := range []types.ModelType{types.ModelTypeEmbedding, types.ModelTypeRerank} {
			r, err := modelruntime.Resolve(modelruntime.Ref{Provider: n.provider, Model: n.model, ModelType: modelType})
			if err != nil {
				t.Errorf("%s/%s as %s: %v", n.provider, n.model, modelType, err)
				continue
			}
			if r.Cataloged {
				t.Errorf("%s/%s as %s matched %q, which is not an entry of that type",
					n.provider, n.model, modelType, r.Spec.Name)
			}
		}
	}
}

func TestDetectByURLRoundTrip(t *testing.T) {
	for _, id := range expectedIDs {
		v, _ := modelruntime.Get(id)
		if len(v.URLPatterns) == 0 {
			continue
		}
		u := v.GetDefaultURL(types.ModelTypeKnowledgeQA)
		if !strings.HasPrefix(u, "https://") || strings.Contains(u, "{") {
			continue // placeholder or http-only self-hosted default
		}
		if got := modelruntime.DetectByURL(u); got != v.ID {
			t.Errorf("DetectByURL(%q) = %q, want %q", u, got, v.ID)
		}
	}
	if got := modelruntime.DetectByURL("http://your_litellm_proxy/v1"); got != "litellm" {
		t.Errorf("litellm placeholder detected as %q", got)
	}
	// GPUStack 2.x serves the OpenAI-compatible surface at /v1; the 0.x
	// /v1-openai path must keep resolving for rows saved before the move.
	if got := modelruntime.DetectByURL("http://your_gpustack_server_url/v1-openai"); got != "gpustack" {
		t.Errorf("gpustack placeholder detected as %q", got)
	}
	if got := modelruntime.DetectByURL("https://api.moonshot.cn/v1"); got != "moonshot" {
		t.Errorf("moonshot.cn detected as %q", got)
	}
}

// TestOpenAIProtocolSelection pins the first-party / relay split: Responses on
// api.openai.com, Chat Completions everywhere else, extra_config.api wins.
func TestOpenAIProtocolSelection(t *testing.T) {
	cases := []struct {
		baseURL string
		extra   map[string]string
		want    api.API
	}{
		{"", nil, api.APIOpenAIResponses},
		{"https://api.openai.com/v1", nil, api.APIOpenAIResponses},
		{"https://API.openai.com/v1/", nil, api.APIOpenAIResponses},
		{"https://my-relay.example.com/v1", nil, api.APIOpenAICompletions},
		{"https://api.openai.com/v1", map[string]string{"api": "openai-completions"}, api.APIOpenAICompletions},
	}
	for _, tc := range cases {
		r, err := modelruntime.Resolve(modelruntime.Ref{
			Provider: "openai", Model: "gpt-5.5", BaseURL: tc.baseURL, Extra: tc.extra,
		})
		if err != nil {
			t.Fatalf("resolve openai %q: %v", tc.baseURL, err)
		}
		if r.API != tc.want {
			t.Errorf("openai base %q extra %v: api = %q, want %q", tc.baseURL, tc.extra, r.API, tc.want)
		}
	}
}

// claudeNoSamplingModels reject non-default temperature/top_p/top_k values.
var claudeNoSamplingModels = []string{
	"claude-opus-4-7", "claude-opus-4-8", "claude-opus-5", "claude-sonnet-5", "claude-fable-5", "claude-fable-5-1",
}

func resolve(t *testing.T, provider, model string) *modelruntime.Resolved {
	t.Helper()
	r, err := modelruntime.Resolve(modelruntime.Ref{Provider: provider, Model: model})
	if err != nil {
		t.Fatalf("resolve %s/%s: %v", provider, model, err)
	}
	return r
}

func TestFamilyExpectations(t *testing.T) {
	if r := resolve(t, "openai", "gpt-5.2"); r.OpenAICompletions.SupportsTemperature {
		t.Error("openai/gpt-5.2 should not support temperature")
	}
	if r := resolve(t, "openai", "gpt-5-turbo-future"); r.OpenAICompletions.SupportsTemperature || !r.Spec.Reasoning {
		t.Error("openai gpt-5* family should be reasoning without temperature")
	}
	if r := resolve(t, "openai", "o4-mini"); r.ThinkingLevels.Supports(api.ReasoningOff) {
		t.Error("openai/o4-mini should not allow thinking off")
	}
	if r := resolve(t, "azure_openai", "gpt-5.4-prod"); r.OpenAICompletions.SupportsTemperature {
		t.Error("azure gpt-5* deployment should not support temperature")
	}
	// Claude Opus 4.7 and later and Sonnet 5 reject non-default sampling
	// values, so the internal pipelines that set a fixed temperature must not
	// send temperature or top_p to these models.
	for _, model := range claudeNoSamplingModels {
		r := resolve(t, "anthropic", model)
		if r.AnthropicMessages.SupportsTemperature {
			t.Errorf("anthropic/%s should not support temperature", model)
		}
		if r.AnthropicMessages.SupportsTopP {
			t.Errorf("anthropic/%s should not support top_p", model)
		}
	}
	if r := resolve(t, "anthropic", "claude-sonnet-4-6"); !r.AnthropicMessages.SupportsTemperature {
		t.Error("anthropic/claude-sonnet-4-6 should still support temperature")
	}
	// A deployment named exactly gpt-5 / gpt-5-mini / gpt-5-nano must agree
	// with the openai vendor: the original GPT-5 trio rejects
	// reasoning_effort "none", so the picker must not offer thinking off.
	// Only GPT-5.1 and later, which the gpt-5* pattern covers, accept it.
	for _, deployment := range []string{"gpt-5", "gpt-5-mini", "gpt-5-nano"} {
		azureSpec := resolve(t, "azure_openai", deployment)
		if azureSpec.ThinkingLevels.Supports(api.ReasoningOff) {
			t.Errorf("azure/%s should not allow thinking off", deployment)
		}
		openaiSpec := resolve(t, "openai", deployment)
		if openaiSpec.ThinkingLevels.Supports(api.ReasoningOff) {
			t.Errorf("openai/%s should not allow thinking off", deployment)
		}
	}
	if r := resolve(t, "azure_openai", "gpt-5.1-prod"); !r.ThinkingLevels.Supports(api.ReasoningOff) {
		t.Error("azure gpt-5* family (GPT-5.1 and later) should allow thinking off")
	}
	if r := resolve(t, "aliyun", "qwen3-max"); !r.OpenAICompletions.ThinkingAlwaysSend {
		t.Error("aliyun/qwen3-max should always send the thinking switch")
	}
	if r := resolve(t, "aliyun", "qwen-plus-2026-01-01"); !r.OpenAICompletions.ThinkingAlwaysSend ||
		!r.OpenAICompletions.ThinkingDisableOnNonStream {
		t.Error("aliyun qwen-plus* family should carry the hybrid thinking flags")
	}
	if r := resolve(t, "aliyun", "qwq-plus"); r.ThinkingLevels.Supports(api.ReasoningOff) {
		t.Error("aliyun/qwq-plus should not allow thinking off")
	}
	if r := resolve(t, "moonshot", "moonshot-v1-8k"); r.OpenAICompletions.FixedTemperature == nil ||
		*r.OpenAICompletions.FixedTemperature != 1 {
		t.Error("moonshot/moonshot-v1-8k should pin temperature to 1")
	}
	if r := resolve(t, "moonshot", "kimi-k2.6"); r.OpenAICompletions.SupportsTemperature {
		t.Error("moonshot/kimi-k2.6 should not support temperature")
	}
	if r := resolve(t, "lkeap", "deepseek-r1"); r.ThinkingLevels.Supports(api.ReasoningOff) {
		t.Error("lkeap/deepseek-r1 should not allow thinking off")
	}
	if r := resolve(t, "lkeap", "my-deepseek-v3-route"); !r.Spec.Reasoning ||
		!r.ThinkingLevels.Supports(api.ReasoningOff) {
		t.Error("lkeap *deepseek-v3* family should be switchable reasoning")
	}
	if r := resolve(t, "zhipu", "glm-5.2"); r.ThinkingLevels.Value(api.ReasoningMedium) != "high" {
		t.Errorf("zhipu/glm-5.2 medium should map to high, got %q", r.ThinkingLevels.Value(api.ReasoningMedium))
	}
	if r := resolve(t, "zhipu", "glm-4.7"); r.ThinkingLevels.Value(api.ReasoningOff) != "none" {
		t.Error("zhipu vendor map should spell off as none")
	}
	if r := resolve(t, "gemini", "gemini-2.5-flash"); r.API != api.APIGoogleGenerativeAI {
		t.Errorf("gemini/gemini-2.5-flash API = %q", r.API)
	}
	if r := resolve(t, "gemini", "gemini-3.8-flash"); r.GoogleGenerativeAI.ThinkingMode != api.GoogleThinkingLevel {
		t.Errorf("gemini/gemini-3.8-flash thinking mode = %q", r.GoogleGenerativeAI.ThinkingMode)
	}
	if r := resolve(t, "anthropic", "claude-opus-5"); r.AnthropicMessages.ThinkingMode !=
		api.AnthropicThinkingAdaptive || !r.AnthropicMessages.SupportsEffort {
		t.Error("anthropic/claude-opus-5 should use adaptive thinking with effort")
	}
	if r := resolve(t, "anthropic", "claude-sonnet-4-5"); r.AnthropicMessages.ThinkingMode !=
		api.AnthropicThinkingBudget {
		t.Error("anthropic/claude-sonnet-4-5 should keep budget thinking")
	}
	if r := resolve(t, "anthropic", "claude-sonnet-4-5-20250929"); !r.Cataloged {
		t.Error("anthropic dated alias should resolve to the catalog entry")
	}
	if r := resolve(t, "openrouter", "anthropic/claude-haiku-4.5"); r.OpenAICompletions.CacheControlFormat !=
		"anthropic" {
		t.Error("openrouter anthropic/* family should use anthropic cache_control")
	}
	if r := resolve(t, "siliconflow", "deepseek-ai/DeepSeek-V4-Pro"); !r.OpenAICompletions.SupportsReasoningEffort ||
		r.ThinkingLevels.Value(api.ReasoningMax) != "max" {
		t.Error("siliconflow DeepSeek-V4-Pro should grade effort high/max")
	}
	if r := resolve(t, "siliconflow", "Pro/zai-org/GLM-5"); r.OpenAICompletions.SupportsReasoningEffort {
		t.Error("siliconflow GLM-5 should not send reasoning_effort")
	}
	if r := resolve(t, "minimax", "MiniMax-M3"); r.OpenAICompletions.ThinkingEnabledValue != "adaptive" {
		t.Error("minimax should enable thinking with type adaptive")
	}
	if r := resolve(t, "volcengine", "doubao-seed-1-6-251015"); r.OpenAICompletions.MaxTokensField !=
		"max_completion_tokens" {
		t.Error("volcengine must keep max_completion_tokens")
	}
	if r := resolve(t, "deepseek", "deepseek-reasoner"); r.ThinkingLevels.Supports(api.ReasoningOff) {
		t.Error("deepseek/deepseek-reasoner should not allow thinking off")
	}
	// required / named tool choices 400 in thinking mode, which is the
	// DeepSeek default, so neither may reach the wire.
	if r := resolve(t, "deepseek", "deepseek-v4-pro"); r.OpenAICompletions.AllowsToolChoice("required") ||
		r.OpenAICompletions.AllowsToolChoice("function") {
		t.Error("deepseek should not send required or named tool choices")
	}
	// GLM-5.2 reads "minimal" as give-up-thinking and folds "low" to high, so
	// rewriting minimal to low here would send the weakest rung as the
	// strongest one.
	if r := resolve(t, "zhipu", "glm-5.2"); r.ThinkingLevels.Value(api.ReasoningMinimal) != "minimal" {
		t.Errorf("zhipu/glm-5.2 minimal should stay minimal, got %q", r.ThinkingLevels.Value(api.ReasoningMinimal))
	}
	// Both entries grade with a top-level reasoning_effort; the vendor
	// default (chat_template_kwargs) would silently drop the level.
	for _, model := range []string{"z-ai/glm-5.3", "moonshotai/kimi-k3"} {
		r := resolve(t, "nvidia", model)
		if r.OpenAICompletions.ThinkingFormat != api.ThinkingFormatOpenAI ||
			!r.OpenAICompletions.SupportsReasoningEffort {
			t.Errorf("nvidia/%s should grade with a top-level reasoning_effort", model)
		}
	}
	if r := resolve(t, "nvidia", "nvidia/nemotron-3-ultra-550b-a55b"); r.OpenAICompletions.ThinkingFormat !=
		api.ThinkingFormatChatTemplateKwargs {
		t.Error("nvidia nemotron should keep the chat-template switch")
	}
	// DashScope errors when qwen3.8-max gets both fields.
	for _, model := range []string{"qwen3.8-max", "qwen3.8-flash", "qwen3.8-plus-2026-09-01"} {
		if r := resolve(t, "aliyun", model); !r.OpenAICompletions.ThinkingBudgetExcludesEffort {
			t.Errorf("aliyun/%s should not send thinking_budget next to reasoning_effort", model)
		}
	}
	if r := resolve(t, "aliyun", "qwen3-max"); r.OpenAICompletions.ThinkingBudgetExcludesEffort {
		t.Error("aliyun/qwen3-max has no effort to conflict with and should keep the budget")
	}
}

func TestHooks(t *testing.T) {
	azure, _ := modelruntime.Get("azure_openai")
	u, q := azure.Endpoint(providers.EndpointRequest{
		BaseURL:   "https://my-res.openai.azure.com/",
		Model:     "gpt-4o deploy",
		ModelType: types.ModelTypeKnowledgeQA,
		Extra:     map[string]string{models.ExtraAPIVersion: "2025-01-01-preview"},
	})
	if u != "https://my-res.openai.azure.com/openai/deployments/gpt-4o%20deploy/chat/completions" {
		t.Errorf("azure endpoint = %q", u)
	}
	if q["api-version"] != "2025-01-01-preview" {
		t.Errorf("azure api-version = %q", q["api-version"])
	}
	// Without an explicit api_version the hook emits the v1 GA data plane,
	// which carries the deployment name in the body and takes no
	// api-version at all.
	u, q = azure.Endpoint(providers.EndpointRequest{BaseURL: "https://x.openai.azure.com", Model: "d"})
	if u != "https://x.openai.azure.com/openai/v1/chat/completions" {
		t.Errorf("azure v1 endpoint = %q", u)
	}
	if len(q) != 0 {
		t.Errorf("azure v1 query = %v, want none", q)
	}
	if azure.Auth != providers.AuthAPIKeyHeader {
		t.Errorf("azure auth = %q", azure.Auth)
	}
	// Embedding obeys the same rule as chat for the same stored row, so one
	// Azure resource never ends up serving chat on v1 and embedding on the
	// dated deployments path. internal/models/embedding calls this hook.
	u, q = azure.Endpoint(providers.EndpointRequest{
		BaseURL: "https://x.openai.azure.com", Model: "embed-deploy",
		ModelType: types.ModelTypeEmbedding,
	})
	if u != "https://x.openai.azure.com/openai/v1/embeddings" {
		t.Errorf("azure v1 embedding endpoint = %q", u)
	}
	if len(q) != 0 {
		t.Errorf("azure v1 embedding query = %v, want none", q)
	}
	u, q = azure.Endpoint(providers.EndpointRequest{
		BaseURL: "https://x.openai.azure.com", Model: "embed-deploy",
		ModelType: types.ModelTypeEmbedding,
		Extra:     map[string]string{models.ExtraAPIVersion: "2024-10-21"},
	})
	if u != "https://x.openai.azure.com/openai/deployments/embed-deploy/embeddings" {
		t.Errorf("azure legacy embedding endpoint = %q", u)
	}
	if q["api-version"] != "2024-10-21" {
		t.Errorf("azure legacy embedding api-version = %q", q["api-version"])
	}

	wk, _ := modelruntime.Get("weknoracloud")
	u, _ = wk.Endpoint(providers.EndpointRequest{
		BaseURL: "https://weknora.weixin.qq.com/", ModelType: types.ModelTypeKnowledgeQA,
	})
	if u != "https://weknora.weixin.qq.com/api/v1/chat/completions" {
		t.Errorf("weknoracloud endpoint = %q", u)
	}
	if wk.Auth != providers.AuthSigned || wk.Signer == nil {
		t.Error("weknoracloud should use a signer")
	}
	if err := wk.ValidateConfig(&providers.Config{}); err != nil {
		t.Errorf("weknoracloud validate should pass without key: %v", err)
	}

	generic, _ := modelruntime.Get("generic")
	if err := generic.ValidateConfig(&providers.Config{ModelName: "m"}); err == nil {
		t.Error("generic validate should require a base URL")
	}
	if err := generic.ValidateConfig(&providers.Config{BaseURL: "http://x/v1", ModelName: "m"}); err != nil {
		t.Errorf("generic validate: %v", err)
	}
	gpustack, _ := modelruntime.Get("gpustack")
	if err := gpustack.ValidateConfig(&providers.Config{APIKey: "k", ModelName: "m"}); err == nil {
		t.Error("gpustack validate should require a base URL")
	}

	for _, id := range []string{"volcengine", "lkeap"} {
		v, _ := modelruntime.Get(id)
		found := false
		for _, f := range v.ExtraFields {
			if f.Key == "secret_key" {
				found = true
				if !f.Secret || len(f.ModelTypes) != 1 || f.ModelTypes[0] != types.ModelTypeRerank {
					t.Errorf("%s secret_key field should be secret and rerank-only", id)
				}
			}
		}
		if !found {
			t.Errorf("%s should expose a secret_key extra field", id)
		}
	}
}

// TestSignedRerankCredentialLabels pins that the two signed rerank vendors
// name their first credential after the identity it actually is. The editor
// renders this instead of a hardcoded vendor table, so losing it silently
// sends operators back to pasting an sk- key into a signature field.
func TestSignedRerankCredentialLabels(t *testing.T) {
	for _, tc := range []struct{ vendor, wantLabel string }{
		{"lkeap", "SecretId"},
		{"volcengine", "Access Key ID"},
	} {
		t.Run(tc.vendor, func(t *testing.T) {
			v, ok := modelruntime.Get(tc.vendor)
			if !ok {
				t.Fatalf("vendor %s is not registered", tc.vendor)
			}
			label := v.CredentialLabelFor(types.ModelTypeRerank)
			if label == nil {
				t.Fatalf("rerank should override the credential label")
			}
			if label.Label != tc.wantLabel {
				t.Errorf("rerank credential label = %q, want %q", label.Label, tc.wantLabel)
			}
			if label.LocalizedLabel("zh-CN") == label.Label {
				t.Errorf("zh-CN label should differ from the default")
			}
			if label.Hint == "" {
				t.Errorf("the hint is what stops an sk- key being pasted here")
			}
			// Chat and embedding on these vendors do take a plain API key.
			if other := v.CredentialLabelFor(types.ModelTypeKnowledgeQA); other != nil {
				t.Errorf("chat should keep the generic API-key wording, got %q", other.Label)
			}
		})
	}
}

// TestClaudeNoSamplingRequestBody builds the request body the way NewRemoteChat
// does, from the resolved catalog entry. With thinking off, the anthropic
// adapter sends top_p when temperature is not supported, so both flags have to
// be off for these models to receive neither field.
func TestClaudeNoSamplingRequestBody(t *testing.T) {
	build := func(t *testing.T, model string, effort api.ReasoningEffort) map[string]any {
		t.Helper()
		r := resolve(t, "anthropic", model)
		c := anthropicmessages.New(anthropicmessages.Config{
			Endpoint:       api.Endpoint{Model: r.RemoteModel},
			Settings:       r.AnthropicMessages,
			ThinkingLevels: r.ThinkingLevels,
			Reasoning:      r.Spec.Reasoning,
		})
		body, err := c.BuildRequestBody(
			[]api.Message{{Role: "user", Content: "Hi"}},
			&api.Options{Temperature: 1, TopP: 0.9, ReasoningEffort: effort},
			false,
		)
		if err != nil {
			t.Fatalf("build %s: %v", model, err)
		}
		return body
	}

	for _, model := range claudeNoSamplingModels {
		efforts := []api.ReasoningEffort{""}
		if r := resolve(t, "anthropic", model); r.ThinkingLevels.Supports(api.ReasoningOff) {
			efforts = append(efforts, api.ReasoningOff)
		}
		for _, effort := range efforts {
			body := build(t, model, effort)
			for _, field := range []string{"temperature", "top_p", "top_k"} {
				if _, ok := body[field]; ok {
					t.Errorf("anthropic/%s (reasoning %q) sent %s", model, effort, field)
				}
			}
		}
	}

	if body := build(t, "claude-sonnet-4-6", api.ReasoningOff); body["temperature"] != 1.0 {
		t.Errorf("anthropic/claude-sonnet-4-6 should still send temperature, got %v", body["temperature"])
	}
}

// TestClaude55Models pins how the Claude 5.5 entries turn thinking off and
// that none of them receives sampling parameters or budget_tokens: Opus 5.5
// cannot turn thinking off, Sonnet 5.5 turns up-front thinking off with
// "between_tools" (it rejects "disabled"), and Haiku 5.5 accepts "disabled".
func TestClaude55Models(t *testing.T) {
	build := func(t *testing.T, model string, effort api.ReasoningEffort) map[string]any {
		t.Helper()
		r := resolve(t, "anthropic", model)
		c := anthropicmessages.New(anthropicmessages.Config{
			Endpoint:       api.Endpoint{Model: r.RemoteModel},
			Settings:       r.AnthropicMessages,
			ThinkingLevels: r.ThinkingLevels,
			Reasoning:      r.Spec.Reasoning,
		})
		body, err := c.BuildRequestBody(
			[]api.Message{{Role: "user", Content: "Hi"}},
			&api.Options{Temperature: 0.3, TopP: 0.9, ReasoningEffort: effort},
			false,
		)
		if err != nil {
			t.Fatalf("build %s: %v", model, err)
		}
		return body
	}

	offBody := map[string]any{
		"claude-opus-5-5":   nil,
		"claude-sonnet-5-5": map[string]any{"type": "between_tools"},
		"claude-haiku-5-5":  map[string]any{"type": "disabled"},
	}
	for model, wantOff := range offBody {
		r := resolve(t, "anthropic", model)
		if !r.Cataloged {
			t.Fatalf("anthropic/%s should be in the catalog", model)
		}
		if r.AnthropicMessages.ThinkingMode != api.AnthropicThinkingAdaptive {
			t.Errorf("anthropic/%s should use adaptive thinking", model)
		}
		if got := r.ThinkingLevels.Supports(api.ReasoningOff); got != (wantOff != nil) {
			t.Errorf("anthropic/%s supports thinking off = %v", model, got)
		}
		for _, level := range []api.ReasoningEffort{api.ReasoningXHigh, api.ReasoningMax} {
			if !r.ThinkingLevels.Supports(level) {
				t.Errorf("anthropic/%s should support %s effort", model, level)
			}
		}

		for _, effort := range []api.ReasoningEffort{"", api.ReasoningOff, api.ReasoningHigh} {
			body := build(t, model, effort)
			for _, field := range []string{"temperature", "top_p", "top_k"} {
				if _, ok := body[field]; ok {
					t.Errorf("anthropic/%s (reasoning %q) sent %s", model, effort, field)
				}
			}
			if thinking, ok := body["thinking"].(map[string]any); ok && thinking["type"] == "enabled" {
				t.Errorf("anthropic/%s (reasoning %q) sent budget thinking", model, effort)
			}
		}
		if wantOff != nil {
			body := build(t, model, api.ReasoningOff)
			if fmt.Sprint(body["thinking"]) != fmt.Sprint(wantOff) {
				t.Errorf("anthropic/%s thinking off sent %v, want %v", model, body["thinking"], wantOff)
			}
		}
	}
}