Files
TradarrV2/backend/internal/ai/terra_ticket44_test.go

77 lines
3.0 KiB
Go

package ai
import (
"context"
"encoding/json"
"errors"
openai "github.com/sashabaranov/go-openai"
"github.com/tradarr/backend/internal/generation"
"github.com/tradarr/backend/internal/scraper"
"github.com/tradarr/backend/internal/summarybudget"
"io"
"net/http"
"strings"
"testing"
)
func TestTicket44TerraFullPromptAdmission(t *testing.T) {
p := &incident37Provider{}
a := fullGenerationSummarizer{provider: p, providerName: "openai", model: "gpt-6.1-terra"}
req := generation.SummaryRequest{Language: "en", Budget: 1000000, Candidates: []generation.Candidate{{ID: "fixture", Article: scraper.Article{Title: "Fixture", Content: "body", Source: "bloomberg"}}}}
_, u, _, err := a.SummarizeWithBudget(context.Background(), req)
if err != nil {
t.Fatal(err)
}
overhead := u.PromptTokens - len("body")
body := strings.Repeat("x", 1000000-overhead-1)
req.Candidates[0].Article.Content = body
_, u, _, err = a.SummarizeWithBudget(context.Background(), req)
if err != nil || u.EffectiveTokens != 1000000 || u.PromptTokens != 999999 || !strings.Contains(p.prompt, body) || p.options.MaxOutputTokens != 4096 {
t.Fatalf("near-limit full prompt rejected/changed: %+v %v", u, err)
}
calls := p.calls
req.Candidates[0].Article.Content = body + "xx"
_, _, _, err = a.SummarizeWithBudget(context.Background(), req)
if !errors.Is(err, summarybudget.ErrNoArticlesFit) || p.calls != calls {
t.Fatal("overflow called provider or was truncated")
}
a.model = "unknown"
req.Candidates[0].Article.Content = "body"
_, _, _, err = a.SummarizeWithBudget(context.Background(), req)
if err != nil || p.calls != calls+1 {
t.Fatal("unknown model blocked before provider")
}
}
func TestTicket44OpenAICompletionLimitAndNoRetry(t *testing.T) {
for _, status := range []int{200, 429, 503} {
t.Run(http.StatusText(status), func(t *testing.T) {
calls := 0
cfg := openai.DefaultConfig("fixture-key")
cfg.HTTPClient = &http.Client{Transport: ticket31Transport(func(r *http.Request) (*http.Response, error) {
calls++
var body map[string]any
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Fatal(err)
}
if body["max_completion_tokens"] != float64(4096) || body["model"] != "gpt-6.1-terra" {
t.Error("missing exact model/completion limit")
}
if _, ok := body["max_tokens"]; ok {
t.Error("deprecated max_tokens sent")
}
raw := `{"choices":[{"message":{"content":"OK"}}]}`
if status != 200 {
raw = `{"error":{"message":"fixture rejection","type":"rate_limit_error"}}`
}
return &http.Response{StatusCode: status, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: io.NopCloser(strings.NewReader(raw))}, nil
})}
p := &openAIProvider{client: openai.NewClientWithConfig(cfg), model: "gpt-6.1-terra"}
_, err := p.Summarize(context.Background(), "fixture prompt", GenOptions{MaxOutputTokens: 4096})
if (status == 200) != (err == nil) || calls != 1 {
t.Fatalf("calls=%d err=%v", calls, err)
}
})
}
}