66 lines
3.0 KiB
Go
66 lines
3.0 KiB
Go
package bloomberg
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestOnlyAggregatedPublisherMetricsCrossTheEnrichmentRPC(t *testing.T) {
|
|
for _, supported := range []bool{true, false} {
|
|
t.Run(map[bool]string{true: "measured", false: "old_service_unsupported"}[supported], func(t *testing.T) {
|
|
remoteCtx, remote := scraper.WithFetchMetrics(context.Background())
|
|
scraper.EnableFetchMetrics(remoteCtx)
|
|
scraper.RecordFetch(remoteCtx, scraper.FetchObservation{HTTPStatus: 403, State: scraper.ContentUnavailable})
|
|
aggregate := remote.Finish()
|
|
b := New("http://fixture.invalid")
|
|
b.client = &http.Client{Transport: dateTransport(func(r *http.Request) (*http.Response, error) {
|
|
var req struct {
|
|
Measure bool `json:"measure_fetches"`
|
|
}
|
|
if json.NewDecoder(r.Body).Decode(&req) != nil || !req.Measure {
|
|
t.Fatal("bench did not opt in")
|
|
}
|
|
payload := map[string]any{"articles": []map[string]any{{"url": "https://www.bloomberg.com/news/articles/fixture", "content": "", "content_state": "unavailable"}}}
|
|
if supported {
|
|
payload["fetch_metrics"] = aggregate
|
|
}
|
|
data, _ := json.Marshal(payload)
|
|
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(string(data))), Header: make(http.Header)}, nil
|
|
})}
|
|
ctx, m := scraper.WithFetchMetrics(context.Background())
|
|
_, err := b.EnrichWithCredentials(ctx, "synthetic-fixture", "synthetic-fixture", []scraper.Article{{URL: "https://www.bloomberg.com/news/articles/fixture"}})
|
|
if err != nil {
|
|
t.Fatal("fixture RPC failed")
|
|
}
|
|
c := m.Finish()
|
|
if c.Supported != supported {
|
|
t.Fatal("missing metrics treated as measured zero")
|
|
}
|
|
if supported && (c.HTTP.FourXX != 1 || c.HTTP.TwoXX != 0 || c.Attempted != 1) {
|
|
t.Fatal("RPC HTTP 200 confused with publisher 403")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoginOutcomeAggregateRemainsNotFetched(t *testing.T) {
|
|
for _, login := range []string{"unconfirmed", "refused"} {
|
|
b := NewWithClient("http://fixture.invalid", &http.Client{Transport: dateTransport(func(r *http.Request) (*http.Response, error) {
|
|
payload := map[string]any{"fetch_metrics": scraper.FetchMetrics{SchemaVersion: 1, Supported: true, Login: login}, "articles": []map[string]any{{"url": "https://www.bloomberg.com/news/articles/fixture", "content": "", "content_state": "unavailable"}}}
|
|
raw, _ := json.Marshal(payload)
|
|
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(string(raw)))}, nil
|
|
})}, io.Discard)
|
|
ctx, m := scraper.WithFetchMetrics(context.Background())
|
|
got, err := b.EnrichWithCredentials(ctx, "offline-fixture", "offline-fixture", []scraper.Article{{URL: "https://www.bloomberg.com/news/articles/fixture"}})
|
|
c := m.Finish()
|
|
if err != nil || len(got) != 1 || got[0].ContentState != scraper.ContentUnavailable || c.Login != login || c.Attempted != 0 || c.HTTP.Network != 0 || c.HTTP.FourXX != 0 {
|
|
t.Fatal("login gate invented article fetches or lost category")
|
|
}
|
|
}
|
|
}
|