feat(bloomberg): enrich article content for summaries (#36)
This commit is contained in:
80
backend/internal/scraper/bloomberg/content_ticket36_test.go
Normal file
80
backend/internal/scraper/bloomberg/content_ticket36_test.go
Normal file
@ -0,0 +1,80 @@
|
||||
package bloomberg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestTicket36EnrichDTOContentAndIdentity(t *testing.T) {
|
||||
b := New("http://fixture.invalid")
|
||||
calls := 0
|
||||
b.client = &http.Client{Transport: dateTransport(func(r *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if r.URL.Path != "/bloomberg/enrich" {
|
||||
t.Error("wrong route")
|
||||
}
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"articles":[{"url":"https://www.bloomberg.com/news/articles/a","content":"synthetic paragraph","published_at":"2026-01-02T03:04:05Z","content_truncated":true}]}`)), Header: make(http.Header)}, nil
|
||||
})}
|
||||
got, err := b.EnrichWithCredentials(context.Background(), "fixture", "fixture", []scraper.Article{{Title: "original title", URL: "https://www.bloomberg.com/news/articles/a"}})
|
||||
if err != nil || calls != 1 || len(got) != 1 || got[0].Content != "synthetic paragraph" || got[0].Title != "original title" || got[0].PublishedAt == nil || !got[0].ContentTruncated {
|
||||
t.Fatal("DTO enrichment lost", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTicket36EnrichmentValidationAndUTF8Limit(t *testing.T) {
|
||||
for _, scenario := range []string{"oversize", "missing", "mismatch", "http", "badjson", "empty"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
b := New("http://fixture.invalid")
|
||||
url := "https://www.bloomberg.com/news/articles/a"
|
||||
existing := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
b.client = &http.Client{Transport: dateTransport(func(r *http.Request) (*http.Response, error) {
|
||||
deadline, ok := r.Context().Deadline()
|
||||
if !ok || time.Until(deadline) > 100*time.Second {
|
||||
t.Fatal("unbounded enrichment request")
|
||||
}
|
||||
status := 200
|
||||
raw := ""
|
||||
switch scenario {
|
||||
case "missing":
|
||||
raw = `{"articles":[]}`
|
||||
case "mismatch":
|
||||
raw = `{"articles":[{"url":"https://other.invalid","content":"private"}]}`
|
||||
case "http":
|
||||
status = 503
|
||||
raw = "PRIVATE_BODY http://private.invalid"
|
||||
case "badjson":
|
||||
raw = "PRIVATE_BODY"
|
||||
default:
|
||||
content := ""
|
||||
if scenario == "oversize" {
|
||||
content = strings.Repeat("🙂é", maxContentBytes)
|
||||
}
|
||||
body, _ := json.Marshal(map[string]any{"articles": []any{map[string]any{"url": url, "content": content, "published_at": nil}}})
|
||||
raw = string(body)
|
||||
}
|
||||
return &http.Response{StatusCode: status, Body: io.NopCloser(strings.NewReader(raw)), Header: make(http.Header)}, nil
|
||||
})}
|
||||
got, err := b.EnrichWithCredentials(context.Background(), "fixture", "fixture", []scraper.Article{{Title: "original", URL: url, PublishedAt: &existing}})
|
||||
if scenario == "oversize" || scenario == "empty" {
|
||||
if err != nil || len(got) != 1 || !got[0].PublishedAt.Equal(existing) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got[0].Content) > maxContentBytes || !utf8.ValidString(got[0].Content) {
|
||||
t.Fatal("invalid content boundary")
|
||||
}
|
||||
if got[0].ContentTruncated != (scenario == "oversize") {
|
||||
t.Fatal("missing limit flag")
|
||||
}
|
||||
} else if err == nil || got != nil || strings.Contains(err.Error(), "PRIVATE") || strings.Contains(err.Error(), "http") {
|
||||
t.Fatal("invalid association/error leaked", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user