Files
supermarket/internal/hugorepo/repo_test.go
T
davidandClaude Opus 5.5 438192faba Phase 3 part 4: changes approved one at a time, as Gitea pull requests
Proposals (at most 3 waiting) for pages on page one without clicks and
for the audit's title and description issues: the AI writes the title
(without the site's suffix, within the pixel budget) and description
from the live page and its searches. A yes syncs a working copy of the
site's Gitea repository (token as a header only), finds the page's Hugo
content file, sets title and description in its front matter, builds
with Hugo to check the page comes out as proposed, pushes a branch and
opens a pull request; it never merges. Merged changes are checked on
the live site, then compared before and after from Search Console.
Hugo is in the image. Schema 15.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 08:04:01 -07:00

93 lines
3.0 KiB
Go

package hugorepo
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"supermarket/internal/hugorepo/hugotest"
)
func TestRepo(t *testing.T) {
ctx := context.Background()
clone := hugotest.Site(t)
r := &Repo{Dir: filepath.Join(t.TempDir(), "sites", "o__site"), CloneURL: clone, Token: "secret-token", Branch: "main", Author: "Pat <pat@example.com>"}
if err := r.Sync(ctx); err != nil {
t.Fatal(err)
}
if err := r.Sync(ctx); err != nil { // a second time fetches
t.Fatal(err)
}
for page, want := range map[string]string{
"/": "content/_index.md", "/pricing/": "content/pricing.md", "/blog/first-post/": "content/blog/first-post/index.md",
"/company/": "content/about.md",
} {
if got, err := r.FindContent(page); err != nil || got != want {
t.Errorf("%s: %q %v", page, got, err)
}
}
if _, err := r.FindContent("/nope/"); err == nil {
t.Error("found a file for a page that doesn't exist")
}
if err := r.NewBranch(ctx, "supermarket/seo-1"); err != nil {
t.Fatal(err)
}
if err := r.SetFrontMatter("content/pricing.md", "title", `Pricing: "plans" from $5`); err != nil {
t.Fatal(err)
}
if err := r.SetFrontMatter("content/pricing.md", "description", "New description"); err != nil {
t.Fatal(err)
}
if err := r.SetFrontMatter("content/blog/first-post/index.md", "description", "A post"); err != nil {
t.Fatal(err)
}
raw, _ := os.ReadFile(filepath.Join(r.Dir, "content/pricing.md"))
if string(raw) != "---\ntitle: \"Pricing: \\\"plans\\\" from $5\"\ndescription: \"New description\"\ndraft: false\n---\nOur prices.\n" {
t.Errorf("pricing.md:\n%s", raw)
}
raw, _ = os.ReadFile(filepath.Join(r.Dir, "content/blog/first-post/index.md"))
if !strings.Contains(string(raw), "description = \"A post\"\n+++") {
t.Errorf("toml:\n%s", raw)
}
if v := r.FrontMatterValue("content/pricing.md", "title"); v != `Pricing: "plans" from $5` {
t.Errorf("read back: %q", v)
}
if HugoVersion(ctx) == "" {
t.Skip("no hugo")
}
out, err := r.Build(ctx)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(out)
title, desc, err := BuiltHead(out, "/pricing/")
if err != nil || title != `Pricing: "plans" from $5 · Site` || desc != "New description" {
t.Errorf("built: %q %q %v", title, desc, err)
}
diff, _ := r.Diff(ctx)
if !strings.Contains(diff, "+title:") {
t.Errorf("diff: %s", diff)
}
if err := r.CommitAndPush(ctx, "supermarket/seo-1", "SEO: test"); err != nil {
t.Fatal(err)
}
if out, err := exec.Command("git", "-C", strings.TrimPrefix(clone, "file://"), "log", "--format=%an <%ae>|%s", "supermarket/seo-1", "-1").CombinedOutput(); err != nil ||
strings.TrimSpace(string(out)) != "Pat <pat@example.com>|SEO: test" {
t.Errorf("pushed: %s %v", out, err)
}
cfg, _ := os.ReadFile(filepath.Join(r.Dir, ".git", "config"))
if strings.Contains(string(cfg), "secret-token") {
t.Error("the token is stored in the working copy")
}
// Sync goes back to a clean main.
if err := r.Sync(ctx); err != nil {
t.Fatal(err)
}
if v := r.FrontMatterValue("content/pricing.md", "title"); v != "Pricing" {
t.Errorf("after sync: %q", v)
}
}