feat(generation): select articles in three passes
This commit is contained in:
131
backend/internal/generation/three_pass_ticket34_test.go
Normal file
131
backend/internal/generation/three_pass_ticket34_test.go
Normal file
@ -0,0 +1,131 @@
|
||||
package generation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type mentionFixture struct {
|
||||
calls []string
|
||||
batches [][]string
|
||||
yes map[string]map[string]bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *mentionFixture) DecideMentions(_ context.Context, mode, pass string, _ []models.UserAsset, c []Candidate) ([]Candidate, error) {
|
||||
f.calls = append(f.calls, pass)
|
||||
f.batches = append(f.batches, candidateTitles(c))
|
||||
if f.err != nil {
|
||||
return nil, f.err
|
||||
}
|
||||
out := []Candidate{}
|
||||
for _, v := range c {
|
||||
if f.yes[pass][v.Article.Title] {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func TestTicket34ThreePassesInputPoolPriorityAndLastBatchTrim(t *testing.T) {
|
||||
dated := func(title string, h int) scraper.Article {
|
||||
d := time.Date(2026, 1, 1, h, 0, 0, 0, time.UTC)
|
||||
return scraper.Article{Title: title, PublishedAt: &d}
|
||||
}
|
||||
a := &contractScraper{articles: []scraper.Article{dated("Acme Holdings", 1), dated("a9", 9), dated("a8", 8), dated("a7", 7), dated("excluded", 0)}}
|
||||
b := &contractScraper{articles: []scraper.Article{{Title: "b1"}, {Title: "b2"}, {Title: "b3"}, {Title: "excluded-b"}}}
|
||||
gate := &mentionFixture{yes: map[string]map[string]bool{"direct": {"a9": true, "b1": true}, "indirect": {"a8": true, "b2": true, "b3": true}}}
|
||||
sum := &contractSummarizer{}
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title", GlobalLimit: 4, Assets: []models.UserAsset{{Name: "Acme Holdings"}}, Sources: []models.Source{{ID: "a", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 4}, {ID: "b", SourceKey: testSourceKey(models.SourceBloomberg), Enabled: true, ArticleLimit: 3}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Decider: gate, Summarizer: sum, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: a, models.SourceBloomberg: b}})
|
||||
result, err := uc.Generate(context.Background(), "u")
|
||||
if err != nil || result.Summary == nil {
|
||||
t.Fatalf("%+v %v", result.Outcome, err)
|
||||
}
|
||||
if !reflect.DeepEqual(candidateTitles(sum.articles[0]), []string{"Acme Holdings", "a9", "b1", "a8"}) {
|
||||
t.Fatal(candidateTitles(sum.articles[0]))
|
||||
}
|
||||
if !reflect.DeepEqual(gate.calls, []string{"direct", "indirect"}) || !reflect.DeepEqual(gate.batches, [][]string{{"a9", "a8", "a7", "b1", "b2", "b3"}, {"a8", "a7", "b2", "b3"}}) {
|
||||
t.Fatal(gate.calls, gate.batches)
|
||||
}
|
||||
if result.Outcome.TotalCandidates != 7 || result.Outcome.SourceStats[0].Candidates != 4 || result.Outcome.SourceStats[1].Candidates != 3 || result.Outcome.SourceStats[0].Selected != 3 || result.Outcome.SourceStats[1].Selected != 1 {
|
||||
t.Fatal(result.Outcome)
|
||||
}
|
||||
}
|
||||
func TestTicket34NoPartialSummaryOnMentionFailure(t *testing.T) {
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title", GlobalLimit: 5, Assets: []models.UserAsset{{Name: "Acme"}}, Sources: []models.Source{{ID: "s", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 5}}}}, saved: map[string][]GenerationRecord{}}
|
||||
sum := &contractSummarizer{}
|
||||
gate := &mentionFixture{err: errors.New("private failure")}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Decider: gate, Summarizer: sum, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: &contractScraper{articles: []scraper.Article{{Title: "Acme"}, {Title: "unmatched"}}}}})
|
||||
r, err := uc.Generate(context.Background(), "u")
|
||||
if err != nil || r.Summary != nil || r.Outcome.Status != "failed" || len(sum.articles) != 0 {
|
||||
t.Fatal(r, err)
|
||||
}
|
||||
}
|
||||
|
||||
type allYesMention struct {
|
||||
passes []string
|
||||
sizes []int
|
||||
}
|
||||
|
||||
func (f *allYesMention) DecideMentions(_ context.Context, _, pass string, _ []models.UserAsset, c []Candidate) ([]Candidate, error) {
|
||||
f.passes = append(f.passes, pass)
|
||||
f.sizes = append(f.sizes, len(c))
|
||||
return c, nil
|
||||
}
|
||||
func TestTicket34StopsAtReachingBatchWithoutReplacingEarlierPass(t *testing.T) {
|
||||
gate := &allYesMention{}
|
||||
sum := &contractSummarizer{}
|
||||
newest := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
articles := []scraper.Article{}
|
||||
for i := 0; i < 130; i++ {
|
||||
d := newest.Add(-time.Duration(i) * time.Minute)
|
||||
articles = append(articles, scraper.Article{Title: fmt.Sprintf("article-%03d", i), PublishedAt: &d})
|
||||
}
|
||||
old := newest.Add(-1000 * time.Hour)
|
||||
articles = append(articles, scraper.Article{Title: "Acme Holdings", PublishedAt: &old})
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title", GlobalLimit: 66, Assets: []models.UserAsset{{Name: "Acme Holdings"}}, Sources: []models.Source{{ID: "s", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 200}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Decider: gate, Summarizer: sum, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: &contractScraper{articles: articles}}})
|
||||
r, err := uc.Generate(context.Background(), "u")
|
||||
if err != nil || r.Outcome.Selected != 66 || !reflect.DeepEqual(gate.passes, []string{"direct", "direct"}) || !reflect.DeepEqual(gate.sizes, []int{64, 64}) {
|
||||
t.Fatal(r.Outcome, gate.passes, gate.sizes, err)
|
||||
}
|
||||
got := candidateTitles(sum.articles[0])
|
||||
if got[0] != "Acme Holdings" || got[65] != "article-064" {
|
||||
t.Fatal("priority/trim changed", got[0], got[65])
|
||||
}
|
||||
}
|
||||
|
||||
type fixtureNames struct{}
|
||||
|
||||
func (fixtureNames) MentionNames(id string) ([]string, error) {
|
||||
if id != "resolved" {
|
||||
return nil, errors.New("unknown")
|
||||
}
|
||||
return []string{"Intel Corporation", "intel"}, nil
|
||||
}
|
||||
func TestTicket34DeterministicMentionUsesResolvedNamesAliasesAndModeOnly(t *testing.T) {
|
||||
for _, mode := range []string{"title", "title_content"} {
|
||||
id := "resolved"
|
||||
gate := &mentionFixture{yes: map[string]map[string]bool{}}
|
||||
sum := &contractSummarizer{}
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: mode, GlobalLimit: 1, Assets: []models.UserAsset{{Name: "untrusted stored spelling", Symbol: "INTC", CompanyID: &id}}, Sources: []models.Source{{ID: "s", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 2}}}}, saved: map[string][]GenerationRecord{}}
|
||||
feed := &contractScraper{articles: []scraper.Article{{Title: "Intelligence and INTC", Content: "Intel invests."}}}
|
||||
r, err := NewUseCase(Dependencies{Repository: repo, Decider: gate, Names: fixtureNames{}, Summarizer: sum, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: feed}}).Generate(context.Background(), "u")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if mode == "title_content" {
|
||||
if r.Summary == nil || len(gate.calls) != 0 {
|
||||
t.Fatal("body alias not accepted in pass 1")
|
||||
}
|
||||
} else if r.Summary != nil || !reflect.DeepEqual(gate.calls, []string{"direct", "indirect"}) {
|
||||
t.Fatal("substring/symbol incorrectly matched", gate.calls)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user