//go:build browser package mapatest import ( "context" "fmt" "testing" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" "amadan.net/oficina/mapa/internal/mapa" ) // The in-page title's quality gates (build note 78): no shift, the // body's typing and draft untouched by a rename, and axe. // headBoxes is the page head's boxes (the kicker, the H1, the editable // text) and the body's first blocks, with the H1's face and size. const headBoxes = `JSON.stringify(Array.prototype.map.call( document.querySelectorAll("[mapa-doc-kicker], h1[mapa-title], " + "[mapa-title-text], article[mapa-body] > *"), function (node) { var box = node.getBoundingClientRect(); return [node.nodeName, box.left, box.top, box.width, box.height].join(" "); }).concat([getComputedStyle(document.querySelector("h1[mapa-title]")).font]))` // L5's rule for the title: the boxes the server drew (scripts off) are // the boxes after the mount, and entering the title to edit it (a // click) moves nothing either, at three widths; the caret lands where // the click fell, inside the text, not at an end. func TestTitleEditingMovesNothing(t *testing.T) { rig, store := browserRig(t, 0) page, err := store.CreateTeamPage(context.Background(), Bo, "Halden Robotics and the long title that wraps on a phone", "One.\n\nTwo.") if err != nil { t.Fatal(err) } path := rig.Origin + mapa.PagePath(page) for _, width := range []int64{1440, 1000, 390} { drive(t, rig, scripts(false), chromedp.EmulateViewport(width, 900), chromedp.Navigate(path), chromedp.WaitVisible("h1[mapa-title]")) server := evaluate(t, rig, headBoxes) drive(t, rig, scripts(true), chromedp.Navigate(path), chromedp.WaitVisible(titleText)) mounted := evaluate(t, rig, headBoxes) drive(t, rig, chromedp.Click(titleText)) editing := evaluate(t, rig, headBoxes) // The note the mount adds after the H1 is empty, and takes no // room until a rename says something. if note := evaluate(t, rig, `String(document.querySelector( "[mapa-title-note]").getBoundingClientRect().height)`); note != "0" { t.Errorf("%dpx: the empty note is %spx high", width, note) } if mounted != server { t.Errorf("%dpx: the mount moved the head:\nserver %s\nmount %s", width, server, mounted) } if editing != mounted { t.Errorf("%dpx: editing moved the head:\nbefore %s\nafter %s", width, mounted, editing) } caret := evaluate(t, rig, `(function (s) { var text = document.querySelector("[mapa-title-text]"); return [text.contains(s.anchorNode), s.anchorOffset > 0 && s.anchorOffset < s.anchorNode.textContent.length].join("|"); })(getSelection())`) if caret != "true|true" { t.Errorf("%dpx: the caret is not where the click fell: %s", width, caret) } } } // postRecorder keeps the body of every POST the page sends. const postRecorder = `(function () { window.__posts = []; var send = window.fetch; window.fetch = function (url, init) { if (init && init.method === "POST") __posts.push(String(init.body)); return send.apply(this, arguments); }; })()` // A rename while the body holds typing, stored as a draft and not yet: // the typing survives, no conflict or merge is shown, the saver's base // is the rename's revision, and the commit holds exactly the typing. // The draft is carried to the rename's revision, so nothing is merged // or rebased against a revision that changed only the title. func TestTitleRenameLeavesTheBodysTypingAlone(t *testing.T) { rig, store := browserRig(t, 0) drive(t, rig, onNewDocument(postRecorder)) page := titledPage(t, rig, store, "Halden", "One.") drive(t, rig, caretAt(0), chromedp.KeyEvent(" Stored.")) eventually(t, "a draft", func() bool { _, found := draftOf(t, store, page.ID) return found }) drive(t, rig, caretAt(0), chromedp.KeyEvent(" Unsent."), editTitle(), chromedp.KeyEvent(" Robotics"), chromedp.KeyEvent("\r")) eventually(t, "the rename", func() bool { title, _ := titleOf(t, store, page.ID) return title == "Halden Robotics" }) revision, _ := current(t, store, page.ID) eventually(t, "the saver on the rename's revision", func() bool { return evaluate(t, rig, `mapaEditor.saver.base`) == fmt.Sprint(revision) }) drive(t, rig, caretAt(0), chromedp.KeyEvent(" After."), chromedp.KeyEvent("s", mod())) eventually(t, "the commit", func() bool { _, body := current(t, store, page.ID) _, found := draftOf(t, store, page.ID) return body == "One. Stored. Unsent. After." && !found }) shown := evaluate(t, rig, `String(document.querySelectorAll( "[mapa-editor-notice], [mapa-conflict]").length)`) if shown != "0" || typed(t, rig) != "One. Stored. Unsent. After." { t.Fatalf("after the rename: %s notices, text %q", shown, typed(t, rig)) } if rebased := evaluate(t, rig, `__posts.filter(function (body) { return /(^|&)rebase=/.test(body); }).join("\n")`); rebased != "" { t.Fatalf("the rename made the editor rebase: %s", rebased) } } // L10 for the title: axe finds nothing serious with the title editable, // with a refusal beside it, and with the links offer; the field is a // named textbox inside the heading, which keeps the title as its name. func TestTitlePassesAxe(t *testing.T) { rig, store := browserRig(t, 0) ctx := context.Background() for _, title := range []string{"Acme", "Plan"} { if _, err := store.CreateTeamPage(ctx, Bo, title, "Call [[Halden]]."); err != nil { t.Fatal(err) } } titledPage(t, rig, store, "Halden", "One.") field := evaluate(t, rig, `(function (text) { return [text.getAttribute("role"), text.getAttribute("aria-label"), text.closest("h1").textContent].join("|"); })(document.querySelector("[mapa-title-text]"))`) if field != "textbox|Title, renames the page|Halden" { t.Fatalf("the title field is %q", field) } if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, the title editable:\n%s", found) } drive(t, rig, selectTitle(), chromedp.KeyEvent("Acme"), chromedp.KeyEvent("\r"), chromedp.WaitVisible(`[mapa-title-note] [role="alert"]`)) if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, a refusal:\n%s", found) } drive(t, rig, chromedp.KeyEvent(kb.Escape), editTitle(), chromedp.KeyEvent(" Robotics"), chromedp.KeyEvent("\r"), chromedp.WaitVisible(`[mapa-title-note] button`)) if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, the links offer:\n%s", found) } }