| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "net/url" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // IN3, IN4, IN6 and IN7 (columns and icons spec §5, §9), from the |
| 13 | // outside: the arrow on a pinned row, its rows, what scope shows, what |
| 14 | // is written (nothing), the scriptless form, cycles and the caps. |
| 15 | |
| 16 | // pinned is a team page made by Ana and pinned by Bo. |
| 17 | func pinned(t *testing.T, rig *rig, title, body string) string { |
| 18 | t.Helper() |
| 19 | path := rig.createPage(t, Ana, title, body) |
| 20 | rig.post(t, Bo, path+"/pin", url.Values{"pin": {"1"}}) |
| 21 | return path |
| 22 | } |
| 23 | |
| 24 | // opened is a page as Bo sees it with the rows of ids open. |
| 25 | func opened(t *testing.T, rig *rig, path string, ids ...string) string { |
| 26 | t.Helper() |
| 27 | request, _ := http.NewRequest(http.MethodGet, path, nil) |
| 28 | request.AddCookie(&http.Cookie{Name: "mapa-open", |
| 29 | Value: strings.Join(ids, " ")}) |
| 30 | recorder := httptest.NewRecorder() |
| 31 | rig.built.As(Bo).ServeHTTP(recorder, request) |
| 32 | return recorder.Body.String() |
| 33 | } |
| 34 | |
| 35 | func TestIN6AnArrowOpensThePagesInside(t *testing.T) { |
| 36 | rig := newRig(t) |
| 37 | projects := pinned(t, rig, "Projects", "See [[Beta]].") |
| 38 | rig.createPage(t, Ana, "Alpha", "In: [[Projects]]\n\nA.") |
| 39 | rig.createPage(t, Ana, "Beta", "In: [[Projects]]\n\nB.") |
| 40 | closed := rig.page(t, Bo, projects) |
| 41 | if !strings.Contains(closed, `aria-label="Pages inside Projects"`) || |
| 42 | !strings.Contains(closed, `aria-expanded="false"`) { |
| 43 | t.Fatal("the pinned row has no closed arrow") |
| 44 | } |
| 45 | answer := rig.post(t, Bo, "/view/open", url.Values{ |
| 46 | "id": {strings.TrimPrefix(projects, "/p/")}, "open": {"1"}, |
| 47 | "return": {projects}}) |
| 48 | if answer.Code != http.StatusSeeOther || |
| 49 | !strings.Contains(answer.Header().Get("Set-Cookie"), "mapa-open=") { |
| 50 | t.Fatalf("the arrow's form: %d", answer.Code) |
| 51 | } |
| 52 | open := opened(t, rig, projects, strings.TrimPrefix(projects, "/p/")) |
| 53 | beta := strings.Index(open, "mapa-side-label>Beta<") |
| 54 | alpha := strings.Index(open, "mapa-side-label>Alpha<") |
| 55 | if !strings.Contains(open, `aria-expanded="true"`) || beta < 0 || |
| 56 | alpha < beta { |
| 57 | t.Fatal("the open row does not list Beta then Alpha") |
| 58 | } |
| 59 | answer = rig.post(t, Bo, "/view/open", url.Values{"id": {"x"}, |
| 60 | "open": {"1"}, "return": {"https://example.com/"}}) |
| 61 | if answer.Header().Get("Location") != "/" { |
| 62 | t.Fatal("a foreign return was followed") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestIN3PrivateInLinesAreNoPageInside(t *testing.T) { |
| 67 | rig := newRig(t) |
| 68 | projects := pinned(t, rig, "Projects", "Text.") |
| 69 | id := strings.TrimPrefix(projects, "/p/") |
| 70 | before := opened(t, rig, projects, id) |
| 71 | rig.post(t, Ana, "/scratch", url.Values{"body": {"In: [[Projects]]"}}) |
| 72 | if after := opened(t, rig, projects, id); after != before { |
| 73 | t.Fatal("Ana's private note changed Bo's sidebar") |
| 74 | } |
| 75 | if strings.Contains(before, "Pages inside Projects") { |
| 76 | t.Fatal("an arrow on a page with nothing inside") |
| 77 | } |
| 78 | // /p/{id}/inside is the app's 404 for a private page, as for one |
| 79 | // that does not exist, to its owner too. |
| 80 | scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}). |
| 81 | Header().Get("Location") |
| 82 | for _, who := range []string{Ana, Bo} { |
| 83 | if code := rig.get(t, who, scratch+"/inside").Code; scratch == "" || |
| 84 | code != http.StatusNotFound { |
| 85 | t.Fatalf("a private page's inside list for %s: %d", who, code) |
| 86 | } |
| 87 | } |
| 88 | if code := rig.get(t, Bo, "/p/nothere/inside").Code; code != |
| 89 | http.StatusNotFound { |
| 90 | t.Fatalf("a missing page's inside list: %d", code) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestIN4OpeningWritesNothing(t *testing.T) { |
| 95 | rig := newRig(t) |
| 96 | projects := pinned(t, rig, "Projects", "Text.") |
| 97 | rig.createPage(t, Ana, "Alpha", "In: [[Projects]]") |
| 98 | before := snapshot(t, rig) |
| 99 | rig.post(t, Bo, "/view/open", url.Values{"id": {"x"}, "open": {"1"}}) |
| 100 | if code := rig.get(t, Bo, projects+"/inside").Code; code != http.StatusOK { |
| 101 | t.Fatalf("all pages inside: %d", code) |
| 102 | } |
| 103 | if snapshot(t, rig) != before { |
| 104 | t.Fatal("opening a row or the list wrote a row") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestIN7CyclesAndCaps(t *testing.T) { |
| 109 | rig := newRig(t) |
| 110 | one := pinned(t, rig, "One", "In: [[Two]]") |
| 111 | two := rig.createPage(t, Ana, "Two", "In: [[One]]") |
| 112 | page := opened(t, rig, one, strings.TrimPrefix(one, "/p/"), |
| 113 | strings.TrimPrefix(two, "/p/")) |
| 114 | // One's row opens Two, whose row lists One again without an arrow. |
| 115 | if strings.Count(page, "mapa-side-label>Two<") != 1 || |
| 116 | strings.Count(page, "mapa-side-label>One<") != 2 || |
| 117 | strings.Count(page, `<button type="submit" mapa-side-arrow`) != 2 { |
| 118 | t.Fatal("a cycle opened more than once") |
| 119 | } |
| 120 | big := pinned(t, rig, "Big", "Text.") |
| 121 | for at := range 51 { |
| 122 | rig.createPage(t, Ana, "Child "+strings.Repeat("x", at+1), |
| 123 | "In: [[Big]]") |
| 124 | } |
| 125 | page = opened(t, rig, big, strings.TrimPrefix(big, "/p/")) |
| 126 | if strings.Count(page, `mapa-side-label>Child x`) != 50 || |
| 127 | !strings.Contains(page, big+`/inside">See all pages inside (51)`) { |
| 128 | t.Fatal("51 pages inside are not 50 rows and a link") |
| 129 | } |
| 130 | if all := rig.page(t, Bo, big+"/inside"); strings.Count(all, |
| 131 | ">Child x") != 51 { |
| 132 | t.Fatal("the list of all pages inside is not all of them") |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // IN7, depth: a chain six deep, every row open, indents no further than |
| 137 | // four levels. |
| 138 | func TestIN7IndentsStopAtFourLevels(t *testing.T) { |
| 139 | rig := newRig(t) |
| 140 | top := pinned(t, rig, "Level 0", "Top.") |
| 141 | ids := []string{strings.TrimPrefix(top, "/p/")} |
| 142 | for level := 1; level <= 6; level++ { |
| 143 | path := rig.createPage(t, Ana, "Level "+string(rune('0'+level)), |
| 144 | "In: [[Level "+string(rune('0'+level-1))+"]]") |
| 145 | ids = append(ids, strings.TrimPrefix(path, "/p/")) |
| 146 | } |
| 147 | page := opened(t, rig, top, ids...) |
| 148 | if !strings.Contains(page, "mapa-side-label>Level 6<") { |
| 149 | t.Fatal("the chain did not open to its end") |
| 150 | } |
| 151 | for _, depth := range regexp.MustCompile(`data-depth="(\d+)"`). |
| 152 | FindAllStringSubmatch(page, -1) { |
| 153 | if depth[1] > "4" { |
| 154 | t.Fatalf("a row indented %s levels", depth[1]) |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // IN7, the total cap: five open rows of 45 pages each draw 200 rows in |
| 160 | // all; the row the cap cuts links to all of its pages. |
| 161 | func TestIN7TheSidebarDrawsAtMost200OpenRows(t *testing.T) { |
| 162 | rig := newRig(t) |
| 163 | var ids []string |
| 164 | for parent := range 5 { |
| 165 | title := "Shelf " + string(rune('A'+parent)) |
| 166 | path := pinned(t, rig, title, "Text.") |
| 167 | ids = append(ids, strings.TrimPrefix(path, "/p/")) |
| 168 | for at := range 45 { |
| 169 | rig.createPage(t, Ana, title+" book "+strings.Repeat("x", at+1), |
| 170 | "In: [["+title+"]]") |
| 171 | } |
| 172 | } |
| 173 | page := sideOf(opened(t, rig, "/pages", ids...)) |
| 174 | books := regexp.MustCompile(`mapa-side-label>Shelf . book x`) |
| 175 | if got := len(books.FindAllString(page, -1)); got != 200 { |
| 176 | t.Fatalf("%d open rows drawn, want 200", got) |
| 177 | } |
| 178 | if strings.Count(page, `/inside">See all pages inside (45)`) != 1 { |
| 179 | t.Fatal("the row the cap cuts has no link to all its pages") |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // IN7 and IN8 without script: a page shown twice (pinned, and inside |
| 184 | // another pinned page) gives its two open lists two ids, every |
| 185 | // aria-controls names a list that is there, and exactly one row is lit. |
| 186 | func TestIN8APageShownTwiceHasTwoListsAndOneLight(t *testing.T) { |
| 187 | rig := newRig(t) |
| 188 | outer := pinned(t, rig, "Outer", "Text.") |
| 189 | inner := pinned(t, rig, "Inner", "In: [[Outer]]") |
| 190 | leaf := rig.createPage(t, Ana, "Leaf", "In: [[Inner]]") |
| 191 | page := opened(t, rig, leaf, strings.TrimPrefix(outer, "/p/"), |
| 192 | strings.TrimPrefix(inner, "/p/")) |
| 193 | side := sideOf(page) |
| 194 | if strings.Count(side, "mapa-side-label>Leaf<") != 2 { |
| 195 | t.Fatal("Leaf is not shown in both of Inner's lists") |
| 196 | } |
| 197 | controls := regexp.MustCompile(`aria-controls="([^"]+)"`). |
| 198 | FindAllStringSubmatch(side, -1) |
| 199 | seen := map[string]bool{} |
| 200 | for _, each := range controls { |
| 201 | if seen[each[1]] || !strings.Contains(side, `id="`+each[1]+`"`) { |
| 202 | t.Fatalf("aria-controls %q repeats or names nothing", each[1]) |
| 203 | } |
| 204 | seen[each[1]] = true |
| 205 | } |
| 206 | if len(seen) != 3 { |
| 207 | t.Fatalf("%d open lists, want 3", len(seen)) |
| 208 | } |
| 209 | // The rail's lit copy of a place is hidden (build note 80). |
| 210 | lit := strings.Count(side, `aria-current="page"`) - |
| 211 | strings.Count(side, `aria-current="page" hidden`) |
| 212 | if lit != 1 { |
| 213 | t.Fatalf("%d rows lit, want 1", lit) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // sideOf is a page's sidebar. |
| 218 | func sideOf(page string) string { |
| 219 | side := page[strings.Index(page, "<nav mapa-side"):] |
| 220 | return side[:strings.Index(side, "</nav>")] |
| 221 | } |
| 222 | |