package backlinks import ( "context" "errors" "testing" "time" "supermarket/internal/seo" ) type fakeWeb map[string]*seo.LivePage func (f fakeWeb) Page(_ context.Context, u string) (*seo.LivePage, error) { if p, ok := f[u]; ok { return p, nil } return nil, errors.New("no route") } func TestCheck(t *testing.T) { d := openDB(t) site := "https://example.com/" now := time.Date(2026, 11, 24, 9, 0, 0, 0, time.UTC) web := fakeWeb{ "https://kept.example/list": {Status: 200, Final: "https://kept.example/list", Links: []string{"https://www.example.com/freescout/"}}, "https://gone.example/list": {Status: 200, Final: "https://gone.example/list", Links: []string{"https://other.example/"}}, "https://dead.example/post": {Status: 404}, "https://moved.example/a": {Status: 200, Final: "https://moved.example/b"}, "https://old.example/x": {Status: 200, Final: "https://old.example/x", Links: []string{"https://example.com/old-page"}}, "https://example.com/freescout/": {Status: 200}, "https://example.com/old-page/": {Status: 404}, } for _, src := range []string{"https://kept.example/list", "https://gone.example/list", "https://dead.example/post", "https://moved.example/a", "https://old.example/x", "https://down.example/"} { upsert(d, site, src, "", "", OriginBing, "2026-11-20") } n, err := CheckDue(context.Background(), d, web, site, 0, now) if err != nil || n != 6 { t.Fatal(n, err) } links, _ := Links(d, site) got := map[string]Link{} for _, l := range links { got[l.Source] = l } for src, want := range map[string]string{ "https://kept.example/list": StatusKept, "https://gone.example/list": StatusGone, "https://dead.example/post": StatusPageGone, "https://moved.example/a": StatusPageGone, "https://old.example/x": StatusTarget404, "https://down.example/": StatusUnchecked, } { if got[src].Status != want { t.Errorf("%s: %s (%s), want %s", src, got[src].Status, got[src].Note, want) } } if k := got["https://kept.example/list"]; k.Target != "/freescout/" || k.LastSeen != "2026-11-24" { t.Errorf("kept: %+v", k) } if o := got["https://old.example/x"]; o.Target != "/old-page/" || !o.Lost() { t.Errorf("target 404: %+v", o) } // Checked: not again within the week, and the day's count is kept. if n, _ := CheckDue(context.Background(), d, web, site, 0, now.Add(time.Hour)); n != 0 { t.Errorf("checked again: %d", n) } var done int d.GetJSON(checkedKey("2026-11-24"), &done) if done != 6 { t.Errorf("day's count: %d", done) } // Adding by hand. if _, err := Add(d, site, "example.com/x", "", now); err == nil { t.Error("took an address without https") } if _, err := Add(d, site, "https://www.example.com/blog/", "", now); err == nil { t.Error("took a page of the site itself") } l, err := Add(d, site, "https://kept.example/list#comments", "/freescout", now) if err != nil || l.Origin != OriginBing || l.Target != "/freescout/" { t.Errorf("added: %+v %v", l, err) // the same link as Bing's, with its target: Bing's row is kept } l, err = Add(d, site, "https://friend.example/hosts", "", now) if err != nil || l.Origin != OriginManual || l.Status != StatusUnchecked { t.Fatalf("added: %+v %v", l, err) } CheckOne(context.Background(), d, web, site, l, now) if l.Note == "" { t.Error("no note after a check") } }