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, 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") || !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, site, Settings{MinClicks: 50, MinPositions: 50, MinImpressions: 100000}) if !q.Quiet() { t.Errorf("not quiet: %+v", q.Moves) } rep, err := Weekly(d, 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) } } }