SEO: a domain property (sc-domain:) covers every subdomain, so the shop, Gitea and forum were counted as the website. What ranks, the morning check, the Monday report, keywords, change candidates and the audit's traffic now read only the website's own host (with or without www). The report keeps pages worth a proposal apart from "Waiting for your yes", explains only moves past the thresholds, and says "places up/down". Also: - Feed summaries drop the posts' scripts and styles. - Spend you log can't be negative. - Test and check buttons can't be clicked twice while they run. - Previews show the site's web fonts and icon sprites. - Backups are read without leaving -wal/-shm files; deleting one removes them, and leftovers are swept at start. - The Overview's setup flags a CRM that Hermes can't reach. - Import now says what came in; adding a prospect that exists says so; prospects can be deleted. - Signal tags and brief addresses keep accented letters (Ü → u). - Page errors start with a capital; clearer messages for bad uploads, CRM retry and cancel, keywords, and the finished audit. - The module you're in stays unfolded in the sidebar. - A picked keyword can be unpicked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
204 lines
7.0 KiB
Go
204 lines
7.0 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|