package mapatest
import (
"encoding/json"
"html"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
)
// Help → Colophon (Paul, 2026-10-26): every open-source project Mapa
// ships, with its licence text. That the inventory is current is make
// credits-check's, in make ci; these hold the page to it.
const colophonPath = "/help/open-source"
// inventoryNames is every component credits/notices.json lists.
func inventoryNames(t *testing.T) []string {
t.Helper()
raw, err := os.ReadFile("../mapa/credits/notices.json")
if err != nil {
t.Fatal(err)
}
var inventory struct{ Components []struct{ Name string } }
if err := json.Unmarshal(raw, &inventory); err != nil {
t.Fatal(err)
}
var names []string
for _, component := range inventory.Components {
names = append(names, component.Name)
}
return names
}
// checkColophon holds a response to the page: 200, its headers, and
// every component once, the ones Mapa is known to ship among them.
func checkColophon(t *testing.T, where string,
response *httptest.ResponseRecorder) string {
t.Helper()
body := response.Body.String()
if response.Code != http.StatusOK {
t.Fatalf("%s: GET %s: %d", where, colophonPath, response.Code)
}
if response.Header().Get("Referrer-Policy") != "strict-origin" ||
response.Header().Get("Cache-Control") != "no-store" {
t.Errorf("%s: headers %v", where, response.Header())
}
names := inventoryNames(t)
for _, want := range []string{"Go", "Lucide and Feather",
"amadan.net/rastrillo/rastrillo", "modernc.org/sqlite",
"prosemirror-model"} {
if !strings.Contains(strings.Join(names, "\n"), want) {
t.Errorf("the inventory does not list %s", want)
}
}
for _, name := range names {
if !strings.Contains(body, ""+html.EscapeString(name)+
"") {
t.Errorf("%s: the page does not list %s", where, name)
}
}
if count := strings.Count(body, ""); count != len(names) {
t.Errorf("%s: %d entries, %d in the inventory", where, count,
len(names))
}
if !strings.Contains(body, "Colophon
") ||
!strings.Contains(body, "/static/credits.") {
t.Errorf("%s: not estilo's credits page", where)
}
return body
}
// Signed in, the page is in Mapa's shell, and Help lists it after
// Markdown in Mapa.
func TestColophonSignedInIsInTheShell(t *testing.T) {
rig := newRig(t)
body := checkColophon(t, "signed in", rig.get(t, Ana, colophonPath))
help := strings.Index(body, "Markdown in Mapa")
link := strings.Index(body, `href="`+colophonPath+`"`)
if help < 0 || link < help {
t.Error("Help does not offer the Colophon after Markdown in Mapa")
}
if !strings.Contains(body, "mapa-side") {
t.Error("the signed-in Colophon is not in Mapa's shell")
}
}
// Signed out, it answers in the door's column, minting no cookie, though the
// door no longer links it (Paul, 2026-11-19: a Colophon link on the sign-in
// door "makes no sense at all"); signed in for real, it is the shell's.
func TestColophonSignedOutAndIn(t *testing.T) {
app := wiredApp(t, t.TempDir(), Ana)
door := send(app.handler, http.MethodGet, "/signin", nil, nil)
if strings.Contains(door.Body.String(),
`href="`+colophonPath+`"`) {
t.Error("the sign-in door links the Colophon")
}
out := send(app.handler, http.MethodGet, colophonPath, nil, nil)
body := checkColophon(t, "signed out", out)
if len(out.Result().Cookies()) != 0 ||
strings.Contains(body, "mapa-side") ||
!strings.Contains(body, `href="/signin"`) {
t.Error("signed out, the Colophon is not the door's plain page")
}
cookies := signIn(t, app, Ana)
in := send(app.handler, http.MethodGet, colophonPath, nil, cookies)
if !strings.Contains(checkColophon(t, "signed in", in), "mapa-side") {
t.Error("signed in for real, the Colophon is not in the shell")
}
}
// createAs makes a team page through the real route, signed in for real.
func createAs(t *testing.T, app wired, cookies []*http.Cookie,
title string) {
t.Helper()
created := send(app.handler, http.MethodPost, "/pages",
url.Values{"title": {title}, "body": {"One."}}, cookies)
if created.Code != http.StatusSeeOther {
t.Fatalf("creating %q: %d", title, created.Code)
}
}
// L7: the Colophon's GET writes nothing, signed in or out.
func TestColophonGetWritesNothing(t *testing.T) {
app := wiredApp(t, t.TempDir(), Ana)
cookies := signIn(t, app, Ana)
createAs(t, app, cookies, "Acme")
tables := &rig{db: app.db}
before := dumpAll(t, tables)
for _, jar := range [][]*http.Cookie{nil, cookies} {
got := send(app.handler, http.MethodGet, colophonPath, nil, jar)
if got.Code != http.StatusOK {
t.Fatalf("GET %s: %d", colophonPath, got.Code)
}
}
if dumpAll(t, tables) != before {
t.Fatal("a Colophon GET wrote to the database")
}
createAs(t, app, cookies, "Beta")
if dumpAll(t, tables) == before {
t.Fatal("the dump is vacuous: a new page did not change it")
}
}