Phase 3 part 3: the audit, ranked by traffic, with AI visibility
A polite crawl of each website (Search Console's busiest pages, the sitemap, then links; up to 300 pages; robots.txt honoured) checks titles and descriptions (characters and pixels), headings, canonical, noindex, status and redirects, broken links (outside ones twice), alt text, Open Graph, JSON-LD, text without JavaScript, and whether the title says what the page's main search asks. Issues are ranked by severity, then the page's clicks and impressions. AI visibility: AI crawlers in robots.txt, llms.txt, structured data, Organization data. Optional PageSpeed checks. Weekly in the background, or Audit now. Schema 14. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
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, `<!doctype html><html lang="en"><head>%s</head><body>%s</body></html>`, 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, `<urlset><url><loc>%[1]s/</loc></url><url><loc>%[1]s/bulking/</loc></url><url><loc>%[1]s/old/</loc></url><url><loc>%[1]s/gone/</loc></url></urlset>`, srv.URL)
|
||||
case "/":
|
||||
page(`<title>Whey reviews and price comparisons, by people who buy it</title><meta name="description" content="We compare whey protein prices per 20 g every month, with the method and the date, so you can buy the best value tub.">
|
||||
<link rel="canonical" href="`+srv.URL+`/"><meta property="og:title" content="x"><script type="application/ld+json">{"@context":"https://schema.org","@type":"Organization","name":"W"}</script>`,
|
||||
`<h1>Whey</h1><h2>Prices</h2><p>`+long+`</p><a href="/bulking/">bulk</a><a href="/missing/">x</a><a href="/admin/">a</a><a href="`+srv.URL+`/ext-dead">dead</a><img src="a.png">`)
|
||||
case "/bulking/":
|
||||
page(`<title>Best whey protein for bulking</title>`, `<h1>Bulking</h1><h1>Again</h1><h2>a</h2><h4>b</h4><p>short</p>`)
|
||||
case "/old/":
|
||||
http.Redirect(w, r, "/bulking/", http.StatusMovedPermanently)
|
||||
case "/gone/", "/missing/", "/ext-dead":
|
||||
http.NotFound(w, r)
|
||||
case "/hidden/":
|
||||
page(`<title>Hidden page with a perfectly fine title here</title><meta name="robots" content="noindex">`, `<h1>Hidden</h1><p>`+long+`</p>`)
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user