| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "runtime" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/rastrillo/rastrillo/harness" |
| 12 | "github.com/chromedp/chromedp" |
| 13 | "github.com/chromedp/chromedp/kb" |
| 14 | |
| 15 | "amadan.net/oficina/mapa/internal/mapa" |
| 16 | ) |
| 17 | |
| 18 | // Link selected words to a page (Paul, 2026-11-16; build note 81): ⌘L, |
| 19 | // the bubble's Page link by mouse and by keyboard, ⌘L with nothing |
| 20 | // selected, the refusal across blocks, the phone's [[, and axe. Each |
| 21 | // answer is the Markdown the editor would save. |
| 22 | |
| 23 | // modifier is mod's key alone, to combine with Shift. |
| 24 | func modifier() chromedp.Modifier { |
| 25 | if runtime.GOOS == "darwin" { |
| 26 | return chromedp.ModifierMeta |
| 27 | } |
| 28 | return chromedp.ModifierCtrl |
| 29 | } |
| 30 | |
| 31 | // selectWords selects the first occurrence of words in the page's text. |
| 32 | func selectWords(words string) chromedp.Action[chromedp.Void] { |
| 33 | return chromedp.Evaluate[chromedp.Void](fmt.Sprintf(`(function (words) { |
| 34 | var view = mapaEditor.view, doc = view.state.doc, found = -1; |
| 35 | doc.descendants(function (node, pos) { |
| 36 | if (found >= 0) return false; |
| 37 | if (!node.isTextblock) return true; |
| 38 | var at = node.textContent.indexOf(words); |
| 39 | if (at >= 0) found = pos + 1 + at; |
| 40 | return false; |
| 41 | }); |
| 42 | var Selection = view.state.selection.constructor; |
| 43 | view.focus(); |
| 44 | view.dispatch(view.state.tr.setSelection( |
| 45 | Selection.create(doc, found, found + words.length))); |
| 46 | })(%q)`, words)) |
| 47 | } |
| 48 | |
| 49 | // pageField is the bubble's Page link field, open. |
| 50 | const pageField = `[mapa-bubble] input[mapa-bubble-page]:not([hidden])` |
| 51 | |
| 52 | // haldenDay is an unwritten day holding text, with Bo's team page |
| 53 | // "Halden Robotics" to link to. |
| 54 | func haldenDay(t *testing.T, rig *harness.Rig, store *mapa.Store, |
| 55 | date, text string) { |
| 56 | t.Helper() |
| 57 | if _, err := store.CreateTeamPage(context.Background(), Bo, |
| 58 | "Halden Robotics", "A customer."); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | emptyDay(t, rig, date) |
| 62 | drive(t, rig, chromedp.KeyEvent(text)) |
| 63 | } |
| 64 | |
| 65 | func TestCmdLLinksTheSelectedWordsToAPage(t *testing.T) { |
| 66 | rig, store := browserRig(t, 0) |
| 67 | haldenDay(t, rig, store, "2026-11-20", "Met the robot firm today") |
| 68 | // A listener after the editor's own: did ⌘L's default go? |
| 69 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`window.addEventListener( |
| 70 | "keydown", function (event) { |
| 71 | if (event.key === "l") window.__prevented = event.defaultPrevented; |
| 72 | })`), selectWords("robot firm"), chromedp.KeyEvent("l", mod()), |
| 73 | chromedp.WaitVisible(pageField)) |
| 74 | if got := read(t, rig, chromedp.Value(pageField)); got != "robot firm" { |
| 75 | t.Fatalf("the field holds %q", got) |
| 76 | } |
| 77 | if !read(t, rig, chromedp.Evaluate[bool](`window.__prevented`)) { |
| 78 | t.Fatal("⌘L's default was not prevented") |
| 79 | } |
| 80 | listShows(t, rig, "Link “robot firm”") |
| 81 | drive(t, rig, chromedp.KeyEvent("Halden")) |
| 82 | listShows(t, rig, "Halden Robotics") |
| 83 | drive(t, rig, enter()) |
| 84 | if got := typed(t, rig); got != "Met the [[Halden Robotics|robot "+ |
| 85 | "firm]] today" { |
| 86 | t.Fatalf("⌘L wrote %q", got) |
| 87 | } |
| 88 | eventually(t, "the chip names the team page", func() bool { |
| 89 | return read(t, rig, chromedp.Evaluate[string]( |
| 90 | `(document.querySelector('article [mapa-chip="team"]') || {}).`+ |
| 91 | `getAttribute && document.querySelector('article `+ |
| 92 | `[mapa-chip="team"]').getAttribute("aria-label")`)) == |
| 93 | "robot firm, team page" |
| 94 | }) |
| 95 | if !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) { |
| 96 | t.Fatal("focus did not come back to the text") |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestCmdLWithNothingSelectedOpensTheLinkList(t *testing.T) { |
| 101 | rig, store := browserRig(t, 0) |
| 102 | haldenDay(t, rig, store, "2026-11-21", "See ") |
| 103 | drive(t, rig, chromedp.KeyEvent("l", mod())) |
| 104 | if got := typed(t, rig); got != "See [[" { |
| 105 | t.Fatalf("⌘L typed %q", got) |
| 106 | } |
| 107 | drive(t, rig, chromedp.KeyEvent("Hal")) |
| 108 | listShows(t, rig, "Halden Robotics") |
| 109 | drive(t, rig, enter()) |
| 110 | if got := typed(t, rig); got != "See [[Halden Robotics]]" { |
| 111 | t.Fatalf("the list wrote %q", got) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestTheBubblesPageLinkByMouse(t *testing.T) { |
| 116 | rig, store := browserRig(t, 0) |
| 117 | haldenDay(t, rig, store, "2026-11-22", "Met Halden Robotics today") |
| 118 | drive(t, rig, selectWords("Halden Robotics"), |
| 119 | chromedp.WaitVisible(`[mapa-bubble]`), |
| 120 | chromedp.Click(`[mapa-bubble] button[aria-label="Page link"]`), |
| 121 | chromedp.WaitVisible(pageField)) |
| 122 | listShows(t, rig, "Halden Robotics") |
| 123 | drive(t, rig, chromedp.Click( |
| 124 | `[mapa-combo]:not([hidden]) li:first-child`)) |
| 125 | if got := typed(t, rig); got != "Met [[Halden Robotics]] today" { |
| 126 | t.Fatalf("the words equal to the title wrote %q", got) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestTheBubblesPageLinkByKeyboard(t *testing.T) { |
| 131 | rig, store := browserRig(t, 0) |
| 132 | haldenDay(t, rig, store, "2026-11-23", "Call the firm on friday") |
| 133 | drive(t, rig, selectWords("friday"), |
| 134 | chromedp.WaitVisible(`[mapa-bubble]`), |
| 135 | chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers( |
| 136 | chromedp.ModifierAlt)), |
| 137 | chromedp.KeyEvent(kb.ArrowRight), chromedp.KeyEvent(kb.ArrowRight), |
| 138 | chromedp.KeyEvent(kb.ArrowRight)) |
| 139 | if got := read(t, rig, chromedp.Evaluate[string]( |
| 140 | `document.activeElement.getAttribute("aria-label")`)); got != |
| 141 | "Page link" { |
| 142 | t.Fatalf("three right of Bold is %q", got) |
| 143 | } |
| 144 | drive(t, rig, enter(), chromedp.WaitVisible(pageField), |
| 145 | chromedp.KeyEvent(kb.Backspace), chromedp.KeyEvent("2026-11-13")) |
| 146 | listShows(t, rig, "Friday, 13 November") |
| 147 | expanded := read(t, rig, chromedp.Evaluate[string](`(function (field) { |
| 148 | return [field.getAttribute("role"), field.getAttribute("aria-expanded"), |
| 149 | document.getElementById(field.getAttribute("aria-controls")) |
| 150 | .getAttribute("role"), |
| 151 | !!document.getElementById(field.getAttribute("aria-activedescendant")) |
| 152 | ].join(); |
| 153 | })(document.querySelector("[mapa-bubble-page]"))`)) |
| 154 | if expanded != "combobox,true,listbox,true" { |
| 155 | t.Fatalf("the field as a combobox: %s", expanded) |
| 156 | } |
| 157 | drive(t, rig, enter()) |
| 158 | if got := typed(t, rig); got != "Call the firm on [[2026-11-13|friday]]" { |
| 159 | t.Fatalf("the date link wrote %q", got) |
| 160 | } |
| 161 | // Esc in the field goes back to the text, linking nothing. |
| 162 | drive(t, rig, selectWords("firm"), chromedp.KeyEvent("l", mod()), |
| 163 | chromedp.WaitVisible(pageField), chromedp.KeyEvent(kb.Escape)) |
| 164 | eventually(t, "Esc back in the text", func() bool { |
| 165 | return read(t, rig, chromedp.Evaluate[bool](focusInEditor)) |
| 166 | }) |
| 167 | if got := typed(t, rig); got != "Call the firm on [[2026-11-13|friday]]" { |
| 168 | t.Fatalf("Esc changed the text: %q", got) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func TestASelectionAcrossBlocksIsRefused(t *testing.T) { |
| 173 | rig, _ := browserRig(t, 0) |
| 174 | emptyDay(t, rig, "2026-11-24") |
| 175 | drive(t, rig, chromedp.KeyEvent("One here"), enter(), |
| 176 | chromedp.KeyEvent("Two there"), |
| 177 | chromedp.KeyEvent(kb.ArrowUp, chromedp.KeyModifiers( |
| 178 | chromedp.ModifierShift)), |
| 179 | chromedp.WaitVisible(`[mapa-bubble]`), |
| 180 | chromedp.KeyEvent("l", mod())) |
| 181 | eventually(t, "the bubble says why", func() bool { |
| 182 | return read(t, rig, chromedp.Text(`[mapa-bubble-note]`)) == |
| 183 | "A page link holds words from one paragraph, heading or "+ |
| 184 | "item. Select words in just one." |
| 185 | }) |
| 186 | if got := typed(t, rig); got != "One here\n\nTwo there" { |
| 187 | t.Fatalf("the refusal changed the text: %q", got) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func TestPageLinkPassesAxe(t *testing.T) { |
| 192 | rig, store := browserRig(t, 0) |
| 193 | haldenDay(t, rig, store, "2026-11-25", "Met the robot firm today") |
| 194 | drive(t, rig, selectWords("robot firm"), |
| 195 | chromedp.WaitVisible(`[mapa-bubble]`)) |
| 196 | if found := axeSerious(t, rig); found != "" { |
| 197 | t.Fatalf("axe, with the bubble:\n%s", found) |
| 198 | } |
| 199 | drive(t, rig, chromedp.KeyEvent("l", mod()), |
| 200 | chromedp.WaitVisible(pageField)) |
| 201 | listShows(t, rig, "Link “robot firm”") |
| 202 | if found := axeSerious(t, rig); found != "" { |
| 203 | t.Fatalf("axe, with Page link's list open:\n%s", found) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // On a phone the accessory row's [[ is ⌘L: over a selection it opens |
| 208 | // Page link. |
| 209 | func TestThePhonesLinkButtonLinksTheSelection(t *testing.T) { |
| 210 | rig, store := browserRig(t, 0, harness.WithCoarsePointer()) |
| 211 | if _, err := store.CreateTeamPage(context.Background(), Bo, |
| 212 | "Halden Robotics", "A customer."); err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | drive(t, rig, scripts(true), chromedp.EmulateViewport(390, 844), |
| 216 | chromedp.Navigate(rig.Origin+"/d/2026-11-26"), |
| 217 | chromedp.WaitVisible(mounted), caretAt(0), |
| 218 | chromedp.KeyEvent("Met the firm"), selectWords("the firm"), |
| 219 | chromedp.WaitVisible(`[mapa-accessory]`), |
| 220 | chromedp.Click(`[mapa-accessory] [aria-label="Link to a page"]`), |
| 221 | chromedp.WaitVisible(pageField), |
| 222 | chromedp.KeyEvent(kb.Backspace), chromedp.KeyEvent("Halden")) |
| 223 | listShows(t, rig, "Halden Robotics") |
| 224 | drive(t, rig, enter()) |
| 225 | if got := typed(t, rig); got != "Met [[Halden Robotics|the firm]]" { |
| 226 | t.Fatalf("the phone's [[ wrote %q", got) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Insert → Link to a page shows ⌘L and runs the same command: over a |
| 231 | // selection it opens Page link. |
| 232 | func TestInsertLinkToAPageOpensPageLink(t *testing.T) { |
| 233 | rig, store := browserRig(t, 0) |
| 234 | haldenDay(t, rig, store, "2026-11-27", "Met the firm") |
| 235 | row := `#mapa-menu-insert [data-mapa-command="insert-link"]` |
| 236 | if got := read(t, rig, chromedp.Evaluate[string](`document.`+ |
| 237 | `querySelector('`+row+` kbd').textContent`)); got != "⌘L" { |
| 238 | t.Fatalf("Insert → Link to a page shows %q", got) |
| 239 | } |
| 240 | drive(t, rig, selectWords("the firm"), |
| 241 | chromedp.Click(`#mapa-menu-insert summary`), |
| 242 | chromedp.Click(row), chromedp.WaitVisible(pageField), |
| 243 | chromedp.KeyEvent("Halden")) |
| 244 | listShows(t, rig, "Halden Robotics") |
| 245 | drive(t, rig, enter()) |
| 246 | if got := typed(t, rig); got != "Met [[Halden Robotics|the firm]]" { |
| 247 | t.Fatalf("Insert → Link to a page wrote %q", got) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // areaValue is the Markdown textarea's text. |
| 252 | const areaValue = `document.querySelector("textarea[mapa-editor]").value` |
| 253 | |
| 254 | // On the Markdown screen ⌘L and Insert → Link to a page never replace |
| 255 | // a selection they cannot wrap (review H1): ⌘A then ⌘L, the address |
| 256 | // bar's habit, once left the page holding "[[". Ctrl+L on a Mac is not |
| 257 | // ⌘L (review low 3). |
| 258 | func TestTheMarkdownScreenNeverReplacesASelection(t *testing.T) { |
| 259 | rig, store := browserRig(t, 0) |
| 260 | body := "line one\nline [two]" |
| 261 | page, err := store.CreateTeamPage(context.Background(), Bo, |
| 262 | "Markdown plan", body) |
| 263 | if err != nil { |
| 264 | t.Fatal(err) |
| 265 | } |
| 266 | selectAll := chromedp.Evaluate[chromedp.Void]( |
| 267 | `document.querySelector("textarea[mapa-editor]").select()`) |
| 268 | drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), |
| 269 | chromedp.Navigate(rig.Origin+mapa.PagePath(page)+"/edit"), |
| 270 | chromedp.WaitVisible(`textarea[mapa-editor]`), |
| 271 | selectAll, chromedp.KeyEvent("l", mod())) |
| 272 | if got := read(t, rig, chromedp.Evaluate[string](areaValue)); got != body { |
| 273 | t.Fatalf("⌘A then ⌘L left %q", got) |
| 274 | } |
| 275 | drive(t, rig, selectAll, chromedp.Click(`#mapa-menu-insert summary`), |
| 276 | chromedp.Click(`#mapa-menu-insert [data-mapa-command="insert-link"]`)) |
| 277 | if got := read(t, rig, chromedp.Evaluate[string](areaValue)); got != body { |
| 278 | t.Fatalf("Insert → Link to a page over everything left %q", got) |
| 279 | } |
| 280 | words := chromedp.Evaluate[chromedp.Void](`(function (area) { |
| 281 | area.focus(); area.setSelectionRange(5, 8); |
| 282 | })(document.querySelector("textarea[mapa-editor]"))`) |
| 283 | if runtime.GOOS == "darwin" { |
| 284 | drive(t, rig, words, chromedp.KeyEvent("l", |
| 285 | chromedp.KeyModifiers(chromedp.ModifierCtrl))) |
| 286 | if got := read(t, rig, chromedp.Evaluate[string]( |
| 287 | areaValue)); got != body { |
| 288 | t.Fatalf("Ctrl+L on a Mac wrote %q", got) |
| 289 | } |
| 290 | } |
| 291 | drive(t, rig, words, chromedp.KeyEvent("l", mod())) |
| 292 | if got := read(t, rig, chromedp.Evaluate[string]( |
| 293 | areaValue)); got != "line [[one]]\nline [two]" { |
| 294 | t.Fatalf("⌘L over one word wrote %q", got) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // ⌘A then ⌘L in the page editor says why it links nothing (review |
| 299 | // low 1), and a link the page changed under says so too (low 2). |
| 300 | func TestPageLinkSaysWhyItLinksNothing(t *testing.T) { |
| 301 | rig, store := browserRig(t, 0) |
| 302 | haldenDay(t, rig, store, "2026-11-29", "Met the robot firm") |
| 303 | drive(t, rig, enter(), chromedp.KeyEvent("Second line"), |
| 304 | chromedp.KeyEvent("a", mod()), chromedp.KeyEvent("l", mod())) |
| 305 | eventually(t, "the bubble says why ⌘A then ⌘L links nothing", |
| 306 | func() bool { |
| 307 | return read(t, rig, chromedp.Text(`[mapa-bubble-note]`)) == |
| 308 | "A page link holds words from one paragraph, heading or "+ |
| 309 | "item. Select words in just one." |
| 310 | }) |
| 311 | drive(t, rig, caretAt(0), selectWords("robot firm"), |
| 312 | chromedp.KeyEvent("l", mod()), chromedp.WaitVisible(pageField), |
| 313 | chromedp.Evaluate[chromedp.Void](`(function (view) { |
| 314 | view.dispatch(view.state.tr.insertText("X", view.state.selection.from + 1)); |
| 315 | })(mapaEditor.view)`)) |
| 316 | listShows(t, rig, "Link “robot firm”") |
| 317 | drive(t, rig, enter()) |
| 318 | eventually(t, "the bubble says the selection changed", func() bool { |
| 319 | return read(t, rig, chromedp.Text(`[mapa-bubble-note]`)) == |
| 320 | "The selection changed. Select the words again." |
| 321 | }) |
| 322 | if got := typed(t, rig); got != "Met the rXobot firm\n\nSecond line" { |
| 323 | t.Fatalf("the refused link wrote %q", got) |
| 324 | } |
| 325 | } |
| 326 | |