oficina / mapa Public

Browser gates for the in-page title

Click, type, Enter renames with the header, sidebar, tab title and
links offer following, no reload; Esc writes nothing; a taken title is
refused inline and keeps the text; the header and the H1 never send
two renames; daily notes, scratch pages and History are not editable
(the team page is the control); entering the title moves nothing at
three widths and the caret lands where clicked; a rename leaves the
body's typing and draft alone, with no rebase; axe passes in each
state.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev d6a615ca203faab4a6b70481da5549a9e22c38ae parent 12cb459
2 files changed, +415 −0
  • internal/mapatest/title_browser_test.go +249 −0
  • internal/mapatest/title_quality_browser_test.go +166 −0
diff --git a/internal/mapatest/title_browser_test.go b/internal/mapatest/title_browser_test.go
new file mode 100644
index 0000000..ae606f8
--- /dev/null
+++ b/internal/mapatest/title_browser_test.go
@@ -0,0 +1,249 @@
+//go:build browser
+
+package mapatest
+
+import (
+ "context"
+ "strings"
+ "testing"
+ "time"
+
+ "amadan.net/rastrillo/rastrillo/harness"
+ "github.com/chromedp/chromedp"
+ "github.com/chromedp/chromedp/kb"
+
+ "amadan.net/oficina/mapa/internal/mapa"
+)
+
+// The in-page title (level-up §4.1, build note 77), in a real Chromium
+// through the real server: click it, type, Enter renames, and the
+// header, the sidebar, the tab's title and the links offer follow with
+// no reload; Esc writes nothing; a refusal is said beside it and keeps
+// the text; the header and the H1 never send two renames; daily notes
+// and scratch pages are not editable; entering it moves nothing; a
+// rename leaves the body's typing and its draft alone; and axe passes.
+// The shared state's logic is editor/title.test.mjs.
+
+// titleText is the H1's editable text, once the script has made it so.
+const titleText = `h1[mapa-title][mapa-rename] [mapa-title-text][contenteditable]`
+
+// headerTitle is the bar's title field.
+const headerTitle = `form[estilo-bar-title] input[estilo-bar-title-input]`
+
+// titleOf is a page's current title and how many versions it has.
+func titleOf(t *testing.T, store *mapa.Store, pageID int64) (string, int) {
+ t.Helper()
+ ctx := context.Background()
+ viewer, _ := store.Viewer(ctx, Ana, nil)
+ history, err := store.History(ctx, viewer, pageID, 0)
+ if err != nil || len(history.Versions) == 0 {
+ t.Fatalf("history: %v", err)
+ }
+ chosen, err := store.ChooseVersion(ctx, viewer, pageID,
+ history.Versions[0].Revision)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return chosen.Record.Title, len(history.Versions)
+}
+
+// titledPage is a team page Bo wrote, pinned by Ana so the sidebar
+// shows it, opened in Ana's editor.
+func titledPage(t *testing.T, rig *harness.Rig, store *mapa.Store,
+ title, body string) mapa.Page {
+ t.Helper()
+ ctx := context.Background()
+ page, err := store.CreateTeamPage(ctx, Bo, title, body)
+ if err != nil {
+ t.Fatal(err)
+ }
+ viewer, err := store.Viewer(ctx, Ana, nil)
+ if err == nil {
+ err = store.SetPin(ctx, viewer, page.ID, true)
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900),
+ chromedp.Navigate(rig.Origin+mapa.PagePath(page)),
+ chromedp.WaitVisible(mounted), chromedp.WaitVisible(titleText),
+ chromedp.Evaluate[chromedp.Void](`window.__sameLoad = true`))
+ return page
+}
+
+// editTitle clicks the H1's text and puts the caret at its end.
+func editTitle() chromedp.Action[chromedp.Void] {
+ return chromedp.Steps(chromedp.Click(titleText),
+ chromedp.KeyEvent(kb.End))
+}
+
+// selectTitle clicks the H1's text and selects all of it, so typing
+// replaces it.
+func selectTitle() chromedp.Action[chromedp.Void] {
+ return chromedp.Steps(chromedp.Click(titleText),
+ chromedp.Evaluate[chromedp.Void](`(function (text) {
+ var range = document.createRange();
+ range.selectNodeContents(text);
+ getSelection().removeAllRanges();
+ getSelection().addRange(range);
+})(document.querySelector("[mapa-title-text]"))`))
+}
+
+func evaluate(t *testing.T, rig *harness.Rig, script string) string {
+ t.Helper()
+ return read(t, rig, chromedp.Evaluate[string](script))
+}
+
+func TestTitleClickTypeEnterRenamesInPlace(t *testing.T) {
+ rig, store := browserRig(t, 0)
+ if _, err := store.CreateTeamPage(context.Background(), Bo, "Plan",
+ "Call [[Halden]]."); err != nil {
+ t.Fatal(err)
+ }
+ page := titledPage(t, rig, store, "Halden", "A robotics customer.")
+ drive(t, rig, editTitle(), chromedp.KeyEvent(" Robotics"),
+ chromedp.KeyEvent("\r"))
+ eventually(t, "the rename", func() bool {
+ title, _ := titleOf(t, store, page.ID)
+ return title == "Halden Robotics"
+ })
+ notice := `[mapa-title-note] button[rst-btn="primary"]`
+ drive(t, rig, chromedp.WaitVisible(notice))
+ got := evaluate(t, rig, `[
+ document.querySelector("h1[mapa-title]").textContent,
+ document.querySelector("form[estilo-bar-title] input").value,
+ document.title,
+ document.querySelector('[mapa-side] a[mapa-side-page][href="`+
+ mapa.PagePath(page)+`"]').textContent.trim(),
+ mapaEditor.view.dom.getAttribute("aria-label"),
+ document.querySelector("[mapa-title-note] p").textContent,
+ String(window.__sameLoad)].join("|")`)
+ want := "Halden Robotics|Halden Robotics|Halden Robotics · Mapa|" +
+ "Halden Robotics|Page text, Halden Robotics|Renamed. 1 page you " +
+ "can edit link here by an old title."
+ if !strings.HasPrefix(got, want) || !strings.HasSuffix(got, "|true") {
+ t.Fatalf("after the rename:\n got %s\nwant %s…|true", got, want)
+ }
+ drive(t, rig, chromedp.Click(notice))
+ eventually(t, "the links updated", func() bool {
+ return strings.Contains(evaluate(t, rig,
+ `document.querySelector("[mapa-title-note]").textContent`),
+ "Updated links in 1 page.")
+ })
+}
+
+func TestTitleEscRevertsAndWritesNothing(t *testing.T) {
+ rig, store := browserRig(t, 0)
+ page := titledPage(t, rig, store, "Halden", "One.")
+ _, versions := titleOf(t, store, page.ID)
+ drive(t, rig, editTitle(), chromedp.KeyEvent(" Gone"),
+ chromedp.KeyEvent(kb.Escape))
+ time.Sleep(1500 * time.Millisecond)
+ if title, now := titleOf(t, store, page.ID); title != "Halden" ||
+ now != versions {
+ t.Fatalf("Esc wrote: %q, %d versions", title, now)
+ }
+ got := evaluate(t, rig, `[document.querySelector("h1[mapa-title]").textContent,
+ document.activeElement === document.querySelector("[mapa-title-text]"),
+ document.querySelector("[mapa-title-note]").textContent].join("|")`)
+ if got != "Halden|false|" {
+ t.Fatalf("after Esc: %q", got)
+ }
+}
+
+func TestTitleRefusalIsInlineAndKeepsTheText(t *testing.T) {
+ rig, store := browserRig(t, 0)
+ if _, err := store.CreateTeamPage(context.Background(), Bo, "Acme",
+ "Taken."); err != nil {
+ t.Fatal(err)
+ }
+ page := titledPage(t, rig, store, "Halden", "One.")
+ drive(t, rig, selectTitle(),
+ chromedp.KeyEvent("acme"), chromedp.KeyEvent("\r"),
+ chromedp.WaitVisible(`[mapa-title-note] [role="alert"]`))
+ got := evaluate(t, rig, `[document.querySelector("h1[mapa-title]").textContent,
+ document.querySelector("[mapa-title-note] [role=alert]").textContent,
+ document.querySelector("[mapa-title-text]").getAttribute("aria-invalid"),
+ document.querySelector("form[estilo-bar-title] input").value].join("|")`)
+ if !strings.HasPrefix(got, "acme|A page with that title already exists. "+
+ "Open the page with that title|true|Halden") {
+ t.Fatalf("the refusal: %q", got)
+ }
+ if title, _ := titleOf(t, store, page.ID); title != "Halden" {
+ t.Fatalf("a refused title was written: %q", title)
+ }
+}
+
+// The header and the H1 share one pending state: the H1's rename is
+// out (held at least 650ms by the busy rule) when the header's Enter
+// comes, and only one rename is sent; then the other way round.
+func TestTitleHeaderAndH1NeverDoubleSubmit(t *testing.T) {
+ rig, store := browserRig(t, 0)
+ page := titledPage(t, rig, store, "Halden", "One.")
+ _, versions := titleOf(t, store, page.ID)
+ drive(t, rig, editTitle(), chromedp.KeyEvent(" One"),
+ chromedp.Focus(headerTitle))
+ pending := evaluate(t, rig, `(function (input) {
+ return [input.value, input.readOnly,
+ document.querySelector("[mapa-title-note]").textContent].join("|");
+})(document.querySelector("form[estilo-bar-title] input"))`)
+ if pending != "Halden One|true|Renaming…" {
+ t.Fatalf("while the H1's rename is out, the header shows %q",
+ pending)
+ }
+ drive(t, rig, chromedp.KeyEvent(" Two"), chromedp.KeyEvent("\r"))
+ eventually(t, "the H1's rename", func() bool {
+ title, _ := titleOf(t, store, page.ID)
+ return title == "Halden One"
+ })
+ time.Sleep(1500 * time.Millisecond)
+ if title, now := titleOf(t, store, page.ID); title != "Halden One" ||
+ now != versions+1 {
+ t.Fatalf("two renames: %q, %d versions after %d", title, now,
+ versions)
+ }
+ drive(t, rig, chromedp.WaitNotPresent(`[mapa-title-note] [rst-spin]`),
+ chromedp.SetValue(headerTitle, "Halden Three"),
+ chromedp.Focus(headerTitle), chromedp.KeyEvent("\r"),
+ chromedp.Click(titleText), chromedp.KeyEvent("Four"),
+ chromedp.KeyEvent("\r"))
+ eventually(t, "the header's rename", func() bool {
+ title, _ := titleOf(t, store, page.ID)
+ return title == "Halden Three"
+ })
+ time.Sleep(1500 * time.Millisecond)
+ if title, now := titleOf(t, store, page.ID); title != "Halden Three" ||
+ now != versions+2 {
+ t.Fatalf("the header then the H1: %q, %d versions", title, now)
+ }
+ if got := text(t, rig, "h1[mapa-title]"); got != "Halden Three" {
+ t.Fatalf("the H1 shows %q", got)
+ }
+}
+
+func TestTitleIsNotEditableOnDailyOrScratchPages(t *testing.T) {
+ rig, store := browserRig(t, 0)
+ scratch, err := store.CreateScratch(context.Background(), Ana, "Mine.")
+ if err != nil {
+ t.Fatal(err)
+ }
+ team, err := store.CreateTeamPage(context.Background(), Bo, "Halden",
+ "One.")
+ if err != nil {
+ t.Fatal(err)
+ }
+ // The team page is the control: its title is editable, so the same
+ // question on the others means what it says.
+ for _, path := range []string{mapa.PagePath(team), "/", "/d/2026-11-12",
+ mapa.PagePath(scratch), "/scratch/new",
+ mapa.PagePath(team) + "/history"} {
+ drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900),
+ chromedp.Navigate(rig.Origin+path), chromedp.WaitVisible(mounted))
+ want := map[bool]string{true: "1", false: "0"}[path ==
+ mapa.PagePath(team)]
+ if got := evaluate(t, rig, `String(document.querySelectorAll(
+ "h1[mapa-title] [contenteditable], h1[mapa-title][contenteditable]").length)`); got != want {
+ t.Errorf("%s: %s editable titles, want %s", path, got, want)
+ }
+ }
+}
diff --git a/internal/mapatest/title_quality_browser_test.go b/internal/mapatest/title_quality_browser_test.go
new file mode 100644
index 0000000..c3779c8
--- /dev/null
+++ b/internal/mapatest/title_quality_browser_test.go
@@ -0,0 +1,166 @@
+//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 77): 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)
+ }
+}