package seo import ( "context" "fmt" "net/http" "net/http/httptest" "strings" "testing" ) func TestRobots(t *testing.T) { r := parseRobots("User-agent: *\nDisallow: /private/\nAllow: /private/ok$\n\nUser-agent: GPTBot\nUser-agent: CCBot\nDisallow: /\n\nUser-agent: ClaudeBot\nDisallow:\n\nSitemap: https://x.example/sitemap.xml\n") for _, c := range []struct { agent, path string want bool }{ {"Googlebot", "/", true}, {"Googlebot", "/private/x", false}, {"Googlebot", "/private/ok", true}, {"GPTBot", "/", false}, {"CCBot", "/blog", false}, {"ClaudeBot", "/private/x", true}, } { if got := r.allowed(c.agent, c.path); got != c.want { t.Errorf("%s %s: %v", c.agent, c.path, got) } } if len(r.sitemaps) != 1 { t.Errorf("sitemaps: %v", r.sitemaps) } } func TestPixelsAndCovers(t *testing.T) { if w := TitlePixels("Whey Protein Price Comparison: Cost per 20 g of Protein"); w < 450 || w > 600 { t.Errorf("title width: %d", w) } if TitlePixels(strings.Repeat("W", 40)) <= TitleMaxPixels { t.Error("40 W's fit in a title") } if covers("Best whey protein for bulking", "whey protein price comparison") >= 0.75 { t.Error("a bulking title covers a price search") } if covers("Nextcloud hosting: our prices", "nextcloud hosting price") < 1 { t.Error("a matching title doesn't cover its search") } } func TestAudit(t *testing.T) { var srv *httptest.Server srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { page := func(head, body string) { w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprintf(w, `
%s%s`, head, body) } long := strings.Repeat("Words people can read without JavaScript. ", 10) switch r.URL.Path { case "/robots.txt": fmt.Fprintf(w, "User-agent: *\nDisallow: /admin/\n\nUser-agent: GPTBot\nDisallow: /\n\nSitemap: %s/sitemap.xml\n", srv.URL) case "/sitemap.xml": fmt.Fprintf(w, ``+long+`
bulkxadead
`)
case "/bulking/":
page(`short
`) case "/old/": http.Redirect(w, r, "/bulking/", http.StatusMovedPermanently) case "/gone/", "/missing/", "/ext-dead": http.NotFound(w, r) case "/hidden/": page(``+long+`
`) default: http.NotFound(w, r) } })) defer srv.Close() d := openDB(t) site := "sc-domain:whey.example" // Search Console: /bulking/ is shown for a price search; /hidden/ is // shown but marked noindex and not linked. week(t, d, site, "2026-11-13", row{clicks: 10, impr: 2600, pos: 7}, []row{ {srv.URL + "/", "whey", 10, 100, 2}, {srv.URL + "/bulking/", "whey protein price comparison", 0, 2244, 7.6}, {srv.URL + "/bulking/", "bulking whey", 0, 232, 12}, {srv.URL + "/hidden/", "hidden", 0, 60, 30}, }) d.PutJSON(throughKey(site), "2026-11-19") f := NewFetcher("SupermarketTest") f.Delay = 0 a, err := RunAudit(context.Background(), d, f, srv.URL, site) if err != nil { t.Fatal(err) } pages := map[string]PageReport{} for _, p := range a.Pages { pages[strings.TrimPrefix(p.URL, srv.URL)] = p } if _, ok := pages["/admin/"]; !ok || pages["/admin/"].Error != "robots.txt blocks it" { t.Errorf("admin: %+v", pages["/admin/"]) } if p := pages["/old/"]; p.Redirects != 1 || p.Status != 200 { t.Errorf("old: %+v", p) } if p := pages["/"]; p.Lang != "en" || !p.OG || len(p.JSONLD) != 1 || p.ImagesNoAlt != 1 || p.TextChars < 200 { t.Errorf("home: %+v", p) } issues := map[string]Issue{} for _, is := range a.Issues { issues[strings.TrimPrefix(is.Page, srv.URL)+" "+is.Code] = is } for _, want := range []string{ "/bulking/ title-query", "/bulking/ h1-many", "/bulking/ heading-order", "/bulking/ description-missing", "/bulking/ thin", "/old/ redirect", "/gone/ status", "/missing/ status", "/hidden/ noindex", "/hidden/ not-in-sitemap", "/ broken-links", "/ alt-missing", } { if _, ok := issues[want]; !ok { t.Errorf("missing issue %q", want) } } tq := issues["/bulking/ title-query"] if tq.Severity != SevWarning || !strings.Contains(tq.Detail, "91% of its impressions come from “whey protein price comparison”") || tq.Traffic.Impressions != 2476 { t.Errorf("title-query: %+v", tq) } if b := issues["/ broken-links"]; !strings.Contains(b.Detail, "/missing/ (404)") || !strings.Contains(b.Detail, "/ext-dead") { t.Errorf("broken links: %+v", b) } if issues["/hidden/ noindex"].Severity != SevError { t.Error("noindex on a page Google shows isn't an error") } // Ranked by severity, then traffic: the first warning is on the page with // the most clicks or impressions. if a.Issues[0].Severity != SevError { t.Errorf("first issue: %+v", a.Issues[0]) } checks := map[string]SiteCheck{} for _, c := range a.Checks { checks[c.Title] = c } if c, ok := checks["robots.txt blocks some AI crawlers"]; !ok || !strings.Contains(c.Detail, "GPTBot") || strings.Contains(c.Detail, "ClaudeBot") { t.Errorf("AI crawlers: %+v", a.Checks) } if c := checks["No llms.txt"]; c.OK { t.Error("llms.txt found") } if !checks["The site says who you are"].OK || !checks["robots.txt names the sitemap"].OK { t.Errorf("checks: %+v", a.Checks) } id, _ := StartAudit(d, srv.URL+"/", now) if err := FinishAudit(d, id, a, nil, now); err != nil { t.Fatal(err) } got, err := LatestAudit(d, srv.URL+"/") if err != nil || got.Status != "done" || len(got.Issues) != len(a.Issues) || LastDoneAudit(d, srv.URL+"/").IsZero() { t.Errorf("stored: %+v %v", got, err) } }