Files
davidandClaude Opus 5.5 77f6e1e1d1 Phase 7 part 2: ranked by what converts
Push pages and change candidates are ordered by the sign-ups a page
would bring if clicked as usual for its position (impressions × the
clicks it misses × its sign-up rate, or the site's), and say so. What
ranks flags pages by what they convert: traffic with no sign-ups, and
pages that convert but Google hardly shows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 07:11:16 -07:00

285 lines
10 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package seo
import (
"strings"
"testing"
"time"
"supermarket/internal/db"
)
// put stores a week of data spread over its 7 days.
type row struct {
page, query string
clicks, impr int
pos float64
}
func week(t *testing.T, d *db.DB, site, from string, site_ row, rows []row) {
t.Helper()
start, _ := time.Parse(dayFormat, from)
// One day carries the week, which is enough for weekly sums.
day := start.Format(dayFormat)
if _, err := d.Exec(`INSERT INTO gsc_site_days VALUES (?, ?, ?, ?, ?)`, site, day, site_.clicks, site_.impr, site_.pos*float64(site_.impr)); err != nil {
t.Fatal(err)
}
pages := map[string]row{}
for _, r := range rows {
p := pages[r.page]
p.page = r.page
p.pos = (p.pos*float64(p.impr) + r.pos*float64(r.impr)) / float64(p.impr+r.impr)
p.clicks += r.clicks
p.impr += r.impr
pages[r.page] = p
if r.query != "" {
if _, err := d.Exec(`INSERT INTO gsc_query_days VALUES (?, ?, ?, ?, ?, ?, ?)`, site, day, r.page, r.query, r.clicks, r.impr, r.pos*float64(r.impr)); err != nil {
t.Fatal(err)
}
}
}
for _, p := range pages {
if _, err := d.Exec(`INSERT INTO gsc_page_days VALUES (?, ?, ?, ?, ?, ?)`, site, day, p.page, p.clicks, p.impr, p.pos*float64(p.impr)); err != nil {
t.Fatal(err)
}
}
}
func TestMorningCheckAndReport(t *testing.T) {
d := openDB(t)
site := "sc-domain:whey.example"
best, review, gone := "https://whey.example/best/", "https://whey.example/review/", "https://whey.example/old/"
// The week before: 2026-11-06 to 11-12. This week: 11-13 to 11-19.
week(t, d, site, "2026-11-06", row{clicks: 59, impr: 9679, pos: 27}, []row{
{best, "whey price comparison", 0, 515, 3.0}, {best, "best whey", 0, 80, 40},
{review, "whey review", 8, 400, 10},
{gone, "old query", 1, 400, 20},
{"https://whey.example/tiny/", "tiny", 1, 20, 30},
})
week(t, d, site, "2026-11-13", row{clicks: 95, impr: 9763, pos: 25.1}, []row{
{best, "whey price comparison", 0, 34, 3.9}, {best, "best whey", 0, 80, 40},
{review, "whey review", 15, 420, 6.7},
{"https://whey.example/tiny/", "tiny", 2, 22, 31},
})
d.PutJSON(throughKey(site), "2026-11-19")
c, err := MorningCheck(d, Scope{Property: site}, DefaultSettings)
if err != nil {
t.Fatal(err)
}
if c.From != "2026-11-13" || c.PrevTo != "2026-11-12" || c.Before.Clicks != 59 || c.After.Clicks != 95 {
t.Fatalf("check: %+v", c)
}
moves := map[string]Move{}
for _, m := range c.Moves {
moves[m.Kind+":"+m.Key] = m
}
if len(c.Moves) != 5 {
t.Errorf("moves: %+v", c.Moves)
}
// The review page: +7 clicks, and the query behind it.
r := moves["page:"+review]
if r.ClickDelta() != 7 || !strings.Contains(strings.Join(r.Why, " "), "“whey review”: 8 → 15 clicks") {
t.Errorf("review: %+v", r)
}
// The "best" page lost 21 positions on paper, under one in reality.
b := moves["page:"+best]
why := strings.Join(b.Why, " ")
if b.PosDelta() < 20 || !strings.Contains(why, "Like for like it moved 0.3 places down") ||
!strings.Contains(why, "“whey price comparison”, 0 → 0 clicks, 515 → 34 impressions, position 3.0 → 3.9") {
t.Errorf("best: %+v %v", b, why)
}
// A page with no row this week is "no data", not a drop to zero.
if g := moves["page:"+gone]; !g.NoData || !strings.Contains(g.Why[0], "no data") {
t.Errorf("gone: %+v", g)
}
if _, ok := moves["page:https://whey.example/tiny/"]; ok {
t.Error("noise reported as a move")
}
if _, ok := moves["query:whey review"]; !ok {
t.Error("the review query's move is missing")
}
// Higher thresholds: quiet.
q, _ := MorningCheck(d, Scope{Property: site}, Settings{MinClicks: 50, MinPositions: 50, MinImpressions: 100000})
if !q.Quiet() {
t.Errorf("not quiet: %+v", q.Moves)
}
rep, err := Weekly(d, Scope{Property: site}, time.Date(2026, 11, 23, 8, 0, 0, 0, time.UTC))
if err != nil {
t.Fatal(err)
}
if rep.WeekTo != "2026-11-19" || len(rep.Pages) != 3 || rep.Pages[0].Key != review ||
rep.Where != "Clicks grew while impressions were flat: the change comes from rank and click rate, not from more visibility. On 95 clicks, one week is not a trend." {
t.Fatalf("report: %+v", rep)
}
if len(rep.Pages[0].Why) == 0 {
t.Error("the report's pages have no causes")
}
if err := Save(d, rep); err != nil {
t.Fatal(err)
}
if err := Save(d, rep); err != nil {
t.Fatal(err)
}
list, _ := Reports(d, site, 10)
if len(list) != 1 || list[0].After.Clicks != 95 || list[0].Pages[0].Key != review {
t.Errorf("stored: %+v", list)
}
text := rep.Text()
for _, want := range []string{"Clicks 59 → 95", "Position 27.0 → 25.1", "3. Waiting for your yes", review} {
if !strings.Contains(text, want) {
t.Errorf("text lacks %q:\n%s", want, text)
}
}
}
func TestWhere(t *testing.T) {
for _, c := range []struct {
b, a Numbers
want string
}{
{Numbers{Clicks: 1000, Impressions: 10000}, Numbers{Clicks: 1200, Impressions: 13000}, "Impressions rose 30%"},
{Numbers{Clicks: 1000, Impressions: 10000}, Numbers{Clicks: 800, Impressions: 7000}, "Impressions fell 30%"},
{Numbers{Clicks: 1000, Impressions: 10000}, Numbers{Clicks: 1000, Impressions: 10000}, "Clicks didn't change."},
} {
if got := where(c.b, c.a); !strings.HasPrefix(got, c.want) {
t.Errorf("%v → %v: %q", c.b, c.a, got)
}
}
}
// A domain property covers every subdomain; a website's scope keeps only
// its own host, with or without www.
func TestScopeKeepsTheWebsite(t *testing.T) {
d := openDB(t)
const site = "sc-domain:whey.example"
week(t, d, site, "2026-11-06", row{clicks: 1000, impr: 5000, pos: 5}, []row{
{"https://shop.whey.example/", "whey shop", 900, 1000, 1},
{"https://whey.example/best/", "best whey", 40, 2000, 6},
{"https://www.whey.example/", "whey", 60, 2000, 3},
})
week(t, d, site, "2026-11-13", row{clicks: 1000, impr: 5000, pos: 5}, []row{
{"https://shop.whey.example/", "whey shop", 900, 1000, 1},
{"https://whey.example/best/", "best whey", 40, 2000, 6},
{"https://www.whey.example/", "whey", 60, 2000, 3},
})
d.PutJSON(throughKey(site), "2026-11-19")
sc := NewScope(site, "https://whey.example")
r, err := WhatRanks(d, sc)
if err != nil || r == nil {
t.Fatal(r, err)
}
if r.Total.Clicks != 200 || r.Total.Impressions != 8000 {
t.Errorf("totals include another host: %+v", r.Total)
}
for _, p := range r.Pages {
if !sc.Has(p.Page) {
t.Errorf("page from another host: %s", p.Page)
}
}
if len(r.Pages) != 2 {
t.Errorf("pages: %+v", r.Pages)
}
c, err := MorningCheck(d, sc, DefaultSettings)
if err != nil || c == nil || c.After.Clicks != 100 || c.Pages != 2 || c.Queries != 2 {
t.Errorf("check: %+v %v", c, err)
}
if whole, _ := WhatRanks(d, Scope{Property: site}); whole == nil || whole.Total.Clicks != 2000 {
t.Errorf("whole property: %+v", whole)
}
if !sc.Has("https://www.whey.example/x") || sc.Has("https://shop.whey.example/") || sc.Has("https://whey.example.evil/") {
t.Error("Has")
}
}
func TestPlaces(t *testing.T) {
for in, want := range map[float64]string{-12.8: "12.8 places up", 0.3: "0.3 places down", 0.01: "less than 0.1 places"} {
if got := places(in); got != want {
t.Errorf("places(%v) = %q, want %q", in, got, want)
}
}
}
func TestDailyGainsAndOutside(t *testing.T) {
d := openDB(t)
const site = "sc-domain:whey.example"
week(t, d, site, "2026-11-06", row{}, []row{
{"https://whey.example/best/", "best whey", 10, 200, 6},
{"https://whey.example/best/", "whey deals", 4, 100, 9},
{"https://gitea.whey.example/x.sql", "filetype:sql intext:password", 0, 5, 3},
})
week(t, d, site, "2026-11-13", row{}, []row{
{"https://whey.example/best/", "best whey", 15, 300, 5},
{"https://whey.example/best/", "whey price", 2, 40, 12},
{"https://gitea.whey.example/x.sql", "filetype:sql intext:password", 3, 9, 2},
{"https://shop.whey.example/", "whey shop", 1, 9, 2},
})
d.PutJSON(throughKey(site), "2026-11-19")
c, err := MorningCheck(d, NewScope(site, "https://whey.example/"), DefaultSettings)
if err != nil || c == nil {
t.Fatal(c, err)
}
if len(c.Days) != 7 || c.Days[0].Date != "2026-11-13" || c.Days[0].Clicks != 17 || c.Days[6].Clicks != 0 || c.PrevDays[0].Clicks != 14 {
t.Errorf("days: %+v %+v", c.Days, c.PrevDays)
}
if len(c.Gains) != 2 || c.Gains[0].Query != "best whey" || c.Gains[1].Query != "whey price" || !c.Gains[1].New {
t.Errorf("gains: %+v", c.Gains)
}
if len(c.Losses) != 1 || c.Losses[0].Query != "whey deals" || !c.Losses[0].NoData {
t.Errorf("losses: %+v", c.Losses)
}
if len(c.Outside) != 2 || c.Outside[0].Host != "gitea.whey.example" || c.Outside[0].Clicks != 3 || c.Outside[0].Query != "filetype:sql intext:password" {
t.Errorf("outside: %+v", c.Outside)
}
text := c.Text("https://whey.example/")
for _, want := range []string{"Clicks 14 → 17 (+21.4%)", "Not counted: 3 clicks went to gitea.whey.example, most for “filetype:sql intext:password”.", "Searches that lost most clicks", "whey deals"} {
if !strings.Contains(text, want) {
t.Errorf("text lacks %q:\n%s", want, text)
}
}
if Percent(37, 30) != "−18.9%" || Percent(5171, 7876) != "+52.3%" || Percent(0, 5) != "" {
t.Error("Percent")
}
}
func TestRankedByWhatConverts(t *testing.T) {
d := openDB(t)
const site = "sc-domain:whey.example"
// Two pages on page one, shown a lot, hardly clicked: /big/ is shown
// more, /sells/ turns its visits into sign-ups.
week(t, d, site, "2026-11-13", row{}, []row{
{"https://whey.example/big/", "big", 2, 3000, 5},
{"https://whey.example/sells/", "sells", 1, 1500, 5},
})
d.PutJSON(throughKey(site), "2026-11-19")
sc := NewScope(site, "https://whey.example/")
r, _ := WhatRanks(d, sc)
if len(r.Push) != 2 || r.Push[0].Page != "https://whey.example/big/" {
t.Fatalf("without conversions, by impressions: %+v", r.Push)
}
sc.Conv = map[string]PageConv{
"/big/": {Visitors: 200, Conversions: 0},
"/sells/": {Visitors: 40, Conversions: 8},
"/quiet/": {Visitors: 20, Conversions: 4}, // converts, but Google hardly shows it
"/other/": {Visitors: 60, Conversions: 1},
}
r, _ = WhatRanks(d, sc)
if r.Push[0].Page != "https://whey.example/sells/" || r.Push[0].Expected < r.Push[1].Expected || r.Push[1].Expected != 0 {
t.Errorf("by sign-ups: %+v", r.Push)
}
if !strings.Contains(r.Push[0].Why, "at its own rate of 20.0% of visits") {
t.Errorf("why: %s", r.Push[0].Why)
}
if r.Flags["/big/"] != FlagNoSignups || r.Flags["/quiet/"] != FlagHidden || r.Flags["/other/"] != "" {
t.Errorf("flags: %+v", r.Flags)
}
if len(r.Flagged) != 2 || r.Flagged[0].Path != "/quiet/" {
t.Errorf("flagged: %+v", r.Flagged)
}
if PathKey("https://www.whey.example/Big") != "/big/" || PathKey("https://whey.example") != "/" {
t.Error("PathKey")
}
}