package generation import ( "context" "errors" "github.com/tradarr/backend/internal/models" "github.com/tradarr/backend/internal/scraper" "testing" "time" ) type contentFeed struct { contractScraper calls int sizes []int fail bool } func (f *contentFeed) Enrich(_ context.Context, a []scraper.Article) ([]scraper.Article, error) { f.calls++ f.sizes = append(f.sizes, len(a)) if f.fail { return nil, errors.New("private error") } out := append([]scraper.Article(nil), a...) for i := range out { out[i].ContentState = scraper.ContentComplete out[i].Content = "synthetic editorial paragraph" d := time.Date(2026, 1, 1, i+1, 0, 0, 0, time.UTC) out[i].PublishedAt = &d } return out, nil } func TestTicket36EnrichmentAfterPoolAndNoReordering(t *testing.T) { for _, mode := range []string{"title_content", "title", "failure", "other_source"} { t.Run(mode, func(t *testing.T) { source := models.SourceInvesting rankingMode := mode if mode == "failure" || mode == "other_source" { rankingMode = "title_content" } if mode == "other_source" { source = models.SourceBloomberg } feed := &contentFeed{fail: mode == "failure", contractScraper: contractScraper{articles: []scraper.Article{{Title: "Fixture first"}, {Title: "Fixture second"}, {Title: "excluded"}}}} repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: rankingMode, GlobalLimit: 2, Assets: []models.UserAsset{{Name: "Fixture"}}, Sources: []models.Source{{ID: "s", SourceKey: testSourceKey(source), Enabled: true, ArticleLimit: 2}}}}, saved: map[string][]GenerationRecord{}} sum := &contractSummarizer{} r, err := NewUseCase(Dependencies{Repository: repo, Decider: &allYesMention{}, Summarizer: sum, Scrapers: map[string]scraper.Scraper{source: feed}}).Generate(context.Background(), "u") if mode == "failure" { if err != nil || r.Summary != nil || r.Outcome.FailureCode != "no_complete_content" || feed.calls != 1 || r.Outcome.ContentFallbacks != 2 { t.Fatal("incomplete enrichment must stop before summary") } return } if err != nil || r.Summary == nil { t.Fatal(err, r.Outcome) } if mode == "title_content" || mode == "other_source" { if feed.calls != 1 || feed.sizes[0] != 2 || sum.articles[0][0].Article.Content == "" || r.Outcome.ContentFallbacks != 0 { t.Fatal("enrichment missing or before N", feed.calls, feed.sizes) } } else if mode == "failure" { if feed.calls != 1 || r.Outcome.ContentFallbacks != 2 { t.Fatal("failed enrichment dropped candidates") } } else if feed.calls != 0 || sum.articles[0][0].Article.Content != "" { t.Fatal("unnecessary enrichment") } if sum.articles[0][0].Article.Title != "Fixture first" { t.Fatal("enriched dates reordered pool") } }) } } func TestTicket36EnrichedDateDoesNotChangeCrossSourceOrder(t *testing.T) { keyB, keyY := models.SourceInvesting, models.SourceBloomberg feed := &contentFeed{contractScraper: contractScraper{articles: []scraper.Article{{Title: "Fixture Bloomberg"}}}} old := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) other := &contractScraper{articles: []scraper.Article{{Title: "Fixture Yahoo", ContentState: scraper.ContentComplete, Content: "synthetic full Yahoo body", PublishedAt: &old}}} repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title_content", GlobalLimit: 2, Assets: []models.UserAsset{{Name: "Fixture"}}, Sources: []models.Source{{ID: "b", SourceKey: &keyB, Enabled: true, ArticleLimit: 1}, {ID: "y", SourceKey: &keyY, Enabled: true, ArticleLimit: 1}}}}, saved: map[string][]GenerationRecord{}} sum := &contractSummarizer{} r, err := NewUseCase(Dependencies{Repository: repo, Decider: &allYesMention{}, Summarizer: sum, Scrapers: map[string]scraper.Scraper{keyB: feed, keyY: other}}).Generate(context.Background(), "u") if err != nil || r.Summary == nil || sum.articles[0][0].Article.Title != "Fixture Yahoo" || sum.articles[0][1].Article.PublishedAt == nil { t.Fatal("new publication date reranked the frozen pool", r.Outcome, err) } }