| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "html" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "os" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | // Help → Colophon (Paul, 2026-10-26): every open-source project Mapa |
| 15 | // ships, with its licence text. That the inventory is current is make |
| 16 | // credits-check's, in make ci; these hold the page to it. |
| 17 | |
| 18 | const colophonPath = "/help/open-source" |
| 19 | |
| 20 | // inventoryNames is every component credits/notices.json lists. |
| 21 | func inventoryNames(t *testing.T) []string { |
| 22 | t.Helper() |
| 23 | raw, err := os.ReadFile("../mapa/credits/notices.json") |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | var inventory struct{ Components []struct{ Name string } } |
| 28 | if err := json.Unmarshal(raw, &inventory); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | var names []string |
| 32 | for _, component := range inventory.Components { |
| 33 | names = append(names, component.Name) |
| 34 | } |
| 35 | return names |
| 36 | } |
| 37 | |
| 38 | // checkColophon holds a response to the page: 200, its headers, and |
| 39 | // every component once, the ones Mapa is known to ship among them. |
| 40 | func checkColophon(t *testing.T, where string, |
| 41 | response *httptest.ResponseRecorder) string { |
| 42 | t.Helper() |
| 43 | body := response.Body.String() |
| 44 | if response.Code != http.StatusOK { |
| 45 | t.Fatalf("%s: GET %s: %d", where, colophonPath, response.Code) |
| 46 | } |
| 47 | if response.Header().Get("Referrer-Policy") != "strict-origin" || |
| 48 | response.Header().Get("Cache-Control") != "no-store" { |
| 49 | t.Errorf("%s: headers %v", where, response.Header()) |
| 50 | } |
| 51 | names := inventoryNames(t) |
| 52 | for _, want := range []string{"Go", "Lucide and Feather", |
| 53 | "amadan.net/rastrillo/rastrillo", "modernc.org/sqlite", |
| 54 | "prosemirror-model"} { |
| 55 | if !strings.Contains(strings.Join(names, "\n"), want) { |
| 56 | t.Errorf("the inventory does not list %s", want) |
| 57 | } |
| 58 | } |
| 59 | for _, name := range names { |
| 60 | if !strings.Contains(body, "<bdi>"+html.EscapeString(name)+ |
| 61 | "</bdi>") { |
| 62 | t.Errorf("%s: the page does not list %s", where, name) |
| 63 | } |
| 64 | } |
| 65 | if count := strings.Count(body, "<details>"); count != len(names) { |
| 66 | t.Errorf("%s: %d entries, %d in the inventory", where, count, |
| 67 | len(names)) |
| 68 | } |
| 69 | if !strings.Contains(body, "<h1>Colophon</h1>") || |
| 70 | !strings.Contains(body, "/static/credits.") { |
| 71 | t.Errorf("%s: not estilo's credits page", where) |
| 72 | } |
| 73 | return body |
| 74 | } |
| 75 | |
| 76 | // Signed in, the page is in Mapa's shell, and Help lists it after |
| 77 | // Markdown in Mapa. |
| 78 | func TestColophonSignedInIsInTheShell(t *testing.T) { |
| 79 | rig := newRig(t) |
| 80 | body := checkColophon(t, "signed in", rig.get(t, Ana, colophonPath)) |
| 81 | help := strings.Index(body, "Markdown in Mapa") |
| 82 | link := strings.Index(body, `href="`+colophonPath+`"`) |
| 83 | if help < 0 || link < help { |
| 84 | t.Error("Help does not offer the Colophon after Markdown in Mapa") |
| 85 | } |
| 86 | if !strings.Contains(body, "mapa-side") { |
| 87 | t.Error("the signed-in Colophon is not in Mapa's shell") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Signed out, it answers in the door's column, minting no cookie, though the |
| 92 | // door no longer links it (Paul, 2026-11-19: a Colophon link on the sign-in |
| 93 | // door "makes no sense at all"); signed in for real, it is the shell's. |
| 94 | func TestColophonSignedOutAndIn(t *testing.T) { |
| 95 | app := wiredApp(t, t.TempDir(), Ana) |
| 96 | door := send(app.handler, http.MethodGet, "/signin", nil, nil) |
| 97 | if strings.Contains(door.Body.String(), |
| 98 | `href="`+colophonPath+`"`) { |
| 99 | t.Error("the sign-in door links the Colophon") |
| 100 | } |
| 101 | out := send(app.handler, http.MethodGet, colophonPath, nil, nil) |
| 102 | body := checkColophon(t, "signed out", out) |
| 103 | if len(out.Result().Cookies()) != 0 || |
| 104 | strings.Contains(body, "mapa-side") || |
| 105 | !strings.Contains(body, `href="/signin"`) { |
| 106 | t.Error("signed out, the Colophon is not the door's plain page") |
| 107 | } |
| 108 | cookies := signIn(t, app, Ana) |
| 109 | in := send(app.handler, http.MethodGet, colophonPath, nil, cookies) |
| 110 | if !strings.Contains(checkColophon(t, "signed in", in), "mapa-side") { |
| 111 | t.Error("signed in for real, the Colophon is not in the shell") |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // createAs makes a team page through the real route, signed in for real. |
| 116 | func createAs(t *testing.T, app wired, cookies []*http.Cookie, |
| 117 | title string) { |
| 118 | t.Helper() |
| 119 | created := send(app.handler, http.MethodPost, "/pages", |
| 120 | url.Values{"title": {title}, "body": {"One."}}, cookies) |
| 121 | if created.Code != http.StatusSeeOther { |
| 122 | t.Fatalf("creating %q: %d", title, created.Code) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // L7: the Colophon's GET writes nothing, signed in or out. |
| 127 | func TestColophonGetWritesNothing(t *testing.T) { |
| 128 | app := wiredApp(t, t.TempDir(), Ana) |
| 129 | cookies := signIn(t, app, Ana) |
| 130 | createAs(t, app, cookies, "Acme") |
| 131 | tables := &rig{db: app.db} |
| 132 | before := dumpAll(t, tables) |
| 133 | for _, jar := range [][]*http.Cookie{nil, cookies} { |
| 134 | got := send(app.handler, http.MethodGet, colophonPath, nil, jar) |
| 135 | if got.Code != http.StatusOK { |
| 136 | t.Fatalf("GET %s: %d", colophonPath, got.Code) |
| 137 | } |
| 138 | } |
| 139 | if dumpAll(t, tables) != before { |
| 140 | t.Fatal("a Colophon GET wrote to the database") |
| 141 | } |
| 142 | createAs(t, app, cookies, "Beta") |
| 143 | if dumpAll(t, tables) == before { |
| 144 | t.Fatal("the dump is vacuous: a new page did not change it") |
| 145 | } |
| 146 | } |
| 147 | |