feat: crawler checks for status, sitemaps, canonical, titles and headings
- page_status, sitemap_error, sitemap_noindex, sitemap_blocked and sitemap_missing against the fetched sitemap files and robots.txt - canonical_missing and canonical_error, missing and duplicate titles and descriptions over the crawl - h1_missing, h1_multiple, heading_skipped, heading_empty and h1_title_mismatch; content headings route to the record's tool - the staging lock's noindex is never counted (Pitfall 12) - TestCrawlerChecks on the planted site with a clean control page
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"html"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -415,3 +416,168 @@ func findIssue(t *testing.T, list []apiIssue, kind, url string) apiIssue {
|
||||
}
|
||||
return found[0]
|
||||
}
|
||||
|
||||
// plantedPage is one page of the audit site: the document (or a raw
|
||||
// answer), whether a sitemap lists it, whether the home page links to it
|
||||
// and the issues it plants ("kind" each, at its own address).
|
||||
type plantedPage struct {
|
||||
doc doc
|
||||
status int
|
||||
header []string
|
||||
raw string
|
||||
listed bool
|
||||
linked bool
|
||||
kinds []string
|
||||
}
|
||||
|
||||
// auditPages is the planted audit site: one page per kind of UI-SPEC C23
|
||||
// and the clean control page /clean.
|
||||
func auditPages() []plantedPage {
|
||||
jsonld := func(v string) string { return `<script type="application/ld+json">` + v + `</script>` }
|
||||
alt := func(lang, href string) string {
|
||||
return `<link rel="alternate" hreflang="` + lang + `" href="` + siteURL + href + `">`
|
||||
}
|
||||
page := func(d doc, listed, linked bool, kinds ...string) plantedPage {
|
||||
return plantedPage{doc: d, listed: listed, linked: linked, kinds: kinds}
|
||||
}
|
||||
return []plantedPage{
|
||||
page(doc{path: "/clean", head: jsonld(`{"@context":"https://schema.org","@graph":[{"@type":"FAQPage","@id":"#faq"}]}`),
|
||||
body: `<p>Plain text of the control page.</p><img src="/deco.png" alt="" width="10" height="10"><img src="/photo.jpg" alt="A photo" width="10" height="10">`}, true, true),
|
||||
// Statuses and sitemaps.
|
||||
{doc: doc{path: "/p/status-500"}, status: 500, linked: true, kinds: []string{"page_status"}},
|
||||
{doc: doc{path: "/p/sitemap-404"}, status: 404, listed: true, kinds: []string{"sitemap_error"}},
|
||||
page(doc{path: "/p/noindex", robots: "noindex, follow"}, true, true, "sitemap_noindex"),
|
||||
page(doc{path: "/p/blocked"}, true, true, "sitemap_blocked"),
|
||||
page(doc{path: "/p/unlisted"}, false, true, "sitemap_missing"),
|
||||
// Head.
|
||||
page(doc{path: "/p/no-canonical", noCanonical: true}, true, true, "canonical_missing"),
|
||||
page(doc{path: "/p/bad-canonical", canonical: siteURL + "/p/canonical-404"}, true, true, "canonical_error"),
|
||||
page(doc{path: "/p/no-title", noTitle: true, h1: []string{"No title page"}}, true, true, "missing_title"),
|
||||
page(doc{path: "/p/dup-title-a", title: "Same title"}, true, true, "duplicate_title"),
|
||||
page(doc{path: "/p/dup-title-b", title: "Same title"}, true, true, "duplicate_title"),
|
||||
page(doc{path: "/p/no-desc", noDesc: true}, true, true, "missing_description"),
|
||||
page(doc{path: "/p/dup-desc-a", description: "Same description"}, true, true, "duplicate_description"),
|
||||
page(doc{path: "/p/dup-desc-b", description: "Same description"}, true, true, "duplicate_description"),
|
||||
// Headings.
|
||||
page(doc{path: "/p/no-h1", h1: []string{}, body: "<h2>Only a subheading</h2>"}, true, true, "h1_missing"),
|
||||
page(doc{path: "/p/two-h1", h1: []string{"Page /p/two-h1", "Second"}}, true, true, "h1_multiple"),
|
||||
page(doc{path: "/p/skip", body: "<h3>Deep</h3>"}, true, true, "heading_skipped"),
|
||||
page(doc{path: "/p/empty-heading", body: "<h2></h2><p>Text</p>"}, true, true, "heading_empty"),
|
||||
page(doc{path: "/p/mismatch", title: "Alpha page", h1: []string{"Completely different"}}, true, true, "h1_title_mismatch"),
|
||||
// JSON-LD.
|
||||
page(doc{path: "/p/jsonld-bad", head: jsonld(`{"@context": "https://schema.org", bad`)}, true, true, "jsonld_invalid"),
|
||||
page(doc{path: "/p/jsonld-two", head: jsonld(`{"@context":"https://schema.org","@type":"WebPage","name":"A"}`) + jsonld(`{"@context":"https://schema.org","@type":"WebPage","name":"B"}`)}, true, true, "jsonld_multiple"),
|
||||
page(doc{path: "/p/jsonld-req", head: jsonld(`{"@context":"https://schema.org","@graph":[{"@type":"BreadcrumbList","@id":"#crumbs"}]}`)}, true, true, "jsonld_required"),
|
||||
page(doc{path: "/p/jsonld-rec", head: jsonld(`{"@context":"https://schema.org","@type":"Organization","name":"Acme","url":"https://www.example.test/"}`)}, true, true, "jsonld_recommended"),
|
||||
// hreflang.
|
||||
page(doc{path: "/en/x", head: alt("en", "/en/x") + alt("pl", "/pl/x")}, true, true, "hreflang_reciprocal"),
|
||||
page(doc{path: "/pl/x", lang: "pl", head: alt("pl", "/pl/x")}, true, true),
|
||||
page(doc{path: "/p/hreflang-err", head: alt("en", "/p/hreflang-err") + alt("de", "/de/missing")}, true, true, "hreflang_error"),
|
||||
// Links.
|
||||
page(doc{path: "/p/generic", body: `<p><a href="/clean">kliknij tutaj</a> and <a href="/clean">Click here</a></p>`}, true, true, "generic_anchor"),
|
||||
{doc: doc{path: "/p/gone"}, status: 404, linked: true, kinds: []string{"broken_link"}},
|
||||
{doc: doc{path: "/p/moved"}, status: 301, header: []string{"Location", "/clean"}, linked: true, kinds: []string{"redirected_link"}},
|
||||
// Images.
|
||||
page(doc{path: "/p/img-noalt", body: `<img src="/a.jpg" width="10" height="10">`}, true, true, "image_alt"),
|
||||
page(doc{path: "/p/img-nosize", body: `<img src="/b.jpg" alt="B">`}, true, true, "image_size"),
|
||||
page(doc{path: "/p/lazy", head: `<meta property="og:image" content="` + siteURL + `/hero.jpg">`, body: `<img src="/hero.jpg" alt="Hero" width="10" height="10" loading="lazy">`}, true, true, "featured_lazy"),
|
||||
// Raw HTML against the CMS records (content_js, check cms).
|
||||
page(doc{path: "/c/js-h1", title: "Template heading"}, true, true, "content_js"),
|
||||
page(doc{path: "/c/js-words", title: "Words page", body: "<p>Just a few words here.</p>"}, true, true, "content_js"),
|
||||
page(doc{path: "/c/js-links", title: "Linked post", links: []string{"/clean"}}, true, true, "content_js"),
|
||||
page(doc{path: "/c/parent", title: "Parent post"}, true, true),
|
||||
// Raw against rendered (content_js, check rendered).
|
||||
page(doc{path: "/p/render", body: "<p>Short raw text.</p>"}, true, true, "content_js"),
|
||||
page(doc{path: "/p/render-h1", h1: []string{}}, true, true, "h1_missing", "content_js"),
|
||||
// Orphans.
|
||||
page(doc{path: "/p/orphan", title: "Orphan topic"}, true, false, "orphan_page"),
|
||||
page(doc{path: "/p/mentions", body: "<p>We wrote about the orphan topic before.</p>"}, true, true),
|
||||
}
|
||||
}
|
||||
|
||||
// auditRecords are the CMS records behind the /c/ pages.
|
||||
func auditRecords() map[string][]crawlRecord {
|
||||
return map[string][]crawlRecord{crawlPostType: {
|
||||
{id: "1", path: "/c/js-h1", title: "Record heading"},
|
||||
{id: "2", path: "/c/js-words", title: "Words page", words: 400},
|
||||
{id: "3", path: "/c/js-links", title: "Linked post", parents: []pact.ContentRef{{Type: crawlPostType, ID: "4"}}},
|
||||
{id: "4", path: "/c/parent", title: "Parent post"},
|
||||
}}
|
||||
}
|
||||
|
||||
// plantAudit serves the audit site and returns the expected crawl issues
|
||||
// ("kind url", sorted).
|
||||
func plantAudit(site *fakeSite) []string {
|
||||
var listed, linked, want []string
|
||||
listed = append(listed, "/")
|
||||
for _, p := range auditPages() {
|
||||
switch {
|
||||
case p.raw != "":
|
||||
site.set(p.doc.path, p.status, p.raw, p.header...)
|
||||
case p.status != 0:
|
||||
site.set(p.doc.path, p.status, p.doc.render(), p.header...)
|
||||
default:
|
||||
site.plant(p.doc)
|
||||
}
|
||||
if p.listed {
|
||||
listed = append(listed, p.doc.path)
|
||||
}
|
||||
if p.linked {
|
||||
linked = append(linked, p.doc.path)
|
||||
}
|
||||
for _, k := range p.kinds {
|
||||
want = append(want, k+" "+p.doc.path)
|
||||
}
|
||||
}
|
||||
home := doc{path: "/", title: "Home", links: linked, body: `<p><a href="https://evil.example.com/x">Partner</a> <a href="http://169.254.169.254/latest">Metadata</a> <a href="http://www.example.test/plain">Plain http</a></p>`}
|
||||
site.plant(home)
|
||||
site.sitemap(listed...)
|
||||
site.set("/robots.txt", http.StatusOK, "User-agent: *\nDisallow: /p/blocked\n", "Content-Type", "text/plain")
|
||||
sort.Strings(want)
|
||||
return want
|
||||
}
|
||||
|
||||
// fakeRenderer is the test renderer of the comparison: the DOM planted
|
||||
// for a path, else the raw page (an identical rendering).
|
||||
type fakeRenderer struct {
|
||||
site *fakeSite
|
||||
dom map[string]string
|
||||
fail error
|
||||
mu sync.Mutex
|
||||
calls int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (r *fakeRenderer) Render(_ context.Context, pageURL string) (string, error) {
|
||||
r.mu.Lock()
|
||||
r.calls++
|
||||
r.mu.Unlock()
|
||||
if r.fail != nil {
|
||||
return "", r.fail
|
||||
}
|
||||
u, err := url.Parse(pageURL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if d, ok := r.dom[u.Path]; ok {
|
||||
return d, nil
|
||||
}
|
||||
r.site.mu.Lock()
|
||||
defer r.site.mu.Unlock()
|
||||
return r.site.pages[u.Path].body, nil
|
||||
}
|
||||
|
||||
func (r *fakeRenderer) Close() error {
|
||||
r.closed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// auditRenderer renders /p/render with a link and 80 more words, and
|
||||
// /p/render-h1 with an H1.
|
||||
func auditRenderer(site *fakeSite) *fakeRenderer {
|
||||
words := strings.Repeat("rendered word ", 40)
|
||||
return &fakeRenderer{site: site, dom: map[string]string{
|
||||
"/p/render": doc{path: "/p/render", body: `<p>Short raw text.</p><p>` + words + `</p><a href="/p/js-only">Only after JavaScript</a>`}.render(),
|
||||
"/p/render-h1": doc{path: "/p/render-h1"}.render(),
|
||||
}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user