Phase 6 part 5, step 3: theme updates by pull request

The theme carries a MANIFEST of its files' SHA-256s. A site's Theme card
shows its copy's version against Supermarket's and any files changed by
hand; Update the theme opens a pull request that writes new and changed
files, removes retired ones, rebuilds, and lists what it replaces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 15:30:48 -07:00
co-authored by Claude Opus 5.5
parent 2a605ac265
commit 7da7c5fd9c
9 changed files with 384 additions and 6 deletions
+79
View File
@@ -14,9 +14,11 @@ import (
"supermarket/internal/ai"
"supermarket/internal/hugorepo"
"supermarket/internal/hugorepo/hugotest"
"supermarket/internal/profile"
"supermarket/internal/secrets"
"supermarket/internal/sites"
"supermarket/internal/theme"
)
func TestNewSite(t *testing.T) {
@@ -127,3 +129,80 @@ func TestNewSite(t *testing.T) {
t.Errorf("build check: %s", opened[0]["body"])
}
}
func TestThemeUpdate(t *testing.T) {
if hugorepo.HugoVersion(context.Background()) == "" {
t.Skip("no hugo")
}
// A site with an older copy of the theme: version 0, a file the theme
// no longer has, and the header changed by hand.
files := map[string]string{"hugo.toml": "baseURL = 'https://acme.example/'\ntitle = 'Acme'\ntheme = 'supermarket'\ndisableKinds = ['taxonomy', 'term', 'RSS', 'sitemap']\n",
"content/_index.md": "---\ntitle: Home\nsections:\n - type: hero\n title: Hi\n---\n"}
old := theme.Files()
old["VERSION"] = "0\n"
old["layouts/partials/retired.html"] = "<p>old</p>"
delete(old, "layouts/404.html")
old["MANIFEST"] = theme.Manifest(old)
old["layouts/partials/header.html"] += "<!-- changed by hand -->"
for p, b := range old {
files[theme.Dir+"/"+p] = b
}
clone := hugotest.Site(t, files)
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/acme":
fmt.Fprintf(w, `{"full_name":"Us/acme","default_branch":"main","clone_url":%q,"permissions":{"push":true}}`, clone)
case r.URL.Path == "/api/v1/repos/Us/acme/pulls" && r.Method == http.MethodPost:
var body map[string]string
json.NewDecoder(r.Body).Decode(&body)
opened = append(opened, body)
fmt.Fprint(w, `{"number":4,"state":"open","html_url":"https://gitea.example/pulls/4"}`)
default:
http.NotFound(w, r)
}
}))
defer g.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Acme", Websites: []profile.Website{{URL: "https://acme.example", Repo: "Us/acme"}}})
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", nil, nil).Body.String()
if !strings.Contains(body, "theme version 0; Supermarket has version 1") || !strings.Contains(body, "Changed by hand in the site's copy of the theme: layouts/partials/header.html.") {
t.Errorf("before: %s", body)
}
if rec := postForm(h, "/sites/theme/update", url.Values{"site": {"0"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("update: %d %s", rec.Code, rec.Body.String())
}
if len(opened) != 1 {
t.Fatalf("pull requests: %v", opened)
}
pr := opened[0]
for _, want := range []string{"from version 0 to 1", "**Changed:**\n- `layouts/partials/header.html`", "**Added:**\n- `layouts/404.html`",
"**Removed:**\n- `layouts/partials/retired.html`", "replaced by this update", "Checked with a build"} {
if !strings.Contains(pr["body"], want) {
t.Errorf("pull request lacks %q:\n%s", want, pr["body"])
}
}
if pr["head"] != "supermarket/theme-1" || pr["title"] != "Sites: update the theme to version 1" {
t.Errorf("pull request: %v", pr)
}
bare := strings.TrimPrefix(clone, "file://")
if out, _ := exec.Command("git", "--git-dir", bare, "show", "supermarket/theme-1:"+theme.Dir+"/VERSION").CombinedOutput(); string(out) != "1\n" {
t.Errorf("VERSION on the branch: %q", out)
}
if out, err := exec.Command("git", "--git-dir", bare, "show", "supermarket/theme-1:"+theme.Dir+"/layouts/partials/retired.html").CombinedOutput(); err == nil {
t.Errorf("the retired file is still there: %s", out)
}
// Once merged, the site is up to date; updating again is refused.
exec.Command("git", "--git-dir", bare, "update-ref", "refs/heads/main", "refs/heads/supermarket/theme-1").Run()
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
body = do(h, http.MethodGet, "/sites", nil, nil).Body.String()
if !strings.Contains(body, "version 1: the latest") || strings.Contains(body, "Changed by hand") || !strings.Contains(body, "https://gitea.example/pulls/4") {
t.Errorf("after: %s", body)
}
if body := postForm(h, "/sites/theme/update", url.Values{"site": {"0"}}).Body.String(); !strings.Contains(body, "already has the theme&#39;s version 1") {
t.Errorf("again: %s", body)
}
}