package harness
import (
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
)
// The repo landing page is GitHub's front door translated: an About
// aggregate (description, homepage, topics — .quarry/repo.json) and a
// tabbed document panel (README | Contributing | … by root convention,
// curated by the same file). Tabs are URL state, so the whole panel
// works scriptless; a tab that does not exist 404s rather than
// rendering an arbitrary file through a curated strip.
func TestLandingAboutPanelAndDocTabs(t *testing.T) {
store := buildStore(t, "aboutseed")
write := func(name, body string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(filepath.Join(store, name)), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(store, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
zrun(t, store, "add", name)
}
write("CONTRIBUTING.md", "# Contributing\nSend patches.\n")
write(".quarry/repo.json", `{"homepage": "https://example.org", "topics": ["forge", "zit"]}`)
zrun(t, store, "commit", "-m", "about config")
srv := ingestAndServe(t, map[string]string{"alpha": store}, t.TempDir())
get := func(path string) (int, string) {
resp, err := http.Get(srv.URL + path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
return resp.StatusCode, string(b)
}
_, landing := get("/r/alpha")
for _, want := range []string{
`https://example.org`, // homepage link
`forge`, `zit`, // topics
`class="doc-tabs"`, // the strip
`>README<`, `>Contributing<`,
"store aboutseed", // README is the default tab, rendered
} {
if !strings.Contains(landing, want) {
t.Fatalf("landing missing %q:\n%s", want, landing)
}
}
// Tab selection is URL state.
_, contrib := get("/r/alpha?doc=CONTRIBUTING.md")
if !strings.Contains(contrib, "Send patches.") {
t.Fatalf("?doc did not switch the panel:\n%s", contrib)
}
// The strip is curated: not a render-any-file endpoint.
if code, _ := get("/r/alpha?doc=src/main.go"); code != http.StatusNotFound {
t.Fatalf("?doc rendered a non-tab file (status %d)", code)
}
// Deeper tree views stay working views: no About, no tabs.
_, sub := get("/r/alpha/tree/trunk/src")
if strings.Contains(sub, `class="doc-tabs"`) || strings.Contains(sub, "example.org") {
t.Fatalf("the About panel leaked into a subdirectory view:\n%s", sub)
}
}