package generation import ( "context" "encoding/json" "io" "net/http" "net/http/httptest" "strings" "testing" "github.com/tradarr/backend/internal/models" "github.com/tradarr/backend/internal/scraper" "github.com/tradarr/backend/internal/scraper/articlecontent" "github.com/tradarr/backend/internal/scraper/bloomberg" ) type ticket40Transport func(*http.Request) (*http.Response, error) func (f ticket40Transport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } type ticket40EvidenceFeed struct { calls *int source string articles []scraper.Article client *http.Client bb *bloomberg.Bloomberg } func (f ticket40EvidenceFeed) Name() string { return f.source } func (f ticket40EvidenceFeed) Scrape(context.Context, []string) ([]scraper.Article, error) { if f.calls != nil { *f.calls++ } return append([]scraper.Article(nil), f.articles...), nil } func (f ticket40EvidenceFeed) Enrich(ctx context.Context, a []scraper.Article) ([]scraper.Article, error) { if f.bb != nil { return f.bb.EnrichWithCredentials(ctx, "fixture", "fixture", a) } return articlecontent.Enrich(ctx, f.client, f.source, a) } func TestTicket40LegacyEnrichmentPreservedAndRetiredSourcesCannotGenerate(t *testing.T) { for _, key := range []string{models.SourceYahooFinance, models.SourceBloomberg, models.SourceCNBCBusiness} { t.Run(key, func(t *testing.T) { const full = "Synthetic full editorial body." base, root := "https://finance.yahoo.com/news/", "caas-body" if key == models.SourceCNBCBusiness { base = "https://www.cnbc.com/2026/01/02/" root = "ArticleBody-articleBody" } if key == models.SourceBloomberg { base = "https://www.bloomberg.com/news/articles/" } calls := 0 feed := ticket40EvidenceFeed{source: key, calls: &calls} for _, kind := range []string{"complete", "partial", "absent"} { feed.articles = append(feed.articles, scraper.Article{Title: "Followed fixture " + kind, URL: base + kind + ".html", Content: "RSS teaser", ContentState: scraper.ContentIncomplete}) } if key == models.SourceBloomberg { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var req struct { URLs []string `json:"urls"` } if r.URL.Path != "/bloomberg/enrich" || json.NewDecoder(r.Body).Decode(&req) != nil { t.Error("unexpected fixture RPC") w.WriteHeader(400) return } result := []map[string]any{} for _, url := range req.URLs { body, state := full, scraper.ContentComplete if strings.Contains(url, "partial") { body = "Available introduction." state = scraper.ContentIncomplete } if strings.Contains(url, "absent") { body = "" state = scraper.ContentUnavailable } result = append(result, map[string]any{"url": url, "content": body, "content_state": state}) } json.NewEncoder(w).Encode(map[string]any{"articles": result}) })) defer server.Close() feed.bb = bloomberg.New(server.URL) } else { feed.client = &http.Client{Transport: ticket40Transport(func(r *http.Request) (*http.Response, error) { body := `

` + full + `

` if strings.Contains(r.URL.Path, "partial") { body += `
` } if strings.Contains(r.URL.Path, "absent") { body = `
` } return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: r}, nil })} } // The legacy adapters remain intact for historical/diagnostic tooling. enriched, err := feed.Enrich(context.Background(), feed.articles) if err != nil || len(enriched) != 3 || scraper.ContentStatus(enriched[0]) != scraper.ContentComplete || scraper.ContentStatus(enriched[1]) != scraper.ContentIncomplete || scraper.ContentStatus(enriched[2]) != scraper.ContentUnavailable { t.Fatal("legacy enrichment port was changed") } repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title_content", GlobalLimit: 3, Assets: []models.UserAsset{{Name: "Followed fixture"}}, Sources: []models.Source{{ID: "s", SourceKey: &key, Enabled: true, ArticleLimit: 3}}}}, saved: map[string][]GenerationRecord{}} sum := &contractSummarizer{} result, err := NewUseCase(Dependencies{Repository: repo, Decider: &allYesMention{}, Summarizer: sum, Scrapers: map[string]scraper.Scraper{key: feed}}).Generate(context.Background(), "u") o := result.Outcome if key == models.SourceYahooFinance || key == models.SourceCNBCBusiness { if err != nil || result.Summary != nil || calls != 0 || len(sum.articles) != 0 || o.TotalCandidates != 0 || len(o.SourceStats) != 0 { t.Fatal("retired source entered new generation") } return } if err != nil || result.Summary == nil || o.Status != "partial" || o.ContentComplete != 1 || o.ContentIncomplete != 1 || o.ContentUnavailable != 1 || o.Selected != 1 || len(sum.articles) != 1 || sum.articles[0][0].Article.Content != full || o.SourceStats[0].Status != "ok" || o.SourceStats[0].SourceKey != key { t.Fatal("enrichment status lost before summary", o, err) } raw, _ := json.Marshal(repo.saved["u"][0].Outcome) if strings.Contains(string(raw), full) || strings.Contains(string(raw), "https://") { t.Fatal("article data persisted in aggregate") } }) } }