feat(generation): select articles in three passes

This commit is contained in:
2026-11-24 00:38:02 +01:00
parent d22f84529f
commit 8022ee0547
27 changed files with 916 additions and 68 deletions

View File

@ -0,0 +1,78 @@
package generation
import (
"context"
"github.com/tradarr/backend/internal/models"
"github.com/tradarr/backend/internal/scraper"
"reflect"
"testing"
"time"
)
func reviewDate(title string, hour int) scraper.Article {
d := time.Date(2026, 1, 1, hour, 0, 0, 0, time.UTC)
return scraper.Article{Title: title, PublishedAt: &d}
}
func TestTicket34MixedSourceUsesReceiptOrderBeforeInputLimit(t *testing.T) {
for _, articles := range [][]scraper.Article{
{{Title: "first-undated"}, reviewDate("second-old", 1), reviewDate("third-new", 9)},
{reviewDate("first-old", 1), reviewDate("second-new", 9), {Title: "third-undated"}},
} {
ordered := recentArticles(articles)
got := []string{ordered[0].Title, ordered[1].Title}
want := []string{articles[0].Title, articles[1].Title}
if !reflect.DeepEqual(got, want) {
t.Fatalf("mixed source reordered before N: got %v want %v", got, want)
}
sum := &contractSummarizer{}
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title", GlobalLimit: 2, Sources: []models.Source{{ID: "s", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 2}}}}, saved: map[string][]GenerationRecord{}}
result, err := NewUseCase(Dependencies{Repository: repo, Decider: &allYesMention{}, Summarizer: sum, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: &contractScraper{articles: articles}}}).Generate(context.Background(), "u")
if err != nil || result.Outcome.TotalCandidates != 2 || len(sum.articles) != 1 || !reflect.DeepEqual(candidateTitles(sum.articles[0]), want) {
t.Fatal("source mode must be chosen before N and preserved through passes", result.Outcome, err)
}
}
}
func TestTicket34MergePreservesEachSourceOrderIncludingMixedDates(t *testing.T) {
gate := &allYesMention{}
sum := &contractSummarizer{}
a := &contractScraper{articles: []scraper.Article{reviewDate("a-old", 1), {Title: "a-undated"}, reviewDate("a-new", 9)}}
b := &contractScraper{articles: []scraper.Article{reviewDate("b-new", 10), reviewDate("b-mid", 5)}}
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title", GlobalLimit: 5, Sources: []models.Source{{ID: "a", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 3}, {ID: "b", SourceKey: testSourceKey(models.SourceBloomberg), Enabled: true, ArticleLimit: 2}}}}, saved: map[string][]GenerationRecord{}}
r, err := NewUseCase(Dependencies{Repository: repo, Decider: gate, Summarizer: sum, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: a, models.SourceBloomberg: b}}).Generate(context.Background(), "u")
if err != nil || r.Summary == nil {
t.Fatal(r, err)
}
got := candidateTitles(sum.articles[0])
want := []string{"b-new", "b-mid", "a-old", "a-undated", "a-new"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("merge violated source order: got %v want %v", got, want)
}
}
func TestTicket34SelectionHasOnlyGlobalOutputLimit(t *testing.T) {
// The selector receives an already bounded pool. No independent per-source
// output quota should be necessary or consulted at this stage.
gate := &allYesMention{}
uc := &useCase{Dependencies: Dependencies{Decider: gate}}
pool := []Candidate{{ID: "a", SourceID: "s", Article: reviewDate("a", 3)}, {ID: "b", SourceID: "s", Article: reviewDate("b", 2)}, {ID: "c", SourceID: "s", Article: reviewDate("c", 1)}}
selected, _, err := uc.selectThreePass(context.Background(), UserInput{RankingMode: "title", GlobalLimit: 2}, pool)
if err != nil || len(selected) != 2 {
t.Fatalf("selector reapplied a source output quota: %d %v", len(selected), err)
}
}
func TestTicket34FullyDatedSourcesMergeChronologicallyWithStableTies(t *testing.T) {
a := recentArticles([]scraper.Article{reviewDate("a-old", 1), reviewDate("a-new", 3)})
b := recentArticles([]scraper.Article{reviewDate("b-mid", 2), reviewDate("b-new", 3)})
pool := []Candidate{}
for _, article := range a {
pool = append(pool, Candidate{SourceID: "a", Article: article})
}
for _, article := range b {
pool = append(pool, Candidate{SourceID: "b", Article: article})
}
got := candidateTitles(mergeSourceCandidates(pool))
if !reflect.DeepEqual(got, []string{"a-new", "b-new", "b-mid", "a-old"}) {
t.Fatal(got)
}
}