feat(summary): send complete articles within user budget (#39)
This commit is contained in:
68
backend/internal/ai/full_summary_ticket39_test.go
Normal file
68
backend/internal/ai/full_summary_ticket39_test.go
Normal file
@ -0,0 +1,68 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tradarr/backend/internal/generation"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"github.com/tradarr/backend/internal/summarybudget"
|
||||
)
|
||||
|
||||
func TestTicket39FinalPromptBudgetFullBodyWatchlistContextAndDates(t *testing.T) {
|
||||
p := &incident37Provider{}
|
||||
var diagnostics []SummaryDiagnostic
|
||||
a := fullGenerationSummarizer{provider: p, providerName: "openai", model: "gpt-4o-mini", diagnostics: func(d SummaryDiagnostic) error { diagnostics = append(diagnostics, d); return nil }}
|
||||
date := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
body := strings.Repeat("Full synthetic editorial é漢🙂.\n", 1000)
|
||||
req := generation.SummaryRequest{Language: "en", Budget: 250000, Context: "Existing generation context", GeneratedAt: date, Assets: []models.UserAsset{{Name: "Followed company", Symbol: "INTERNAL"}}, Candidates: []generation.Candidate{{ID: "first", Article: scraper.Article{Title: "First title", Content: body, Source: "yahoo_finance", PublishedAt: &date}}, {ID: "second", Article: scraper.Article{Title: "Second title", Content: "Entire second body.", Source: "cnbc_business"}}}}
|
||||
_, usage, err := a.SummarizeWithBudget(context.Background(), req)
|
||||
if err != nil || p.calls != 1 || usage.EffectiveTokens != 123904 || usage.RequestedTokens != 250000 || p.options.MaxOutputTokens != 4096 || p.options.NumCtx != 128000 {
|
||||
t.Fatalf("budget/forwarding incorrect: %+v %v", usage, err)
|
||||
}
|
||||
var payload struct {
|
||||
Indices []map[string]string
|
||||
Articles []struct {
|
||||
ID, Title, Content, Source string
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
}
|
||||
Context struct {
|
||||
Instructions, Language string
|
||||
GeneratedAt time.Time `json:"generated_at"`
|
||||
}
|
||||
}
|
||||
at := strings.Index(p.prompt, "\n")
|
||||
if at < 0 || json.Unmarshal([]byte(p.prompt[at+1:]), &payload) != nil {
|
||||
t.Fatal("invalid prompt")
|
||||
}
|
||||
if len(payload.Articles) != 2 || payload.Articles[0].Content != body || !payload.Articles[0].PublishedAt.Equal(date) || payload.Articles[1].PublishedAt != nil || payload.Articles[0].Source != "yahoo_finance" || len(payload.Indices) != 1 || payload.Indices[0]["name"] != "Followed company" || payload.Context.Instructions != req.Context || payload.Context.Language != "en" || !payload.Context.GeneratedAt.Equal(date) {
|
||||
t.Fatal("full prompt lost input")
|
||||
}
|
||||
if len(p.prompt) <= 24*1024 || strings.Contains(p.prompt, "content_truncated") {
|
||||
t.Fatal("legacy truncation remains")
|
||||
}
|
||||
if usage.PromptTokens != len(p.prompt)+1024 {
|
||||
t.Fatal("whole prompt not counted")
|
||||
}
|
||||
req.Budget = usage.PromptTokens - 1
|
||||
_, _, err = a.SummarizeWithBudget(context.Background(), req)
|
||||
if !errors.Is(err, summarybudget.ErrExceeded) || p.calls != 1 {
|
||||
t.Fatal("overflow reached provider")
|
||||
}
|
||||
a.model = "unknown"
|
||||
_, _, err = a.SummarizeWithBudget(context.Background(), req)
|
||||
if !errors.Is(err, summarybudget.ErrUnknown) || p.calls != 1 {
|
||||
t.Fatal("unknown capacity reached provider")
|
||||
}
|
||||
raw, _ := json.Marshal(diagnostics)
|
||||
for _, secret := range []string{"Full synthetic", "Followed company", "Existing generation"} {
|
||||
if strings.Contains(string(raw), secret) {
|
||||
t.Fatal("sensitive diagnostics")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user