package mapatest
import (
"html"
"net/http"
"net/url"
"strings"
"testing"
)
// createPage makes a team page through the real route and returns its
// path.
func (rig *rig) createPage(t *testing.T, who, title, body string) string {
t.Helper()
created := rig.post(t, who, "/pages",
url.Values{"title": {title}, "body": {body}})
if created.Code != http.StatusSeeOther {
t.Fatalf("creating %q: %d\n%s", title, created.Code,
created.Body.String())
}
return created.Header().Get("Location")
}
func (rig *rig) page(t *testing.T, who, path string) string {
t.Helper()
shown := rig.get(t, who, path)
if shown.Code != http.StatusOK {
t.Fatalf("GET %s: %d", path, shown.Code)
}
return shown.Body.String()
}
func TestATeamPageIsCreatedReadAndEditedByEveryone(t *testing.T) {
rig := newRig(t)
acme := rig.createPage(t, Ana, "Acme", "Customer since 2024.")
body := rig.page(t, Bo, acme)
if !strings.Contains(body, "Acme") ||
!strings.Contains(body, "Customer since 2024.") {
t.Fatalf("Bo cannot read Ana's team page:\n%s", body)
}
edit := rig.page(t, Bo, acme+"/edit")
revision := between(edit, `name="revision" value="`, `"`)
saved := rig.post(t, Bo, acme, url.Values{"revision": {revision},
"body": {"Customer since 2024. Renewal in March."}})
if saved.Code != http.StatusSeeOther {
t.Fatalf("Bo's save: %d", saved.Code)
}
if !strings.Contains(rig.page(t, Ana, acme), "Renewal in March.") {
t.Fatal("Ana does not see Bo's edit")
}
stale := rig.post(t, Ana, acme, url.Values{"revision": {revision},
"body": {"overwrite"}})
if stale.Header().Get("Location") != acme+"/conflict" {
t.Fatalf("a stale save to the same block went to %q",
stale.Header().Get("Location"))
}
if !strings.Contains(rig.page(t, Ana, acme+"/conflict"), "overwrite") {
t.Fatal("the conflict view does not show the person's text")
}
}
func TestAPendingLinkOffersToCreateItsPage(t *testing.T) {
rig := newRig(t)
notes := rig.createPage(t, Ana, "Notes", "Ask about [[Halden Robotics]].")
body := rig.page(t, Bo, notes)
href := `href="/new?title=Halden+Robotics" mapa-chip="pending"`
if !strings.Contains(body, href) {
t.Fatalf("no pending link:\n%s", body)
}
offer := rig.page(t, Bo, "/new?title=Halden+Robotics")
if !strings.Contains(offer, `value="Halden Robotics"`) {
t.Fatal("following a pending link does not offer to create it")
}
halden := rig.createPage(t, Bo, "Halden Robotics", "")
if !strings.Contains(rig.page(t, Ana, notes), `href="`+halden+`"`) {
t.Fatal("the pending link did not bind to its new page")
}
if !strings.Contains(rig.page(t, Ana, halden), `>Notes`) {
t.Fatal("Halden Robotics does not show its backlink")
}
}
func TestATakenTitleSaysSoAndLinksToTheHolder(t *testing.T) {
rig := newRig(t)
acme := rig.createPage(t, Ana, "Acme", "")
again := rig.post(t, Bo, "/pages", url.Values{"title": {"ACME"}})
if again.Code != http.StatusUnprocessableEntity ||
!strings.Contains(again.Body.String(), `href="`+acme+`"`) {
t.Fatalf("a taken title: %d", again.Code)
}
date := rig.post(t, Bo, "/pages", url.Values{"title": {"2026-11-12"}})
if date.Code != http.StatusUnprocessableEntity {
t.Fatalf("a date title: %d", date.Code)
}
}
// A body that comes with a refused POST /pages rides along in a hidden
// field (level-up build note 77; Fable's L4 on ux/fixes-1): escaped, so
// nothing in it is markup, and with its lines intact, so sending the
// form again with a free title makes the page with exactly that text.
func TestARefusedNewPageKeepsItsBodyHidden(t *testing.T) {
rig := newRig(t)
rig.createPage(t, Ana, "Acme", "")
body := "Line one & \n\n" +
"\"Quoted\" 'line' three\n- [ ] a task\n"
refused := rig.post(t, Bo, "/pages",
url.Values{"title": {"acme"}, "body": {body}})
shown := refused.Body.String()
if refused.Code != http.StatusUnprocessableEntity {
t.Fatalf("a taken title answered %d", refused.Code)
}
field := between(shown, ``)
if strings.ContainsAny(field, `<>"`) || strings.Contains(shown,
"