Candidates a site has no page for, from Search Console searches it ranks weakly for, the profile's keywords and customers' problems, the signals' search lines, and competitors' sitemaps; each checked against our own pages. Buyer searches first. On request, Claude's web search reads the live top 3 and types each result (big brand, marketplace, forum, small site), and keywords are ranked by who has to be beaten. Pick, dismiss or bring back. Schema 16. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
4.9 KiB
Go
135 lines
4.9 KiB
Go
package seo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"supermarket/internal/ai"
|
|
"supermarket/internal/inventory"
|
|
"supermarket/internal/profile"
|
|
"supermarket/internal/signals"
|
|
)
|
|
|
|
type fakeResearcher struct {
|
|
text string
|
|
sources []ai.Source
|
|
got ai.ResearchRequest
|
|
}
|
|
|
|
func (f *fakeResearcher) Research(_ context.Context, req ai.ResearchRequest) (*ai.ResearchResult, error) {
|
|
f.got = req
|
|
return &ai.ResearchResult{Text: f.text, Sources: f.sources, Searches: 2}, nil
|
|
}
|
|
|
|
func TestFindKeywords(t *testing.T) {
|
|
d := openDB(t)
|
|
var ours, rival *httptest.Server
|
|
ours = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/sitemap.xml" {
|
|
fmt.Fprintf(w, `<urlset><url><loc>%[1]s/</loc></url><url><loc>%[1]s/nextcloud-hosting/</loc></url><url><loc>%[1]s/pricing/</loc></url></urlset>`, ours.URL)
|
|
return
|
|
}
|
|
http.NotFound(w, r)
|
|
}))
|
|
defer ours.Close()
|
|
rival = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintf(w, `<urlset><url><loc>%[1]s/blog/nextcloud-vs-google-drive/</loc></url><url><loc>%[1]s/about/</loc></url>
|
|
<url><loc>%[1]s/blog/nextcloud-hosting/</loc></url><url><loc>%[1]s/2024/05/12345/</loc></url></urlset>`, rival.URL)
|
|
}))
|
|
defer rival.Close()
|
|
site, prop := ours.URL+"/", "sc-domain:ours.example"
|
|
// Search Console: a search we're weak on, one we're fine on, one too small.
|
|
for i, q := range []struct {
|
|
q string
|
|
impr int
|
|
pos float64
|
|
}{{"self hosted office suite price", 120, 34}, {"nextcloud", 500, 4}, {"tiny", 5, 50}} {
|
|
d.Exec(`INSERT INTO gsc_query_days VALUES (?, '2026-11-10', ?, ?, 0, ?, ?)`, prop, fmt.Sprintf("%s/p%d/", ours.URL, i), q.q, q.impr, q.pos*float64(q.impr))
|
|
}
|
|
d.PutJSON(throughKey(prop), "2026-11-19")
|
|
inventory.Save(d, &inventory.Entry{Name: "Rival", Relationship: inventory.Competitor, Websites: []string{rival.URL}})
|
|
sig := &signals.Signal{Name: "Leaving M365", Spot: "microsoft 365 alternative for small business"}
|
|
signals.Save(d, sig)
|
|
p := &profile.Profile{Name: "Us", Keywords: []string{"private cloud for business", "nextcloud hosting"},
|
|
Products: []profile.Product{{Name: "Federated Core"}}}
|
|
|
|
n, err := FindKeywords(context.Background(), d, site, prop, p, "test", time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ks, _ := Keywords(d, site, KeywordNew)
|
|
got := map[string]Keyword{}
|
|
for _, k := range ks {
|
|
got[k.Keyword] = k
|
|
}
|
|
if n != len(ks) {
|
|
t.Errorf("added %d, stored %d", n, len(ks))
|
|
}
|
|
for _, want := range []string{"self hosted office suite price", "private cloud for business",
|
|
"microsoft 365 alternative for small business", "nextcloud vs google drive"} {
|
|
if _, ok := got[want]; !ok {
|
|
t.Errorf("missing %q (have %v)", want, keys(got))
|
|
}
|
|
}
|
|
for _, not := range []string{"nextcloud hosting", "nextcloud", "tiny", "about", "12345", "federated core alternative"} {
|
|
if _, ok := got[not]; ok {
|
|
t.Errorf("%q shouldn't be a candidate", not)
|
|
}
|
|
}
|
|
if k := got["self hosted office suite price"]; k.Impressions != 120 || k.Position != 34 || k.Intent != "buyer" || k.Sources[0] != "Search Console" {
|
|
t.Errorf("gsc keyword: %+v", k)
|
|
}
|
|
if got["nextcloud vs google drive"].Sources[0] != "competitor Rival" {
|
|
t.Errorf("competitor keyword: %+v", got["nextcloud vs google drive"])
|
|
}
|
|
if ks[0].Intent != "buyer" || ks[0].Keyword != "self hosted office suite price" {
|
|
t.Errorf("order: %v", ks[0])
|
|
}
|
|
// Again: nothing new.
|
|
if n, _ := FindKeywords(context.Background(), d, site, prop, p, "test", time.Now()); n != 0 {
|
|
t.Errorf("found again: %d", n)
|
|
}
|
|
|
|
// The live top 3, and the ranking by who has to be beaten.
|
|
k := got["nextcloud vs google drive"]
|
|
fr := &fakeResearcher{
|
|
text: `Here: {"results":[{"url":"https://forum.example/t/1","title":"Thread","kind":"forum","note":"old"},
|
|
{"url":"https://small.example/post","title":"Post","kind":"small site","note":"2019"},
|
|
{"url":"https://made-up.example/x","title":"X","kind":"big brand","note":"?"}]}`,
|
|
sources: []ai.Source{{URL: "https://forum.example/t/1"}, {URL: "https://small.example/post/"}},
|
|
}
|
|
kk, err := ReadTop(context.Background(), d, fr, k.ID, site, time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fr.got.MaxSearches != 3 || !strings.Contains(fr.got.Prompt, "nextcloud vs google drive") {
|
|
t.Errorf("request: %+v", fr.got)
|
|
}
|
|
if len(kk.Top) != 3 || kk.Top[2].Kind != "unread" || kk.Searches != 2 || kk.Hard() != 3 {
|
|
t.Errorf("top: %+v", kk)
|
|
}
|
|
ks, _ = Keywords(d, site, KeywordNew)
|
|
if ks[0].ID != k.ID {
|
|
t.Errorf("a read, winnable keyword should come first: %v", ks[0].Keyword)
|
|
}
|
|
if err := SetKeywordStatus(d, k.ID, KeywordPicked, time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if picked, _ := Keywords(d, site, KeywordPicked); len(picked) != 1 || picked[0].PickedAt.IsZero() {
|
|
t.Errorf("picked: %+v", picked)
|
|
}
|
|
}
|
|
|
|
func keys(m map[string]Keyword) []string {
|
|
var out []string
|
|
for k := range m {
|
|
out = append(out, k)
|
|
}
|
|
return out
|
|
}
|