//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 78), 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) } // The row's ⋯ is renamed with its row (build note 88). more := evaluate(t, rig, `document.querySelector( '[mapa-side] a[mapa-side-page][href="`+mapa.PagePath(page)+ `"] + [mapa-row-menu] [mapa-row-more]').getAttribute("aria-label")`) if more != "More actions for Halden Robotics" { t.Fatalf("after the rename the row's ⋯ is %q", more) } 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)] got := evaluate(t, rig, `String(document.querySelectorAll( "h1[mapa-title] [contenteditable], h1[mapa-title][contenteditable]" ).length)`) if got != want { t.Errorf("%s: %s editable titles, want %s", path, got, want) } } }