package watcherguru
import (
"context"
"io"
"net/http"
"reflect"
"strings"
"testing"
)
func TestTicket21UsesAuthorizedHostAndPreservesFallbackPaths(t *testing.T) {
var paths []string
watcher := &WatcherGuru{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
if r.URL.Scheme != "https" || r.URL.Host != "watcherguru.com" {
t.Errorf("request origin=%s://%s; want https://watcherguru.com", r.URL.Scheme, r.URL.Host)
}
if r.Method != http.MethodGet {
t.Errorf("method=%s; want GET", r.Method)
}
paths = append(paths, r.URL.Path)
body := ""
if r.URL.Path == "/news/" {
body = `
Synthetic market headline for host contract`
}
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil
})}}
articles, err := watcher.Scrape(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(paths, []string{"/feed/", "/news/feed/", "/news/"}) {
t.Fatalf("paths=%v", paths)
}
if len(articles) != 1 || articles[0].URL != "https://watcherguru.com/news/synthetic-market" {
t.Fatalf("relative link resolution changed: %+v", articles)
}
}
func TestTicket21RSSSuccessStillStopsBeforeFallback(t *testing.T) {
calls := 0
watcher := &WatcherGuru{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
calls++
if r.URL.String() != "https://watcherguru.com/feed/" {
t.Errorf("request=%s", r.URL)
}
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`- Synthetic RSS headlinehttps://fixture.invalid/articleSynthetic evidence
`)), Request: r}, nil
})}}
articles, err := watcher.Scrape(context.Background(), nil)
if err != nil || calls != 1 || len(articles) != 1 {
t.Fatalf("calls=%d articles=%d err=%v", calls, len(articles), err)
}
if articles[0].Title != "Synthetic RSS headline" || articles[0].Content != "Synthetic evidence" || articles[0].URL != "https://fixture.invalid/article" {
t.Fatalf("RSS contract changed: %+v", articles[0])
}
}
func TestTicket21ExtractsAbsoluteNewDomainLinkWithoutNewsPath(t *testing.T) {
const articleURL = "https://watcherguru.com/synthetic-market-update"
const title = "Synthetic absolute headline for new domain"
var paths []string
watcher := &WatcherGuru{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
if r.URL.Scheme != "https" || r.URL.Host != "watcherguru.com" {
t.Fatalf("unexpected request origin: %s", r.URL)
}
paths = append(paths, r.URL.Path)
body := ""
if r.URL.Path == "/news/" {
body = `` + title + ``
}
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil
})}}
articles, err := watcher.Scrape(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(paths, []string{"/feed/", "/news/feed/", "/news/"}) {
t.Fatalf("paths=%v", paths)
}
if len(articles) != 1 {
t.Fatalf("articles=%d; want absolute new-domain link without /news/", len(articles))
}
if articles[0].URL != articleURL || articles[0].Title != title || articles[0].Content != title {
t.Fatalf("unexpected article: %+v", articles[0])
}
}