Files
supermarket/internal/seo/engines_test.go
davidandClaude Opus 5.5 2302fb0436 Phase 3 part 7: Bing, IndexNow and search engine setup
SEO → Search engines: a pull request that adds the IndexNow key file, a
robots.txt naming the sitemap, and turns the sitemap on when the Hugo
config disables it (checked with a build); IndexNow submissions of pages
that go live through Changes or Briefs once the key file is served; the
sitemaps Search Console has; and, with an API key, Bing's traffic,
searches and crawl issues. NewlyLive no longer queries while a result
set holds the database's only connection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 08:45:13 -07:00

114 lines
3.9 KiB
Go

package seo
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"supermarket/internal/api/gitea"
"supermarket/internal/hugorepo"
"supermarket/internal/hugorepo/hugotest"
)
func TestIndexNow(t *testing.T) {
d := openDB(t)
key, err := IndexNowKey(d)
if err != nil || len(key) != 32 {
t.Fatalf("key: %q %v", key, err)
}
if again, _ := IndexNowKey(d); again != key {
t.Error("the key changed")
}
var got map[string]any
in := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewDecoder(r.Body).Decode(&got)
w.WriteHeader(http.StatusAccepted)
}))
defer in.Close()
IndexNowURL = in.URL
defer func() { IndexNowURL = "https://api.indexnow.org/indexnow" }()
site := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/"+key+".txt" {
fmt.Fprintln(w, key)
return
}
http.NotFound(w, r)
}))
defer site.Close()
f := NewFetcher("test")
if !KeyLive(context.Background(), f, site.URL, key) || KeyLive(context.Background(), f, site.URL, "other") {
t.Error("key check")
}
// A change went live: it's waiting to be submitted, then it isn't.
now := time.Now()
c := &Change{Site: site.URL + "/", Page: site.URL + "/pricing/", NewTitle: "x", CreatedAt: now}
insertChange(d, c)
d.Exec(`UPDATE seo_changes SET status = 'live', live_at = ? WHERE id = ?`, now.UTC().Format(time.RFC3339), c.ID)
pages, _ := NewlyLive(d, site.URL+"/", now)
if len(pages) != 1 {
t.Fatalf("newly live: %v", pages)
}
p, err := SubmitIndexNow(context.Background(), d, site.URL+"/", key, pages, now)
if err != nil || p.Status != 202 {
t.Fatalf("submit: %+v %v", p, err)
}
if got["key"] != key || got["keyLocation"] != site.URL+"/"+key+".txt" || !strings.HasPrefix(site.URL, "http://"+got["host"].(string)) {
t.Errorf("request: %+v", got)
}
if pages, _ := NewlyLive(d, site.URL+"/", now); len(pages) != 0 {
t.Errorf("submitted twice: %v", pages)
}
if len(Pings(d, site.URL+"/")) != 1 {
t.Error("the ping isn't kept")
}
}
func TestSetupSite(t *testing.T) {
if hugorepo.HugoVersion(context.Background()) == "" {
t.Skip("no hugo")
}
d := openDB(t)
clone := hugotest.Site(t)
var body map[string]string
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/api/v1/repos/o/site":
json.NewEncoder(w).Encode(map[string]any{"full_name": "o/site", "default_branch": "main", "clone_url": clone, "permissions": map[string]any{"push": true}})
case r.URL.Path == "/api/v1/repos/o/site/pulls":
json.NewDecoder(r.Body).Decode(&body)
json.NewEncoder(w).Encode(map[string]any{"number": 3, "html_url": "https://gitea.example/pr/3"})
}
}))
defer g.Close()
ap := &Applier{Gitea: gitea.New(g.URL, "tok"), Repo: "o/site", ReposDir: filepath.Join(t.TempDir(), "sites"), Author: "P <p@example.com>"}
st, err := ap.SetupSite(context.Background(), d, "https://site.example", "abc123", time.Now())
if err != nil {
t.Fatal(err)
}
if st.PRNumber != 3 || len(st.Done) != 3 || !strings.HasPrefix(st.Note, "Checked with a build") {
t.Fatalf("setup: %+v", st)
}
if !strings.Contains(body["body"], "Turns the sitemap back on") || !strings.Contains(body["body"], "https://site.example/sitemap.xml") {
t.Errorf("pull request: %s", body["body"])
}
repo := strings.TrimPrefix(clone, "file://")
for file, want := range map[string]string{
"static/abc123.txt": "abc123", "static/robots.txt": "Sitemap: https://site.example/sitemap.xml", "hugo.toml": "disableKinds = ['taxonomy', 'term', 'RSS']",
} {
out, err := exec.Command("git", "-C", repo, "show", "supermarket/search-engines:"+file).CombinedOutput()
if err != nil || !strings.Contains(string(out), want) {
t.Errorf("%s: %s %v", file, out, err)
}
}
if LastSetup(d, "https://site.example") == nil {
t.Error("the setup isn't kept")
}
}