Phase 7 part 5: add a section to an existing page
Sites → Sections and pages → Add a section, on any page built from sections: choose one of the site's section types and where it goes; the AI writes it from the page, its searches, the profile and the Inventory, with the same rules as a new page. It's reviewed like a page (claims to confirm, to-dos), and the pull request inserts it into the page's file as text, so only that section is added; the build checks it. What to write next suggests a FAQ for a section page found with questions, and a comparison for one searched against something else. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -611,3 +611,84 @@ func TestLeadForms(t *testing.T) {
|
||||
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": `<h2>{{ .title }}</h2>`,
|
||||
"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/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, "<main>")
|
||||
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, `<option value="faq"`) || !strings.Contains(body, "Before section 2 (faq)") {
|
||||
t.Fatalf("form: %s", body)
|
||||
}
|
||||
rec := postForm(h, "/sites/pages/add?site=0", url.Values{"page": {"/freescout/"}, "type": {"faq"}, "at": {"1"}, "notes": {"answer the searches"}})
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("add: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
loc := rec.Header().Get("Location")
|
||||
body = do(h, http.MethodGet, loc, nil, nil).Body.String()
|
||||
for _, want := range []string{"A new <strong>faq</strong> section for", "as its section 2", "Is FreeScout hosted for us?", "Check the answer against the plans"} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("review lacks %q", want)
|
||||
}
|
||||
}
|
||||
if strings.Contains(body, `name="title"`) || strings.Contains(body, "Make a variant") {
|
||||
t.Error("a section shows a page's title field or variants")
|
||||
}
|
||||
rec = postForm(h, loc+"/pr", url.Values{"sections": {"- type: faq\n items:\n - q: Is FreeScout hosted for us?\n a: Yes, on infrastructure you own.\n"}, "needs": {"Check the answer against the plans"}})
|
||||
if len(pulls) != 1 {
|
||||
t.Fatalf("pr: %d %v %s", rec.Code, pulls, rec.Body.String())
|
||||
}
|
||||
if pulls[0]["title"] != "Sites: a new section on /freescout/" || !strings.Contains(pulls[0]["body"], "Only that page's sections list changes") {
|
||||
t.Errorf("pull: %+v", pulls[0])
|
||||
}
|
||||
// The file on the branch: the new section second, the rest untouched.
|
||||
out, err := exec.Command("git", "-C", strings.TrimPrefix(clone, "file://"), "show", pulls[0]["head"]+":content/freescout.md").CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatal(err, string(out))
|
||||
}
|
||||
want := "---\ntitle: FreeScout\nsections:\n - type: hero\n title: Help desk you own\n - type: faq\n items:\n - q: Is FreeScout hosted for us?\n a: Yes, on infrastructure you own.\n - type: faq\n items:\n - q: Old question?\n"
|
||||
if !strings.HasPrefix(string(out), want) {
|
||||
t.Errorf("file on the branch:\n%s", out)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user