| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/rastrillo/rastrillo/harness" |
| 13 | "github.com/chromedp/cdproto/cdp" |
| 14 | "github.com/chromedp/cdproto/input" |
| 15 | "github.com/chromedp/chromedp" |
| 16 | |
| 17 | "amadan.net/oficina/mapa/internal/mapa" |
| 18 | ) |
| 19 | |
| 20 | // The page's empty writing area takes a click (level-up build note |
| 21 | // 84): below the text, beside a line and in the margins around the |
| 22 | // column, the click focuses the editor and places the caret; the |
| 23 | // title, links, chips and the panel keep doing their own thing. |
| 24 | |
| 25 | // caret is where the editor's selection is, and whether it has focus. |
| 26 | type caret struct { |
| 27 | Focused bool |
| 28 | From int |
| 29 | To int |
| 30 | Offset int // the caret's offset in its textblock |
| 31 | Length int // that textblock's length |
| 32 | Text string |
| 33 | Last bool // the textblock is the document's last block |
| 34 | Dirty bool // the saver has something to commit |
| 35 | } |
| 36 | |
| 37 | const readCaret = `JSON.stringify((function (view) { |
| 38 | var sel = view.state.selection, $from = sel.$from; |
| 39 | return { |
| 40 | Focused: view.hasFocus(), From: sel.from, To: sel.to, |
| 41 | Offset: $from.parentOffset, Length: $from.parent.content.size, |
| 42 | Text: $from.parent.textContent, |
| 43 | Last: $from.depth === 1 && $from.index(0) === view.state.doc.childCount - 1, |
| 44 | Dirty: mapaEditor.saver.committable() }; |
| 45 | })(mapaEditor.view))` |
| 46 | |
| 47 | func caretNow(t *testing.T, rig *harness.Rig) caret { |
| 48 | t.Helper() |
| 49 | var got caret |
| 50 | raw := read(t, rig, chromedp.Evaluate[string](readCaret)) |
| 51 | if err := json.Unmarshal([]byte(raw), &got); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | return got |
| 55 | } |
| 56 | |
| 57 | // rect is an element's box, as the page lays it out. |
| 58 | type rect struct{ Left, Top, Right, Bottom float64 } |
| 59 | |
| 60 | func box(t *testing.T, rig *harness.Rig, selector string) rect { |
| 61 | t.Helper() |
| 62 | var got rect |
| 63 | raw := read(t, rig, chromedp.Evaluate[string](fmt.Sprintf( |
| 64 | `JSON.stringify((function (b) { |
| 65 | return {Left: b.left, Top: b.top, Right: b.right, Bottom: b.bottom}; |
| 66 | })(document.querySelector(%q).getBoundingClientRect()))`, selector))) |
| 67 | if err := json.Unmarshal([]byte(raw), &got); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | return got |
| 71 | } |
| 72 | |
| 73 | // blur takes focus out of the page's text. |
| 74 | func blur(t *testing.T, rig *harness.Rig) { |
| 75 | t.Helper() |
| 76 | drive(t, rig, chromedp.Evaluate[chromedp.Void]( |
| 77 | `document.activeElement && document.activeElement.blur()`)) |
| 78 | } |
| 79 | |
| 80 | func TestAClickFarBelowAnEmptyDayWritesThere(t *testing.T) { |
| 81 | rig, _ := browserRig(t, 0) |
| 82 | drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), |
| 83 | chromedp.Navigate(rig.Origin+"/d/2026-11-03"), |
| 84 | chromedp.WaitVisible(mounted)) |
| 85 | column := box(t, rig, "article[mapa-body]") |
| 86 | drive(t, rig, chromedp.MouseClickXY((column.Left+column.Right)/2, 820)) |
| 87 | got := caretNow(t, rig) |
| 88 | if !got.Focused || got.From != 1 || got.Length != 0 || got.Dirty { |
| 89 | t.Fatalf("a click far below the placeholder: %+v", got) |
| 90 | } |
| 91 | if cursor := read(t, rig, chromedp.Evaluate[string]( |
| 92 | `getComputedStyle(document.elementFromPoint(`+ |
| 93 | fmt.Sprint((column.Left+column.Right)/2)+ |
| 94 | `, 820)).cursor`)); cursor != "text" { |
| 95 | t.Errorf("the empty area's cursor is %q", cursor) |
| 96 | } |
| 97 | drive(t, rig, chromedp.KeyEvent("Hello there")) |
| 98 | if got := typed(t, rig); got != "Hello there" { |
| 99 | t.Fatalf("typed %q", got) |
| 100 | } |
| 101 | if found := axeSerious(t, rig); found != "" { |
| 102 | t.Fatalf("axe, after the click:\n%s", found) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | const besideBody = "A long first line that runs well across the column " + |
| 107 | "before it ends.\n\nShort.\n\n- an item\n- the last item" |
| 108 | |
| 109 | func TestAClickBelowOrBesideTheTextPlacesTheCaret(t *testing.T) { |
| 110 | rig, store := browserRig(t, 0) |
| 111 | teamPage(t, rig, store, besideBody) |
| 112 | column := box(t, rig, "article[mapa-body]") |
| 113 | short := box(t, rig, "article[mapa-body] > p:nth-child(2)") |
| 114 | middle := (short.Top + short.Bottom) / 2 |
| 115 | // Below the last block: the end of the document, which after a list |
| 116 | // is the empty paragraph the editor keeps; nothing is written. |
| 117 | drive(t, rig, chromedp.MouseClickXY(column.Left+40, column.Bottom-30)) |
| 118 | if got := caretNow(t, rig); !got.Focused || !got.Last || |
| 119 | got.Length != 0 || got.Dirty { |
| 120 | t.Fatalf("below the list: %+v", got) |
| 121 | } |
| 122 | if got := typed(t, rig); got != besideBody { |
| 123 | t.Fatalf("a click wrote %q", got) |
| 124 | } |
| 125 | blur(t, rig) |
| 126 | drive(t, rig, chromedp.MouseClickXY(column.Right+24, middle)) |
| 127 | if got := caretNow(t, rig); !got.Focused || got.Text != "Short." || |
| 128 | got.Offset != got.Length { |
| 129 | t.Fatalf("the right margin beside Short.: %+v", got) |
| 130 | } |
| 131 | drive(t, rig, chromedp.MouseClickXY(column.Left-24, middle)) |
| 132 | if got := caretNow(t, rig); got.Text != "Short." || got.Offset != 0 { |
| 133 | t.Fatalf("the left margin beside Short.: %+v", got) |
| 134 | } |
| 135 | // A selection elsewhere: a plain click is a plain click. |
| 136 | drive(t, rig, chromedp.Evaluate[chromedp.Void]( |
| 137 | `mapaEditor.view.dispatch(mapaEditor.view.state.tr.setSelection( |
| 138 | mapaEditor.view.state.selection.constructor.create( |
| 139 | mapaEditor.view.state.doc, 2, 20)))`), |
| 140 | chromedp.MouseClickXY(column.Right+24, middle)) |
| 141 | if got := caretNow(t, rig); got.From != got.To || |
| 142 | got.Text != "Short." { |
| 143 | t.Fatalf("a click with a selection elsewhere: %+v", got) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestAClickBelowAParagraphEndsIt(t *testing.T) { |
| 148 | rig, store := browserRig(t, 0) |
| 149 | teamPage(t, rig, store, "- a list\n\nThe end, which is a paragraph.") |
| 150 | column := box(t, rig, "article[mapa-body]") |
| 151 | drive(t, rig, chromedp.MouseClickXY(column.Right-10, column.Bottom-30)) |
| 152 | got := caretNow(t, rig) |
| 153 | if !got.Focused || !got.Last || got.Offset != got.Length || |
| 154 | got.Text != "The end, which is a paragraph." { |
| 155 | t.Fatalf("below the last paragraph: %+v", got) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // noFollow keeps the page from leaving on a link's click, so where |
| 160 | // focus went can be read afterwards. |
| 161 | const noFollow = `document.addEventListener("click", function (event) { |
| 162 | if (event.target.closest("a")) event.preventDefault(); |
| 163 | }, true)` |
| 164 | |
| 165 | func TestClicksOnThePagesOwnControlsKeepOutOfTheBody(t *testing.T) { |
| 166 | rig, store := browserRig(t, 0) |
| 167 | if _, err := store.CreateTeamPage(context.Background(), Bo, "Target", |
| 168 | "Here."); err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | teamPage(t, rig, store, "Go to [[Target]].") |
| 172 | column := box(t, rig, "article[mapa-body]") |
| 173 | // The empty area does take the click, so what follows is a choice. |
| 174 | drive(t, rig, chromedp.MouseClickXY(column.Right+24, column.Bottom-30)) |
| 175 | if !caretNow(t, rig).Focused { |
| 176 | t.Fatal("the empty area took no click") |
| 177 | } |
| 178 | drive(t, rig, chromedp.Evaluate[chromedp.Void](noFollow)) |
| 179 | for _, selector := range []string{ |
| 180 | "h1[mapa-title] [mapa-title-text]", |
| 181 | "a[mapa-doc-edit]", |
| 182 | `aside[mapa-links] a[mapa-chip="team"]`, |
| 183 | "aside[mapa-links] h2", |
| 184 | } { |
| 185 | blur(t, rig) |
| 186 | drive(t, rig, chromedp.Click(selector)) |
| 187 | if got := caretNow(t, rig); got.Focused { |
| 188 | t.Errorf("a click on %s went into the body: %+v", selector, got) |
| 189 | } |
| 190 | } |
| 191 | // A chip in the text follows it, as before. |
| 192 | blur(t, rig) |
| 193 | drive(t, rig, chromedp.Click(`article a[mapa-chip="team"]`)) |
| 194 | eventually(t, "the chip leads to Target", func() bool { |
| 195 | return strings.Contains(read(t, rig, chromedp.Title()), "Target") |
| 196 | }) |
| 197 | } |
| 198 | |
| 199 | // gesture is a touch gesture, as a phone sends it. |
| 200 | func gesture[P any](command cdp.Command[P, cdp.Empty], |
| 201 | params P) chromedp.Action[chromedp.Void] { |
| 202 | return chromedp.Func(func(ctx context.Context, |
| 203 | target *chromedp.Target) error { |
| 204 | _, err := cdp.Call(ctx, target, command, params) |
| 205 | return err |
| 206 | }) |
| 207 | } |
| 208 | |
| 209 | func TestATapFocusesAndAScrollDoesNot(t *testing.T) { |
| 210 | rig, _ := browserRig(t, 0) |
| 211 | drive(t, rig, scripts(true), |
| 212 | chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile), |
| 213 | chromedp.Navigate(rig.Origin+"/d/2026-11-04"), |
| 214 | chromedp.WaitVisible(mounted)) |
| 215 | column := box(t, rig, "article[mapa-body]") |
| 216 | across, down := (column.Left+column.Right)/2, column.Bottom-40 |
| 217 | drive(t, rig, gesture(input.SynthesizeScrollGesture, |
| 218 | input.SynthesizeScrollGestureParams{X: across, Y: down, |
| 219 | YDistance: -240, |
| 220 | GestureSourceType: input.GestureSourceTypeTouch})) |
| 221 | if got := caretNow(t, rig); got.Focused { |
| 222 | t.Fatalf("a scroll from the empty area focused it: %+v", got) |
| 223 | } |
| 224 | drive(t, rig, gesture(input.SynthesizeTapGesture, |
| 225 | input.SynthesizeTapGestureParams{X: across, Y: down, |
| 226 | GestureSourceType: input.GestureSourceTypeTouch})) |
| 227 | if got := caretNow(t, rig); !got.Focused || got.From != 1 { |
| 228 | t.Fatalf("a tap far below the placeholder: %+v", got) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func TestAClickBelowTheMarkdownScreenWritesInIt(t *testing.T) { |
| 233 | rig, store := browserRig(t, 0) |
| 234 | page, err := store.CreateTeamPage(context.Background(), Bo, "Plain", |
| 235 | "One line.") |
| 236 | if err != nil { |
| 237 | t.Fatal(err) |
| 238 | } |
| 239 | // A screen tall enough to leave room under the preview, whose form |
| 240 | // now ends with Upload and insert (images §4.4); the press is |
| 241 | // measured from what it must miss, not from a fixed height. |
| 242 | drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 1000), |
| 243 | chromedp.Navigate(rig.Origin+mapa.PagePath(page)+"/edit"), |
| 244 | chromedp.WaitVisible("textarea[mapa-editor]"), |
| 245 | chromedp.WaitVisible("article[mapa-preview] p")) |
| 246 | below := read(t, rig, chromedp.Evaluate[float64](`Math.max( |
| 247 | document.querySelector("form[mapa-edit-form]").getBoundingClientRect() |
| 248 | .bottom, document.querySelector("article[mapa-preview]") |
| 249 | .getBoundingClientRect().bottom) + 30`)) |
| 250 | if below > 990 { |
| 251 | t.Fatalf("no room below the form and its preview: %v", below) |
| 252 | } |
| 253 | drive(t, rig, chromedp.MouseClickXY(700, below)) |
| 254 | got := read(t, rig, chromedp.Evaluate[string](`(function (area) { |
| 255 | return [document.activeElement === area, area.selectionStart, |
| 256 | area.value.length].join(" "); |
| 257 | })(document.querySelector("textarea[mapa-editor]"))`)) |
| 258 | if got != "true 9 9" { |
| 259 | t.Fatalf("below the form: focused, caret, length = %s", got) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | func TestAClickInTheMarginMidCompositionEndsIt(t *testing.T) { |
| 264 | rig, store := browserRig(t, 0) |
| 265 | teamPage(t, rig, store, "First line.\n\nShort.") |
| 266 | column := box(t, rig, "article[mapa-body]") |
| 267 | drive(t, rig, caretAt(0), composing("にほん")) |
| 268 | if !read(t, rig, chromedp.Evaluate[bool](`mapaEditor.view.composing`)) { |
| 269 | t.Fatal("no composition began") |
| 270 | } |
| 271 | drive(t, rig, chromedp.MouseClickXY(column.Right+24, column.Bottom-30)) |
| 272 | eventually(t, "the composition ends", func() bool { |
| 273 | return !read(t, rig, chromedp.Evaluate[bool]( |
| 274 | `mapaEditor.view.composing`)) |
| 275 | }) |
| 276 | eventually(t, "the composed text is read in", func() bool { |
| 277 | return strings.Contains(typed(t, rig), "First line.にほん") |
| 278 | }) |
| 279 | } |
| 280 | |