package mapatest import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" ) // getWith is a GET carrying cookies, as a browser that set them sends. func (rig *rig) getWith(who, target string, cookies ...*http.Cookie) string { recorder := httptest.NewRecorder() request := httptest.NewRequest(http.MethodGet, target, nil) for _, cookie := range cookies { request.AddCookie(cookie) } rig.built.As(who).ServeHTTP(recorder, request) return recorder.Body.String() } // The Links toggle says what the panel is (review M2): expanded when it // is open, collapsed when the cookie closed it, from first paint. func TestTheLinksToggleSaysWhetherThePanelIsOpen(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.") toggle := func(page string) string { return between(page, `aria-controls="mapa-links"`, ">") } open := rig.getWith(Ana, acme) if !strings.Contains(toggle(open), `disabled`) || !strings.Contains(between(open, `aria-expanded="`, `" aria-controls="mapa-links"`), "true") { t.Fatalf("the open panel's toggle: %q", toggle(open)) } closed := rig.getWith(Ana, acme, &http.Cookie{Name: "mapa-links", Value: "closed"}) collapsed := `aria-expanded="false" aria-controls="mapa-links"` if !strings.Contains(closed, collapsed) { t.Fatal("the closed panel's toggle does not say collapsed") } if !strings.Contains(closed, `id="mapa-links" aria-label="Links" hidden`) { t.Fatal("the closed panel is drawn") } } // The view preferences are cookies, never rows (L7): neither form // changes a table, whatever it is sent. func TestViewFormsWriteNoRow(t *testing.T) { rig := newRig(t) rig.createPage(t, Ana, "Acme", "One.") before := dumpAll(t, rig) for _, route := range []string{"/view/sidebar", "/view/links"} { for _, state := range []string{"open", "rail", "closed", ""} { answer := rig.post(t, Ana, route, url.Values{ "state": {state}, "return": {"/pages"}}) if answer.Code != http.StatusSeeOther { t.Fatalf("%s %q answered %d", route, state, answer.Code) } } } if dumpAll(t, rig) != before { t.Fatal("a view form wrote to the database") } } // Help → Markdown in Mapa is a page (review M1), and every row of the // Help menu leads somewhere that answers. func TestHelpMarkdownIsAPage(t *testing.T) { rig := newRig(t) page := rig.page(t, Ana, "/help/markdown") for _, words := range []string{"Markdown in Mapa", "[[Page title]]", "[[2026-11-12]]", "no escapes"} { if !strings.Contains(page, words) { t.Errorf("the Markdown help lacks %q", words) } } for _, href := range strings.Split(between(page, `id="mapa-menu-help"`, ""), `href="`)[1:] { target, _, _ := strings.Cut(href, `"`) if answer := rig.get(t, Ana, target); answer.Code != 200 { t.Errorf("Help's %s answered %d", target, answer.Code) } } }