fix(ai): diagnose summary provider failures (#37)
This commit is contained in:
151
backend/internal/ai/summary_diagnostics_ticket37_test.go
Normal file
151
backend/internal/ai/summary_diagnostics_ticket37_test.go
Normal file
@ -0,0 +1,151 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
"github.com/tradarr/backend/internal/generation"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTicket37TerminalDiagnosticsLocalTransport(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, category, network, body string
|
||||
status int
|
||||
err error
|
||||
}{
|
||||
{"success", "success", "", `{"response":"PRIVATE_RESULT"}`, 200, nil},
|
||||
{"http", "provider_http_error", "", "PRIVATE_BODY", 429, nil},
|
||||
{"response", "provider_response_error", "", "PRIVATE_BODY", 200, nil},
|
||||
{"empty", "empty_response", "", `{"response":" "}`, 200, nil},
|
||||
{"network", "provider_http_error", "transport", "", 0, &url.Error{URL: "https://PRIVATE_URL", Err: errors.New("PRIVATE_SECRET")}},
|
||||
{"timeout", "timeout", "", "", 0, context.DeadlineExceeded},
|
||||
{"canceled", "canceled", "", "", 0, context.Canceled},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := newOllama("https://PRIVATE_ENDPOINT", "PRIVATE_MODEL")
|
||||
calls := 0
|
||||
p.client = &http.Client{Transport: ticket31Transport(func(*http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if tc.err != nil {
|
||||
return nil, tc.err
|
||||
}
|
||||
return &http.Response{StatusCode: tc.status, Header: http.Header{"PRIVATE_HEADER": []string{"PRIVATE_SECRET"}}, Body: io.NopCloser(strings.NewReader(tc.body))}, nil
|
||||
})}
|
||||
var events []SummaryDiagnostic
|
||||
var logs bytes.Buffer
|
||||
old := log.Writer()
|
||||
log.SetOutput(&logs)
|
||||
defer log.SetOutput(old)
|
||||
a := generationSummarizer{provider: p, diagnostics: func(d SummaryDiagnostic) error { events = append(events, d); return LogSummaryDiagnostic(d) }}
|
||||
result, err := a.Summarize(context.Background(), "fr", nil, []generation.Candidate{{ID: "PRIVATE_ID", Article: scraper.Article{Title: "PRIVATE_TITLE", Content: strings.Repeat("PRIVATE_CONTENT", 5000)}}})
|
||||
if calls != 1 || len(events) != 1 {
|
||||
t.Fatal("expected one event/request")
|
||||
}
|
||||
d := events[0]
|
||||
status := 0
|
||||
if tc.name == "http" {
|
||||
status = 429
|
||||
}
|
||||
if d.Category != tc.category || d.HTTPStatus != status || d.NetworkCategory != tc.network || d.ArticleCount != 1 || d.PromptBytes <= 0 || d.PromptBytes > d.PromptBudgetBytes || d.PromptBudgetBytes != 24576 || d.TruncatedArticles != 1 || d.DurationMS < 0 {
|
||||
t.Fatalf("safe diagnostic incorrect: %+v", d)
|
||||
}
|
||||
if (err != nil) != (tc.err != nil || tc.name == "http" || tc.name == "response") || (tc.name == "success" && result != "PRIVATE_RESULT") {
|
||||
t.Fatal("provider behavior changed")
|
||||
}
|
||||
raw, _ := json.Marshal(events)
|
||||
if strings.Contains(string(raw)+logs.String(), "PRIVATE_") {
|
||||
t.Fatal("sensitive diagnostic")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestTicket37MetadataAndCollectorIsolation(t *testing.T) {
|
||||
p := &contentProvider{}
|
||||
var events []SummaryDiagnostic
|
||||
a := generationSummarizer{provider: p, diagnostics: func(d SummaryDiagnostic) error { events = append(events, d); return nil }}
|
||||
_, err := a.Summarize(context.Background(), "fr", nil, []generation.Candidate{{Article: scraper.Article{Title: strings.Repeat("PRIVATE_TITLE", 3000)}}})
|
||||
if err == nil || p.calls != 0 || len(events) != 1 || events[0].Category != "metadata_budget_exceeded" || events[0].PromptBytes <= 24576 {
|
||||
t.Fatal("missing preflight diagnostic")
|
||||
}
|
||||
sentinel := errors.New("PRIVATE_ERROR")
|
||||
for _, failure := range []string{"error", "panic", "nil"} {
|
||||
for _, providerErr := range []error{nil, sentinel} {
|
||||
provider := &incident37Provider{err: providerErr}
|
||||
collector := SummaryDiagnosticCollector(func(SummaryDiagnostic) error {
|
||||
if failure == "panic" {
|
||||
panic("PRIVATE_PANIC")
|
||||
}
|
||||
return errors.New("PRIVATE_COLLECTOR")
|
||||
})
|
||||
if failure == "nil" {
|
||||
collector = nil
|
||||
}
|
||||
got, e := (generationSummarizer{provider: provider, diagnostics: collector}).Summarize(context.Background(), "fr", nil, nil)
|
||||
if e != providerErr || got != "synthetic synthesis" || provider.calls != 1 {
|
||||
t.Fatal("collector changed provider outcome")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTicket37HTTPStatusAcrossProviderAdapters(t *testing.T) {
|
||||
transport := ticket31Transport(func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 503, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`{"error":{"message":"PRIVATE_BODY","type":"PRIVATE_TYPE"}}`))}, nil
|
||||
})
|
||||
client := &http.Client{Transport: transport}
|
||||
ollama := newOllama("https://PRIVATE_URL", "PRIVATE_MODEL")
|
||||
ollama.client = client
|
||||
anthropic := newAnthropic("PRIVATE_KEY", "PRIVATE_MODEL")
|
||||
anthropic.client = client
|
||||
gemini := newGemini("PRIVATE_KEY", "PRIVATE_MODEL")
|
||||
gemini.client = client
|
||||
cfg := openai.DefaultConfig("PRIVATE_KEY")
|
||||
cfg.HTTPClient = client
|
||||
oa := &openAIProvider{client: openai.NewClientWithConfig(cfg), model: "PRIVATE_MODEL"}
|
||||
for _, p := range []Provider{ollama, anthropic, gemini, oa} {
|
||||
t.Run(p.Name(), func(t *testing.T) {
|
||||
events := []SummaryDiagnostic{}
|
||||
_, err := (generationSummarizer{provider: p, diagnostics: func(d SummaryDiagnostic) error { events = append(events, d); return nil }}).Summarize(context.Background(), "fr", nil, nil)
|
||||
if err == nil || len(events) != 1 || events[0].Category != "provider_http_error" || events[0].HTTPStatus != 503 {
|
||||
t.Fatal("HTTP status lost")
|
||||
}
|
||||
raw, _ := json.Marshal(events)
|
||||
if strings.Contains(string(raw), "PRIVATE_") {
|
||||
t.Fatal("provider data leaked")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTicket37PipelineCollectorAndDefaultSilence(t *testing.T) {
|
||||
var logs bytes.Buffer
|
||||
old := log.Writer()
|
||||
log.SetOutput(&logs)
|
||||
defer log.SetOutput(old)
|
||||
pipeline := NewPipeline(nil, nil)
|
||||
_, _ = (generationSummarizer{p: pipeline, provider: &contentProvider{}}).Summarize(context.Background(), "fr", nil, nil)
|
||||
if logs.Len() != 0 {
|
||||
t.Fatal("default diagnostic must be silent")
|
||||
}
|
||||
count := 0
|
||||
pipeline.SetSummaryDiagnosticCollector(func(d SummaryDiagnostic) error {
|
||||
count++
|
||||
if d.Category != "success" {
|
||||
t.Fatal("missing success")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
_, _ = (generationSummarizer{p: pipeline, provider: &contentProvider{}}).Summarize(context.Background(), "fr", nil, nil)
|
||||
if count != 1 {
|
||||
t.Fatal("pipeline collector not wired")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user