package web
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"supermarket/internal/profile"
"supermarket/internal/seo"
)
func TestAuditPage(t *testing.T) {
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `
HomeHi
x`)
case "/sitemap.xml":
fmt.Fprintf(w, `%s/`, srv.URL)
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: srv.URL}}})
if body := do(h, http.MethodGet, "/seo/audit", nil, nil).Body.String(); !strings.Contains(body, "Not audited yet") {
t.Fatal("no empty state")
}
if rec := postForm(h, "/seo/audit?site=0", nil); rec.Code != http.StatusSeeOther {
t.Fatalf("start: %d %s", rec.Code, rec.Body)
}
deadline := time.Now().Add(10 * time.Second)
for {
a, _ := seo.LatestAudit(s.DB, srv.URL)
if a != nil && a.Status != "running" {
if a.Status != "done" {
t.Fatalf("audit: %+v", a)
}
break
}
if time.Now().After(deadline) {
t.Fatal("the audit didn't finish")
}
time.Sleep(50 * time.Millisecond)
}
if rec := do(h, http.MethodGet, "/seo/audit/status?site=0", nil, nil); !strings.HasPrefix(rec.Header().Get("HX-Redirect"), "/seo/audit?site=0") {
t.Error("the status poll doesn't reload when done")
}
body := do(h, http.MethodGet, "/seo/audit", nil, nil).Body.String()
for _, want := range []string{"AI visibility", "No llms.txt", "Answers 404", "/nope/", "Title very short", "pages read"} {
if !strings.Contains(body, want) {
t.Errorf("audit page lacks %q", want)
}
}
if body := do(h, http.MethodGet, "/seo/audit?severity=error", nil, nil).Body.String(); strings.Contains(body, "Title very short") {
t.Error("the severity filter shows notices")
}
}