oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package mapatest
4
5import (
6 "context"
7 "encoding/json"
8 "slices"
9 "strings"
10 "testing"
11
12 "amadan.net/rastrillo/rastrillo/harness"
13 "github.com/chromedp/cdproto/cdp"
14 "github.com/chromedp/cdproto/network"
15 "github.com/chromedp/chromedp"
16 "github.com/chromedp/chromedp/kb"
17
18 "amadan.net/oficina/mapa/internal/mapa"
19)
20
21// IN5 and IN8 (columns and icons spec §5.3, §9) in a real Chromium: the
22// arrow opens the pages inside at once, with script, by Enter, → and ←,
23// and keeps focus; a page shown twice keeps two list ids and one lit
24// row; opened rows are on the first paint, and mounting moves none of
25// them; axe finds nothing serious.
26
27func TestIN8TheArrowByKeyboard(t *testing.T) {
28 rig, store := browserRig(t, 0)
29 ctx := context.Background()
30 projects, err := store.CreateTeamPage(ctx, Bo, "Projects", "See.")
31 if err != nil {
32 t.Fatal(err)
33 }
34 for _, title := range []string{"Alpha", "Beta"} {
35 if _, err := store.CreateTeamPage(ctx, Bo, title,
36 "In: [[Projects]]\n\nText."); err != nil {
37 t.Fatal(err)
38 }
39 }
40 titledPage(t, rig, store, "Desk", "Work.")
41 viewer, _ := store.Viewer(ctx, Ana, nil)
42 if err := store.SetPin(ctx, viewer, projects.ID, true); err != nil {
43 t.Fatal(err)
44 }
45 drive(t, rig, chromedp.Reload(), chromedp.WaitVisible(mounted),
46 chromedp.Focus(`[mapa-side-arrow][aria-label="Pages inside Projects"]`),
47 chromedp.KeyEvent(kb.Enter),
48 chromedp.WaitVisible(`ul[mapa-side-inside]`))
49 focused := evaluate(t, rig,
50 `document.activeElement.getAttribute("aria-expanded")`)
51 if focused != "true" {
52 t.Fatalf("focus after opening: aria-expanded %q", focused)
53 }
54 if found := axeSerious(t, rig); found != "" {
55 t.Fatalf("axe, a row open:\n%s", found)
56 }
57 drive(t, rig, chromedp.Navigate(rig.Origin+mapa.PagePath(projects)),
58 chromedp.WaitVisible(`ul[mapa-side-inside] [mapa-side-label]`))
59}
60
61// sideState is the sidebar's open lists' ids, its aria-controls, its
62// lit rows that show, and the focused element's name.
63const sideState = `JSON.stringify((function () {
64 var nav = document.querySelector("nav[mapa-side]");
65 var all = function (selector) {
66 return [].slice.call(nav.querySelectorAll(selector));
67 };
68 var ids = all("ul[mapa-side-inside]").map(function (list) {
69 return list.id;
70 });
71 var controls = all("[aria-controls]").map(function (button) {
72 return button.getAttribute("aria-controls");
73 });
74 var lit = all('[aria-current="page"]').filter(function (row) {
75 return !row.hidden;
76 }).length;
77 return { ids: ids, controls: controls, lit: lit,
78 focus: document.activeElement.getAttribute("aria-label") || "" };
79})())`
80
81type sideReading struct {
82 IDs []string `json:"ids"`
83 Controls []string `json:"controls"`
84 Lit int `json:"lit"`
85 Focus string `json:"focus"`
86}
87
88func readSide(t *testing.T, rig *harness.Rig) sideReading {
89 t.Helper()
90 var reading sideReading
91 raw := read(t, rig, chromedp.Evaluate[string](sideState))
92 if err := json.Unmarshal([]byte(raw), &reading); err != nil {
93 t.Fatal(err)
94 }
95 return reading
96}
97
98// IN8 with script: Inner is pinned and inside pinned Outer, so it shows
99// twice. Opening each by → keeps every list id distinct and named by its
100// button, one lit row, and focus on the arrow pressed; ← closes it.
101func TestIN8ArrowKeysAndAPageShownTwice(t *testing.T) {
102 rig, store := browserRig(t, 0)
103 ctx := context.Background()
104 viewer, _ := store.Viewer(ctx, Ana, nil)
105 var leaf mapa.Page
106 for _, each := range []struct{ title, body string }{
107 {"Outer", "Text."}, {"Inner", "In: [[Outer]]"},
108 {"Leaf", "In: [[Inner]]\n\nWords."}} {
109 page, err := store.CreateTeamPage(ctx, Bo, each.title, each.body)
110 if err != nil {
111 t.Fatal(err)
112 }
113 leaf = page
114 if each.title != "Leaf" {
115 if err := store.SetPin(ctx, viewer, page.ID, true); err != nil {
116 t.Fatal(err)
117 }
118 }
119 }
120 drive(t, rig, chromedp.EmulateViewport(1440, 900),
121 chromedp.Navigate(rig.Origin+mapa.PagePath(leaf)),
122 chromedp.WaitVisible(mounted))
123 press := func(label string, key string) {
124 drive(t, rig, chromedp.Focus(`[mapa-side-arrow][aria-label="`+
125 label+`"]`), chromedp.KeyEvent(key))
126 }
127 press("Pages inside Outer", kb.ArrowRight)
128 eventually(t, "Outer open", func() bool {
129 return len(readSide(t, rig).IDs) == 1
130 })
131 // Inner's pinned row is the second arrow named for Inner.
132 drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelectorAll(
133 '[mapa-side-arrow][aria-label="Pages inside Inner"]')[1].focus()`),
134 chromedp.KeyEvent(kb.ArrowRight))
135 eventually(t, "Inner open in both places", func() bool {
136 return len(readSide(t, rig).IDs) == 3
137 })
138 reading := readSide(t, rig)
139 seen := map[string]bool{}
140 for _, id := range reading.IDs {
141 if seen[id] || !slices.Contains(reading.Controls, id) {
142 t.Fatalf("list ids %v, controls %v", reading.IDs, reading.Controls)
143 }
144 seen[id] = true
145 }
146 if reading.Lit != 1 || reading.Focus != "Pages inside Inner" {
147 t.Fatalf("after opening: %+v", reading)
148 }
149 focused := evaluate(t, rig, `String([].indexOf.call(
150 document.querySelectorAll(
151 '[mapa-side-arrow][aria-label="Pages inside Inner"]'),
152 document.activeElement))`)
153 if focused != "1" {
154 t.Fatalf("focus went to Inner's arrow %s, not the one pressed", focused)
155 }
156 drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft))
157 eventually(t, "Inner closed", func() bool {
158 return len(readSide(t, rig).IDs) == 1
159 })
160 if reading := readSide(t, rig); reading.Lit != 1 {
161 t.Fatalf("after closing: %+v", reading)
162 }
163}
164
165// sideBoxes is every sidebar row's and open list's box.
166const sideBoxes = `JSON.stringify([].map.call(
167 document.querySelectorAll("nav[mapa-side] [mapa-side-row], " +
168 "nav[mapa-side] ul[mapa-side-inside]"),
169 function (row) {
170 var box = row.getBoundingClientRect();
171 return [box.left, box.top, box.width, box.height].join(" ");
172 }))`
173
174// IN5: with the cookie set, opened rows are on the server's HTML, and
175// mounting moves none of the sidebar's rows.
176func TestIN5OpenRowsAreOnTheFirstPaint(t *testing.T) {
177 rig, store := browserRig(t, 0)
178 ctx := context.Background()
179 projects, err := store.CreateTeamPage(ctx, Bo, "Projects", "See.")
180 if err != nil {
181 t.Fatal(err)
182 }
183 if _, err := store.CreateTeamPage(ctx, Bo, "Alpha",
184 "In: [[Projects]]\n\nText."); err != nil {
185 t.Fatal(err)
186 }
187 viewer, _ := store.Viewer(ctx, Ana, nil)
188 if err := store.SetPin(ctx, viewer, projects.ID, true); err != nil {
189 t.Fatal(err)
190 }
191 drive(t, rig, onNewDocument(shiftRecorder),
192 openCookie(rig, strings.TrimPrefix(mapa.PagePath(projects), "/p/")))
193 load := func(live bool) string {
194 drive(t, rig, scripts(live), chromedp.EmulateViewport(1440, 900),
195 chromedp.Navigate(rig.Origin+mapa.PagePath(projects)),
196 chromedp.WaitVisible(`ul[mapa-side-inside]`))
197 if live {
198 drive(t, rig, chromedp.WaitVisible(mounted))
199 }
200 return read(t, rig, chromedp.Evaluate[string](sideBoxes))
201 }
202 if before, after := load(false), load(true); before != after {
203 t.Fatalf("the sidebar moved on mounting:\n%s\n%s", before, after)
204 }
205 shifts := evaluate(t, rig, `JSON.stringify(window.__shifts || [])`)
206 if shifts != "[]" {
207 t.Fatalf("layout shifts %s", shifts)
208 }
209}
210
211// openCookie sets the mapa-open cookie.
212func openCookie(rig *harness.Rig, value string) chromedp.Action[chromedp.Void] {
213 return chromedp.Func(func(ctx context.Context,
214 target *chromedp.Target) error {
215 _, err := cdp.Call(ctx, target, network.SetCookie,
216 network.SetCookieParams{Name: "mapa-open", Value: value,
217 URL: rig.Origin + "/"})
218 return err
219 })
220}
221