package model import ( "bytes" "context" "log/slog" "net/http" "strings" "testing" "git.yeij.top/AskaEth/Cyrene_v2/internal/config" ) // captureLog returns a logger writing text records into a buffer, plus the // buffer, so a test can assert the WARN an adapter emits. func captureLog() (*slog.Logger, *bytes.Buffer) { buf := &bytes.Buffer{} return slog.New(slog.NewTextHandler(buf, &slog.HandlerOptions{Level: slog.LevelDebug})), buf } // boolPtr is a convenience for the tri-state Thinking.Enable. func boolPtr(v bool) *bool { return &v } // TestAnthropicChat_ThinkingBudgetLeavesRoomForTheAnswer is the design/04 §3.2 // red line: a budget large enough to eat max_tokens must not be sent as-is — // the adapter raises max_tokens so thinking and the visible answer both fit, and // it logs the adjustment (never a silent rewrite). func TestAnthropicChat_ThinkingBudgetLeavesRoomForTheAnswer(t *testing.T) { t.Parallel() ch := make(chan anthropicCaptured, 1) srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch) log, buf := captureLog() ad := NewAnthropicChat( config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"}, WithAnthropicLogger(log)) const budget = 4096 // max_tokens equals the budget: without the guard the whole budget would be // consumed by thinking and the answer would come back empty. if _, err := ad.Chat(context.Background(), Request{ Model: "claude-test", Messages: []Message{TextMessage(RoleUser, "hi")}, MaxTokens: budget, Thinking: &Thinking{BudgetTokens: budget}, }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } body := (<-ch).body think, ok := body["thinking"].(map[string]any) if !ok { t.Fatalf("body.thinking = %v, want an enabled directive", body["thinking"]) } if think["type"] != anthropicThinkingTypeEnabled { t.Errorf("thinking.type = %v, want %q", think["type"], anthropicThinkingTypeEnabled) } gotBudget, _ := think["budget_tokens"].(float64) maxTokens, _ := body["max_tokens"].(float64) if int(gotBudget) != budget { t.Errorf("thinking.budget_tokens = %v, want %d", gotBudget, budget) } if maxTokens <= gotBudget { t.Fatalf("max_tokens = %v, want > budget_tokens %v (thinking would eat the answer)", maxTokens, gotBudget) } if maxTokens < gotBudget+anthropicThinkingBodyReserve { t.Errorf("max_tokens = %v, want >= budget %v + body reserve %d", maxTokens, gotBudget, anthropicThinkingBodyReserve) } if !strings.Contains(buf.String(), "max_tokens raised") { t.Errorf("log = %q, want a WARN explaining the max_tokens raise", buf.String()) } } // TestAnthropicChat_ThinkingBudgetBelowMaxTokensUnchanged pins the non-saturated // case: when the caller's max_tokens already leaves room, it is sent untouched // and the budget stays strictly below it. func TestAnthropicChat_ThinkingBudgetBelowMaxTokensUnchanged(t *testing.T) { t.Parallel() ch := make(chan anthropicCaptured, 1) srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch) log, buf := captureLog() ad := NewAnthropicChat( config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"}, WithAnthropicLogger(log)) if _, err := ad.Chat(context.Background(), Request{ Model: "claude-test", Messages: []Message{TextMessage(RoleUser, "hi")}, MaxTokens: anthropicDefaultMaxTokens, Thinking: &Thinking{BudgetTokens: 4096}, }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } body := (<-ch).body think := body["thinking"].(map[string]any) gotBudget, _ := think["budget_tokens"].(float64) maxTokens, _ := body["max_tokens"].(float64) if maxTokens != float64(anthropicDefaultMaxTokens) { t.Errorf("max_tokens = %v, want the requested %d (unchanged)", maxTokens, anthropicDefaultMaxTokens) } if gotBudget >= maxTokens { t.Errorf("budget_tokens %v >= max_tokens %v", gotBudget, maxTokens) } if strings.Contains(buf.String(), "max_tokens raised") { t.Errorf("unexpected adjustment log: %q", buf.String()) } } // TestAnthropicChat_ThinkingDisabledOmitsField asserts the explicit off switch // sends no thinking directive at all (Anthropic treats a missing field as off). func TestAnthropicChat_ThinkingDisabledOmitsField(t *testing.T) { t.Parallel() ch := make(chan anthropicCaptured, 1) srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch) ad := NewAnthropicChat(config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"}) if _, err := ad.Chat(context.Background(), Request{ Model: "claude-test", Messages: []Message{TextMessage(RoleUser, "hi")}, Thinking: &Thinking{BudgetTokens: 4096, Enable: boolPtr(false)}, }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } if body := (<-ch).body; body["thinking"] != nil { t.Fatalf("body.thinking = %v, want the field omitted", body["thinking"]) } } // TestAnthropicChat_ThinkingBudgetRaisedToProtocolMinimum asserts a budget below // the API minimum is raised (sending it as-is would 400) and reported. func TestAnthropicChat_ThinkingBudgetRaisedToProtocolMinimum(t *testing.T) { t.Parallel() ch := make(chan anthropicCaptured, 1) srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch) log, buf := captureLog() ad := NewAnthropicChat( config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"}, WithAnthropicLogger(log)) if _, err := ad.Chat(context.Background(), Request{ Model: "claude-test", Messages: []Message{TextMessage(RoleUser, "hi")}, Thinking: &Thinking{BudgetTokens: 512}, }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } think := (<-ch).body["thinking"].(map[string]any) if got, _ := think["budget_tokens"].(float64); int(got) != anthropicMinThinkingBudget { t.Errorf("thinking.budget_tokens = %v, want the minimum %d", got, anthropicMinThinkingBudget) } if !strings.Contains(buf.String(), "protocol minimum") { t.Errorf("log = %q, want a WARN about the minimum budget", buf.String()) } } // TestOpenAIChat_ReasoningEffortPassthrough asserts a supported tier on a known // provider is forwarded verbatim, with no warning. func TestOpenAIChat_ReasoningEffortPassthrough(t *testing.T) { t.Parallel() ch := make(chan capturedRequest, 1) srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch) log, buf := captureLog() ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "gpt-test"}, WithOpenAILogger(log)) if _, err := ad.Chat(context.Background(), Request{ Model: "gpt-4o", Messages: []Message{TextMessage(RoleUser, "hi")}, ReasoningEffort: "high", }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } if got := (<-ch).body["reasoning_effort"]; got != "high" { t.Fatalf("body.reasoning_effort = %v, want high", got) } if buf.Len() != 0 { t.Errorf("unexpected log for a supported value: %q", buf.String()) } } // TestOpenAIChat_ReasoningEffortAliasMappedForDeepSeek asserts a DeepSeek // compatibility alias is normalized to the tier the service applies (medium → // high per the official mapping table) and reported, never graded down to a // weaker tier of our own choosing. func TestOpenAIChat_ReasoningEffortAliasMappedForDeepSeek(t *testing.T) { t.Parallel() ch := make(chan capturedRequest, 1) srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch) log, buf := captureLog() ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "deepseek-flash"}, WithOpenAILogger(log)) if _, err := ad.Chat(context.Background(), Request{ Model: "deepseek-flash", Messages: []Message{TextMessage(RoleUser, "hi")}, ReasoningEffort: "medium", }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } if got := (<-ch).body["reasoning_effort"]; got != "high" { t.Fatalf("body.reasoning_effort = %v, want the officially mapped high", got) } rec := buf.String() for _, want := range []string{"level=WARN", "reasoning_effort", "deepseek", "medium", "high"} { if !strings.Contains(rec, want) { t.Errorf("log %q missing %q", rec, want) } } } // TestOpenAIChat_ReasoningEffortDroppedForUnknownProvider asserts an // unrecognized provider/model leaves the request untouched and reports the // ignored field (design/04 §3.2: never silently ineffective). func TestOpenAIChat_ReasoningEffortDroppedForUnknownProvider(t *testing.T) { t.Parallel() ch := make(chan capturedRequest, 1) srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch) log, buf := captureLog() ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "mystery-1"}, WithOpenAILogger(log)) if _, err := ad.Chat(context.Background(), Request{ Model: "mystery-1", Messages: []Message{TextMessage(RoleUser, "hi")}, ReasoningEffort: "high", }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } body := (<-ch).body if _, present := body["reasoning_effort"]; present { t.Fatalf("body.reasoning_effort = %v, want the field dropped", body["reasoning_effort"]) } rec := buf.String() for _, want := range []string{"level=WARN", "reasoning_effort", "unknown", "high"} { if !strings.Contains(rec, want) { t.Errorf("log %q missing %q", rec, want) } } } // TestOpenAIChat_QwenEnableThinkingReportedNotForwarded documents the deliberate // choice for the Qwen3 hard switch: the OpenAI-compatible protocol has no // portable wire shape for enable_thinking, so this version reports it and leaves // the request byte-identical instead of guessing a shape that could 400. func TestOpenAIChat_QwenEnableThinkingReportedNotForwarded(t *testing.T) { t.Parallel() ch := make(chan capturedRequest, 1) srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch) log, buf := captureLog() ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "qwen3-max"}, WithOpenAILogger(log)) plain := make(chan capturedRequest, 1) plainSrv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, plain) plainAd := NewOpenAIChat(config.LLMConfig{BaseURL: plainSrv.URL, Model: "qwen3-max"}) if _, err := plainAd.Chat(context.Background(), Request{ Model: "qwen3-max", Messages: []Message{TextMessage(RoleUser, "hi")}, }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } if _, err := ad.Chat(context.Background(), Request{ Model: "qwen3-max", Messages: []Message{TextMessage(RoleUser, "hi")}, Thinking: &Thinking{Enable: boolPtr(false)}, }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } body := (<-ch).body for _, key := range []string{"enable_thinking", "chat_template_kwargs", "extra_body", "thinking"} { if _, present := body[key]; present { t.Errorf("body.%s = %v, want the request unchanged", key, body[key]) } } if len(body) != len((<-plain).body) { t.Errorf("request shape changed with a thinking directive: %v", body) } rec := buf.String() for _, want := range []string{"level=WARN", "enable_thinking", "qwen"} { if !strings.Contains(rec, want) { t.Errorf("log %q missing %q", rec, want) } } } // TestOpenAIChat_ThinkingBudgetReportedIgnored asserts a token budget (an // Anthropic-style directive) is reported as unmappable on an OpenAI-compatible // endpoint rather than silently dropped. func TestOpenAIChat_ThinkingBudgetReportedIgnored(t *testing.T) { t.Parallel() ch := make(chan capturedRequest, 1) srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch) log, buf := captureLog() ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "gpt-4o"}, WithOpenAILogger(log)) if _, err := ad.Chat(context.Background(), Request{ Model: "gpt-4o", Messages: []Message{TextMessage(RoleUser, "hi")}, Thinking: &Thinking{BudgetTokens: 8192}, }, Options{}); err != nil { t.Fatalf("Chat error: %v", err) } if _, present := (<-ch).body["thinking"]; present { t.Fatal("body.thinking was sent to an OpenAI-compatible endpoint") } rec := buf.String() for _, want := range []string{"level=WARN", "thinking.budget_tokens", "gpt"} { if !strings.Contains(rec, want) { t.Errorf("log %q missing %q", rec, want) } } } // TestAnthropicChatStream_ThinkingBudgetLeavesRoomForTheAnswer runs the same // red-line check on the streaming path, which builds its own request body. func TestAnthropicChatStream_ThinkingBudgetLeavesRoomForTheAnswer(t *testing.T) { t.Parallel() ch := make(chan anthropicCaptured, 1) const sse = "event: message_start\ndata: {\"type\":\"message_start\"}\n\n" + "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n" + "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n" + "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n" srv := newAnthropicServer(t, http.StatusOK, "text/event-stream", sse, ch) ad := NewAnthropicChat(config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"}) if _, err := ad.ChatStream(context.Background(), Request{ Model: "claude-test", Messages: []Message{TextMessage(RoleUser, "hi")}, MaxTokens: 2048, Thinking: &Thinking{BudgetTokens: 2048}, }, func(StreamEvent) {}); err != nil { t.Fatalf("ChatStream error: %v", err) } body := (<-ch).body think, ok := body["thinking"].(map[string]any) if !ok { t.Fatalf("stream body.thinking = %v, want an enabled directive", body["thinking"]) } gotBudget, _ := think["budget_tokens"].(float64) maxTokens, _ := body["max_tokens"].(float64) if gotBudget >= maxTokens { t.Fatalf("stream max_tokens = %v, want > budget_tokens %v", maxTokens, gotBudget) } } // TestReasoningVendorFor_Classification locks the vendor detection the whitelist // depends on, including the fallback to the base URL. func TestReasoningVendorFor_Classification(t *testing.T) { t.Parallel() cases := []struct { model string baseURL string want reasoningVendor }{ {model: "deepseek-v4-pro", want: vendorDeepSeek}, {model: "Qwen3-Max", want: vendorQwen}, {model: "gpt-4o", want: vendorOpenAI}, {model: "o3-mini", want: vendorOpenAI}, {model: "chatgpt-4o-latest", want: vendorOpenAI}, {model: "claude-sonnet-4", baseURL: "https://api.openai.com/v1", want: vendorOpenAI}, {model: "qwen-plus", baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1", want: vendorQwen}, {model: "some-model", baseURL: "https://api.deepseek.com/v1", want: vendorDeepSeek}, {model: "some-model", baseURL: "http://localhost:8000/v1", want: vendorUnknown}, } for _, tt := range cases { if got := reasoningVendorFor(tt.model, tt.baseURL); got != tt.want { t.Errorf("reasoningVendorFor(%q, %q) = %q, want %q", tt.model, tt.baseURL, got, tt.want) } } }