Files

148 lines
7.3 KiB
Go

package quanwenrss
import (
"os"
"strings"
"testing"
"time"
"github.com/tradarr/backend/internal/scraper"
)
func TestContractualRSSPreservesCleanFullBodyAndDate(t *testing.T) {
raw, err := os.ReadFile("testdata/investing.xml")
if err != nil {
t.Fatal(err)
}
got, err := Parse(raw, Investing)
if err != nil || len(got) != 2 {
t.Fatal("synthetic RSS articles were not parsed")
}
expected := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
if got[0].PublishedAt == nil || !got[0].PublishedAt.Equal(expected) || got[1].PublishedAt != nil {
t.Fatal("RSS dates were invented or lost")
}
if !strings.Contains(got[0].Content, "Final synthetic paragraph.") || strings.Contains(got[0].Content, "EXCLUDED") || strings.Contains(got[0].Content, "<") {
t.Fatal("RSS editorial body was not cleaned")
}
if scraper.ContentStatus(got[0]) != scraper.ContentComplete || scraper.ContentStatus(got[1]) != scraper.ContentIncomplete {
t.Fatal("contractual encoded body or description completeness is wrong")
}
}
func TestMergeDeduplicatesBeforeApplyingSourceLimitAndKeepsStableListing(t *testing.T) {
date := time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC)
listed := "https://www.bloomberg.com/news/articles/2026-01-02/synthetic"
first := []scraper.Article{{Title: "Synthetic initial", URL: listed, Content: "Synthetic teaser", ContentState: scraper.ContentIncomplete}, {Title: "Synthetic second", URL: listed + "-second"}}
second := []scraper.Article{{Title: "Synthetic duplicate", URL: listed + "?utm_source=fixture", Content: "Synthetic longer but unproven body", ContentState: scraper.ContentIncomplete, PublishedAt: &date}, {Title: "Synthetic third", URL: listed + "-third"}}
got := Merge([][]scraper.Article{first, second})
if len(got) != 3 || got[0].URL != listed || got[0].Title != "Synthetic initial" || got[0].PublishedAt == nil {
t.Fatal("source merge lost stable association or consumed source capacity with duplicate")
}
if scraper.ContentStatus(got[0]) == scraper.ContentComplete {
t.Fatal("merge invented completeness")
}
}
func TestRSSRejectsUnsafeOrOversizedDocumentsWithoutRawErrors(t *testing.T) {
for _, raw := range []string{
`<!DOCTYPE rss [<!ENTITY p "PRIVATE_CANARY">]><rss><channel/></rss>`,
`<rss><channel><item>`, strings.Repeat("x", MaxFeedBytes+1),
`<rss><channel>` + strings.Repeat("<x>", MaxDepth+1) + strings.Repeat("</x>", MaxDepth+1) + `</channel></rss>`,
} {
_, err := Parse([]byte(raw), Investing)
if err == nil || strings.Contains(err.Error(), "PRIVATE") || strings.Contains(err.Error(), "http") {
t.Fatal("RSS bound or sanitized error contract failed")
}
}
}
func TestRSSArticleReferencesAreNotTransportTargets(t *testing.T) {
raw := `<rss xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel>`
for _, u := range []string{"https://127.0.0.1/private", "http://www.investing.com/news/fixture", "https://www.investing.com.evil.invalid/news/fixture", "https://private:secret@www.investing.com/news/fixture", "https://www.bloomberg.com/news/articles/fixture"} {
raw += `<item><title>Synthetic</title><link>` + u + `</link></item>`
}
raw += `</channel></rss>`
got, err := Parse([]byte(raw), Investing)
if err != nil || len(got) != 0 {
t.Fatal("unsafe RSS association was accepted")
}
}
func TestBloombergSyntheticFixtureAndNamespacedContent(t *testing.T) {
raw, err := os.ReadFile("testdata/bloomberg.xml")
if err != nil {
t.Fatal(err)
}
got, err := Parse(raw, Bloomberg)
if err != nil || len(got) != 1 || got[0].PublishedAt == nil || scraper.ContentStatus(got[0]) != scraper.ContentComplete {
t.Fatal("Bloomberg RSS content/date contract failed")
}
// A lookalike extension cannot assert or replace trusted content:encoded.
fake := `<rss xmlns:fake="urn:fixture"><channel><item><title>Synthetic headline</title><link>https://www.investing.com/news/synthetic-fixture</link><description>Synthetic teaser.</description><fake:encoded>Untrusted extension payload</fake:encoded></item></channel></rss>`
got, err = Parse([]byte(fake), Investing)
if err != nil || len(got) != 1 || got[0].Content != "Synthetic teaser." {
t.Fatal("untrusted encoded namespace was accepted")
}
}
func TestRSSItemFieldAndHTMLLimitsFailWithoutTruncation(t *testing.T) {
item := `<item><title>Synthetic headline</title><link>https://www.investing.com/news/synthetic-fixture</link></item>`
for _, raw := range []string{
`<rss><channel>` + strings.Repeat(item, MaxItems+1) + `</channel></rss>`,
`<rss><channel><item><title>` + strings.Repeat("x", MaxFieldBytes+1) + `</title></item></channel></rss>`,
`<rss><channel><item><description><![CDATA[` + strings.Repeat("<div>", 1000) + `]]></description><title>Synthetic</title><link>https://www.investing.com/news/synthetic-fixture</link></item></channel></rss>`,
`<rss><channel><item><title>A</title><title>B</title></item></channel></rss>`,
`<rss><channel/><channel/></rss>`,
} {
if _, err := Parse([]byte(raw), Investing); err == nil || err.Error() != "rss_feed_invalid" {
t.Fatal("RSS resource limit/ambiguity was ignored or raw error escaped")
}
}
}
func TestMergeKeepsSemanticLinksDistinctAndRemovesConflictingDates(t *testing.T) {
first := time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC)
second := first.Add(time.Hour)
link := "https://www.investing.com/news/fixture?story=1"
got := Merge([][]scraper.Article{{{URL: link, PublishedAt: &first}}, {{URL: link + "&utm_source=fixture", PublishedAt: &second}}, {{URL: link, PublishedAt: &first}, {URL: "https://www.investing.com/news/fixture?story=2"}}})
if len(got) != 2 || got[0].PublishedAt != nil {
t.Fatal("semantic identities were conflated or conflicting date reinstated")
}
}
func TestTicket41ContractualEncodedBodyCanBeCompleteButMarkersCannot(t *testing.T) {
for _, source := range []string{Bloomberg, Investing} {
file := "testdata/bloomberg.xml"
if source == Investing {
file = "testdata/investing.xml"
}
raw, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
full, err := Parse(raw, source)
if err != nil || len(full) == 0 || scraper.ContentStatus(full[0]) != scraper.ContentComplete {
t.Fatal("contractual full-body field was not recognized")
}
for _, marker := range []string{`<div class="paywall"></div>`, `<div id="challengeGate"></div>`, `<div data-content-state="partial"></div>`, `<div data-content-truncated="true"></div>`, `<div class="placeholder"></div>`} {
blocked := strings.Replace(string(raw), "</article>", marker+"</article>", 1)
got, err := Parse([]byte(blocked), source)
if err != nil || len(got) == 0 || scraper.ContentStatus(got[0]) == scraper.ContentComplete {
t.Fatal("restriction/partial marker was promoted to complete")
}
}
}
}
func TestStableGUIDOnlyItemsDoNotInventLinksOrCollapseUnrelatedItems(t *testing.T) {
raw := `<rss xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel>`
for _, id := range []string{"opaque-one", "opaque-two", "opaque-one"} {
raw += `<item><guid>` + id + `</guid><title>Synthetic headline</title><content:encoded><![CDATA[<p>Synthetic complete body.</p>]]></content:encoded></item>`
}
raw += `</channel></rss>`
got, err := Parse([]byte(raw), Investing)
if err != nil || len(got) != 3 {
t.Fatal("stable GUID-only items lost")
}
merged := Merge([][]scraper.Article{got})
if len(merged) != 2 || merged[0].URL != "" || scraper.ContentStatus(merged[0]) != scraper.ContentComplete {
t.Fatal("opaque identity invented URL or collapsed distinct GUIDs")
}
}