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 "context"
5 "fmt"
6 "net/http"
7 "net/http/httptest"
8 "net/url"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/mapa/internal/mapa"
14)
15
16// Paul's ruling of 2026-11-16 on the sidebar (build note 80), as the
17// server draws it, so as the screen is with scripts off: Recently
18// edited is team pages only, the Scratch screen lists scratch pages,
19// a pinned note keeps a lock, and exactly one row has the full
20// highlight. The browser half is sidebar_browser_test.go.
21
22const secretLine = "Zanzibar secret first line"
23
24// sideNav is the sidebar's markup on a page.
25func sideNav(html string) string {
26 return between(html, "<nav mapa-side", "</nav>")
27}
28
29// barNav is estilo's nav in the bar, the Menu sheet's rows below 800px.
30func barNav(html string) string {
31 return between(html, "estilo-bar-nav", "</nav>")
32}
33
34// withSide is a GET carrying the mapa-sidebar cookie, "" for none.
35func (rig *rig) withSide(t *testing.T, who, path, state string) string {
36 t.Helper()
37 request := httptest.NewRequest(http.MethodGet, path, nil)
38 if state != "" {
39 request.AddCookie(&http.Cookie{Name: "mapa-sidebar", Value: state})
40 }
41 recorder := httptest.NewRecorder()
42 rig.built.As(who).ServeHTTP(recorder, request)
43 if recorder.Code != http.StatusOK {
44 t.Fatalf("GET %s: %d", path, recorder.Code)
45 }
46 return recorder.Body.String()
47}
48
49// lit is the label of the one sidebar row with aria-current that the
50// open sidebar (rail false) or the rail shows, failing unless there is
51// exactly one. CSS decides what is shown: the open sidebar hides a
52// place's lit copy (mapa-side-holds="rail"); the rail hides the page
53// rows (mapa-side-page) and a place's unlit copy.
54func lit(t *testing.T, side string, rail bool) string {
55 t.Helper()
56 var labels []string
57 for _, row := range strings.Split(side, "<a ")[1:] {
58 tag, _, _ := strings.Cut(row, ">")
59 hidden := strings.Contains(tag, `mapa-side-holds="rail"`)
60 if rail {
61 hidden = strings.Contains(tag, "mapa-side-page") ||
62 strings.Contains(tag, `mapa-side-holds="open"`)
63 }
64 if !hidden && strings.Contains(tag, `aria-current="page"`) {
65 labels = append(labels, between(row, "<span mapa-side-label>",
66 "<"))
67 }
68 }
69 if len(labels) != 1 {
70 t.Fatalf("rail %v: %d rows have the full highlight %v:\n%s", rail,
71 len(labels), labels, side)
72 }
73 return labels[0]
74}
75
76// unstyled is how many rows carry aria-current with no CSS at all,
77// where only the hidden attribute hides a row (review low 4).
78func unstyled(side string) int {
79 count := 0
80 for _, row := range strings.Split(side, "<a ")[1:] {
81 tag, _, _ := strings.Cut(row, ">")
82 if strings.Contains(tag, `aria-current="page"`) &&
83 !strings.Contains(tag, " hidden") {
84 count++
85 }
86 }
87 return count
88}
89
90// writeNotes gives Ana a team page, a scratch page and today's note,
91// each written by her, and answers their paths.
92func writeNotes(t *testing.T, rig *rig) (team, scratch, day string) {
93 t.Helper()
94 team = rig.createPage(t, Ana, "Quarterly plan", "Team text.")
95 made := rig.post(t, Ana, "/scratch",
96 url.Values{"body": {secretLine + "\n\nMore."}})
97 scratch = made.Header().Get("Location")
98 day = "/d/" + time.Now().UTC().Format(time.DateOnly)
99 if saved := rig.post(t, Ana, day, url.Values{
100 "body": {"Daily words."}}); saved.Code != http.StatusSeeOther {
101 t.Fatalf("writing today: %d", saved.Code)
102 }
103 return team, scratch, day
104}
105
106func TestRecentlyEditedListsTeamPagesOnly(t *testing.T) {
107 rig := newRig(t)
108 team, scratch, day := writeNotes(t, rig)
109 for _, path := range []string{"/pages", team, scratch, day, "/"} {
110 for _, state := range []string{"", "open", "rail"} {
111 html := rig.withSide(t, Ana, path, state)
112 side := sideNav(html)
113 recent := between(side, "Recently edited", "mapa-side-foot")
114 if !strings.Contains(recent, "Quarterly plan") {
115 t.Fatalf("%s: the team page is not in Recently edited",
116 path)
117 }
118 if strings.Contains(side, secretLine) ||
119 strings.Contains(barNav(html), secretLine) {
120 t.Fatalf("%s (%q): a scratch page's first line is in the "+
121 "sidebar or the Menu sheet", path, state)
122 }
123 if strings.Contains(recent, `href="`+scratch+`"`) ||
124 strings.Contains(recent, `href="`+day+`"`) {
125 t.Fatalf("%s: a note of Ana's own is in Recently edited:"+
126 "\n%s", path, recent)
127 }
128 }
129 }
130}
131
132func TestTheScratchScreenListsYourScratchPages(t *testing.T) {
133 rig := newRig(t)
134 rig.post(t, Ana, "/scratch", url.Values{"body": {"Older note"}})
135 rig.post(t, Ana, "/scratch", url.Values{"body": {"Newer note"}})
136 html := rig.page(t, Ana, "/scratch")
137 list := between(html, `aria-label="Your scratch pages"`, "</ul>")
138 if !strings.Contains(html, "Only you see these") ||
139 strings.Index(list, "Newer note") < 0 ||
140 strings.Index(list, "Newer note") > strings.Index(list,
141 "Older note") {
142 t.Fatalf("the Scratch screen:\n%s", list)
143 }
144}
145
146func TestAPinnedNoteStaysWithALock(t *testing.T) {
147 rig := newRig(t)
148 _, scratch, _ := writeNotes(t, rig)
149 rig.post(t, Ana, scratch+"/pin", url.Values{"pin": {"1"}})
150 pinned := between(sideNav(rig.page(t, Ana, "/pages")), "Pinned",
151 "Recently edited")
152 if !strings.Contains(pinned, secretLine) ||
153 !strings.Contains(pinned, "mapa-side-lock") ||
154 !strings.Contains(pinned, "only you see this") {
155 t.Fatalf("the pinned scratch page:\n%s", pinned)
156 }
157}
158
159// One full highlight for each kind of page, open and in the rail.
160func TestOneFullHighlightForEachPage(t *testing.T) {
161 rig := newRig(t)
162 team, scratch, day := writeNotes(t, rig)
163 pinned := rig.createPage(t, Ana, "Pinned plan", "Pinned.")
164 rig.post(t, Ana, pinned+"/pin", url.Values{"pin": {"1"}})
165 theirs := rig.createPage(t, Bo, "Bo's page", "Not Ana's.")
166 pinnedNote := rig.post(t, Ana, "/scratch",
167 url.Values{"body": {"Pinned note"}}).Header().Get("Location")
168 rig.post(t, Ana, pinnedNote+"/pin", url.Values{"pin": {"1"}})
169 cases := []struct{ path, open, rail string }{
170 {scratch, "Scratch", "Scratch"},
171 {"/", "Today", "Today"},
172 {day, "Today", "Today"},
173 {"/d/2025-01-06", "Today", "Today"},
174 {team, "Quarterly plan", "All pages"},
175 {pinned, "Pinned plan", "All pages"},
176 {theirs, "All pages", "All pages"},
177 {pinnedNote, "Pinned note", "Scratch"},
178 }
179 for _, each := range cases {
180 html := rig.page(t, Ana, each.path)
181 for rail, want := range map[bool]string{false: each.open,
182 true: each.rail} {
183 if got := lit(t, sideNav(html), rail); got != want {
184 t.Errorf("%s (rail %v): lit %q, want %q", each.path, rail,
185 got, want)
186 }
187 }
188 if got := unstyled(sideNav(html)); got != 1 {
189 t.Errorf("%s: with no CSS %d rows are lit", each.path, got)
190 }
191 // The Menu sheet has places only: the page's place is lit.
192 if got := strings.Count(barNav(html),
193 `aria-current="page"`); got != 1 {
194 t.Errorf("%s: the Menu sheet lights %d rows", each.path, got)
195 }
196 }
197}
198
199// Recently edited still holds six team pages after more private writes
200// than the window of events it first reads (review M1): the team filter
201// is in the query, so private writes cannot push team pages out of it.
202func TestRecentlyEditedOutlastsManyPrivateWrites(t *testing.T) {
203 rig := newRig(t)
204 for number := 1; number <= 7; number++ {
205 rig.createPage(t, Ana, fmt.Sprintf("Team page %d", number), "Text.")
206 }
207 for number := 0; number < 210; number++ {
208 rig.post(t, Ana, "/scratch",
209 url.Values{"body": {fmt.Sprintf("Note %d", number)}})
210 }
211 recent := between(sideNav(rig.page(t, Ana, "/pages")),
212 "Recently edited", "mapa-side-foot")
213 if got := strings.Count(recent, "mapa-side-page"); got != 6 {
214 t.Fatalf("Recently edited holds %d rows:\n%s", got, recent)
215 }
216 for number := 2; number <= 7; number++ {
217 if !strings.Contains(recent, fmt.Sprintf("Team page %d<", number)) {
218 t.Fatalf("Team page %d is not in Recently edited", number)
219 }
220 }
221}
222
223// recentRows is the titles in Ana's Recently edited, in order.
224func recentRows(t *testing.T, rig *rig) []string {
225 t.Helper()
226 recent := between(sideNav(rig.page(t, Ana, "/pages")),
227 "Recently edited", "mapa-side-foot")
228 var titles []string
229 for _, row := range strings.Split(recent, "<span mapa-side-label>")[1:] {
230 title, _, _ := strings.Cut(row, "<")
231 titles = append(titles, title)
232 }
233 return titles
234}
235
236// Six team pages, then 900 edits to one: the other five are still
237// there (review round 6 L2). A window of the newest events, capped,
238// held only the one page.
239func TestRecentlyEditedOutlastsManyEditsToOnePage(t *testing.T) {
240 rig := newRig(t)
241 ctx := context.Background()
242 var heavy mapa.Page
243 for number := 1; number <= 6; number++ {
244 page, err := rig.built.Store.CreateTeamPage(ctx, Ana,
245 fmt.Sprintf("Team page %d", number), "Text.")
246 if err != nil {
247 t.Fatal(err)
248 }
249 heavy = page
250 }
251 revision := heavy.Revision
252 for number := range 900 {
253 if _, err := rig.built.Store.Commit(ctx, Ana, heavy.ID, revision,
254 fmt.Sprintf("Edit %d.", number)); err != nil {
255 t.Fatal(err)
256 }
257 err := rig.db.G.Raw("SELECT revision FROM pages WHERE id = ?",
258 heavy.ID).Scan(&revision).Error
259 if err != nil {
260 t.Fatal(err)
261 }
262 }
263 got := strings.Join(recentRows(t, rig), ", ")
264 if got != "Team page 6, Team page 5, Team page 4, Team page 3, "+
265 "Team page 2, Team page 1" {
266 t.Fatalf("Recently edited: %s", got)
267 }
268}
269
270// Deleted team pages among the recent writes leave six live rows, and
271// no deleted title (review round 6 L3).
272func TestRecentlyEditedLeavesOutDeletedPages(t *testing.T) {
273 rig := newRig(t)
274 ctx := context.Background()
275 for number := 1; number <= 9; number++ {
276 page, err := rig.built.Store.CreateTeamPage(ctx, Ana,
277 fmt.Sprintf("Team page %d", number), "Text.")
278 if err != nil {
279 t.Fatal(err)
280 }
281 if number%3 == 0 {
282 if err := rig.built.Store.Delete(ctx, Ana, page.ID); err != nil {
283 t.Fatal(err)
284 }
285 }
286 }
287 got := strings.Join(recentRows(t, rig), ", ")
288 if got != "Team page 8, Team page 7, Team page 5, Team page 4, "+
289 "Team page 2, Team page 1" {
290 t.Fatalf("Recently edited: %s", got)
291 }
292}
293