//go:build browser package mapatest import ( "context" "encoding/json" "fmt" "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" "amadan.net/oficina/mapa/internal/mapa" ) // The page's empty writing area takes a click (level-up build note // 84): below the text, beside a line and in the margins around the // column, the click focuses the editor and places the caret; the // title, links, chips and the panel keep doing their own thing. // caret is where the editor's selection is, and whether it has focus. type caret struct { Focused bool From int To int Offset int // the caret's offset in its textblock Length int // that textblock's length Text string Last bool // the textblock is the document's last block Dirty bool // the saver has something to commit } const readCaret = `JSON.stringify((function (view) { var sel = view.state.selection, $from = sel.$from; return { Focused: view.hasFocus(), From: sel.from, To: sel.to, Offset: $from.parentOffset, Length: $from.parent.content.size, Text: $from.parent.textContent, Last: $from.depth === 1 && $from.index(0) === view.state.doc.childCount - 1, Dirty: mapaEditor.saver.committable() }; })(mapaEditor.view))` func caretNow(t *testing.T, rig *harness.Rig) caret { t.Helper() var got caret raw := read(t, rig, chromedp.Evaluate[string](readCaret)) if err := json.Unmarshal([]byte(raw), &got); err != nil { t.Fatal(err) } return got } // rect is an element's box, as the page lays it out. type rect struct{ Left, Top, Right, Bottom float64 } func box(t *testing.T, rig *harness.Rig, selector string) rect { t.Helper() var got rect raw := read(t, rig, chromedp.Evaluate[string](fmt.Sprintf( `JSON.stringify((function (b) { return {Left: b.left, Top: b.top, Right: b.right, Bottom: b.bottom}; })(document.querySelector(%q).getBoundingClientRect()))`, selector))) if err := json.Unmarshal([]byte(raw), &got); err != nil { t.Fatal(err) } return got } // blur takes focus out of the page's text. func blur(t *testing.T, rig *harness.Rig) { t.Helper() drive(t, rig, chromedp.Evaluate[chromedp.Void]( `document.activeElement && document.activeElement.blur()`)) } func TestAClickFarBelowAnEmptyDayWritesThere(t *testing.T) { rig, _ := browserRig(t, 0) drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), chromedp.Navigate(rig.Origin+"/d/2026-11-03"), chromedp.WaitVisible(mounted)) column := box(t, rig, "article[mapa-body]") drive(t, rig, chromedp.MouseClickXY((column.Left+column.Right)/2, 820)) got := caretNow(t, rig) if !got.Focused || got.From != 1 || got.Length != 0 || got.Dirty { t.Fatalf("a click far below the placeholder: %+v", got) } if cursor := read(t, rig, chromedp.Evaluate[string]( `getComputedStyle(document.elementFromPoint(`+ fmt.Sprint((column.Left+column.Right)/2)+ `, 820)).cursor`)); cursor != "text" { t.Errorf("the empty area's cursor is %q", cursor) } drive(t, rig, chromedp.KeyEvent("Hello there")) if got := typed(t, rig); got != "Hello there" { t.Fatalf("typed %q", got) } if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, after the click:\n%s", found) } } const besideBody = "A long first line that runs well across the column " + "before it ends.\n\nShort.\n\n- an item\n- the last item" func TestAClickBelowOrBesideTheTextPlacesTheCaret(t *testing.T) { rig, store := browserRig(t, 0) teamPage(t, rig, store, besideBody) column := box(t, rig, "article[mapa-body]") short := box(t, rig, "article[mapa-body] > p:nth-child(2)") middle := (short.Top + short.Bottom) / 2 // Below the last block: the end of the document, which after a list // is the empty paragraph the editor keeps; nothing is written. drive(t, rig, chromedp.MouseClickXY(column.Left+40, column.Bottom-30)) if got := caretNow(t, rig); !got.Focused || !got.Last || got.Length != 0 || got.Dirty { t.Fatalf("below the list: %+v", got) } if got := typed(t, rig); got != besideBody { t.Fatalf("a click wrote %q", got) } blur(t, rig) drive(t, rig, chromedp.MouseClickXY(column.Right+24, middle)) if got := caretNow(t, rig); !got.Focused || got.Text != "Short." || got.Offset != got.Length { t.Fatalf("the right margin beside Short.: %+v", got) } drive(t, rig, chromedp.MouseClickXY(column.Left-24, middle)) if got := caretNow(t, rig); got.Text != "Short." || got.Offset != 0 { t.Fatalf("the left margin beside Short.: %+v", got) } // A selection elsewhere: a plain click is a plain click. drive(t, rig, chromedp.Evaluate[chromedp.Void]( `mapaEditor.view.dispatch(mapaEditor.view.state.tr.setSelection( mapaEditor.view.state.selection.constructor.create( mapaEditor.view.state.doc, 2, 20)))`), chromedp.MouseClickXY(column.Right+24, middle)) if got := caretNow(t, rig); got.From != got.To || got.Text != "Short." { t.Fatalf("a click with a selection elsewhere: %+v", got) } } func TestAClickBelowAParagraphEndsIt(t *testing.T) { rig, store := browserRig(t, 0) teamPage(t, rig, store, "- a list\n\nThe end, which is a paragraph.") column := box(t, rig, "article[mapa-body]") drive(t, rig, chromedp.MouseClickXY(column.Right-10, column.Bottom-30)) got := caretNow(t, rig) if !got.Focused || !got.Last || got.Offset != got.Length || got.Text != "The end, which is a paragraph." { t.Fatalf("below the last paragraph: %+v", got) } } // noFollow keeps the page from leaving on a link's click, so where // focus went can be read afterwards. const noFollow = `document.addEventListener("click", function (event) { if (event.target.closest("a")) event.preventDefault(); }, true)` func TestClicksOnThePagesOwnControlsKeepOutOfTheBody(t *testing.T) { rig, store := browserRig(t, 0) if _, err := store.CreateTeamPage(context.Background(), Bo, "Target", "Here."); err != nil { t.Fatal(err) } teamPage(t, rig, store, "Go to [[Target]].") column := box(t, rig, "article[mapa-body]") // The empty area does take the click, so what follows is a choice. drive(t, rig, chromedp.MouseClickXY(column.Right+24, column.Bottom-30)) if !caretNow(t, rig).Focused { t.Fatal("the empty area took no click") } drive(t, rig, chromedp.Evaluate[chromedp.Void](noFollow)) for _, selector := range []string{ "h1[mapa-title] [mapa-title-text]", "a[mapa-doc-edit]", `aside[mapa-links] a[mapa-chip="team"]`, "aside[mapa-links] h2", } { blur(t, rig) drive(t, rig, chromedp.Click(selector)) if got := caretNow(t, rig); got.Focused { t.Errorf("a click on %s went into the body: %+v", selector, got) } } // A chip in the text follows it, as before. blur(t, rig) drive(t, rig, chromedp.Click(`article a[mapa-chip="team"]`)) eventually(t, "the chip leads to Target", func() bool { return strings.Contains(read(t, rig, chromedp.Title()), "Target") }) } // gesture is a touch gesture, as a phone sends it. func gesture[P any](command cdp.Command[P, cdp.Empty], params P) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { _, err := cdp.Call(ctx, target, command, params) return err }) } func TestATapFocusesAndAScrollDoesNot(t *testing.T) { rig, _ := browserRig(t, 0) drive(t, rig, scripts(true), chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile), chromedp.Navigate(rig.Origin+"/d/2026-11-04"), chromedp.WaitVisible(mounted)) column := box(t, rig, "article[mapa-body]") across, down := (column.Left+column.Right)/2, column.Bottom-40 drive(t, rig, gesture(input.SynthesizeScrollGesture, input.SynthesizeScrollGestureParams{X: across, Y: down, YDistance: -240, GestureSourceType: input.GestureSourceTypeTouch})) if got := caretNow(t, rig); got.Focused { t.Fatalf("a scroll from the empty area focused it: %+v", got) } drive(t, rig, gesture(input.SynthesizeTapGesture, input.SynthesizeTapGestureParams{X: across, Y: down, GestureSourceType: input.GestureSourceTypeTouch})) if got := caretNow(t, rig); !got.Focused || got.From != 1 { t.Fatalf("a tap far below the placeholder: %+v", got) } } func TestAClickBelowTheMarkdownScreenWritesInIt(t *testing.T) { rig, store := browserRig(t, 0) page, err := store.CreateTeamPage(context.Background(), Bo, "Plain", "One line.") if err != nil { t.Fatal(err) } // A screen tall enough to leave room under the preview, whose form // now ends with Upload and insert (images §4.4); the press is // measured from what it must miss, not from a fixed height. drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 1000), chromedp.Navigate(rig.Origin+mapa.PagePath(page)+"/edit"), chromedp.WaitVisible("textarea[mapa-editor]"), chromedp.WaitVisible("article[mapa-preview] p")) below := read(t, rig, chromedp.Evaluate[float64](`Math.max( document.querySelector("form[mapa-edit-form]").getBoundingClientRect() .bottom, document.querySelector("article[mapa-preview]") .getBoundingClientRect().bottom) + 30`)) if below > 990 { t.Fatalf("no room below the form and its preview: %v", below) } drive(t, rig, chromedp.MouseClickXY(700, below)) got := read(t, rig, chromedp.Evaluate[string](`(function (area) { return [document.activeElement === area, area.selectionStart, area.value.length].join(" "); })(document.querySelector("textarea[mapa-editor]"))`)) if got != "true 9 9" { t.Fatalf("below the form: focused, caret, length = %s", got) } } func TestAClickInTheMarginMidCompositionEndsIt(t *testing.T) { rig, store := browserRig(t, 0) teamPage(t, rig, store, "First line.\n\nShort.") column := box(t, rig, "article[mapa-body]") drive(t, rig, caretAt(0), composing("にほん")) if !read(t, rig, chromedp.Evaluate[bool](`mapaEditor.view.composing`)) { t.Fatal("no composition began") } drive(t, rig, chromedp.MouseClickXY(column.Right+24, column.Bottom-30)) eventually(t, "the composition ends", func() bool { return !read(t, rig, chromedp.Evaluate[bool]( `mapaEditor.view.composing`)) }) eventually(t, "the composed text is read in", func() bool { return strings.Contains(typed(t, rig), "First line.にほん") }) }