feat(scraper): bound article enrichment diagnostics (#40)

This commit is contained in:
2026-11-25 00:07:45 +01:00
parent a3c70670f6
commit 6883b3976b
53 changed files with 4049 additions and 68 deletions

View File

@ -0,0 +1,103 @@
package bloomberg
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/tradarr/backend/internal/scraper"
)
const bloombergFixtureURL = "https://www.bloomberg.com/news/articles/fixture"
func TestIngestionPipelineBloombergListThenAuthenticatedEnrichmentPort(t *testing.T) {
shortSymptom := strings.Repeat("A", 320) + "\n\n" + strings.Repeat("B", 339) // 661 synthetic characters, two paragraphs.
for _, tc := range []struct {
name, content, state, date string
wantComplete, wantDate bool
badAssociation bool
}{
{name: "full editorial body and reliable date", content: "Synthetic initial paragraph.\n\nFinal editorial paragraph.", state: scraper.ContentComplete, date: "2026-01-02T03:04:05Z", wantComplete: true, wantDate: true},
{name: "list metadata without enriched body", content: "", state: scraper.ContentUnavailable},
{name: "partial paywall body", content: "Synthetic initial paragraph.", state: scraper.ContentIncomplete, date: "2026-01-02T03:04:05Z", wantDate: true},
{name: "challenge body", content: "Synthetic challenge placeholder.", state: scraper.ContentIncomplete, date: "2026-01-02T03:04:05Z", wantDate: true},
{name: "661-character body with nullable date", content: shortSymptom, state: scraper.ContentComplete, wantComplete: true},
{name: "wrong article association", content: "Synthetic foreign article body.", state: scraper.ContentComplete, date: "2026-01-02T03:04:05Z", wantDate: true, badAssociation: true},
} {
t.Run(tc.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/bloomberg/scrape":
var credentials scraperRequest
if r.Method != http.MethodPost || json.NewDecoder(r.Body).Decode(&credentials) != nil || credentials.Username != "fixture-user" || credentials.Password != "fixture-password" {
t.Error("Bloomberg list collector did not receive fixture authentication request")
w.WriteHeader(http.StatusBadRequest)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"articles": []any{map[string]any{"title": "Synthetic fixture headline", "url": bloombergFixtureURL}}})
case "/bloomberg/enrich":
var request struct {
Username string `json:"username"`
Password string `json:"password"`
URLs []string `json:"urls"`
}
if r.Method != http.MethodPost || json.NewDecoder(r.Body).Decode(&request) != nil || request.Username != "fixture-user" || request.Password != "fixture-password" || len(request.URLs) != 1 || request.URLs[0] != bloombergFixtureURL {
t.Error("Bloomberg enricher was not given the exact article selected by the list collector")
w.WriteHeader(http.StatusBadRequest)
return
}
articleURL := bloombergFixtureURL
if tc.badAssociation {
articleURL += "-other"
}
_ = json.NewEncoder(w).Encode(map[string]any{"articles": []any{map[string]any{
"url": articleURL, "content": tc.content, "content_state": tc.state,
"content_truncated": false, "published_at": tc.date,
}}})
default:
t.Error("unexpected Bloomberg fixture route")
http.Error(w, "fixture route unavailable", http.StatusNotFound)
}
}))
defer server.Close()
feed := New(server.URL)
listed, err := feed.ScrapeWithCredentials(context.Background(), "fixture-user", "fixture-password", nil)
if err != nil || len(listed) != 1 {
t.Fatal("Bloomberg fixture collector did not return the single listed article")
}
if listed[0].URL != bloombergFixtureURL || scraper.ContentStatus(listed[0]) == scraper.ContentComplete {
t.Fatal("Bloomberg list metadata was counted as a full body")
}
got, err := feed.EnrichWithCredentials(context.Background(), "fixture-user", "fixture-password", listed)
if tc.badAssociation {
if err == nil || len(got) != 0 {
t.Fatal("Bloomberg accepted enrichment for an article outside the collected list")
}
return
}
if err != nil || len(got) != 1 || got[0].URL != listed[0].URL {
t.Fatal("Bloomberg authenticated enrichment failed or detached the listed article")
}
if (scraper.ContentStatus(got[0]) == scraper.ContentComplete) != tc.wantComplete {
t.Fatal("Bloomberg full-body classification mismatch")
}
if (got[0].PublishedAt != nil) != tc.wantDate {
t.Fatal("Bloomberg publication-date availability mismatch")
}
if tc.wantComplete && tc.name != "661-character body with nullable date" && !strings.Contains(got[0].Content, "Final editorial paragraph.") {
t.Fatal("Bloomberg final editorial text was not collected")
}
if tc.name == "661-character body with nullable date" && len(got[0].Content) != 661 {
t.Fatal("Bloomberg synthetic full-body fixture was clipped")
}
if tc.wantDate && !got[0].PublishedAt.Equal(time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)) {
t.Fatal("Bloomberg did not associate the expected publication date")
}
})
}
}