//go:build browser package mapatest import ( "context" "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "amadan.net/oficina/mapa/internal/mapa" ) // What an open description field does when the page around it moves // (build note I22, Fable M2, L3, L6): leaving by a link, closing the // tab and ⌘S take what was typed with them; a block drawn again keeps // it; another window taking focus does not close it, and coming back // to somewhere else does. // bareImagePage is a page holding one image with no description, opened // in Ana's editor, with a word typed so that leaving commits. func bareImagePage(t *testing.T, rig *harness.Rig, store *mapa.Store) mapa.Page { t.Helper() bare := addImage(t, store, Bo, 600, 300) page := teamPage(t, rig, store, "Photo:\n\n"+bare.Markdown("")+ "\n\nBelow.") drive(t, rig, caretAt(2), chromedp.KeyEvent(" Edited.")) eventually(t, "a draft", func() bool { _, found := draftOf(t, store, page.ID) return found }) drive(t, rig, chromedp.Click(`[mapa-alt-badge="missing"]`), chromedp.WaitVisible(altField)) return page } // commitHolds waits for the page's committed text to hold want. func commitHolds(t *testing.T, store *mapa.Store, page mapa.Page, want string) { t.Helper() eventually(t, "the commit holds "+want, func() bool { _, body := current(t, store, page.ID) return strings.Contains(body, want) }) } // A link pressed without taking focus (Safari's tap does not) leaves // with the description in the commit. func TestAltGoesWithALinkPressed(t *testing.T) { rig, store := browserRig(t, 0) page := bareImagePage(t, rig, store) drive(t, rig, chromedp.KeyEvent("A harbour"), chromedp.Evaluate[chromedp.Void]( `document.querySelector('[mapa-side] a[href="/pages"]').click()`)) arrive(t, rig, "/pages") commitHolds(t, store, page, "![A harbour](/i/") // One commit, the leaving one, and it held the description: not a // commit without it, mended by the tab's closing after (Fable M2). viewer, _ := store.Viewer(context.Background(), Ana, nil) history, err := store.History(context.Background(), viewer, page.ID, 0) if err != nil || len(history.Versions) != 2 { t.Fatalf("%d versions after leaving, not 2: %v", len(history.Versions), err) } } // Closing the tab (here, going elsewhere by address) commits it too. func TestAltGoesWithTheTabClosing(t *testing.T) { rig, store := browserRig(t, 0) page := bareImagePage(t, rig, store) drive(t, rig, chromedp.KeyEvent("A quay"), chromedp.Navigate(rig.Origin+"/pages")) commitHolds(t, store, page, "![A quay](/i/") } // ⌘S in the field keeps the text and saves the page. func TestAltSavesWithCommandS(t *testing.T) { rig, store := browserRig(t, 0) page := bareImagePage(t, rig, store) drive(t, rig, chromedp.KeyEvent("A pier"), chromedp.KeyEvent("s", mod())) commitHolds(t, store, page, "![A pier](/i/") if read(t, rig, chromedp.Evaluate[bool]( `document.querySelector('[mapa-alt]') !== null`)) { t.Fatal("the field is still open after ⌘S") } } // The image's block drawn again while the field is open (a rebase, the // inline choice, here a heading made of it): the field goes with the // image and keeps its focus and its text (Fable L6). func TestAltSurvivesItsBlockDrawnAgain(t *testing.T) { rig, store := browserRig(t, 0) bareImagePage(t, rig, store) drive(t, rig, chromedp.KeyEvent("Kept on redraw"), chromedp.Evaluate[chromedp.Void](`(function () { var view = mapaEditor.view, found = -1; view.state.doc.descendants(function (node, pos) { if (found < 0 && node.type.name === "image") found = pos; }); var heading = view.state.schema.nodes.heading; view.dispatch(view.state.tr.setBlockType(found, found + 1, heading, { level: 2 })); })()`), settle()) if !read(t, rig, chromedp.Evaluate[bool](`document.activeElement === document.querySelector('[mapa-alt] textarea')`)) { t.Fatal("the field lost its focus when its block was drawn again") } drive(t, rig, chromedp.KeyEvent(" too"), enter()) saidAlt(t, rig, "Kept on redraw too") } // Another window taking focus does not close the field; focus coming // back to somewhere else does, keeping the text. func TestAltWaitsForAnotherWindow(t *testing.T) { rig, store := browserRig(t, 0) bareImagePage(t, rig, store) drive(t, rig, chromedp.KeyEvent("Away and back"), chromedp.Evaluate[chromedp.Void](`(function () { document.hasFocus = function () { return false; }; document.querySelector('[mapa-alt] textarea').blur(); })()`), settle()) if read(t, rig, chromedp.Evaluate[bool]( `document.querySelector('[mapa-alt]') === null`)) { t.Fatal("another window taking focus closed the field") } drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function () { delete document.hasFocus; window.dispatchEvent(new Event("focus")); })()`)) saidAlt(t, rig, "Away and back") } // A press on the field's label or its band keeps the field open, with // focus in it (Fable M1). func TestAltStaysOpenWhenItsLabelIsPressed(t *testing.T) { rig, store := browserRig(t, 0) bareImagePage(t, rig, store) drive(t, rig, chromedp.KeyEvent("Still"), chromedp.Click(`[mapa-alt] label`), settle()) if !read(t, rig, chromedp.Evaluate[bool](`document.activeElement === document.querySelector('[mapa-alt] textarea')`)) { t.Fatal("pressing the label closed the field") } drive(t, rig, chromedp.KeyEvent(" here"), enter()) saidAlt(t, rig, "Still here") } // A long description on a short image stays inside the image's box: the // field scrolls instead of growing past the picture's top (Fable L1). func TestAltOnAShortImageStaysInside(t *testing.T) { rig, store := browserRig(t, 0) page := teamPage(t, rig, store, "Strip:\n\nBelow.") upload(t, rig, page, 600, 120) drive(t, rig, chromedp.KeyEvent(strings.Repeat("A long strip of sky. ", 20))) inside := read(t, rig, chromedp.Evaluate[bool](`(function () { var wrap = document.querySelector('[mapa-alt]').getBoundingClientRect(); var pic = document.querySelector('article[mapa-body] img[src^="/i/"]') .getBoundingClientRect(); return wrap.top >= pic.top - 0.5 && wrap.bottom <= pic.bottom + 0.5; })()`)) if !inside { t.Fatal("a long description grew the field past the picture") } } // settle lets the page's timers run: a wait the page itself measures, // since a sleep on this side may find the page's timers held. func settle() chromedp.Action[chromedp.Void] { return chromedp.Evaluate[chromedp.Void](`new Promise(function (done) { setTimeout(function () { setTimeout(done, 50); }, 50); })`, chromedp.EvalAwaitPromise) }