`,
"layouts/partials/sections/faq.html": "{{ $s := . }}{{ range $s.items }}{{ .q }}{{ end }}",
"content/landing.md": "---\ntitle: Landing\nsections:\n - type: hero\n title: Hello\n subhead: There\n - type: gizmo\n---\n",
})
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if 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)
return
}
http.NotFound(w, r)
}))
defer g.Close()
s, h := newTestServer(t, nil)
if body := do(h, http.MethodGet, "/sites", nil, nil).Body.String(); !strings.Contains(body, "Add your website") {
t.Errorf("no sites: %s", body)
}
profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: "https://us.example/", Repo: "Us/site"}}})
body := do(h, http.MethodGet, "/sites", nil, nil).Body.String()
if !strings.Contains(body, "Not read yet") || !strings.Contains(body, "disabled") {
t.Errorf("before reading: %s", body)
}
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
if rec := postForm(h, "/sites/sync", url.Values{"site": {"0"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("sync: %d %s", rec.Code, rec.Body.String())
}
body = do(h, http.MethodGet, "/sites?site=0", nil, nil).Body.String()
for _, want := range []string{"Read from the repository", "branch main", "init", // the commit
"hero", "1 use on 1 page", "The README says: title, subhead",
"faq", "not used yet", "gizmo", "no layout",
`href="https://us.example/landing/"`, "hero · gizmo", "- type: hero\n title: Hello",
"other pages, written in Markdown", "First post"} {
if !strings.Contains(body, want) {
t.Errorf("missing %q in %s", want, body)
}
}
if strings.Contains(body, "error-text") {
t.Errorf("an error: %s", body)
}
// A failed read keeps the last good copy and says why.
g.Close()
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
body = do(h, http.MethodGet, "/sites", nil, nil).Body.String()
if !strings.Contains(body, "The last read failed") || !strings.Contains(body, "hero") {
t.Errorf("after a failed read: %s", body)
}
}
func TestSitePreviews(t *testing.T) {
if hugorepo.HugoVersion(context.Background()) == "" {
t.Skip("no hugo")
}
clone := hugotest.Site(t, map[string]string{
"layouts/_default/single.html": `{{ define "main" }}
{{ .Title }}
Prices{{ .Content }}{{ end }}`,
"static/logo.png": "png",
})
// A second branch, as a pull request would have.
work := t.TempDir()
for _, args := range [][]string{
{"clone", "-q", clone, work},
{"-C", work, "checkout", "-qb", "supermarket/page-1"},
} {
if out, err := exec.Command("git", args...).CombinedOutput(); err != nil {
t.Fatalf("git %v: %s", args, out)
}
}
os.WriteFile(filepath.Join(work, "content", "new.md"), []byte("---\ntitle: Brand new\ndraft: true\n---\nFresh.\n"), 0o644)
for _, args := range [][]string{
{"-C", work, "add", "."},
{"-C", work, "-c", "user.name=T", "-c", "user.email=t@example.com", "commit", "-qm", "Add a page"},
{"-C", work, "push", "-q", "origin", "supermarket/page-1"},
} {
if out, err := exec.Command("git", args...).CombinedOutput(); err != nil {
t.Fatalf("git %v: %s", args, out)
}
}
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if 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)
return
}
http.NotFound(w, r)
}))
defer g.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: "https://us.example/", Repo: "Us/site"}}})
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
if body := do(h, http.MethodGet, "/sites/previews", nil, nil).Body.String(); !strings.Contains(body, "under Sections and pages first") {
t.Errorf("before reading: %s", body)
}
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
body := do(h, http.MethodGet, "/sites/previews", nil, nil).Body.String()
if !strings.Contains(body, "main") || !strings.Contains(body, "supermarket/page-1") ||
!strings.Contains(body, "Add a page") || strings.Contains(body, "Open preview") {
t.Errorf("branches: %s", body)
}
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1/", nil, nil); rec.Code != http.StatusNotFound {
t.Errorf("before building: %d", rec.Code)
}
rec := postForm(h, "/sites/previews/build", url.Values{"site": {"0"}, "branch": {"supermarket/page-1"}})
if rec.Code != http.StatusSeeOther {
t.Fatalf("build: %d %s", rec.Code, rec.Body.String())
}
body = do(h, http.MethodGet, "/sites/previews", nil, nil).Body.String()
if !strings.Contains(body, `href="/preview/0/supermarket-page-1/"`) || strings.Contains(body, "moved on") {
t.Errorf("after building: %s", body)
}
// The draft page is there, its links stay inside the preview, and the
// page is sandboxed.
rec = do(h, http.MethodGet, "/preview/0/supermarket-page-1/new/", nil, nil)
got := rec.Body.String()
if rec.Code != http.StatusOK || !strings.Contains(got, "
Brand new
") || !strings.Contains(got, `href="/preview/0/supermarket-page-1/pricing/"`) ||
!strings.Contains(got, `src="/preview/0/supermarket-page-1/logo.png"`) {
t.Errorf("preview page: %d %s", rec.Code, got)
}
if csp := rec.Header().Get("Content-Security-Policy"); !strings.HasPrefix(csp, "sandbox allow-scripts") || strings.Contains(csp, "allow-same-origin") {
t.Errorf("csp: %s", csp)
}
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1/logo.png", nil, nil); rec.Code != http.StatusOK || rec.Body.String() != "png" {
t.Errorf("static file: %d", rec.Code)
}
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1", nil, nil); rec.Code != http.StatusMovedPermanently {
t.Errorf("no slash: %d", rec.Code)
}
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1/../../../app.db", nil, nil); rec.Code == http.StatusOK {
t.Errorf("escaped the preview: %d", rec.Code)
}
// The working copy is back on the published branch.
if _, err := os.Stat(filepath.Join(s.siteDir("Us/site"), "content", "new.md")); err == nil {
t.Error("the working copy was left on the branch")
}
// A post from a preview page (its origin is "null") is refused.
if rec := do(h, http.MethodPost, "/sites/previews/delete", strings.NewReader("site=0&branch=supermarket/page-1"),
map[string]string{"Origin": "null", "Content-Type": "application/x-www-form-urlencoded"}); rec.Code != http.StatusForbidden {
t.Errorf("post from a preview: %d", rec.Code)
}
// The branch moves on: the preview says so.
os.WriteFile(filepath.Join(work, "content", "new.md"), []byte("---\ntitle: Newer\n---\n"), 0o644)
exec.Command("git", "-C", work, "-c", "user.name=T", "-c", "user.email=t@example.com", "commit", "-qam", "Again").Run()
exec.Command("git", "-C", work, "push", "-q", "origin", "supermarket/page-1").Run()
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
if body := do(h, http.MethodGet, "/sites/previews", nil, nil).Body.String(); !strings.Contains(body, "moved on") {
t.Errorf("stale: %s", body)
}
postForm(h, "/sites/previews/delete", url.Values{"site": {"0"}, "branch": {"supermarket/page-1"}})
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1/new/", nil, nil); rec.Code != http.StatusNotFound {
t.Errorf("after deleting: %d", rec.Code)
}
if _, err := os.Stat(s.previewDir("Us/site", "supermarket-page-1")); err == nil {
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": `
{{ .title }}
{{ range .ctas }}{{ .label }}{{ end }}`,
"layouts/partials/sections/faq.html": "{{ $s := . }}{{ range $s.items }}
{{ .q }}
{{ .a }}{{ end }}",
"layouts/_default/single.html": `{{ define "main" }}
{{ .Title }}
{{ 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 }()
live := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/for-churches/" {
http.NotFound(w, r)
return
}
fmt.Fprint(w, `For churches · Us
For churches
Run your church on one bill
`)
}))
defer live.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Description: "Widgets.", Websites: []profile.Website{{URL: live.URL + "/", 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, "
For churches
") || !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, ">live<") || !strings.Contains(body, "On the live site since") || !strings.Contains(body, "Live: the title and the words match.") ||
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)
}
}
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": `
{{ .title }}
{{ range .ctas }}{{ .label }}{{ end }}`,
"layouts/_default/single.html": `{{ define "main" }}
{{ .Title }}
{{ 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 }()
gone := false
live := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/for-churches/" || gone {
http.NotFound(w, r)
return
}
fmt.Fprint(w, `For churches
One bill
`)
}))
defer live.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Description: "Widgets.", Websites: []profile.Website{{URL: live.URL + "/", Repo: "Us/site", PlausibleSite: "us.example"}}})
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="`+live.URL+`/for-churches/"`) || !strings.Contains(body, ">live<") || !strings.Contains(body, "
42
") ||
!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 B") ||
!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, "removal merged, still on the site") {
t.Errorf("removal merged: %s", body)
}
// The site's deploy takes it off; the publishing page's check sees it.
gone = true
p, _ := sites.GetPage(s.DB, 1)
p.CheckedAt = time.Time{}
sites.SavePage(s.DB, p)
if body := do(h, http.MethodGet, "/sites/pages/1", nil, nil).Body.String(); !strings.Contains(body, "Off the live site since") || !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")
}
}
func TestSitesPublishing(t *testing.T) {
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/repos/Us/site/pulls":
if r.URL.Query().Get("state") != "closed" {
t.Errorf("query: %s", r.URL.RawQuery)
}
fmt.Fprint(w, `[
{"number":5,"state":"closed","merged":true,"merged_at":"2026-11-22T09:00:00Z","title":"Sites: landing page /for-churches/","head":{"ref":"supermarket/site-page-1"},"html_url":"https://g/5"},
{"number":4,"state":"closed","merged":false,"title":"Abandoned","head":{"ref":"x"}},
{"number":3,"state":"closed","merged":true,"merged_at":"2026-11-21T09:00:00Z","title":"Fix the footer","head":{"ref":"footer"},"user":{"login":"carly"},"html_url":"https://g/3"}]`)
case "/api/v1/repos/Us/site/pulls/5":
fmt.Fprint(w, `{"number":5,"state":"closed","merged":true,"merged_at":"2026-11-22T09:00:00Z"}`)
default:
http.NotFound(w, r)
}
}))
defer g.Close()
live := httptest.NewServer(http.NotFoundHandler())
defer live.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: live.URL + "/", Repo: "Us/site"}}})
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
p := &sites.SitePage{Site: live.URL, Repo: "Us/site", Kind: "landing", Path: "/for-churches/", Title: "For churches",
Status: sites.PagePR, PRNumber: 5, Branch: "supermarket/site-page-1", Sections: "- type: hero\n title: Hi\n"}
if err := sites.SavePage(s.DB, p); err != nil {
t.Fatal(err)
}
body := do(h, http.MethodGet, "/sites/publishing", nil, nil).Body.String()
for _, want := range []string{
// The merge was noticed, and the page isn't live yet.
`For churches`, "Not on the site yet (it answers 404).",
`href="https://g/5"`, "Sites: landing page /for-churches/", "
Supermarket
", "merged, not live yet",
"Fix the footer", "carly",
} {
if !strings.Contains(body, want) {
t.Errorf("missing %q in %s", want, body)
}
}
if strings.Contains(body, "Abandoned") {
t.Error("an unmerged pull request is listed")
}
// The page editor offers a check now.
if body := do(h, http.MethodGet, "/sites/pages/1", nil, nil).Body.String(); !strings.Contains(body, "Check the live site now") {
t.Errorf("page: %s", body)
}
}
func TestLeadForms(t *testing.T) {
if hugorepo.HugoVersion(context.Background()) == "" {
t.Skip("no hugo")
}
partial := "{{/*\n A lead form.\n*/}}\n\n" +
"\n"
clone := hugotest.Site(t, map[string]string{
"hugo.toml": "baseURL = 'https://site.example/'\ntitle = 'Site'\ndisableKinds = ['taxonomy', 'term', 'RSS', 'sitemap']\n\n[params.leads]\n action = \"\"\n email = \"info@site.example\"\n",
"layouts/partials/render-sections.html": `{{ range .Params.sections }}{{ partial (printf "sections/%s.html" .type) . }}{{ end }}`,
"layouts/partials/sections/form.html": partial,
"layouts/_default/single.html": `{{ define "main" }}
{{ .Title }}
{{ partial "render-sections.html" . }}{{ end }}`,
"content/churches.md": "---\ntitle: Churches\nsections:\n - type: form\n subject: Church\n---\n",
})
var opened []map[string]string
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)
opened = append(opened, body)
fmt.Fprint(w, `{"number":9,"state":"open","html_url":"https://gitea.example/pulls/9"}`)
case r.URL.Path == "/api/v1/repos/Us/site/pulls/9":
fmt.Fprint(w, `{"number":9,"state":"open"}`)
default:
http.NotFound(w, r)
}
}))
defer g.Close()
crm := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodOptions {
t.Errorf("the test sent a %s", r.Method)
}
w.Header().Set("Access-Control-Allow-Origin", "*")
}))
defer crm.Close()
captureClient = crm.Client
defer func() { captureClient = sites.TestClient }()
capture := crm.URL + "/api/v1/LeadCapture/key123"
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: "https://site.example", Repo: "Us/site"}}})
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
body := do(h, http.MethodGet, "/sites/forms", nil, nil).Body.String()
if !strings.Contains(body, "The forms open an email") || !strings.Contains(body, "Pages with a form: /churches/") ||
!strings.Contains(body, "First Name, Last Name, Email, Account Name, Phone, Description") {
t.Errorf("before: %s", body)
}
if body := postForm(h, "/sites/forms/save", url.Values{"site": {"0"}, "capture": {"https://crm.example/api/v1/Lead"}}).Body.String(); !strings.Contains(body, "isn't a Lead Capture address") {
t.Errorf("bad address: %s", body)
}
if body := postForm(h, "/sites/forms/save", url.Values{"site": {"0"}, "capture": {capture}}).Body.String(); !strings.Contains(body, "Saved and tested") {
t.Errorf("save: %s", body)
}
if rec := postForm(h, "/sites/forms/connect", url.Values{"site": {"0"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("connect: %d %s", rec.Code, rec.Body.String())
}
if len(opened) != 1 || opened[0]["head"] != "supermarket/lead-forms" || !strings.Contains(opened[0]["body"], "/churches/ loads the script and posts to the Lead Capture") {
t.Fatalf("pull request: %v", opened)
}
show := func(path string) string {
out, err := exec.Command("git", "--git-dir", strings.TrimPrefix(clone, "file://"), "show", "supermarket/lead-forms:"+path).CombinedOutput()
if err != nil {
t.Fatalf("%s: %s", path, out)
}
return string(out)
}
if cfg := show("hugo.toml"); !strings.Contains(cfg, " action = \""+capture+"\"\n email = \"info@site.example\"") {
t.Errorf("hugo.toml: %s", cfg)
}
if p := show("layouts/partials/sections/form.html"); !strings.Contains(p, "window.leadPost(form, data)") || !strings.Contains(p, "supermarket-leads.js") {
t.Errorf("partial: %s", p)
}
if js := show("static/js/supermarket-leads.js"); !strings.Contains(js, "window.leadPost = function") {
t.Errorf("script: %s", js)
}
if body := do(h, http.MethodGet, "/sites/forms", nil, nil).Body.String(); !strings.Contains(body, "A pull request is open") || !strings.Contains(body, "https://gitea.example/pulls/9") {
t.Errorf("after: %s", body)
}
// Once merged and read again, the page says they're connected.
exec.Command("git", "--git-dir", strings.TrimPrefix(clone, "file://"), "update-ref", "refs/heads/main", "refs/heads/supermarket/lead-forms").Run()
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
if body := do(h, http.MethodGet, "/sites/forms", nil, nil).Body.String(); !strings.Contains(body, "Connected: the forms on https://site.example send their answers to EspoCRM") {
t.Errorf("connected: %s", body)
}
}
// A section added to a page already on the site: written from the site's
// types, reviewed like a page, and pushed as a pull request that only adds
// the section to the page's file.
func TestAddASection(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": `
{{ .title }}
`,
"layouts/partials/sections/faq.html": "{{ $s := . }}{{ range $s.items }}
{{ .q }}
{{ .a }}{{ end }}",
"layouts/_default/single.html": `{{ define "main" }}
{{ .Title }}
{{ partial "render-sections.html" . }}{{ .Content }}{{ end }}`,
"content/freescout.md": "---\ntitle: FreeScout\nsections:\n - type: hero\n title: Help desk you own\n - type: faq\n items:\n - q: Old question?\n a: Old answer.\n---\n",
})
var pulls []map[string]string
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, body)
fmt.Fprint(w, `{"number":8,"state":"open","html_url":"https://gitea.example/Us/site/pulls/8"}`)
default:
http.NotFound(w, r)
}
}))
defer g.Close()
composed, _ := json.Marshal(map[string]any{
"sections_yaml": "- type: faq\n items:\n - q: Is FreeScout hosted for us?\n a: Yes, on infrastructure you own.\n",
"needs": []string{"Check the answer against the plans"}})
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://site.example/", Repo: "Us/site"}}})
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
postForm(h, "/sites/sync?site=0", nil) // read the repository
body := do(h, http.MethodGet, "/sites?site=0", nil, nil).Body.String()
if !strings.Contains(body, "/sites/pages/add?site=0&page=%2ffreescout%2f") {
i := strings.Index(body, "")
t.Fatalf("no Add a section link: %s", body[i:min(len(body), i+3000)])
}
body = do(h, http.MethodGet, "/sites/pages/add?site=0&page=/freescout/", nil, nil).Body.String()
if !strings.Contains(body, `