128 lines
5.2 KiB
Go
128 lines
5.2 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
openai "github.com/sashabaranov/go-openai"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTicket43OllamaSmallGenerationHasExplicitOutputLimit(t *testing.T) {
|
|
p := newOllama("https://fixture.invalid", "exact-saved-model")
|
|
calls := 0
|
|
p.client = &http.Client{Transport: ticket31Transport(func(r *http.Request) (*http.Response, error) {
|
|
calls++
|
|
var body struct {
|
|
Model, Prompt string
|
|
Options map[string]int
|
|
}
|
|
if json.NewDecoder(r.Body).Decode(&body) != nil || body.Model != "exact-saved-model" || body.Prompt != modelTestPrompt || body.Options["num_predict"] != modelTestMaxOutputTokens {
|
|
t.Error("Ollama test call omitted the fixed output cap or exact saved model")
|
|
}
|
|
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`{"response":"OK"}`))}, nil
|
|
})}
|
|
if _, err := p.Summarize(context.Background(), modelTestPrompt, GenOptions{MaxOutputTokens: modelTestMaxOutputTokens}); err != nil || calls != 1 {
|
|
t.Fatal("small generation retried or failed")
|
|
}
|
|
}
|
|
func TestTicket43AdapterStructuredErrorCodesAndRawBodiesAreNotExposed(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, body string
|
|
want error
|
|
}{
|
|
{"anthropic", `{"error":{"type":"authentication_error","message":"PRIVATE_BODY"}}`, ErrModelTestAuth},
|
|
{"gemini", `{"error":{"status":"RESOURCE_EXHAUSTED","message":"PRIVATE_BODY"}}`, ErrModelTestRateLimited},
|
|
{"ollama", `{"error":{"code":"model_not_found","message":"PRIVATE_BODY"}}`, ErrModelTestModelUnavailable},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
client := &http.Client{Transport: ticket31Transport(func(*http.Request) (*http.Response, error) {
|
|
return &http.Response{StatusCode: 400, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(tc.body))}, nil
|
|
})}
|
|
var provider Provider
|
|
switch tc.name {
|
|
case "anthropic":
|
|
p := newAnthropic("PRIVATE_KEY", "exact")
|
|
p.client = client
|
|
provider = p
|
|
case "gemini":
|
|
p := newGemini("PRIVATE_KEY", "exact")
|
|
p.client = client
|
|
provider = p
|
|
case "ollama":
|
|
p := newOllama("https://PRIVATE_ENDPOINT", "exact")
|
|
p.client = client
|
|
provider = p
|
|
}
|
|
_, err := provider.Summarize(context.Background(), modelTestPrompt, GenOptions{MaxOutputTokens: modelTestMaxOutputTokens})
|
|
if err == nil || strings.Contains(err.Error(), "PRIVATE") || classifyModelTestError(err) != tc.want {
|
|
t.Error("structured adapter rejection lost its category or exposed raw response")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTicket43HTTPGenerationAdaptersUseExactModelCredentialsAndSmallOutput(t *testing.T) {
|
|
for _, name := range []string{"openai", "anthropic", "gemini", "ollama"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
const key = "PRIVATE_KEY+&"
|
|
calls := 0
|
|
client := &http.Client{Transport: ticket31Transport(func(r *http.Request) (*http.Response, error) {
|
|
calls++
|
|
var body map[string]any
|
|
if r.Method != "POST" || json.NewDecoder(r.Body).Decode(&body) != nil {
|
|
t.Error("generation was replaced by discovery")
|
|
}
|
|
response := ""
|
|
switch name {
|
|
case "openai":
|
|
if body["model"] != "exact-saved-model" || body["max_completion_tokens"] != float64(modelTestMaxOutputTokens) || r.Header.Get("Authorization") != "Bearer "+key {
|
|
t.Error("OpenAI generation configuration changed")
|
|
}
|
|
response = `{"choices":[{"message":{"role":"assistant","content":"OK"}}]}`
|
|
case "anthropic":
|
|
if body["model"] != "exact-saved-model" || body["max_tokens"] != float64(modelTestMaxOutputTokens) || r.Header.Get("x-api-key") != key {
|
|
t.Error("Anthropic generation configuration changed")
|
|
}
|
|
response = `{"content":[{"type":"text","text":"OK"}]}`
|
|
case "gemini":
|
|
config, _ := body["generationConfig"].(map[string]any)
|
|
if !strings.HasSuffix(r.URL.Path, "/models/exact-saved-model:generateContent") || r.URL.Query().Get("key") != key || config["maxOutputTokens"] != float64(modelTestMaxOutputTokens) {
|
|
t.Error("Gemini generation configuration changed")
|
|
}
|
|
response = `{"candidates":[{"content":{"parts":[{"text":"OK"}]}}]}`
|
|
case "ollama":
|
|
config, _ := body["options"].(map[string]any)
|
|
if body["model"] != "exact-saved-model" || r.URL.Host != "saved-fixture.invalid" || config["num_predict"] != float64(modelTestMaxOutputTokens) {
|
|
t.Error("Ollama generation configuration changed")
|
|
}
|
|
response = `{"response":"OK"}`
|
|
}
|
|
return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: io.NopCloser(strings.NewReader(response))}, nil
|
|
})}
|
|
provider, err := NewProvider(name, key, "exact-saved-model", "https://saved-fixture.invalid")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
switch p := provider.(type) {
|
|
case *openAIProvider:
|
|
cfg := openai.DefaultConfig(key)
|
|
cfg.HTTPClient = client
|
|
p.client = openai.NewClientWithConfig(cfg)
|
|
case *anthropicProvider:
|
|
p.client = client
|
|
case *geminiProvider:
|
|
p.client = client
|
|
case *OllamaProvider:
|
|
p.client = client
|
|
}
|
|
response, err := provider.Summarize(context.Background(), modelTestPrompt, GenOptions{MaxOutputTokens: modelTestMaxOutputTokens, NumCtx: 2048})
|
|
if err != nil || response != "OK" || calls != 1 {
|
|
t.Fatal("exact-model generation failed or retried")
|
|
}
|
|
})
|
|
}
|
|
}
|