| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/chromedp/chromedp" |
| 11 | "github.com/chromedp/chromedp/kb" |
| 12 | |
| 13 | "amadan.net/oficina/mapa/internal/mapa" |
| 14 | ) |
| 15 | |
| 16 | // The in-page title's quality gates (build note 78): no shift, the |
| 17 | // body's typing and draft untouched by a rename, and axe. |
| 18 | |
| 19 | // headBoxes is the page head's boxes (the kicker, the H1, the editable |
| 20 | // text) and the body's first blocks, with the H1's face and size. |
| 21 | const headBoxes = `JSON.stringify(Array.prototype.map.call( |
| 22 | document.querySelectorAll("[mapa-doc-kicker], h1[mapa-title], " + |
| 23 | "[mapa-title-text], article[mapa-body] > *"), |
| 24 | function (node) { |
| 25 | var box = node.getBoundingClientRect(); |
| 26 | return [node.nodeName, box.left, box.top, box.width, box.height].join(" "); |
| 27 | }).concat([getComputedStyle(document.querySelector("h1[mapa-title]")).font]))` |
| 28 | |
| 29 | // L5's rule for the title: the boxes the server drew (scripts off) are |
| 30 | // the boxes after the mount, and entering the title to edit it (a |
| 31 | // click) moves nothing either, at three widths; the caret lands where |
| 32 | // the click fell, inside the text, not at an end. |
| 33 | func TestTitleEditingMovesNothing(t *testing.T) { |
| 34 | rig, store := browserRig(t, 0) |
| 35 | page, err := store.CreateTeamPage(context.Background(), Bo, |
| 36 | "Halden Robotics and the long title that wraps on a phone", |
| 37 | "One.\n\nTwo.") |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | path := rig.Origin + mapa.PagePath(page) |
| 42 | for _, width := range []int64{1440, 1000, 390} { |
| 43 | drive(t, rig, scripts(false), chromedp.EmulateViewport(width, 900), |
| 44 | chromedp.Navigate(path), chromedp.WaitVisible("h1[mapa-title]")) |
| 45 | server := evaluate(t, rig, headBoxes) |
| 46 | drive(t, rig, scripts(true), chromedp.Navigate(path), |
| 47 | chromedp.WaitVisible(titleText)) |
| 48 | mounted := evaluate(t, rig, headBoxes) |
| 49 | drive(t, rig, chromedp.Click(titleText)) |
| 50 | editing := evaluate(t, rig, headBoxes) |
| 51 | // The note the mount adds after the H1 is empty, and takes no |
| 52 | // room until a rename says something. |
| 53 | if note := evaluate(t, rig, `String(document.querySelector( |
| 54 | "[mapa-title-note]").getBoundingClientRect().height)`); note != "0" { |
| 55 | t.Errorf("%dpx: the empty note is %spx high", width, note) |
| 56 | } |
| 57 | if mounted != server { |
| 58 | t.Errorf("%dpx: the mount moved the head:\nserver %s\nmount %s", |
| 59 | width, server, mounted) |
| 60 | } |
| 61 | if editing != mounted { |
| 62 | t.Errorf("%dpx: editing moved the head:\nbefore %s\nafter %s", |
| 63 | width, mounted, editing) |
| 64 | } |
| 65 | caret := evaluate(t, rig, `(function (s) { |
| 66 | var text = document.querySelector("[mapa-title-text]"); |
| 67 | return [text.contains(s.anchorNode), s.anchorOffset > 0 && |
| 68 | s.anchorOffset < s.anchorNode.textContent.length].join("|"); |
| 69 | })(getSelection())`) |
| 70 | if caret != "true|true" { |
| 71 | t.Errorf("%dpx: the caret is not where the click fell: %s", width, |
| 72 | caret) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // postRecorder keeps the body of every POST the page sends. |
| 78 | const postRecorder = `(function () { |
| 79 | window.__posts = []; |
| 80 | var send = window.fetch; |
| 81 | window.fetch = function (url, init) { |
| 82 | if (init && init.method === "POST") __posts.push(String(init.body)); |
| 83 | return send.apply(this, arguments); |
| 84 | }; |
| 85 | })()` |
| 86 | |
| 87 | // A rename while the body holds typing, stored as a draft and not yet: |
| 88 | // the typing survives, no conflict or merge is shown, the saver's base |
| 89 | // is the rename's revision, and the commit holds exactly the typing. |
| 90 | // The draft is carried to the rename's revision, so nothing is merged |
| 91 | // or rebased against a revision that changed only the title. |
| 92 | func TestTitleRenameLeavesTheBodysTypingAlone(t *testing.T) { |
| 93 | rig, store := browserRig(t, 0) |
| 94 | drive(t, rig, onNewDocument(postRecorder)) |
| 95 | page := titledPage(t, rig, store, "Halden", "One.") |
| 96 | drive(t, rig, caretAt(0), chromedp.KeyEvent(" Stored.")) |
| 97 | eventually(t, "a draft", func() bool { |
| 98 | _, found := draftOf(t, store, page.ID) |
| 99 | return found |
| 100 | }) |
| 101 | drive(t, rig, caretAt(0), chromedp.KeyEvent(" Unsent."), editTitle(), |
| 102 | chromedp.KeyEvent(" Robotics"), chromedp.KeyEvent("\r")) |
| 103 | eventually(t, "the rename", func() bool { |
| 104 | title, _ := titleOf(t, store, page.ID) |
| 105 | return title == "Halden Robotics" |
| 106 | }) |
| 107 | revision, _ := current(t, store, page.ID) |
| 108 | eventually(t, "the saver on the rename's revision", func() bool { |
| 109 | return evaluate(t, rig, `mapaEditor.saver.base`) == |
| 110 | fmt.Sprint(revision) |
| 111 | }) |
| 112 | drive(t, rig, caretAt(0), chromedp.KeyEvent(" After."), |
| 113 | chromedp.KeyEvent("s", mod())) |
| 114 | eventually(t, "the commit", func() bool { |
| 115 | _, body := current(t, store, page.ID) |
| 116 | _, found := draftOf(t, store, page.ID) |
| 117 | return body == "One. Stored. Unsent. After." && !found |
| 118 | }) |
| 119 | shown := evaluate(t, rig, `String(document.querySelectorAll( |
| 120 | "[mapa-editor-notice], [mapa-conflict]").length)`) |
| 121 | if shown != "0" || typed(t, rig) != "One. Stored. Unsent. After." { |
| 122 | t.Fatalf("after the rename: %s notices, text %q", shown, |
| 123 | typed(t, rig)) |
| 124 | } |
| 125 | if rebased := evaluate(t, rig, `__posts.filter(function (body) { |
| 126 | return /(^|&)rebase=/.test(body); }).join("\n")`); rebased != "" { |
| 127 | t.Fatalf("the rename made the editor rebase: %s", rebased) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // L10 for the title: axe finds nothing serious with the title editable, |
| 132 | // with a refusal beside it, and with the links offer; the field is a |
| 133 | // named textbox inside the heading, which keeps the title as its name. |
| 134 | func TestTitlePassesAxe(t *testing.T) { |
| 135 | rig, store := browserRig(t, 0) |
| 136 | ctx := context.Background() |
| 137 | for _, title := range []string{"Acme", "Plan"} { |
| 138 | if _, err := store.CreateTeamPage(ctx, Bo, title, |
| 139 | "Call [[Halden]]."); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | } |
| 143 | titledPage(t, rig, store, "Halden", "One.") |
| 144 | field := evaluate(t, rig, `(function (text) { |
| 145 | return [text.getAttribute("role"), text.getAttribute("aria-label"), |
| 146 | text.closest("h1").textContent].join("|"); |
| 147 | })(document.querySelector("[mapa-title-text]"))`) |
| 148 | if field != "textbox|Title, renames the page|Halden" { |
| 149 | t.Fatalf("the title field is %q", field) |
| 150 | } |
| 151 | if found := axeSerious(t, rig); found != "" { |
| 152 | t.Fatalf("axe, the title editable:\n%s", found) |
| 153 | } |
| 154 | drive(t, rig, selectTitle(), |
| 155 | chromedp.KeyEvent("Acme"), chromedp.KeyEvent("\r"), |
| 156 | chromedp.WaitVisible(`[mapa-title-note] [role="alert"]`)) |
| 157 | if found := axeSerious(t, rig); found != "" { |
| 158 | t.Fatalf("axe, a refusal:\n%s", found) |
| 159 | } |
| 160 | drive(t, rig, chromedp.KeyEvent(kb.Escape), editTitle(), |
| 161 | chromedp.KeyEvent(" Robotics"), chromedp.KeyEvent("\r"), |
| 162 | chromedp.WaitVisible(`[mapa-title-note] button`)) |
| 163 | if found := axeSerious(t, rig); found != "" { |
| 164 | t.Fatalf("axe, the links offer:\n%s", found) |
| 165 | } |
| 166 | } |
| 167 | |