package articlecontent
import (
"context"
"errors"
"github.com/tradarr/backend/internal/scraper"
"io"
"net/http"
"strings"
"testing"
)
type unreadableMetricBody struct{ t *testing.T }
func (b unreadableMetricBody) Read([]byte) (int, error) {
b.t.Fatal("non-200 body read for diagnostics")
return 0, io.EOF
}
func (unreadableMetricBody) Close() error { return nil }
func TestArticleFetchMetricsDescribeHTTPAndParserWithoutChangingClassification(t *testing.T) {
const target = "https://finance.yahoo.com/news/metrics.html"
base := `
Synthetic retained content.
`
for _, tc := range []struct {
name string
status int
html string
network bool
root scraper.EvidenceFlag
identity scraper.IdentityFlag
blocking scraper.EvidenceFlag
state string
}{
{"recognized", 200, base, false, scraper.ObservedYes, scraper.IdentityYes, scraper.ObservedNo, scraper.ContentComplete},
{"unknown_root", 200, strings.Replace(base, "caas-body", "unrecognized-root", 1), false, scraper.ObservedNo, scraper.IdentityYes, scraper.ObservedNo, scraper.ContentUnavailable},
{"unknown_identity", 200, `Synthetic retained content.
`, false, scraper.ObservedYes, scraper.IdentityNo, scraper.ObservedNo, scraper.ContentIncomplete},
{"mismatch", 200, strings.Replace(base, target, target+"-other", 1), false, scraper.ObservedYes, scraper.IdentityMismatch, scraper.ObservedNo, scraper.ContentUnavailable},
{"challenge", 200, `` + strings.Repeat("漢", n) + `
`
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: r}, nil
})}
Enrich(ctx, client, "yahoo_finance", []scraper.Article{{URL: "https://finance.yahoo.com/news/length.html"}})
c := m.Finish()
if n == 0 && c.Length.Zero != 1 || n > 0 && n <= 500 && c.Length.Small != 1 || n > 500 && n <= 2000 && c.Length.Medium != 1 || n > 2000 && c.Length.Large != 1 {
t.Fatal("character bucket incorrect")
}
}
}