Supermarket looks at each Inventory partner's pages that list providers, hosts or partners (from their sitemap and home page) and keeps the ones that list others but not the site; once a month by itself, or on request. On request (up to 4 web searches), the AI finds lists of alternatives to a competitor, and each page is read to keep the ones that name others but not the company. Each opportunity can be marked asked, won or dismissed; it's marked won when a link from its site shows up. SEO → Links lists them; open ones join What to write next. Nothing is sent: asking is the user's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
3.6 KiB
Go
78 lines
3.6 KiB
Go
package backlinks
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"supermarket/internal/ai"
|
|
"supermarket/internal/inventory"
|
|
)
|
|
|
|
type fakeMaps map[string][]string
|
|
|
|
func (f fakeMaps) AllPages(_ context.Context, site, _ string) ([]string, error) { return f[site], nil }
|
|
|
|
type fakeSearch struct{ text string }
|
|
|
|
func (f fakeSearch) Research(context.Context, ai.ResearchRequest) (*ai.ResearchResult, error) {
|
|
return &ai.ResearchResult{Text: f.text, Searches: 2, Sources: []ai.Source{{URL: "https://blog.example/zendesk-alternatives"}, {URL: "https://listed.example/best"}}}, nil
|
|
}
|
|
|
|
func TestOpportunities(t *testing.T) {
|
|
d := openDB(t)
|
|
site := "https://example.com/"
|
|
now := time.Date(2026, 11, 24, 9, 0, 0, 0, time.UTC)
|
|
inventory.Save(d, &inventory.Entry{Name: "FreeScout", Relationship: inventory.Partner, Websites: []string{"https://freescout.net/"}})
|
|
inventory.Save(d, &inventory.Entry{Name: "Lone", Relationship: inventory.Partner}) // no website: skipped
|
|
others := []string{"https://a.example/", "https://b.example/", "https://c.example/"}
|
|
web := fakeWeb{
|
|
"https://freescout.net/": {Status: 200, Inside: []string{"https://freescout.net/about/", "https://freescout.net/hosting-providers/"}},
|
|
"https://freescout.net/hosting-providers/": {Status: 200, Title: "Hosting providers", Links: others},
|
|
"https://freescout.net/partners/": {Status: 200, Title: "Partners", Links: append([]string{"https://www.example.com/freescout/"}, others...)},
|
|
"https://freescout.net/support/": {Status: 200, Title: "Support", Links: []string{"https://github.com/x"}}, // lists no one
|
|
"https://blog.example/zendesk-alternatives": {Status: 200, Title: "Zendesk alternatives", Text: "Help Scout, Freshdesk…", Links: others},
|
|
"https://listed.example/best": {Status: 200, Title: "Best", Text: "Example Computer is great", Links: others},
|
|
}
|
|
maps := fakeMaps{"https://freescout.net/": {"https://freescout.net/partners/", "https://freescout.net/support/", "https://freescout.net/blog/post"}}
|
|
|
|
n, err := FindPartnerPages(context.Background(), d, web, maps, site, now)
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("partners: %d %v", n, err)
|
|
}
|
|
opps, _ := Opportunities(d, site)
|
|
if len(opps) != 2 || opps[0].Page != "https://freescout.net/hosting-providers/" || opps[0].Status != OppOpen || opps[0].About != "FreeScout" ||
|
|
opps[1].Status != OppListed {
|
|
t.Fatalf("opps: %+v", opps)
|
|
}
|
|
|
|
z := &inventory.Entry{Name: "Zendesk", Relationship: inventory.Competitor}
|
|
text := `{"pages": [{"url": "https://blog.example/zendesk-alternatives", "title": "x"}, {"url": "https://listed.example/best", "title": "y"}, {"url": "https://made-up.example/", "title": "z"}]}`
|
|
added, searches, err := FindAlternativeLists(context.Background(), d, fakeSearch{text}, web, site, "Example Computer", z, now)
|
|
if err != nil || added != 1 || searches != 2 {
|
|
t.Fatalf("alternatives: %d %d %v", added, searches, err)
|
|
}
|
|
|
|
// Asked, then the link appears: won.
|
|
opps, _ = Opportunities(d, site)
|
|
var open Opportunity
|
|
for _, o := range opps {
|
|
if o.Page == "https://freescout.net/hosting-providers/" {
|
|
open = o
|
|
}
|
|
}
|
|
if err := SetStatus(d, site, open.ID, OppAsked, now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
upsert(d, site, "https://freescout.net/hosting-providers/", "/", "", OriginBing, "2026-12-01")
|
|
if n, _ := MarkWon(d, site, now); n != 1 {
|
|
t.Errorf("won: %d", n)
|
|
}
|
|
if o, _ := Opportunities(d, site); o[0].Status != OppOpen || o[0].Kind != KindAlternatives {
|
|
t.Errorf("after won, open first: %+v", o[0])
|
|
}
|
|
if err := SetStatus(d, site, open.ID, "bogus", now); err == nil {
|
|
t.Error("took a bogus status")
|
|
}
|
|
}
|