Files
supermarket/internal/web/keywords_test.go
davidandClaude Opus 5.5 c0a5ddf77f Phase 3 part 5: the next keywords
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>
2026-11-22 08:11:27 -07:00

63 lines
2.4 KiB
Go

package web
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"supermarket/internal/ai"
"supermarket/internal/profile"
"supermarket/internal/secrets"
"supermarket/internal/seo"
)
type cannedResearcher struct{ cannedAI }
func (cannedResearcher) Research(context.Context, ai.ResearchRequest) (*ai.ResearchResult, error) {
return &ai.ResearchResult{Text: `{"results":[{"url":"https://forum.example/1","title":"A thread","kind":"forum","note":"old"}]}`,
Sources: []ai.Source{{URL: "https://forum.example/1"}}, Searches: 1}, nil
}
func TestKeywordsPage(t *testing.T) {
newAIClient = func(ai.Config, *secrets.Store) (ai.Client, error) { return cannedResearcher{}, nil }
defer func() { newAIClient = ai.New }()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `<urlset></urlset>`)
}))
defer srv.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Keywords: []string{"private cloud pricing"}, Websites: []profile.Website{{URL: srv.URL}}})
if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "No candidates yet") {
t.Fatal("no empty state")
}
if rec := postForm(h, "/seo/keywords?site=0", nil); rec.Code != http.StatusSeeOther {
t.Fatalf("find: %d %s", rec.Code, rec.Body)
}
body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String()
for _, want := range []string{"private cloud pricing", "buyer", "From profile keyword", "Read the live top 3"} {
if !strings.Contains(body, want) {
t.Errorf("page lacks %q", want)
}
}
ks, _ := seo.Keywords(s.DB, srv.URL, seo.KeywordNew)
id := fmt.Sprint(ks[0].ID)
if rec := postForm(h, "/seo/keywords/"+id+"/top", nil); rec.Code != http.StatusSeeOther {
t.Fatalf("top: %d %s", rec.Code, rec.Body)
}
if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "A thread") || !strings.Contains(body, "winnable for a small site") {
t.Error("the top results aren't shown")
}
postForm(h, "/seo/keywords/"+id+"/pick", nil)
if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "<h2>Picked</h2>") {
t.Error("the pick isn't shown")
}
postForm(h, "/seo/keywords/"+id+"/dismiss", nil)
if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "Dismissed (1)") {
t.Error("dismissed isn't shown")
}
}