oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "strings"
8 "testing"
9)
10
11// getWith is a GET carrying cookies, as a browser that set them sends.
12func (rig *rig) getWith(who, target string,
13 cookies ...*http.Cookie) string {
14 recorder := httptest.NewRecorder()
15 request := httptest.NewRequest(http.MethodGet, target, nil)
16 for _, cookie := range cookies {
17 request.AddCookie(cookie)
18 }
19 rig.built.As(who).ServeHTTP(recorder, request)
20 return recorder.Body.String()
21}
22
23// The Links toggle says what the panel is (review M2): expanded when it
24// is open, collapsed when the cookie closed it, from first paint.
25func TestTheLinksToggleSaysWhetherThePanelIsOpen(t *testing.T) {
26 rig := newRig(t)
27 acme := rig.createPage(t, Ana, "Acme", "One.")
28 toggle := func(page string) string {
29 return between(page, `aria-controls="mapa-links"`, ">")
30 }
31 open := rig.getWith(Ana, acme)
32 if !strings.Contains(toggle(open), `disabled`) ||
33 !strings.Contains(between(open, `aria-expanded="`,
34 `" aria-controls="mapa-links"`), "true") {
35 t.Fatalf("the open panel's toggle: %q", toggle(open))
36 }
37 closed := rig.getWith(Ana, acme,
38 &http.Cookie{Name: "mapa-links", Value: "closed"})
39 collapsed := `aria-expanded="false" aria-controls="mapa-links"`
40 if !strings.Contains(closed, collapsed) {
41 t.Fatal("the closed panel's toggle does not say collapsed")
42 }
43 if !strings.Contains(closed, `id="mapa-links" aria-label="Links" hidden`) {
44 t.Fatal("the closed panel is drawn")
45 }
46}
47
48// The view preferences are cookies, never rows (L7): neither form
49// changes a table, whatever it is sent.
50func TestViewFormsWriteNoRow(t *testing.T) {
51 rig := newRig(t)
52 rig.createPage(t, Ana, "Acme", "One.")
53 before := dumpAll(t, rig)
54 for _, route := range []string{"/view/sidebar", "/view/links"} {
55 for _, state := range []string{"open", "rail", "closed", ""} {
56 answer := rig.post(t, Ana, route, url.Values{
57 "state": {state}, "return": {"/pages"}})
58 if answer.Code != http.StatusSeeOther {
59 t.Fatalf("%s %q answered %d", route, state, answer.Code)
60 }
61 }
62 }
63 if dumpAll(t, rig) != before {
64 t.Fatal("a view form wrote to the database")
65 }
66}
67
68// Help → Markdown in Mapa is a page (review M1), and every row of the
69// Help menu leads somewhere that answers.
70func TestHelpMarkdownIsAPage(t *testing.T) {
71 rig := newRig(t)
72 page := rig.page(t, Ana, "/help/markdown")
73 for _, words := range []string{"Markdown in Mapa", "[[Page title]]",
74 "[[2026-11-12]]", "no escapes"} {
75 if !strings.Contains(page, words) {
76 t.Errorf("the Markdown help lacks %q", words)
77 }
78 }
79 for _, href := range strings.Split(between(page, `id="mapa-menu-help"`,
80 "</details>"), `href="`)[1:] {
81 target, _, _ := strings.Cut(href, `"`)
82 if answer := rig.get(t, Ana, target); answer.Code != 200 {
83 t.Errorf("Help's %s answered %d", target, answer.Code)
84 }
85 }
86}
87