| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/rastrillo/rastrillo/harness" |
| 12 | "github.com/chromedp/chromedp" |
| 13 | "github.com/chromedp/chromedp/kb" |
| 14 | ) |
| 15 | |
| 16 | // L10, accessibility (level-up spec §6.12, §7), for the page editor: |
| 17 | // axe-core finds nothing serious; the editor is a named multi-line |
| 18 | // textbox; task boxes are named buttons; the keyboard reaches [[, /, |
| 19 | // the toolbar and Make team page, and focus comes back each time; the |
| 20 | // page reflows at 320px. ⌘K, the calendar, History and Restore have |
| 21 | // their own keyboard walks (palette_browser_test.go, |
| 22 | // browser_history_test.go). |
| 23 | |
| 24 | // axe is axe-core, vendored (testdata/axe/README): the gate fetches |
| 25 | // nothing. |
| 26 | // |
| 27 | //go:embed testdata/axe/axe.min.js |
| 28 | var axe string |
| 29 | |
| 30 | // axeSerious runs axe over the page and lists what is serious or |
| 31 | // critical, by rule and the first element it names. |
| 32 | func axeSerious(t *testing.T, rig *harness.Rig) string { |
| 33 | t.Helper() |
| 34 | drive(t, rig, chromedp.Evaluate[chromedp.Void](axe)) |
| 35 | return read(t, rig, chromedp.Evaluate[string](`axe.run(document, { |
| 36 | resultTypes: ["violations"] |
| 37 | }).then(function (result) { |
| 38 | return result.violations.filter(function (rule) { |
| 39 | return rule.impact === "serious" || rule.impact === "critical"; |
| 40 | }).map(function (rule) { |
| 41 | return rule.id + " " + rule.nodes.map(function (node) { |
| 42 | return node.target.join(" "); |
| 43 | }).slice(0, 3).join(", "); |
| 44 | }).join("\n"); |
| 45 | })`, chromedp.EvalAwaitPromise)) |
| 46 | } |
| 47 | |
| 48 | const accessBody = "# Plan\n\nMet [[Access Target]] and [[Not yet]] " + |
| 49 | "on [[2026-11-12]]. ^intro\n\n- [ ] Send Tom the summary\n- [x] Book" + |
| 50 | "\n\n> Quoted\n\n```\ncode\n```\n\n---\n\nEnd." |
| 51 | |
| 52 | func TestL10TheEditorPassesAxe(t *testing.T) { |
| 53 | rig, store := browserRig(t, 0) |
| 54 | if _, err := store.CreateTeamPage(context.Background(), Bo, |
| 55 | "Access Target", "Here."); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | teamPage(t, rig, store, accessBody) |
| 59 | if found := axeSerious(t, rig); found != "" { |
| 60 | t.Fatalf("axe, the page in the editor:\n%s", found) |
| 61 | } |
| 62 | role := read(t, rig, chromedp.Evaluate[string](`(function (dom) { |
| 63 | return [dom.getAttribute("role"), dom.getAttribute("aria-multiline"), |
| 64 | dom.getAttribute("aria-label")].join("|"); |
| 65 | })(mapaEditor.view.dom)`)) |
| 66 | if !strings.HasPrefix(role, "textbox|true|Page text, Plan") { |
| 67 | t.Fatalf("the editor is %q", role) |
| 68 | } |
| 69 | boxes := read(t, rig, chromedp.Evaluate[string](`Array.prototype.map.call( |
| 70 | document.querySelectorAll("button[mapa-task-box]"), function (box) { |
| 71 | return box.getAttribute("aria-label") + "=" + |
| 72 | box.getAttribute("aria-pressed"); |
| 73 | }).join("|")`)) |
| 74 | if boxes != "Not done: Send Tom the summary=false|Done: Book=true" { |
| 75 | t.Fatalf("the task boxes are %q", boxes) |
| 76 | } |
| 77 | drive(t, rig, chromedp.Click(`button[mapa-task-box]`)) |
| 78 | if got := typed(t, rig); !strings.Contains(got, |
| 79 | "- [x] Send Tom the summary") { |
| 80 | t.Fatalf("the box did not toggle: %q", got) |
| 81 | } |
| 82 | drive(t, rig, caretAt(0), chromedp.KeyEvent(" [[Acc")) |
| 83 | listShows(t, rig, "Access Target") |
| 84 | if found := axeSerious(t, rig); found != "" { |
| 85 | t.Fatalf("axe, with the [[ list open:\n%s", found) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // focusInEditor is whether focus is in the page's text. |
| 90 | const focusInEditor = `mapaEditor.view.dom.contains(document.activeElement)` |
| 91 | |
| 92 | func TestL10TheKeyboardWalkReturnsFocus(t *testing.T) { |
| 93 | rig, _ := browserRig(t, 0) |
| 94 | emptyDay(t, rig, "2026-11-08") |
| 95 | drive(t, rig, chromedp.KeyEvent("Keep. [[No")) |
| 96 | listShows(t, rig, "Link “No”") |
| 97 | drive(t, rig, chromedp.KeyEvent(kb.Escape)) |
| 98 | if options(t, rig) != "" || !read(t, rig, |
| 99 | chromedp.Evaluate[bool](focusInEditor)) { |
| 100 | t.Fatal("Esc did not close [[ in place") |
| 101 | } |
| 102 | drive(t, rig, enter(), chromedp.KeyEvent("/quo")) |
| 103 | listShows(t, rig, "Quote") |
| 104 | drive(t, rig, chromedp.KeyEvent(kb.Escape), |
| 105 | chromedp.KeyEvent(kb.Backspace+kb.Backspace+kb.Backspace+ |
| 106 | kb.Backspace), |
| 107 | chromedp.KeyEvent("Share me."), |
| 108 | chromedp.KeyEvent(kb.Home, chromedp.KeyModifiers( |
| 109 | chromedp.ModifierShift)), |
| 110 | chromedp.KeyEvent(kb.ArrowUp, chromedp.KeyModifiers( |
| 111 | chromedp.ModifierShift)), |
| 112 | chromedp.WaitVisible(`[mapa-bubble]`), |
| 113 | chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers( |
| 114 | chromedp.ModifierAlt))) |
| 115 | active := read(t, rig, chromedp.Evaluate[string]( |
| 116 | `document.activeElement.getAttribute("aria-label")`)) |
| 117 | if active != "Bold" { |
| 118 | t.Fatalf("⌥F10 went to %q", active) |
| 119 | } |
| 120 | drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft), enter(), |
| 121 | chromedp.WaitVisible(`dialog[mapa-make-team] input`), |
| 122 | chromedp.KeyEvent(kb.Escape)) |
| 123 | eventually(t, "focus back from Make team page", func() bool { |
| 124 | return read(t, rig, chromedp.Evaluate[bool](focusInEditor)) |
| 125 | }) |
| 126 | drive(t, rig, chromedp.KeyEvent("k", mod()), |
| 127 | chromedp.WaitVisible(`dialog[mapa-palette] input`), |
| 128 | chromedp.KeyEvent(kb.Escape)) |
| 129 | eventually(t, "focus back from ⌘K", func() bool { |
| 130 | return read(t, rig, chromedp.Evaluate[bool](focusInEditor)) |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | func TestL10ThePageReflowsAt320(t *testing.T) { |
| 135 | rig, store := browserRig(t, 0) |
| 136 | teamPage(t, rig, store, accessBody+"\n\n"+strings.Repeat( |
| 137 | "Averyveryverylongwordwithoutanybreaks", 4)) |
| 138 | drive(t, rig, chromedp.EmulateViewport(320, 640), |
| 139 | chromedp.WaitVisible(mounted)) |
| 140 | wide := read(t, rig, chromedp.Evaluate[int]( |
| 141 | `document.documentElement.scrollWidth`)) |
| 142 | if wide > 320 { |
| 143 | t.Fatalf("at 320px the page is %dpx wide", wide) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // §6.11 on a phone (a coarse pointer, 390px): the text is 16px, so iOS |
| 148 | // does not zoom on focus; the accessory row is over the keyboard while |
| 149 | // writing, and its [[ opens the list; the bubble sits below the |
| 150 | // selection, clear of iOS's callout. |
| 151 | func TestThePhoneWritesWithTheAccessoryRow(t *testing.T) { |
| 152 | rig, _ := browserRig(t, 0, harness.WithCoarsePointer()) |
| 153 | drive(t, rig, scripts(true), chromedp.EmulateViewport(390, 844), |
| 154 | chromedp.Navigate(rig.Origin+"/d/2026-11-09"), |
| 155 | chromedp.WaitVisible(mounted), caretAt(0), |
| 156 | chromedp.WaitVisible(`[mapa-accessory]`)) |
| 157 | size := read(t, rig, chromedp.Evaluate[float64]( |
| 158 | `parseFloat(getComputedStyle(mapaEditor.view.dom`+ |
| 159 | `.querySelector("p")).fontSize)`)) |
| 160 | if size < 16 { |
| 161 | t.Fatalf("the page's text is %vpx on a phone", size) |
| 162 | } |
| 163 | drive(t, rig, chromedp.Click(`[mapa-accessory] [aria-label="Bold"]`), |
| 164 | chromedp.KeyEvent("strong"), |
| 165 | chromedp.Click(`[mapa-accessory] [aria-label="Bold"]`), |
| 166 | chromedp.KeyEvent(" and "), |
| 167 | chromedp.Click(`[mapa-accessory] [aria-label="Link to a page"]`), |
| 168 | chromedp.KeyEvent("Zeb")) |
| 169 | listShows(t, rig, "Link “Zeb”") |
| 170 | drive(t, rig, enter()) |
| 171 | if got := typed(t, rig); got != "**strong** and [[Zeb]]" { |
| 172 | t.Fatalf("the row wrote %q", got) |
| 173 | } |
| 174 | drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft, chromedp.KeyModifiers( |
| 175 | chromedp.ModifierShift|chromedp.ModifierAlt)), |
| 176 | chromedp.WaitVisible(`[mapa-bubble]`)) |
| 177 | below := read(t, rig, chromedp.Evaluate[bool](`(function () { |
| 178 | var bubble = document.querySelector("[mapa-bubble]").getBoundingClientRect(); |
| 179 | var range = getSelection().getRangeAt(0).getBoundingClientRect(); |
| 180 | return bubble.top >= range.bottom; |
| 181 | })()`)) |
| 182 | if !below { |
| 183 | t.Fatal("on a coarse pointer the bubble is not below the selection") |
| 184 | } |
| 185 | } |
| 186 | |