Phase 5 part 3: build pages from a site's own sections
Sites → New pages: the AI composes a landing, product, alternative, comparison or blog page as the site's own sections, from the profile and optionally a product, competitor, buying signal or SEO brief. Code checks every section type, warns on unknown fields and missing pages, tags a signal's links with its campaign and finds experience claims. Pages are edited here, pushed to their own branch with a preview, and opened as pull requests after the claims are confirmed. Schema 22. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -9,9 +10,13 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/signals"
|
||||
|
||||
"supermarket/internal/hugorepo"
|
||||
"supermarket/internal/hugorepo/hugotest"
|
||||
"supermarket/internal/profile"
|
||||
@@ -177,3 +182,126 @@ func TestSitePreviews(t *testing.T) {
|
||||
t.Error("the files are still there")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSitePageBuilder(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/partials/sections/faq.html": "{{ $s := . }}{{ range $s.items }}<h3>{{ .q }}</h3>{{ .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 ctas:\n - label: Go\n url: /pricing/\n style: primary\n---\n",
|
||||
})
|
||||
var pulls []url.Values
|
||||
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/api/v1/repos/Us/site":
|
||||
fmt.Fprintf(w, `{"full_name":"Us/site","default_branch":"main","clone_url":%q,"permissions":{"push":true}}`, clone)
|
||||
case r.URL.Path == "/api/v1/repos/Us/site/pulls" && r.Method == http.MethodPost:
|
||||
var body map[string]string
|
||||
json.NewDecoder(r.Body).Decode(&body)
|
||||
pulls = append(pulls, url.Values{"head": {body["head"]}, "base": {body["base"]}, "title": {body["title"]}, "body": {body["body"]}})
|
||||
fmt.Fprint(w, `{"number":7,"state":"open","html_url":"https://gitea.example/Us/site/pulls/7"}`)
|
||||
case r.URL.Path == "/api/v1/repos/Us/site/pulls/7":
|
||||
fmt.Fprint(w, `{"number":7,"state":"closed","merged":true,"merged_at":"2026-11-22T10:00:00Z","html_url":"https://gitea.example/Us/site/pulls/7"}`)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer g.Close()
|
||||
composed, _ := json.Marshal(map[string]any{"path": "/for-churches/", "title": "For churches", "description": "One bill for your church.",
|
||||
"sections_yaml": "- type: hero\n title: Run your church on one bill\n ctas:\n - label: Start\n url: /pricing/\n style: primary\n- type: faq\n items:\n - q: Does it work?\n a: We tested it with our own team.\n",
|
||||
"needs": []string{"A real quote from a church"}})
|
||||
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"}
|
||||
if err := signals.Save(s.DB, sig); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body := do(h, http.MethodGet, "/sites/pages", nil, nil).Body.String(); !strings.Contains(body, "Gitea token") || !strings.Contains(body, "No pages built yet") {
|
||||
t.Errorf("before: %s", body)
|
||||
}
|
||||
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
|
||||
|
||||
rec := postForm(h, "/sites/pages", url.Values{"site": {"0"}, "kind": {"landing"}, "signal": {strconv.FormatInt(sig.ID, 10)}, "notes": {"for churches"}})
|
||||
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/sites/pages/1" {
|
||||
t.Fatalf("new: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
body := do(h, http.MethodGet, "/sites/pages/1", nil, nil).Body.String()
|
||||
for _, want := range []string{"For churches", "/for-churches/", "the buying signal “Churches”", "url: /pricing/?utm_campaign=churches&utm_medium=landing",
|
||||
"A real quote from a church", `name="confirm" value="We tested it with our own team."`, "Open a pull request"} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("page lacks %q", want)
|
||||
}
|
||||
}
|
||||
if body := do(h, http.MethodGet, "/sites/pages", nil, nil).Body.String(); !strings.Contains(body, `href="/sites/pages/1">For churches`) {
|
||||
t.Errorf("list: %s", body)
|
||||
}
|
||||
|
||||
form := func(sections string, confirm ...string) url.Values {
|
||||
v := url.Values{"title": {"For churches"}, "description": {"One bill."}, "path": {"/for-churches/"}, "notes": {"for churches"},
|
||||
"sections": {sections}, "needs": {"A real quote from a church"}}
|
||||
v["confirm"] = confirm
|
||||
return v
|
||||
}
|
||||
// A section type the site doesn't have is caught on saving.
|
||||
postForm(h, "/sites/pages/1/save", form("- type: gizmo\n"))
|
||||
if body := do(h, http.MethodGet, "/sites/pages/1", nil, nil).Body.String(); !strings.Contains(body, `the site has no "gizmo" section`) {
|
||||
t.Errorf("bad type: %s", body)
|
||||
}
|
||||
good := "- type: hero\n title: Run your church on one bill\n ctas:\n - label: Start\n url: /pricing/\n- type: faq\n items:\n - q: Does it work?\n a: We tested it with our own team.\n"
|
||||
// …and pushing refuses until it's fixed.
|
||||
if body := postForm(h, "/sites/pages/1/preview", form("- type: gizmo\n")).Body.String(); !strings.Contains(body, "Fix this before pushing") {
|
||||
t.Errorf("pushed a bad page: %s", body)
|
||||
}
|
||||
|
||||
// Push and preview: the page is on its branch, built, and previewed.
|
||||
body = postForm(h, "/sites/pages/1/preview", form(good)).Body.String()
|
||||
if !strings.Contains(body, "Pushed to the branch supermarket/site-page-1. The preview is ready.") ||
|
||||
!strings.Contains(body, `href="/preview/0/supermarket-site-page-1/for-churches/"`) || len(pulls) != 0 {
|
||||
t.Errorf("preview: %s", body)
|
||||
}
|
||||
got := do(h, http.MethodGet, "/preview/0/supermarket-site-page-1/for-churches/", nil, nil).Body.String()
|
||||
if !strings.Contains(got, "<h1>For churches</h1>") || !strings.Contains(got, "Run your church on one bill") ||
|
||||
!strings.Contains(got, `href="/preview/0/supermarket-site-page-1/pricing/?utm_campaign=churches&utm_medium=landing"`) {
|
||||
t.Errorf("previewed page: %s", got)
|
||||
}
|
||||
|
||||
// The claim must be confirmed before the pull request.
|
||||
if body := postForm(h, "/sites/pages/1/pr", form(good)).Body.String(); !strings.Contains(body, "confirm or remove this claim first") || len(pulls) != 0 {
|
||||
t.Errorf("unconfirmed: %s", body)
|
||||
}
|
||||
body = postForm(h, "/sites/pages/1/pr", form(good, "We tested it with our own team.")).Body.String()
|
||||
if !strings.Contains(body, "The pull request is open.") || len(pulls) != 1 {
|
||||
t.Fatalf("pr: %s", body)
|
||||
}
|
||||
pr := pulls[0]
|
||||
if pr.Get("head") != "supermarket/site-page-1" || pr.Get("base") != "main" || pr.Get("title") != "Sites: landing page /for-churches/" ||
|
||||
!strings.Contains(pr.Get("body"), "We tested it with our own team.") || !strings.Contains(pr.Get("body"), "A real quote from a church") ||
|
||||
!strings.Contains(pr.Get("body"), "Checked with a build") {
|
||||
t.Errorf("pull request: %v", pr)
|
||||
}
|
||||
// The file is on the branch, with the address fixed.
|
||||
out, err := exec.Command("git", "--git-dir", strings.TrimPrefix(clone, "file://"), "show", "supermarket/site-page-1:content/for-churches.md").CombinedOutput()
|
||||
if err != nil || !strings.HasPrefix(string(out), "---\ntitle: For churches\n") || !strings.Contains(string(out), " - type: faq\n") {
|
||||
t.Errorf("the file: %v %s", err, out)
|
||||
}
|
||||
|
||||
// Gitea says it's merged: the page shows it, and can't be edited.
|
||||
body = do(h, http.MethodGet, "/sites/pages/1", nil, nil).Body.String()
|
||||
if !strings.Contains(body, ">merged<") || strings.Contains(body, `name="sections"`) {
|
||||
t.Errorf("merged: %s", body)
|
||||
}
|
||||
if body := postForm(h, "/sites/pages/1/save", form(good)).Body.String(); !strings.Contains(body, "can't be changed") {
|
||||
t.Errorf("edit after merge: %s", body)
|
||||
}
|
||||
postForm(h, "/sites/pages/1/delete", nil)
|
||||
if rec := do(h, http.MethodGet, "/sites/pages/1", nil, nil); rec.Code != http.StatusNotFound {
|
||||
t.Errorf("after delete: %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user