//go:build browser package mapatest import ( "context" "fmt" "strings" "testing" "time" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" "amadan.net/oficina/mapa/internal/mapa" ) // Paul's ruling of 2026-11-16 on the sidebar (build note 80), in real // Chromium, scripts off and on, open and in the rail: no note of your // own in Recently edited, in the rail's tooltips or in the Menu sheet, // and exactly one row the person can see with the full highlight. // litShown is the label of every element with aria-current="page" that // is on screen, the sidebar's and the bar's alike. const litShown = `Array.prototype.filter.call( document.querySelectorAll('[aria-current="page"]'), function (el) { return el.checkVisibility(); } ).map(function (el) { var label = el.querySelector("[mapa-side-label]") || el; return label.textContent.trim(); }).join(" | ")` // sidebarText is all the sidebar and the bar's nav hold, shown or not: // the rail's tooltips are its labels. const sidebarText = `[document.querySelector("[mapa-side]"), document.querySelector("[estilo-bar-nav]")].map(function (el) { return el ? el.textContent : ""; }).join("\n")` // ownNotes are Ana's: a team page she wrote, a pinned team page, a // scratch page, a pinned scratch page and today's note. type ownNotes struct { team, pinned, scratch, pinnedNote, day, theirs string } func writeOwnNotes(t *testing.T, store *mapa.Store) ownNotes { t.Helper() ctx := context.Background() // Ana's viewer is read once her first write has made her a member. viewer := func() mapa.Viewer { found, err := store.Viewer(ctx, Ana, nil) if err != nil { t.Fatal(err) } return found } page := func(made mapa.Page, err error) string { t.Helper() if err != nil { t.Fatal(err) } return mapa.PagePath(made) } pin := func(path string, made mapa.Page) { if err := store.SetPin(ctx, viewer(), made.ID, true); err != nil { t.Fatalf("pinning %s: %v", path, err) } } notes := ownNotes{} team, err := store.CreateTeamPage(ctx, Ana, "Quarterly plan", "Text.") notes.team = page(team, err) pinned, err := store.CreateTeamPage(ctx, Ana, "Pinned plan", "Text.") notes.pinned = page(pinned, err) pin(notes.pinned, pinned) scratch, err := store.CreateScratch(ctx, Ana, secretLine+"\n\nMore.") notes.scratch = page(scratch, err) note, err := store.CreateScratch(ctx, Ana, "Pinned note") notes.pinnedNote = page(note, err) pin(notes.pinnedNote, note) theirs, err := store.CreateTeamPage(ctx, Bo, "Bo's page", "Text.") notes.theirs = page(theirs, err) notes.day = "/d/" + time.Now().UTC().Format(time.DateOnly) if _, _, err := store.SaveDay(ctx, viewer(), notes.day[3:], 0, 0, "Daily words."); err != nil { t.Fatal(err) } return notes } func TestTheSidebarHasOneHighlightAndNoPrivateLines(t *testing.T) { rig, store := browserRig(t, 0) notes := writeOwnNotes(t, store) pinnedNote := "Pinned note, only you see this" cases := []struct{ path, open, rail string }{ {notes.scratch, "Scratch", "Scratch"}, {"/", "Today", "Today"}, {notes.day, "Today", "Today"}, {"/d/2025-01-06", "Today", "Today"}, {notes.team, "Quarterly plan", "All pages"}, {notes.pinned, "Pinned plan", "All pages"}, {notes.theirs, "All pages", "All pages"}, {notes.pinnedNote, pinnedNote, "Scratch"}, {"/pages", "All pages", "All pages"}, } for _, live := range []bool{false, true} { for _, side := range sideCases { for _, each := range cases { name := fmt.Sprintf("%s at %dpx, %s (cookie %q), scripts %v", each.path, side.width, side.show, side.cookie, live) drive(t, rig, scripts(live), sideCookie(rig, side.cookie), chromedp.EmulateViewport(side.width, 877), chromedp.Navigate(rig.Origin+each.path), chromedp.WaitVisible("[mapa-side]")) want := each.open if side.show == "rail" { want = each.rail } if got := read(t, rig, chromedp.Evaluate[string]( litShown)); got != want { t.Errorf("%s: lit %q, want %q", name, got, want) } held := read(t, rig, chromedp.Evaluate[string](sidebarText)) if strings.Contains(held, secretLine) { t.Errorf("%s: a scratch page's first line is in the "+ "sidebar", name) } } } } } // Below 800px the places are rows in estilo's Menu sheet: no note of // your own is among them, and the page's place is the one lit. func TestTheMenuSheetHoldsNoPrivateLines(t *testing.T) { rig, store := browserRig(t, 0) notes := writeOwnNotes(t, store) for _, live := range []bool{false, true} { drive(t, rig, scripts(live), sideCookie(rig, ""), chromedp.EmulateViewport(390, 844), chromedp.Navigate(rig.Origin+notes.team), chromedp.WaitVisible("main h1")) held := read(t, rig, chromedp.Evaluate[string](sidebarText)) lit := read(t, rig, chromedp.Evaluate[string]( `Array.prototype.map.call(document.querySelectorAll( '[estilo-bar-nav] [aria-current="page"]'), function (el) { return el.textContent.trim(); }).join(" | ")`)) if strings.Contains(held, secretLine) || lit != "All pages" { t.Errorf("scripts %v: the Menu sheet lights %q; private line "+ "shown: %v", live, lit, strings.Contains(held, secretLine)) } } } // ⌘\ folds the sidebar in place, with script: the highlight goes from // the page's row to its place, and back. func TestFoldingInPlaceMovesTheHighlight(t *testing.T) { rig, store := browserRig(t, 0) notes := writeOwnNotes(t, store) drive(t, rig, scripts(true), sideCookie(rig, "open"), chromedp.EmulateViewport(1400, 877), chromedp.Navigate(rig.Origin+notes.team), chromedp.WaitVisible("[mapa-side]")) for _, want := range []string{"Quarterly plan", "All pages", "Quarterly plan"} { if got := read(t, rig, chromedp.Evaluate[string]( litShown)); got != want { t.Fatalf("lit %q, want %q", got, want) } drive(t, rig, chromedp.KeyEvent(`\`, mod()), chromedp.Sleep(50*time.Millisecond)) } drive(t, rig, chromedp.KeyEvent(kb.Escape)) }