fix(scrapers): add sanitized collection diagnostics

Report source, HTTP status, article counts, and outcome without logging response bodies; preserve the existing extraction and parsing flow.

Refs #20

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 15:08:35 +01:00
parent d65222169a
commit 4131cf903e
4 changed files with 224 additions and 17 deletions

View File

@ -0,0 +1,78 @@
package watcherguru
import (
"context"
"io"
"net/http"
"os"
"strings"
"testing"
)
type ticket20RoundTrip func(*http.Request) (*http.Response, error)
func (f ticket20RoundTrip) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func ticket20CaptureStdout(t *testing.T, run func()) string {
t.Helper()
reader, writer, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
previous := os.Stdout
os.Stdout = writer
defer func() { os.Stdout = previous }()
run()
if err := writer.Close(); err != nil {
t.Fatal(err)
}
data, err := io.ReadAll(reader)
_ = reader.Close()
if err != nil {
t.Fatal(err)
}
return string(data)
}
func TestTicket20WatcherGuruDiagnosticsReportBothRSSAttemptsAndEmptyHTMLWithoutContent(t *testing.T) {
const rssPrivate = "PRIVATE_WATCHERGURU_RSS_RESPONSE_DO_NOT_LOG"
const htmlPrivate = "PRIVATE_WATCHERGURU_HTML_RESPONSE_DO_NOT_LOG"
var paths []string
watcher := &WatcherGuru{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
paths = append(paths, r.URL.Path)
switch r.URL.Path {
case "/feed/":
return &http.Response{StatusCode: http.StatusBadGateway, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`<html>` + rssPrivate + `</html>`)), Request: r}, nil
case "/news/feed/":
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`<?xml version="1.0"?><rss><channel><!-- ` + rssPrivate + ` --></channel></rss>`)), Request: r}, nil
case "/news/":
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`<html><body><!-- ` + htmlPrivate + ` --><p>no candidate article</p></body></html>`)), Request: r}, nil
default:
return nil, io.ErrUnexpectedEOF
}
})}}
var count int
logs := ticket20CaptureStdout(t, func() {
articles, err := watcher.Scrape(context.Background(), nil)
if err != nil {
t.Fatalf("Scrape returned error: %v", err)
}
count = len(articles)
})
if strings.Join(paths, ",") != "/feed/,/news/feed/,/news/" {
t.Fatalf("fake request path sequence=%v", paths)
}
for _, diagnostic := range []string{"rss feed=/feed/ status=502", "rss feed=/news/feed/ status=200 articles=0", "html status=200 articles=0"} {
if !strings.Contains(logs, diagnostic) {
t.Errorf("WatcherGuru diagnostic lacks %q: %q", diagnostic, logs)
}
}
if count != 0 {
t.Errorf("articles=%d, want 0", count)
}
for _, secret := range []string{rssPrivate, htmlPrivate, "no candidate article"} {
if strings.Contains(logs, secret) {
t.Errorf("WatcherGuru diagnostics leaked response/article content %q", secret)
}
}
}