| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/rastrillo/rastrillo/harness" |
| 12 | "github.com/chromedp/cdproto/cdp" |
| 13 | "github.com/chromedp/cdproto/input" |
| 14 | "github.com/chromedp/chromedp" |
| 15 | "github.com/chromedp/chromedp/kb" |
| 16 | |
| 17 | "amadan.net/oficina/mapa/internal/mapa" |
| 18 | ) |
| 19 | |
| 20 | // Typing in the page editor (level-up spec §6.6): [[, the / menu, the |
| 21 | // Markdown shortcuts, the bubble, chips, and the keyboard's way out. |
| 22 | // Each answer is read from the editor's own Markdown, which is what is |
| 23 | // saved. |
| 24 | |
| 25 | // enter is the Enter key as a browser sends it: one keyDown carrying |
| 26 | // "\r", which the page may cancel. chromedp's KeyEvent sends a separate |
| 27 | // char event, which a cancelled keydown does not stop, so an Enter the |
| 28 | // editor took would be typed a second time. |
| 29 | func enter() chromedp.Action[chromedp.Void] { |
| 30 | return chromedp.Func(func(ctx context.Context, |
| 31 | target *chromedp.Target) error { |
| 32 | down := input.DispatchKeyEventParams{ |
| 33 | Type: input.DispatchKeyEventTypeKeyDown, Key: "Enter", |
| 34 | Code: "Enter", WindowsVirtualKeyCode: 13, |
| 35 | Text: "\r", UnmodifiedText: "\r"} |
| 36 | if _, err := cdp.Call(ctx, target, input.DispatchKeyEvent, |
| 37 | down); err != nil { |
| 38 | return err |
| 39 | } |
| 40 | up := input.DispatchKeyEventParams{ |
| 41 | Type: input.DispatchKeyEventTypeKeyUp, Key: "Enter", |
| 42 | Code: "Enter", WindowsVirtualKeyCode: 13} |
| 43 | _, err := cdp.Call(ctx, target, input.DispatchKeyEvent, up) |
| 44 | return err |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | // emptyDay opens an unwritten day in the editor, caret in its body. |
| 49 | func emptyDay(t *testing.T, rig *harness.Rig, date string) { |
| 50 | t.Helper() |
| 51 | drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), |
| 52 | chromedp.Navigate(rig.Origin+"/d/"+date), |
| 53 | chromedp.WaitVisible(mounted), caretAt(0)) |
| 54 | } |
| 55 | |
| 56 | // options is the open list's rows, as read. |
| 57 | func options(t *testing.T, rig *harness.Rig) string { |
| 58 | t.Helper() |
| 59 | return read(t, rig, chromedp.Evaluate[string](`Array.prototype.map.call( |
| 60 | document.querySelectorAll("[mapa-combo]:not([hidden]) li"), |
| 61 | function (row) { return row.textContent; }).join(" | ")`)) |
| 62 | } |
| 63 | |
| 64 | // listShows waits for the open list to hold want. |
| 65 | func listShows(t *testing.T, rig *harness.Rig, want string) { |
| 66 | t.Helper() |
| 67 | eventually(t, "the list shows "+want, func() bool { |
| 68 | return strings.Contains(options(t, rig), want) |
| 69 | }) |
| 70 | } |
| 71 | |
| 72 | func TestTheLinkListMakesChips(t *testing.T) { |
| 73 | rig, store := browserRig(t, 0) |
| 74 | ctx := context.Background() |
| 75 | acme, err := store.CreateTeamPage(ctx, Bo, "Acme Labs", "A customer.") |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if err := store.Rename(ctx, Bo, acme.ID, "Acme Robotics"); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | emptyDay(t, rig, "2026-11-02") |
| 83 | drive(t, rig, chromedp.KeyEvent("Met [[Acme L")) |
| 84 | listShows(t, rig, "Old title of Acme Robotics") |
| 85 | expanded := read(t, rig, chromedp.Evaluate[string]( |
| 86 | `(function (dom) { |
| 87 | return [document.getElementById(dom.getAttribute("aria-controls")) |
| 88 | .getAttribute("role"), !!document.getElementById( |
| 89 | dom.getAttribute("aria-activedescendant"))].join(); |
| 90 | })(mapaEditor.view.dom)`)) |
| 91 | if expanded != "listbox,true" { |
| 92 | t.Fatalf("the editor as a combobox: %s", expanded) |
| 93 | } |
| 94 | drive(t, rig, enter(), chromedp.KeyEvent(" and [[Zebra")) |
| 95 | listShows(t, rig, "Link “Zebra”") |
| 96 | drive(t, rig, chromedp.KeyEvent(kb.ArrowUp), enter(), |
| 97 | chromedp.KeyEvent(" on [[2026-11-12")) |
| 98 | listShows(t, rig, "Thursday, 12 November") |
| 99 | drive(t, rig, enter()) |
| 100 | if got := typed(t, rig); got != "Met [[Acme Labs]] and [[Zebra]] on "+ |
| 101 | "[[2026-11-12]]" { |
| 102 | t.Fatalf("the chips write %q", got) |
| 103 | } |
| 104 | labels := read(t, rig, chromedp.Evaluate[string](`Array.prototype.map.call( |
| 105 | document.querySelectorAll("article [mapa-chip]"), |
| 106 | function (chip) { return chip.getAttribute("aria-label"); }).join(" | ")`)) |
| 107 | if labels != "Acme Labs, now called Acme Robotics | Zebra, page not "+ |
| 108 | "written yet | Thursday, 12 November, your note" { |
| 109 | t.Fatalf("the chips' names: %s", labels) |
| 110 | } |
| 111 | // Backspace into a chip: its [[text again, and the list again. |
| 112 | drive(t, rig, chromedp.KeyEvent(kb.Backspace)) |
| 113 | listShows(t, rig, "Thursday, 12 November") |
| 114 | if got := typed(t, rig); !strings.HasSuffix(got, " on [[2026-11-12") { |
| 115 | t.Fatalf("Backspace into the chip left %q", got) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestTheSlashMenuAndTheShortcutsMakeBlocks(t *testing.T) { |
| 120 | rig, _ := browserRig(t, 0) |
| 121 | emptyDay(t, rig, "2026-11-03") |
| 122 | drive(t, rig, chromedp.KeyEvent("Intro"), enter(), |
| 123 | chromedp.KeyEvent("/check")) |
| 124 | listShows(t, rig, "Checklist") |
| 125 | drive(t, rig, enter(), chromedp.KeyEvent("Send it"), |
| 126 | enter(), enter(), |
| 127 | chromedp.KeyEvent("## Plan"), enter(), |
| 128 | chromedp.KeyEvent("- one"), enter(), |
| 129 | chromedp.KeyEvent("two"), chromedp.KeyEvent(kb.Tab), |
| 130 | enter(), enter(), |
| 131 | enter(), |
| 132 | chromedp.KeyEvent("> said"), enter(), |
| 133 | chromedp.KeyEvent("---"), chromedp.KeyEvent("**bold** after")) |
| 134 | want := "Intro\n\n- [ ] Send it\n\n## Plan\n\n- one\n - two\n\n" + |
| 135 | "> said\n\n---\n\n**bold** after" |
| 136 | if got := typed(t, rig); got != want { |
| 137 | t.Fatalf("the blocks write\n%q\nwant\n%q", got, want) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestTheBubbleMarksTheSelection(t *testing.T) { |
| 142 | rig, _ := browserRig(t, 0) |
| 143 | emptyDay(t, rig, "2026-11-04") |
| 144 | drive(t, rig, chromedp.KeyEvent("plain word"), |
| 145 | chromedp.KeyEvent(kb.ArrowLeft, chromedp.KeyModifiers( |
| 146 | chromedp.ModifierShift|chromedp.ModifierAlt)), |
| 147 | chromedp.WaitVisible(`[mapa-bubble]`), |
| 148 | chromedp.Click(`[mapa-bubble] button[aria-label="Bold"]`)) |
| 149 | if got := typed(t, rig); got != "plain **word**" { |
| 150 | t.Fatalf("Bold made %q", got) |
| 151 | } |
| 152 | // Web link stays beside Page link (build note 81), by mouse and |
| 153 | // by ⇧⌘K. |
| 154 | drive(t, rig, chromedp.Click(`[mapa-bubble] button[aria-label="Web link"]`), |
| 155 | chromedp.WaitVisible(`[mapa-bubble-field]`), |
| 156 | chromedp.SendKeys(`[mapa-bubble-field]`, "https://example.com"), |
| 157 | enter()) |
| 158 | if got := typed(t, rig); got != "plain [word](https://example.com)" { |
| 159 | t.Fatalf("Web link made %q", got) |
| 160 | } |
| 161 | drive(t, rig, selectWords("plain"), |
| 162 | chromedp.KeyEvent("K", chromedp.KeyModifiers( |
| 163 | chromedp.ModifierShift|modifier())), |
| 164 | chromedp.WaitVisible(`[mapa-bubble-field]`), |
| 165 | chromedp.SendKeys(`[mapa-bubble-field]`, "https://example.org"), |
| 166 | enter()) |
| 167 | if got := typed(t, rig); got != "[plain](https://example.org) "+ |
| 168 | "[word](https://example.com)" { |
| 169 | t.Fatalf("⇧⌘K made %q", got) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // L10's way out: Tab in a list indents; Esc, then Tab, leaves. |
| 174 | func TestEscThenTabLeavesTheEditor(t *testing.T) { |
| 175 | rig, _ := browserRig(t, 0) |
| 176 | emptyDay(t, rig, "2026-11-05") |
| 177 | drive(t, rig, chromedp.KeyEvent("- one"), enter(), |
| 178 | chromedp.KeyEvent("two"), chromedp.KeyEvent(kb.Tab)) |
| 179 | if got := typed(t, rig); got != "- one\n - two" { |
| 180 | t.Fatalf("Tab in a list made %q", got) |
| 181 | } |
| 182 | drive(t, rig, chromedp.KeyEvent(kb.Escape), chromedp.KeyEvent(kb.Tab), |
| 183 | chromedp.Sleep(100*time.Millisecond)) |
| 184 | inside := read(t, rig, chromedp.Evaluate[bool]( |
| 185 | `mapaEditor.view.dom.contains(document.activeElement)`)) |
| 186 | if inside { |
| 187 | t.Fatal("Esc, then Tab, stayed in the editor") |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func TestAPlainClickOnAChipFollowsIt(t *testing.T) { |
| 192 | rig, store := browserRig(t, 0) |
| 193 | page, err := store.CreateTeamPage(context.Background(), Bo, "Target", |
| 194 | "Here.") |
| 195 | if err != nil { |
| 196 | t.Fatal(err) |
| 197 | } |
| 198 | teamPage(t, rig, store, "Go to [[Target]].") |
| 199 | drive(t, rig, chromedp.Click(`article a[mapa-chip="team"]`)) |
| 200 | arrive(t, rig, mapa.PagePath(page)) |
| 201 | } |
| 202 | |
| 203 | // §6.7 and L9: Make team page from a selection commits the note, takes |
| 204 | // the block numbers from what it committed, and leaves a link. |
| 205 | func TestMakeTeamPageFromASelection(t *testing.T) { |
| 206 | rig, _ := browserRig(t, 0) |
| 207 | emptyDay(t, rig, "2026-11-06") |
| 208 | drive(t, rig, chromedp.KeyEvent("Keep this."), enter(), |
| 209 | chromedp.KeyEvent("# Pilot logins"), enter(), |
| 210 | chromedp.KeyEvent("Share this one."), |
| 211 | chromedp.KeyEvent(kb.ArrowUp, chromedp.KeyModifiers( |
| 212 | chromedp.ModifierShift)), |
| 213 | chromedp.WaitVisible(`[mapa-bubble] [aria-label="Make team page"]`), |
| 214 | chromedp.Click(`[mapa-bubble] [aria-label="Make team page"]`), |
| 215 | chromedp.WaitVisible(`dialog[mapa-make-team] input`)) |
| 216 | tinted := read(t, rig, chromedp.Evaluate[int]( |
| 217 | `document.querySelectorAll("article [mapa-moving]").length`)) |
| 218 | title := read(t, rig, chromedp.Value(`dialog[mapa-make-team] input`)) |
| 219 | if tinted != 2 || title != "Pilot logins" { |
| 220 | t.Fatalf("widened to %d blocks, titled %q", tinted, title) |
| 221 | } |
| 222 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function () { |
| 223 | var box = document.querySelector("dialog[mapa-make-team]"); |
| 224 | window.__clicked = performance.now(); |
| 225 | box.addEventListener("close", function () { |
| 226 | window.__held = performance.now() - __clicked; |
| 227 | }); |
| 228 | })()`), chromedp.Click(`dialog[mapa-make-team] [type="submit"]`), |
| 229 | chromedp.WaitVisible(`[mapa-editor-notice] a`)) |
| 230 | // L11: the button holds its spinner for 650ms. |
| 231 | held := read(t, rig, chromedp.Poll[float64](`window.__held`)) |
| 232 | if held < 650 { |
| 233 | t.Fatalf("Make team page answered after %.0fms", held) |
| 234 | } |
| 235 | if got := typed(t, rig); got != "Keep this.\n\n[[Pilot logins]]" { |
| 236 | t.Fatalf("the note holds %q", got) |
| 237 | } |
| 238 | href := read(t, rig, chromedp.AttributeValue(`[mapa-editor-notice] a`, |
| 239 | "href")).Value |
| 240 | drive(t, rig, chromedp.Navigate(rig.Origin+href), |
| 241 | chromedp.WaitVisible(mounted)) |
| 242 | if got := typed(t, rig); got != "# Pilot logins\n\nShare this one." { |
| 243 | t.Fatalf("the team page holds %q", got) |
| 244 | } |
| 245 | } |
| 246 | |