Files
supermarket/internal/web/sites_test.go
T
davidandClaude Opus 5.5 1152cd448b Phase 5 part 1: read a site's section types and pages
Sites → Sections and pages reads the website's Hugo repository: each
section type with the README's description, the fields in use and the
layout's, the pages using it and a real example; the pages and their
sections; the latest commits. Working copies are now held by one job at a
time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 13:52:35 -07:00

67 lines
2.7 KiB
Go

package web
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"supermarket/internal/hugorepo/hugotest"
"supermarket/internal/profile"
"supermarket/internal/secrets"
)
func TestSitesPage(t *testing.T) {
clone := hugotest.Site(t, map[string]string{
"README.md": "| `hero` | `title`, `subhead` |\n",
"layouts/partials/sections/hero.html": `<h1>{{ .title }}</h1><p>{{ .subhead }}</p>`,
"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
"<summary>hero", "1 use on 1 page", "The README says:</strong> <code>title</code>, <code>subhead</code>",
"<summary>faq", "not used yet", "<summary>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, "<summary>hero") {
t.Errorf("after a failed read: %s", body)
}
}