package mapatest import ( "net/http" "net/http/httptest" "net/url" "regexp" "strings" "testing" ) // IN3, IN4, IN6 and IN7 (columns and icons spec §5, §9), from the // outside: the arrow on a pinned row, its rows, what scope shows, what // is written (nothing), the scriptless form, cycles and the caps. // pinned is a team page made by Ana and pinned by Bo. func pinned(t *testing.T, rig *rig, title, body string) string { t.Helper() path := rig.createPage(t, Ana, title, body) rig.post(t, Bo, path+"/pin", url.Values{"pin": {"1"}}) return path } // opened is a page as Bo sees it with the rows of ids open. func opened(t *testing.T, rig *rig, path string, ids ...string) string { t.Helper() request, _ := http.NewRequest(http.MethodGet, path, nil) request.AddCookie(&http.Cookie{Name: "mapa-open", Value: strings.Join(ids, " ")}) recorder := httptest.NewRecorder() rig.built.As(Bo).ServeHTTP(recorder, request) return recorder.Body.String() } func TestIN6AnArrowOpensThePagesInside(t *testing.T) { rig := newRig(t) projects := pinned(t, rig, "Projects", "See [[Beta]].") rig.createPage(t, Ana, "Alpha", "In: [[Projects]]\n\nA.") rig.createPage(t, Ana, "Beta", "In: [[Projects]]\n\nB.") closed := rig.page(t, Bo, projects) if !strings.Contains(closed, `aria-label="Pages inside Projects"`) || !strings.Contains(closed, `aria-expanded="false"`) { t.Fatal("the pinned row has no closed arrow") } answer := rig.post(t, Bo, "/view/open", url.Values{ "id": {strings.TrimPrefix(projects, "/p/")}, "open": {"1"}, "return": {projects}}) if answer.Code != http.StatusSeeOther || !strings.Contains(answer.Header().Get("Set-Cookie"), "mapa-open=") { t.Fatalf("the arrow's form: %d", answer.Code) } open := opened(t, rig, projects, strings.TrimPrefix(projects, "/p/")) beta := strings.Index(open, "mapa-side-label>Beta<") alpha := strings.Index(open, "mapa-side-label>Alpha<") if !strings.Contains(open, `aria-expanded="true"`) || beta < 0 || alpha < beta { t.Fatal("the open row does not list Beta then Alpha") } answer = rig.post(t, Bo, "/view/open", url.Values{"id": {"x"}, "open": {"1"}, "return": {"https://example.com/"}}) if answer.Header().Get("Location") != "/" { t.Fatal("a foreign return was followed") } } func TestIN3PrivateInLinesAreNoPageInside(t *testing.T) { rig := newRig(t) projects := pinned(t, rig, "Projects", "Text.") id := strings.TrimPrefix(projects, "/p/") before := opened(t, rig, projects, id) rig.post(t, Ana, "/scratch", url.Values{"body": {"In: [[Projects]]"}}) if after := opened(t, rig, projects, id); after != before { t.Fatal("Ana's private note changed Bo's sidebar") } if strings.Contains(before, "Pages inside Projects") { t.Fatal("an arrow on a page with nothing inside") } // /p/{id}/inside is the app's 404 for a private page, as for one // that does not exist, to its owner too. scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}). Header().Get("Location") for _, who := range []string{Ana, Bo} { if code := rig.get(t, who, scratch+"/inside").Code; scratch == "" || code != http.StatusNotFound { t.Fatalf("a private page's inside list for %s: %d", who, code) } } if code := rig.get(t, Bo, "/p/nothere/inside").Code; code != http.StatusNotFound { t.Fatalf("a missing page's inside list: %d", code) } } func TestIN4OpeningWritesNothing(t *testing.T) { rig := newRig(t) projects := pinned(t, rig, "Projects", "Text.") rig.createPage(t, Ana, "Alpha", "In: [[Projects]]") before := snapshot(t, rig) rig.post(t, Bo, "/view/open", url.Values{"id": {"x"}, "open": {"1"}}) if code := rig.get(t, Bo, projects+"/inside").Code; code != http.StatusOK { t.Fatalf("all pages inside: %d", code) } if snapshot(t, rig) != before { t.Fatal("opening a row or the list wrote a row") } } func TestIN7CyclesAndCaps(t *testing.T) { rig := newRig(t) one := pinned(t, rig, "One", "In: [[Two]]") two := rig.createPage(t, Ana, "Two", "In: [[One]]") page := opened(t, rig, one, strings.TrimPrefix(one, "/p/"), strings.TrimPrefix(two, "/p/")) // One's row opens Two, whose row lists One again without an arrow. if strings.Count(page, "mapa-side-label>Two<") != 1 || strings.Count(page, "mapa-side-label>One<") != 2 || strings.Count(page, `