Phase 5 part 4: landing pages per buying signal
Each signal's page lists its landing pages with their Gitea status, live address and 30-day visitors from Plausible, and builds a new one. Pages can be duplicated as variants (B, C…, with their own utm_content), taken off the site through a pull request that deletes the file, or have their pull request closed. The scorecard counts live pages. Schema 23. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/signals"
|
||||
"supermarket/internal/sites"
|
||||
|
||||
"supermarket/internal/hugorepo"
|
||||
"supermarket/internal/hugorepo/hugotest"
|
||||
@@ -305,3 +307,139 @@ func TestSitePageBuilder(t *testing.T) {
|
||||
t.Errorf("after delete: %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignalLandingPages(t *testing.T) {
|
||||
if hugorepo.HugoVersion(context.Background()) == "" {
|
||||
t.Skip("no hugo")
|
||||
}
|
||||
clone := hugotest.Site(t, map[string]string{
|
||||
"layouts/partials/render-sections.html": `{{ range .Params.sections }}{{ partial (printf "sections/%s.html" .type) . }}{{ end }}`,
|
||||
"layouts/partials/sections/hero.html": `<h2>{{ .title }}</h2>{{ range .ctas }}<a href="{{ .url }}">{{ .label }}</a>{{ end }}`,
|
||||
"layouts/_default/single.html": `{{ define "main" }}<h1>{{ .Title }}</h1>{{ partial "render-sections.html" . }}{{ .Content }}{{ end }}`,
|
||||
"content/landing.md": "---\ntitle: Landing\nsections:\n - type: hero\n title: Hello\n---\n",
|
||||
})
|
||||
var opened []map[string]string
|
||||
merged := map[string]bool{}
|
||||
var closed []string
|
||||
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
const repo = "/api/v1/repos/Us/site"
|
||||
switch {
|
||||
case r.URL.Path == repo:
|
||||
fmt.Fprintf(w, `{"full_name":"Us/site","default_branch":"main","clone_url":%q,"permissions":{"push":true}}`, clone)
|
||||
case r.URL.Path == repo+"/pulls" && r.Method == http.MethodPost:
|
||||
var body map[string]string
|
||||
json.NewDecoder(r.Body).Decode(&body)
|
||||
opened = append(opened, body)
|
||||
fmt.Fprintf(w, `{"number":%d,"state":"open","html_url":"https://gitea.example/pulls/%d"}`, len(opened), len(opened))
|
||||
case strings.HasPrefix(r.URL.Path, repo+"/pulls/") && r.Method == http.MethodPatch:
|
||||
closed = append(closed, strings.TrimPrefix(r.URL.Path, repo+"/pulls/"))
|
||||
fmt.Fprint(w, `{}`)
|
||||
case strings.HasPrefix(r.URL.Path, repo+"/pulls/"):
|
||||
n := strings.TrimPrefix(r.URL.Path, repo+"/pulls/")
|
||||
if merged[n] {
|
||||
fmt.Fprintf(w, `{"number":%s,"state":"closed","merged":true,"merged_at":"2026-11-22T10:00:00Z"}`, n)
|
||||
} else {
|
||||
fmt.Fprintf(w, `{"number":%s,"state":"open"}`, n)
|
||||
}
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer g.Close()
|
||||
var plausibleQuery string
|
||||
pl := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
raw, _ := io.ReadAll(r.Body)
|
||||
plausibleQuery = string(raw)
|
||||
fmt.Fprint(w, `{"results":[{"dimensions":["/for-churches/"],"metrics":[42]},{"dimensions":["/for-churches-b/"],"metrics":[7]}]}`)
|
||||
}))
|
||||
defer pl.Close()
|
||||
composed, _ := json.Marshal(map[string]any{"path": "/for-churches/", "title": "For churches", "description": "d",
|
||||
"sections_yaml": "- type: hero\n title: One bill\n ctas:\n - label: Start\n url: /pricing/\n", "needs": []string{}})
|
||||
newAIClient = func(ai.Config, *secrets.Store) (ai.Client, error) { return cannedAI{string(composed)}, nil }
|
||||
defer func() { newAIClient = ai.New }()
|
||||
|
||||
s, h := newTestServer(t, nil)
|
||||
profile.Save(s.DB, &profile.Profile{Name: "Us", Description: "Widgets.", Websites: []profile.Website{{URL: "https://us.example/", Repo: "Us/site"}}})
|
||||
sig := &signals.Signal{Name: "Churches", Slug: "churches", Status: "testing"}
|
||||
signals.Save(s.DB, sig)
|
||||
sigURL := "/signals/" + strconv.FormatInt(sig.ID, 10)
|
||||
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
|
||||
s.DB.PutJSON(plausibleURLKey, pl.URL)
|
||||
s.Secrets.Set(map[string]string{secrets.PlausibleAPIKey: "pk"})
|
||||
|
||||
body := do(h, http.MethodGet, sigURL, nil, nil).Body.String()
|
||||
if !strings.Contains(body, "No landing page for this signal yet") || !strings.Contains(body, `name="signal" value="`+strconv.FormatInt(sig.ID, 10)) {
|
||||
t.Errorf("before: %s", body)
|
||||
}
|
||||
// Built from the signal's page, pushed with a pull request, merged.
|
||||
postForm(h, "/sites/pages", url.Values{"site": {"0"}, "kind": {"landing"}, "signal": {strconv.FormatInt(sig.ID, 10)}})
|
||||
postForm(h, "/sites/pages/1/pr", url.Values{"title": {"For churches"}, "description": {"d"}, "path": {"/for-churches/"},
|
||||
"sections": {"- type: hero\n title: One bill\n ctas:\n - label: Start\n url: /pricing/\n"}})
|
||||
if len(opened) != 1 {
|
||||
t.Fatalf("no pull request: %v", opened)
|
||||
}
|
||||
merged["1"] = true // and really merged: main moves to the page's branch
|
||||
if out, err := exec.Command("git", "--git-dir", strings.TrimPrefix(clone, "file://"), "update-ref", "refs/heads/main", "refs/heads/supermarket/site-page-1").CombinedOutput(); err != nil {
|
||||
t.Fatalf("merge: %s", out)
|
||||
}
|
||||
body = do(h, http.MethodGet, sigURL, nil, nil).Body.String()
|
||||
if !strings.Contains(body, `href="https://us.example/for-churches/"`) || !strings.Contains(body, ">merged<") || !strings.Contains(body, "<td>42</td>") ||
|
||||
!strings.Contains(body, "Remove from the site") {
|
||||
t.Errorf("live page: %s", body)
|
||||
}
|
||||
if !strings.Contains(plausibleQuery, `"site_id":"us.example"`) || !strings.Contains(plausibleQuery, `"/for-churches/"`) {
|
||||
t.Errorf("plausible query: %s", plausibleQuery)
|
||||
}
|
||||
if body := do(h, http.MethodGet, "/signals", nil, nil).Body.String(); !strings.Contains(body, `title="Landing pages on your website">1 page<`) {
|
||||
t.Errorf("scorecard: %s", body)
|
||||
}
|
||||
|
||||
// A variant: its own address, B, links with its own utm_content.
|
||||
rec := postForm(h, "/sites/pages/1/duplicate", url.Values{})
|
||||
if rec.Header().Get("Location") != "/sites/pages/2" {
|
||||
t.Fatalf("duplicate: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
body = do(h, http.MethodGet, "/sites/pages/2", nil, nil).Body.String()
|
||||
if !strings.Contains(body, `value="/for-churches-b/"`) || !strings.Contains(body, "Variant <strong>B</strong>") ||
|
||||
!strings.Contains(body, "utm_campaign=churches&utm_content=for-churches-b&utm_medium=landing") {
|
||||
t.Errorf("variant: %s", body)
|
||||
}
|
||||
// A second one is C.
|
||||
postForm(h, "/sites/pages/2/duplicate", url.Values{})
|
||||
if p, _ := sites.GetPage(s.DB, 3); p.Path != "/for-churches-c/" || p.Source.Variant != "C" || p.Source.VariantOf != 1 {
|
||||
t.Errorf("third: %+v", p)
|
||||
}
|
||||
if p, _ := sites.GetPage(s.DB, 1); p.Source.Variant != "A" {
|
||||
t.Errorf("original: %+v", p.Source)
|
||||
}
|
||||
// Variant B goes up as a pull request, then is closed without merging.
|
||||
postForm(h, "/sites/pages/2/pr", url.Values{"title": {"For churches B"}, "description": {"d"}, "path": {"/for-churches-b/"},
|
||||
"sections": {"- type: hero\n title: Shorter\n"}})
|
||||
postForm(h, "/sites/pages/2/close", url.Values{"return": {sigURL}})
|
||||
if p, _ := sites.GetPage(s.DB, 2); p.Status != sites.PageClosed || len(closed) != 1 || closed[0] != "2" {
|
||||
t.Errorf("closed: %s %v", p.Status, closed)
|
||||
}
|
||||
|
||||
// Removing the original: a pull request deleting its file.
|
||||
rec = postForm(h, "/sites/pages/1/remove", url.Values{"return": {sigURL}})
|
||||
if rec.Header().Get("Location") != sigURL || len(opened) != 3 {
|
||||
t.Fatalf("remove: %d %s %v", rec.Code, rec.Body.String(), opened)
|
||||
}
|
||||
if pr := opened[2]; pr["head"] != "supermarket/remove-site-page-1" || pr["title"] != "Sites: remove /for-churches/" {
|
||||
t.Errorf("removal pull request: %v", pr)
|
||||
}
|
||||
out, _ := exec.Command("git", "--git-dir", strings.TrimPrefix(clone, "file://"), "ls-tree", "-r", "--name-only", "supermarket/remove-site-page-1").CombinedOutput()
|
||||
if strings.Contains(string(out), "for-churches.md") || !strings.Contains(string(out), "landing.md") {
|
||||
t.Errorf("files on the removal branch: %s", out)
|
||||
}
|
||||
if body := do(h, http.MethodGet, sigURL, nil, nil).Body.String(); !strings.Contains(body, "removal pull request open") {
|
||||
t.Errorf("removing: %s", body)
|
||||
}
|
||||
merged["3"] = true
|
||||
if body := do(h, http.MethodGet, sigURL, nil, nil).Body.String(); !strings.Contains(body, ">removed<") {
|
||||
t.Errorf("removed: %s", body)
|
||||
}
|
||||
if body := do(h, http.MethodGet, "/signals", nil, nil).Body.String(); strings.Contains(body, "Landing pages on your website") {
|
||||
t.Error("still counted as live")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user