//go:build browser package mapatest import ( "context" "fmt" "runtime" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" "amadan.net/oficina/mapa/internal/mapa" ) // Link selected words to a page (Paul, 2026-11-16; build note 81): ⌘L, // the bubble's Page link by mouse and by keyboard, ⌘L with nothing // selected, the refusal across blocks, the phone's [[, and axe. Each // answer is the Markdown the editor would save. // modifier is mod's key alone, to combine with Shift. func modifier() chromedp.Modifier { if runtime.GOOS == "darwin" { return chromedp.ModifierMeta } return chromedp.ModifierCtrl } // selectWords selects the first occurrence of words in the page's text. func selectWords(words string) chromedp.Action[chromedp.Void] { return chromedp.Evaluate[chromedp.Void](fmt.Sprintf(`(function (words) { var view = mapaEditor.view, doc = view.state.doc, found = -1; doc.descendants(function (node, pos) { if (found >= 0) return false; if (!node.isTextblock) return true; var at = node.textContent.indexOf(words); if (at >= 0) found = pos + 1 + at; return false; }); var Selection = view.state.selection.constructor; view.focus(); view.dispatch(view.state.tr.setSelection( Selection.create(doc, found, found + words.length))); })(%q)`, words)) } // pageField is the bubble's Page link field, open. const pageField = `[mapa-bubble] input[mapa-bubble-page]:not([hidden])` // haldenDay is an unwritten day holding text, with Bo's team page // "Halden Robotics" to link to. func haldenDay(t *testing.T, rig *harness.Rig, store *mapa.Store, date, text string) { t.Helper() if _, err := store.CreateTeamPage(context.Background(), Bo, "Halden Robotics", "A customer."); err != nil { t.Fatal(err) } emptyDay(t, rig, date) drive(t, rig, chromedp.KeyEvent(text)) } func TestCmdLLinksTheSelectedWordsToAPage(t *testing.T) { rig, store := browserRig(t, 0) haldenDay(t, rig, store, "2026-11-20", "Met the robot firm today") // A listener after the editor's own: did ⌘L's default go? drive(t, rig, chromedp.Evaluate[chromedp.Void](`window.addEventListener( "keydown", function (event) { if (event.key === "l") window.__prevented = event.defaultPrevented; })`), selectWords("robot firm"), chromedp.KeyEvent("l", mod()), chromedp.WaitVisible(pageField)) if got := read(t, rig, chromedp.Value(pageField)); got != "robot firm" { t.Fatalf("the field holds %q", got) } if !read(t, rig, chromedp.Evaluate[bool](`window.__prevented`)) { t.Fatal("⌘L's default was not prevented") } listShows(t, rig, "Link “robot firm”") drive(t, rig, chromedp.KeyEvent("Halden")) listShows(t, rig, "Halden Robotics") drive(t, rig, enter()) if got := typed(t, rig); got != "Met the [[Halden Robotics|robot "+ "firm]] today" { t.Fatalf("⌘L wrote %q", got) } eventually(t, "the chip names the team page", func() bool { return read(t, rig, chromedp.Evaluate[string]( `(document.querySelector('article [mapa-chip="team"]') || {}).`+ `getAttribute && document.querySelector('article `+ `[mapa-chip="team"]').getAttribute("aria-label")`)) == "robot firm, team page" }) if !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) { t.Fatal("focus did not come back to the text") } } func TestCmdLWithNothingSelectedOpensTheLinkList(t *testing.T) { rig, store := browserRig(t, 0) haldenDay(t, rig, store, "2026-11-21", "See ") drive(t, rig, chromedp.KeyEvent("l", mod())) if got := typed(t, rig); got != "See [[" { t.Fatalf("⌘L typed %q", got) } drive(t, rig, chromedp.KeyEvent("Hal")) listShows(t, rig, "Halden Robotics") drive(t, rig, enter()) if got := typed(t, rig); got != "See [[Halden Robotics]]" { t.Fatalf("the list wrote %q", got) } } func TestTheBubblesPageLinkByMouse(t *testing.T) { rig, store := browserRig(t, 0) haldenDay(t, rig, store, "2026-11-22", "Met Halden Robotics today") drive(t, rig, selectWords("Halden Robotics"), chromedp.WaitVisible(`[mapa-bubble]`), chromedp.Click(`[mapa-bubble] button[aria-label="Page link"]`), chromedp.WaitVisible(pageField)) listShows(t, rig, "Halden Robotics") drive(t, rig, chromedp.Click( `[mapa-combo]:not([hidden]) li:first-child`)) if got := typed(t, rig); got != "Met [[Halden Robotics]] today" { t.Fatalf("the words equal to the title wrote %q", got) } } func TestTheBubblesPageLinkByKeyboard(t *testing.T) { rig, store := browserRig(t, 0) haldenDay(t, rig, store, "2026-11-23", "Call the firm on friday") drive(t, rig, selectWords("friday"), chromedp.WaitVisible(`[mapa-bubble]`), chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers( chromedp.ModifierAlt)), chromedp.KeyEvent(kb.ArrowRight), chromedp.KeyEvent(kb.ArrowRight), chromedp.KeyEvent(kb.ArrowRight)) if got := read(t, rig, chromedp.Evaluate[string]( `document.activeElement.getAttribute("aria-label")`)); got != "Page link" { t.Fatalf("three right of Bold is %q", got) } drive(t, rig, enter(), chromedp.WaitVisible(pageField), chromedp.KeyEvent(kb.Backspace), chromedp.KeyEvent("2026-11-13")) listShows(t, rig, "Friday, 13 November") expanded := read(t, rig, chromedp.Evaluate[string](`(function (field) { return [field.getAttribute("role"), field.getAttribute("aria-expanded"), document.getElementById(field.getAttribute("aria-controls")) .getAttribute("role"), !!document.getElementById(field.getAttribute("aria-activedescendant")) ].join(); })(document.querySelector("[mapa-bubble-page]"))`)) if expanded != "combobox,true,listbox,true" { t.Fatalf("the field as a combobox: %s", expanded) } drive(t, rig, enter()) if got := typed(t, rig); got != "Call the firm on [[2026-11-13|friday]]" { t.Fatalf("the date link wrote %q", got) } // Esc in the field goes back to the text, linking nothing. drive(t, rig, selectWords("firm"), chromedp.KeyEvent("l", mod()), chromedp.WaitVisible(pageField), chromedp.KeyEvent(kb.Escape)) eventually(t, "Esc back in the text", func() bool { return read(t, rig, chromedp.Evaluate[bool](focusInEditor)) }) if got := typed(t, rig); got != "Call the firm on [[2026-11-13|friday]]" { t.Fatalf("Esc changed the text: %q", got) } } func TestASelectionAcrossBlocksIsRefused(t *testing.T) { rig, _ := browserRig(t, 0) emptyDay(t, rig, "2026-11-24") drive(t, rig, chromedp.KeyEvent("One here"), enter(), chromedp.KeyEvent("Two there"), chromedp.KeyEvent(kb.ArrowUp, chromedp.KeyModifiers( chromedp.ModifierShift)), chromedp.WaitVisible(`[mapa-bubble]`), chromedp.KeyEvent("l", mod())) eventually(t, "the bubble says why", func() bool { return read(t, rig, chromedp.Text(`[mapa-bubble-note]`)) == "A page link holds words from one paragraph, heading or "+ "item. Select words in just one." }) if got := typed(t, rig); got != "One here\n\nTwo there" { t.Fatalf("the refusal changed the text: %q", got) } } func TestPageLinkPassesAxe(t *testing.T) { rig, store := browserRig(t, 0) haldenDay(t, rig, store, "2026-11-25", "Met the robot firm today") drive(t, rig, selectWords("robot firm"), chromedp.WaitVisible(`[mapa-bubble]`)) if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, with the bubble:\n%s", found) } drive(t, rig, chromedp.KeyEvent("l", mod()), chromedp.WaitVisible(pageField)) listShows(t, rig, "Link “robot firm”") if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, with Page link's list open:\n%s", found) } } // On a phone the accessory row's [[ is ⌘L: over a selection it opens // Page link. func TestThePhonesLinkButtonLinksTheSelection(t *testing.T) { rig, store := browserRig(t, 0, harness.WithCoarsePointer()) if _, err := store.CreateTeamPage(context.Background(), Bo, "Halden Robotics", "A customer."); err != nil { t.Fatal(err) } drive(t, rig, scripts(true), chromedp.EmulateViewport(390, 844), chromedp.Navigate(rig.Origin+"/d/2026-11-26"), chromedp.WaitVisible(mounted), caretAt(0), chromedp.KeyEvent("Met the firm"), selectWords("the firm"), chromedp.WaitVisible(`[mapa-accessory]`), chromedp.Click(`[mapa-accessory] [aria-label="Link to a page"]`), chromedp.WaitVisible(pageField), chromedp.KeyEvent(kb.Backspace), chromedp.KeyEvent("Halden")) listShows(t, rig, "Halden Robotics") drive(t, rig, enter()) if got := typed(t, rig); got != "Met [[Halden Robotics|the firm]]" { t.Fatalf("the phone's [[ wrote %q", got) } } // Insert → Link to a page shows ⌘L and runs the same command: over a // selection it opens Page link. func TestInsertLinkToAPageOpensPageLink(t *testing.T) { rig, store := browserRig(t, 0) haldenDay(t, rig, store, "2026-11-27", "Met the firm") row := `#mapa-menu-insert [data-mapa-command="insert-link"]` if got := read(t, rig, chromedp.Evaluate[string](`document.`+ `querySelector('`+row+` kbd').textContent`)); got != "⌘L" { t.Fatalf("Insert → Link to a page shows %q", got) } drive(t, rig, selectWords("the firm"), chromedp.Click(`#mapa-menu-insert summary`), chromedp.Click(row), chromedp.WaitVisible(pageField), chromedp.KeyEvent("Halden")) listShows(t, rig, "Halden Robotics") drive(t, rig, enter()) if got := typed(t, rig); got != "Met [[Halden Robotics|the firm]]" { t.Fatalf("Insert → Link to a page wrote %q", got) } } // areaValue is the Markdown textarea's text. const areaValue = `document.querySelector("textarea[mapa-editor]").value` // On the Markdown screen ⌘L and Insert → Link to a page never replace // a selection they cannot wrap (review H1): ⌘A then ⌘L, the address // bar's habit, once left the page holding "[[". Ctrl+L on a Mac is not // ⌘L (review low 3). func TestTheMarkdownScreenNeverReplacesASelection(t *testing.T) { rig, store := browserRig(t, 0) body := "line one\nline [two]" page, err := store.CreateTeamPage(context.Background(), Bo, "Markdown plan", body) if err != nil { t.Fatal(err) } selectAll := chromedp.Evaluate[chromedp.Void]( `document.querySelector("textarea[mapa-editor]").select()`) drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), chromedp.Navigate(rig.Origin+mapa.PagePath(page)+"/edit"), chromedp.WaitVisible(`textarea[mapa-editor]`), selectAll, chromedp.KeyEvent("l", mod())) if got := read(t, rig, chromedp.Evaluate[string](areaValue)); got != body { t.Fatalf("⌘A then ⌘L left %q", got) } drive(t, rig, selectAll, chromedp.Click(`#mapa-menu-insert summary`), chromedp.Click(`#mapa-menu-insert [data-mapa-command="insert-link"]`)) if got := read(t, rig, chromedp.Evaluate[string](areaValue)); got != body { t.Fatalf("Insert → Link to a page over everything left %q", got) } words := chromedp.Evaluate[chromedp.Void](`(function (area) { area.focus(); area.setSelectionRange(5, 8); })(document.querySelector("textarea[mapa-editor]"))`) if runtime.GOOS == "darwin" { drive(t, rig, words, chromedp.KeyEvent("l", chromedp.KeyModifiers(chromedp.ModifierCtrl))) if got := read(t, rig, chromedp.Evaluate[string]( areaValue)); got != body { t.Fatalf("Ctrl+L on a Mac wrote %q", got) } } drive(t, rig, words, chromedp.KeyEvent("l", mod())) if got := read(t, rig, chromedp.Evaluate[string]( areaValue)); got != "line [[one]]\nline [two]" { t.Fatalf("⌘L over one word wrote %q", got) } } // ⌘A then ⌘L in the page editor says why it links nothing (review // low 1), and a link the page changed under says so too (low 2). func TestPageLinkSaysWhyItLinksNothing(t *testing.T) { rig, store := browserRig(t, 0) haldenDay(t, rig, store, "2026-11-29", "Met the robot firm") drive(t, rig, enter(), chromedp.KeyEvent("Second line"), chromedp.KeyEvent("a", mod()), chromedp.KeyEvent("l", mod())) eventually(t, "the bubble says why ⌘A then ⌘L links nothing", func() bool { return read(t, rig, chromedp.Text(`[mapa-bubble-note]`)) == "A page link holds words from one paragraph, heading or "+ "item. Select words in just one." }) drive(t, rig, caretAt(0), selectWords("robot firm"), chromedp.KeyEvent("l", mod()), chromedp.WaitVisible(pageField), chromedp.Evaluate[chromedp.Void](`(function (view) { view.dispatch(view.state.tr.insertText("X", view.state.selection.from + 1)); })(mapaEditor.view)`)) listShows(t, rig, "Link “robot firm”") drive(t, rig, enter()) eventually(t, "the bubble says the selection changed", func() bool { return read(t, rig, chromedp.Text(`[mapa-bubble-note]`)) == "The selection changed. Select the words again." }) if got := typed(t, rig); got != "Met the rXobot firm\n\nSecond line" { t.Fatalf("the refused link wrote %q", got) } }