//go:build browser package mapatest import ( "context" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" "amadan.net/oficina/mapa/internal/mapa" ) // Typing in the page editor (level-up spec §6.6): [[, the / menu, the // Markdown shortcuts, the bubble, chips, and the keyboard's way out. // Each answer is read from the editor's own Markdown, which is what is // saved. // enter is the Enter key as a browser sends it: one keyDown carrying // "\r", which the page may cancel. chromedp's KeyEvent sends a separate // char event, which a cancelled keydown does not stop, so an Enter the // editor took would be typed a second time. func enter() chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { down := input.DispatchKeyEventParams{ Type: input.DispatchKeyEventTypeKeyDown, Key: "Enter", Code: "Enter", WindowsVirtualKeyCode: 13, Text: "\r", UnmodifiedText: "\r"} if _, err := cdp.Call(ctx, target, input.DispatchKeyEvent, down); err != nil { return err } up := input.DispatchKeyEventParams{ Type: input.DispatchKeyEventTypeKeyUp, Key: "Enter", Code: "Enter", WindowsVirtualKeyCode: 13} _, err := cdp.Call(ctx, target, input.DispatchKeyEvent, up) return err }) } // emptyDay opens an unwritten day in the editor, caret in its body. func emptyDay(t *testing.T, rig *harness.Rig, date string) { t.Helper() drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), chromedp.Navigate(rig.Origin+"/d/"+date), chromedp.WaitVisible(mounted), caretAt(0)) } // options is the open list's rows, as read. func options(t *testing.T, rig *harness.Rig) string { t.Helper() return read(t, rig, chromedp.Evaluate[string](`Array.prototype.map.call( document.querySelectorAll("[mapa-combo]:not([hidden]) li"), function (row) { return row.textContent; }).join(" | ")`)) } // listShows waits for the open list to hold want. func listShows(t *testing.T, rig *harness.Rig, want string) { t.Helper() eventually(t, "the list shows "+want, func() bool { return strings.Contains(options(t, rig), want) }) } func TestTheLinkListMakesChips(t *testing.T) { rig, store := browserRig(t, 0) ctx := context.Background() acme, err := store.CreateTeamPage(ctx, Bo, "Acme Labs", "A customer.") if err != nil { t.Fatal(err) } if err := store.Rename(ctx, Bo, acme.ID, "Acme Robotics"); err != nil { t.Fatal(err) } emptyDay(t, rig, "2026-11-02") drive(t, rig, chromedp.KeyEvent("Met [[Acme L")) listShows(t, rig, "Old title of Acme Robotics") expanded := read(t, rig, chromedp.Evaluate[string]( `(function (dom) { return [document.getElementById(dom.getAttribute("aria-controls")) .getAttribute("role"), !!document.getElementById( dom.getAttribute("aria-activedescendant"))].join(); })(mapaEditor.view.dom)`)) if expanded != "listbox,true" { t.Fatalf("the editor as a combobox: %s", expanded) } drive(t, rig, enter(), chromedp.KeyEvent(" and [[Zebra")) listShows(t, rig, "Link “Zebra”") drive(t, rig, chromedp.KeyEvent(kb.ArrowUp), enter(), chromedp.KeyEvent(" on [[2026-11-12")) listShows(t, rig, "Thursday, 12 November") drive(t, rig, enter()) if got := typed(t, rig); got != "Met [[Acme Labs]] and [[Zebra]] on "+ "[[2026-11-12]]" { t.Fatalf("the chips write %q", got) } labels := read(t, rig, chromedp.Evaluate[string](`Array.prototype.map.call( document.querySelectorAll("article [mapa-chip]"), function (chip) { return chip.getAttribute("aria-label"); }).join(" | ")`)) if labels != "Acme Labs, now called Acme Robotics | Zebra, page not "+ "written yet | Thursday, 12 November, your note" { t.Fatalf("the chips' names: %s", labels) } // Backspace into a chip: its [[text again, and the list again. drive(t, rig, chromedp.KeyEvent(kb.Backspace)) listShows(t, rig, "Thursday, 12 November") if got := typed(t, rig); !strings.HasSuffix(got, " on [[2026-11-12") { t.Fatalf("Backspace into the chip left %q", got) } } func TestTheSlashMenuAndTheShortcutsMakeBlocks(t *testing.T) { rig, _ := browserRig(t, 0) emptyDay(t, rig, "2026-11-03") drive(t, rig, chromedp.KeyEvent("Intro"), enter(), chromedp.KeyEvent("/check")) listShows(t, rig, "Checklist") drive(t, rig, enter(), chromedp.KeyEvent("Send it"), enter(), enter(), chromedp.KeyEvent("## Plan"), enter(), chromedp.KeyEvent("- one"), enter(), chromedp.KeyEvent("two"), chromedp.KeyEvent(kb.Tab), enter(), enter(), enter(), chromedp.KeyEvent("> said"), enter(), chromedp.KeyEvent("---"), chromedp.KeyEvent("**bold** after")) want := "Intro\n\n- [ ] Send it\n\n## Plan\n\n- one\n - two\n\n" + "> said\n\n---\n\n**bold** after" if got := typed(t, rig); got != want { t.Fatalf("the blocks write\n%q\nwant\n%q", got, want) } } func TestTheBubbleMarksTheSelection(t *testing.T) { rig, _ := browserRig(t, 0) emptyDay(t, rig, "2026-11-04") drive(t, rig, chromedp.KeyEvent("plain word"), chromedp.KeyEvent(kb.ArrowLeft, chromedp.KeyModifiers( chromedp.ModifierShift|chromedp.ModifierAlt)), chromedp.WaitVisible(`[mapa-bubble]`), chromedp.Click(`[mapa-bubble] button[aria-label="Bold"]`)) if got := typed(t, rig); got != "plain **word**" { t.Fatalf("Bold made %q", got) } // Web link stays beside Page link (build note 81), by mouse and // by ⇧⌘K. drive(t, rig, chromedp.Click(`[mapa-bubble] button[aria-label="Web link"]`), chromedp.WaitVisible(`[mapa-bubble-field]`), chromedp.SendKeys(`[mapa-bubble-field]`, "https://example.com"), enter()) if got := typed(t, rig); got != "plain [word](https://example.com)" { t.Fatalf("Web link made %q", got) } drive(t, rig, selectWords("plain"), chromedp.KeyEvent("K", chromedp.KeyModifiers( chromedp.ModifierShift|modifier())), chromedp.WaitVisible(`[mapa-bubble-field]`), chromedp.SendKeys(`[mapa-bubble-field]`, "https://example.org"), enter()) if got := typed(t, rig); got != "[plain](https://example.org) "+ "[word](https://example.com)" { t.Fatalf("⇧⌘K made %q", got) } } // L10's way out: Tab in a list indents; Esc, then Tab, leaves. func TestEscThenTabLeavesTheEditor(t *testing.T) { rig, _ := browserRig(t, 0) emptyDay(t, rig, "2026-11-05") drive(t, rig, chromedp.KeyEvent("- one"), enter(), chromedp.KeyEvent("two"), chromedp.KeyEvent(kb.Tab)) if got := typed(t, rig); got != "- one\n - two" { t.Fatalf("Tab in a list made %q", got) } drive(t, rig, chromedp.KeyEvent(kb.Escape), chromedp.KeyEvent(kb.Tab), chromedp.Sleep(100*time.Millisecond)) inside := read(t, rig, chromedp.Evaluate[bool]( `mapaEditor.view.dom.contains(document.activeElement)`)) if inside { t.Fatal("Esc, then Tab, stayed in the editor") } } func TestAPlainClickOnAChipFollowsIt(t *testing.T) { rig, store := browserRig(t, 0) page, err := store.CreateTeamPage(context.Background(), Bo, "Target", "Here.") if err != nil { t.Fatal(err) } teamPage(t, rig, store, "Go to [[Target]].") drive(t, rig, chromedp.Click(`article a[mapa-chip="team"]`)) arrive(t, rig, mapa.PagePath(page)) } // §6.7 and L9: Make team page from a selection commits the note, takes // the block numbers from what it committed, and leaves a link. func TestMakeTeamPageFromASelection(t *testing.T) { rig, _ := browserRig(t, 0) emptyDay(t, rig, "2026-11-06") drive(t, rig, chromedp.KeyEvent("Keep this."), enter(), chromedp.KeyEvent("# Pilot logins"), enter(), chromedp.KeyEvent("Share this one."), chromedp.KeyEvent(kb.ArrowUp, chromedp.KeyModifiers( chromedp.ModifierShift)), chromedp.WaitVisible(`[mapa-bubble] [aria-label="Make team page"]`), chromedp.Click(`[mapa-bubble] [aria-label="Make team page"]`), chromedp.WaitVisible(`dialog[mapa-make-team] input`)) tinted := read(t, rig, chromedp.Evaluate[int]( `document.querySelectorAll("article [mapa-moving]").length`)) title := read(t, rig, chromedp.Value(`dialog[mapa-make-team] input`)) if tinted != 2 || title != "Pilot logins" { t.Fatalf("widened to %d blocks, titled %q", tinted, title) } drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function () { var box = document.querySelector("dialog[mapa-make-team]"); window.__clicked = performance.now(); box.addEventListener("close", function () { window.__held = performance.now() - __clicked; }); })()`), chromedp.Click(`dialog[mapa-make-team] [type="submit"]`), chromedp.WaitVisible(`[mapa-editor-notice] a`)) // L11: the button holds its spinner for 650ms. held := read(t, rig, chromedp.Poll[float64](`window.__held`)) if held < 650 { t.Fatalf("Make team page answered after %.0fms", held) } if got := typed(t, rig); got != "Keep this.\n\n[[Pilot logins]]" { t.Fatalf("the note holds %q", got) } href := read(t, rig, chromedp.AttributeValue(`[mapa-editor-notice] a`, "href")).Value drive(t, rig, chromedp.Navigate(rig.Origin+href), chromedp.WaitVisible(mounted)) if got := typed(t, rig); got != "# Pilot logins\n\nShare this one." { t.Fatalf("the team page holds %q", got) } }