84 lines
3.5 KiB
Go
84 lines
3.5 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/tradarr/backend/internal/generation"
|
|
"github.com/tradarr/backend/internal/models"
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
"github.com/tradarr/backend/internal/summarybudget"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestTicket47PromptBudgetEviction(t *testing.T) {
|
|
p := &incident37Provider{}
|
|
a := fullGenerationSummarizer{provider: p, providerName: "openai", model: "unknown-catalog-model"}
|
|
old := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
recent := old.Add(time.Hour)
|
|
candidates := []generation.Candidate{}
|
|
for i, date := range []*time.Time{&recent, nil, &old, &old, nil} {
|
|
candidates = append(candidates, generation.Candidate{ID: string(rune('a' + i)), Article: scraper.Article{Title: "PRIVATE_TITLE", Content: strings.Repeat("é<&🙂", 100), PublishedAt: date}})
|
|
}
|
|
req := generation.SummaryRequest{Budget: 1000000, Context: "PRIVATE_FIXED_CONTEXT", Assets: []models.UserAsset{{Name: "PRIVATE_WATCHLIST"}}, Candidates: candidates}
|
|
// Successive fitting subsets prove the exact eviction order and retention order.
|
|
for _, keep := range [][]int{{0, 1, 2, 3, 4}, {0, 1, 2, 3}, {0, 2, 3}, {0, 2}, {0}} {
|
|
subset := req
|
|
subset.Candidates = nil
|
|
for _, i := range keep {
|
|
subset.Candidates = append(subset.Candidates, candidates[i])
|
|
}
|
|
_, u, _, err := a.SummarizeWithBudget(context.Background(), subset)
|
|
if err != nil {
|
|
t.Fatal("catalog model blocked", err)
|
|
}
|
|
expected := p.prompt
|
|
fitted := req
|
|
fitted.Budget = u.PromptTokens
|
|
_, got, removed, err := a.SummarizeWithBudget(context.Background(), fitted)
|
|
if err != nil || p.prompt != expected || got.PromptTokens != len(p.prompt)+1024 || got.EffectiveTokens != fitted.Budget || p.options.NumCtx != 0 {
|
|
t.Fatalf("incorrect full prompt fitting: %+v %v", got, err)
|
|
}
|
|
raw, _ := json.Marshal(got)
|
|
var fields map[string]any
|
|
json.Unmarshal(raw, &fields)
|
|
if _, duplicate := fields["articles_removed_for_prompt_budget"]; duplicate {
|
|
t.Fatal("counter duplicated in usage")
|
|
}
|
|
if removed != 5-len(keep) {
|
|
t.Fatal("wrong removed count")
|
|
}
|
|
|
|
}
|
|
calls := p.calls
|
|
req.Budget = 1
|
|
if _, _, _, err := a.SummarizeWithBudget(context.Background(), req); !errors.Is(err, summarybudget.ErrFixedContextExceeded) || p.calls != calls {
|
|
t.Fatal("fixed overflow called provider")
|
|
}
|
|
req.Budget = 1800
|
|
req.Candidates = []generation.Candidate{{Article: scraper.Article{Content: strings.Repeat("x", 10000)}}}
|
|
if _, _, _, err := a.SummarizeWithBudget(context.Background(), req); !errors.Is(err, summarybudget.ErrNoArticlesFit) || p.calls != calls {
|
|
t.Fatal("no article fits but provider called")
|
|
}
|
|
if candidates[0].Article.Content != strings.Repeat("é<&🙂", 100) || len(candidates) != 5 {
|
|
t.Fatal("input mutated")
|
|
}
|
|
}
|
|
|
|
func TestTicket47FixedContextCannotBeEvicted(t *testing.T) {
|
|
p := &incident37Provider{}
|
|
a := fullGenerationSummarizer{provider: p, model: "unverified"}
|
|
req := generation.SummaryRequest{Budget: 2500, Context: strings.Repeat("PRIVATE_FIXED", 1000), Candidates: []generation.Candidate{{Article: scraper.Article{Content: "small"}}}}
|
|
_, _, removed, err := a.SummarizeWithBudget(context.Background(), req)
|
|
if !errors.Is(err, summarybudget.ErrFixedContextExceeded) || p.calls != 0 || removed != 0 {
|
|
t.Fatal("fixed context removed or sent")
|
|
}
|
|
req.Context = "short"
|
|
req.Candidates = nil
|
|
if _, _, _, err = a.SummarizeWithBudget(context.Background(), req); !errors.Is(err, summarybudget.ErrNoArticlesFit) || p.calls != 0 {
|
|
t.Fatal("empty article generation sent")
|
|
}
|
|
}
|