65 lines
2.7 KiB
Go
65 lines
2.7 KiB
Go
package articlecontent
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
)
|
|
|
|
func TestCandidateBodyLengthIsCountedBeforeIdentityRejection(t *testing.T) {
|
|
lengths := []int{0, 1, 500, 501, 2000, 2001}
|
|
pages := make(map[string]string, len(lengths))
|
|
articles := make([]scraper.Article, 0, len(lengths))
|
|
rawValues := []string{"candidate_title_canary", "placeholder_text_canary"}
|
|
for i, n := range lengths {
|
|
listed := "https://finance.yahoo.com/news/candidate-listed-" + string(rune('a'+i)) + ".html"
|
|
canonical := "https://finance.yahoo.com/news/candidate-canonical-mismatch-" + string(rune('a'+i)) + ".html"
|
|
og := "https://finance.yahoo.com/news/candidate-og-mismatch-" + string(rune('a'+i)) + ".html"
|
|
jsonld := "https://finance.yahoo.com/news/candidate-jsonld-mismatch-" + string(rune('a'+i)) + ".html"
|
|
candidate := strings.Repeat("x", n)
|
|
rawValues = append(rawValues, listed, canonical, og, jsonld)
|
|
if len(candidate) >= 10 {
|
|
rawValues = append(rawValues, candidate)
|
|
}
|
|
pages[listed] = `<link rel="canonical" href="` + canonical + `"><meta property="og:url" content="` + og + `"><script type="application/ld+json">{"@type":"NewsArticle","url":"` + jsonld + `"}</script><div class="caas-body"><p>` + candidate + `</p><div class="article-placeholder">placeholder_text_canary</div></div>`
|
|
articles = append(articles, scraper.Article{URL: listed, Title: "candidate_title_canary"})
|
|
}
|
|
ctx, meter := scraper.WithFetchMetrics(context.Background())
|
|
client := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) {
|
|
body, ok := pages[r.URL.String()]
|
|
if !ok {
|
|
return nil, io.ErrUnexpectedEOF
|
|
}
|
|
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil
|
|
})}
|
|
got, err := Enrich(ctx, client, "yahoo_finance", articles)
|
|
metrics := meter.Finish()
|
|
if err != nil || len(got) != len(lengths) {
|
|
t.Fatal("fixture enrichment failed")
|
|
}
|
|
for i, article := range got {
|
|
if article.Content != "" || scraper.ContentStatus(article) != scraper.ContentUnavailable {
|
|
t.Fatalf("identity-rejected fixture %d unexpectedly retained extracted content", i)
|
|
}
|
|
}
|
|
if metrics.Identity.Mismatch != len(lengths) || metrics.Length.Zero != len(lengths) || metrics.Content.Unavailable != len(lengths) {
|
|
t.Fatal("identity rejection changed the unavailable/empty extracted-content counters")
|
|
}
|
|
data := requireAnonymousAggregate(t, metrics, rawValues...)
|
|
for _, tc := range []struct {
|
|
bucket string
|
|
want int
|
|
}{
|
|
{"0", 1},
|
|
{"1-500", 2},
|
|
{"501-2000", 2},
|
|
{">2000", 1},
|
|
} {
|
|
requireAggregateCounter(t, data, []string{"candidate_body_characters", tc.bucket}, tc.want)
|
|
}
|
|
}
|