feat: internal links widget with counts and suggestions
- E10 golem15-seo-links in the SEO tab after the graph preview (types with a body): inbound and outbound counts from the last finished crawl, the orphan warning, up to 10 pages to link to (Copy address) and 10 that could link here - classes/linkgraph.go builds the report from the crawl's pages and links; the crawler's orphan suggestions share NormalText - GET /_seo/api/v1/links and seo_get_link_suggestions; widget action seo_links mapped in AdminOperations - element bundle rebuilt
This commit is contained in:
114
linking_test.go
114
linking_test.go
@@ -222,3 +222,117 @@ func TestAutolinks(t *testing.T) {
|
||||
t.Fatalf("history vias = %v", vias)
|
||||
}
|
||||
}
|
||||
|
||||
// linkReport is GET /_seo/api/v1/links and the seo_links action's answer.
|
||||
type linkReport struct {
|
||||
CrawledAt *string `json:"crawledAt"`
|
||||
Inbound *int `json:"inbound"`
|
||||
Outbound *int `json:"outbound"`
|
||||
Orphan bool `json:"orphan"`
|
||||
Out []struct {
|
||||
Phrase string `json:"phrase"`
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
} `json:"out"`
|
||||
In []struct {
|
||||
Phrase string `json:"phrase"`
|
||||
Title string `json:"title"`
|
||||
AdminURL string `json:"adminUrl"`
|
||||
} `json:"in"`
|
||||
}
|
||||
|
||||
func (e *testEnv) links(t *testing.T, token, path string) linkReport {
|
||||
t.Helper()
|
||||
var out linkReport
|
||||
if err := json.Unmarshal(e.expect(t, http.StatusOK, http.MethodGet, "/_seo/api/v1/links?url="+urlQueryEscape(path), token, nil).Body.Bytes(), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TestLinkSuggestions covers E10's answer (UI-SPEC C19, D-82): before the
|
||||
// first crawl the counts are null and the lists empty; after it, the
|
||||
// inbound and outbound counts, the orphan state, the pages this text
|
||||
// mentions by H1 or focus keyphrase and does not link to, and the pages
|
||||
// whose text mentions this page's title or focus keyphrase and do not link
|
||||
// here, with their admin forms; the SEO tab's seo_links widget answers the
|
||||
// same, and E10 is in the SEO tab after the graph preview.
|
||||
func TestLinkSuggestions(t *testing.T) {
|
||||
env := bootCrawlEnv(t)
|
||||
token := env.admin(t, true)
|
||||
hericium := env.createPage(t, token, map[string]any{"title": "Hericium", "slug": "hericium", "published": true})
|
||||
env.createPage(t, token, map[string]any{"title": "Shiitake", "slug": "shiitake", "published": true})
|
||||
oyster := env.createPage(t, token, map[string]any{"title": "Oyster", "slug": "oyster", "published": true})
|
||||
env.expect(t, http.StatusOK, http.MethodPut, "/_seo/api/v1/seo", token, map[string]any{"url": "/pages/hericium", "values": map[string]any{"keyphrase": "lwia grzywa"}})
|
||||
env.expect(t, http.StatusOK, http.MethodPut, "/_seo/api/v1/seo", token, map[string]any{"url": "/pages/oyster", "values": map[string]any{"keyphrase": "boczniak"}})
|
||||
|
||||
before := env.links(t, token, "/pages/hericium")
|
||||
if before.CrawledAt != nil || before.Inbound != nil || before.Outbound != nil || before.Orphan || len(before.Out) != 0 || len(before.In) != 0 {
|
||||
t.Fatalf("before the first crawl = %+v", before)
|
||||
}
|
||||
|
||||
site := newFakeSite()
|
||||
site.sitemap("/", "/pages/hericium", "/pages/shiitake", "/pages/oyster")
|
||||
site.plant(
|
||||
doc{path: "/", title: "Home", links: []string{"/pages/shiitake", "/pages/oyster"}},
|
||||
doc{path: "/pages/hericium", title: "Hericium", body: "<p>Hericium grows near shiitake and boczniak.</p>"},
|
||||
doc{path: "/pages/shiitake", title: "Shiitake", body: "<p>Shiitake differs from lwia grzywa.</p>", links: []string{"/pages/oyster"}},
|
||||
doc{path: "/pages/oyster", title: "Oyster", body: "<p>Oyster tastes almost like Hericium.</p>", links: []string{"/pages/shiitake"}},
|
||||
)
|
||||
env.crawlOnce(t, token, siteCtx(t, site))
|
||||
|
||||
got := env.links(t, token, siteURL+"/pages/hericium")
|
||||
if got.CrawledAt == nil || got.Inbound == nil || *got.Inbound != 0 || got.Outbound == nil || *got.Outbound != 0 || !got.Orphan {
|
||||
t.Fatalf("hericium counts = %+v", got)
|
||||
}
|
||||
outs := map[string]string{}
|
||||
for _, o := range got.Out {
|
||||
outs[o.URL] = o.Phrase + "|" + o.Title
|
||||
}
|
||||
if len(got.Out) != 2 || outs[siteURL+"/pages/shiitake"] != "Shiitake|Shiitake" || outs[siteURL+"/pages/oyster"] != "boczniak|Oyster" {
|
||||
t.Fatalf("hericium out = %+v", got.Out)
|
||||
}
|
||||
ins := map[string]string{}
|
||||
for _, i := range got.In {
|
||||
ins[i.Title] = i.Phrase + "|" + i.AdminURL
|
||||
}
|
||||
if len(got.In) != 2 || ins["Shiitake"] == "" || !strings.HasPrefix(ins["Shiitake"], "lwia grzywa|/backend/acme/pages/pages/update/") || ins["Oyster"] != "Hericium|/backend/acme/pages/pages/update/"+oyster {
|
||||
t.Fatalf("hericium in = %+v", got.In)
|
||||
}
|
||||
|
||||
// Shiitake: linked from home and oyster, links to oyster (not
|
||||
// suggested again although its text names no other page).
|
||||
sh := env.links(t, token, "/pages/shiitake")
|
||||
if *sh.Inbound != 2 || *sh.Outbound != 1 || sh.Orphan {
|
||||
t.Fatalf("shiitake counts = %+v", sh)
|
||||
}
|
||||
if len(sh.Out) != 1 || sh.Out[0].URL != siteURL+"/pages/hericium" || sh.Out[0].Phrase != "lwia grzywa" {
|
||||
t.Fatalf("shiitake out = %+v", sh.Out)
|
||||
}
|
||||
if len(sh.In) != 1 || sh.In[0].Title != "Hericium" || sh.In[0].AdminURL != "/backend/acme/pages/pages/update/"+hericium {
|
||||
t.Fatalf("shiitake in = %+v", sh.In)
|
||||
}
|
||||
if rec := env.call(t, http.MethodGet, "/_seo/api/v1/links?url=javascript:x", token, nil); rec.Code != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("a bad url = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// The SEO tab: E10 after the graph preview, its action answers the
|
||||
// same report.
|
||||
findField(t, env.formSchema(t, token, "acme/pages/pages"), "seo_links")
|
||||
n, _ := strconv.Atoi(hericium)
|
||||
rec := env.expect(t, http.StatusOK, http.MethodPost, adminAPI("/acme/pages/pages/widgets/seo_links"), token, map[string]any{"record_id": n, "payload": map[string]any{"op": "links", "locale": "en"}})
|
||||
var widget struct {
|
||||
Data struct {
|
||||
Data linkReport `json:"data"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &widget); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w := widget.Data.Data; !w.Orphan || len(w.Out) != 2 || len(w.In) != 2 || w.Inbound == nil {
|
||||
t.Fatalf("widget answer = %s", rec.Body.String())
|
||||
}
|
||||
if rec := env.call(t, http.MethodPost, adminAPI("/acme/pages/pages/widgets/seo_links"), token, map[string]any{"record_id": n, "payload": map[string]any{"op": "graph"}}); rec.Code != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("unknown op = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user