//go:build browser package mapatest import ( "context" "encoding/json" "slices" "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" "amadan.net/oficina/mapa/internal/mapa" ) // IN5 and IN8 (columns and icons spec §5.3, §9) in a real Chromium: the // arrow opens the pages inside at once, with script, by Enter, → and ←, // and keeps focus; a page shown twice keeps two list ids and one lit // row; opened rows are on the first paint, and mounting moves none of // them; axe finds nothing serious. func TestIN8TheArrowByKeyboard(t *testing.T) { rig, store := browserRig(t, 0) ctx := context.Background() projects, err := store.CreateTeamPage(ctx, Bo, "Projects", "See.") if err != nil { t.Fatal(err) } for _, title := range []string{"Alpha", "Beta"} { if _, err := store.CreateTeamPage(ctx, Bo, title, "In: [[Projects]]\n\nText."); err != nil { t.Fatal(err) } } titledPage(t, rig, store, "Desk", "Work.") viewer, _ := store.Viewer(ctx, Ana, nil) if err := store.SetPin(ctx, viewer, projects.ID, true); err != nil { t.Fatal(err) } drive(t, rig, chromedp.Reload(), chromedp.WaitVisible(mounted), chromedp.Focus(`[mapa-side-arrow][aria-label="Pages inside Projects"]`), chromedp.KeyEvent(kb.Enter), chromedp.WaitVisible(`ul[mapa-side-inside]`)) focused := evaluate(t, rig, `document.activeElement.getAttribute("aria-expanded")`) if focused != "true" { t.Fatalf("focus after opening: aria-expanded %q", focused) } if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, a row open:\n%s", found) } drive(t, rig, chromedp.Navigate(rig.Origin+mapa.PagePath(projects)), chromedp.WaitVisible(`ul[mapa-side-inside] [mapa-side-label]`)) } // sideState is the sidebar's open lists' ids, its aria-controls, its // lit rows that show, and the focused element's name. const sideState = `JSON.stringify((function () { var nav = document.querySelector("nav[mapa-side]"); var all = function (selector) { return [].slice.call(nav.querySelectorAll(selector)); }; var ids = all("ul[mapa-side-inside]").map(function (list) { return list.id; }); var controls = all("[aria-controls]").map(function (button) { return button.getAttribute("aria-controls"); }); var lit = all('[aria-current="page"]').filter(function (row) { return !row.hidden; }).length; return { ids: ids, controls: controls, lit: lit, focus: document.activeElement.getAttribute("aria-label") || "" }; })())` type sideReading struct { IDs []string `json:"ids"` Controls []string `json:"controls"` Lit int `json:"lit"` Focus string `json:"focus"` } func readSide(t *testing.T, rig *harness.Rig) sideReading { t.Helper() var reading sideReading raw := read(t, rig, chromedp.Evaluate[string](sideState)) if err := json.Unmarshal([]byte(raw), &reading); err != nil { t.Fatal(err) } return reading } // IN8 with script: Inner is pinned and inside pinned Outer, so it shows // twice. Opening each by → keeps every list id distinct and named by its // button, one lit row, and focus on the arrow pressed; ← closes it. func TestIN8ArrowKeysAndAPageShownTwice(t *testing.T) { rig, store := browserRig(t, 0) ctx := context.Background() viewer, _ := store.Viewer(ctx, Ana, nil) var leaf mapa.Page for _, each := range []struct{ title, body string }{ {"Outer", "Text."}, {"Inner", "In: [[Outer]]"}, {"Leaf", "In: [[Inner]]\n\nWords."}} { page, err := store.CreateTeamPage(ctx, Bo, each.title, each.body) if err != nil { t.Fatal(err) } leaf = page if each.title != "Leaf" { if err := store.SetPin(ctx, viewer, page.ID, true); err != nil { t.Fatal(err) } } } drive(t, rig, chromedp.EmulateViewport(1440, 900), chromedp.Navigate(rig.Origin+mapa.PagePath(leaf)), chromedp.WaitVisible(mounted)) press := func(label string, key string) { drive(t, rig, chromedp.Focus(`[mapa-side-arrow][aria-label="`+ label+`"]`), chromedp.KeyEvent(key)) } press("Pages inside Outer", kb.ArrowRight) eventually(t, "Outer open", func() bool { return len(readSide(t, rig).IDs) == 1 }) // Inner's pinned row is the second arrow named for Inner. drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelectorAll( '[mapa-side-arrow][aria-label="Pages inside Inner"]')[1].focus()`), chromedp.KeyEvent(kb.ArrowRight)) eventually(t, "Inner open in both places", func() bool { return len(readSide(t, rig).IDs) == 3 }) reading := readSide(t, rig) seen := map[string]bool{} for _, id := range reading.IDs { if seen[id] || !slices.Contains(reading.Controls, id) { t.Fatalf("list ids %v, controls %v", reading.IDs, reading.Controls) } seen[id] = true } if reading.Lit != 1 || reading.Focus != "Pages inside Inner" { t.Fatalf("after opening: %+v", reading) } focused := evaluate(t, rig, `String([].indexOf.call( document.querySelectorAll( '[mapa-side-arrow][aria-label="Pages inside Inner"]'), document.activeElement))`) if focused != "1" { t.Fatalf("focus went to Inner's arrow %s, not the one pressed", focused) } drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft)) eventually(t, "Inner closed", func() bool { return len(readSide(t, rig).IDs) == 1 }) if reading := readSide(t, rig); reading.Lit != 1 { t.Fatalf("after closing: %+v", reading) } } // sideBoxes is every sidebar row's and open list's box. const sideBoxes = `JSON.stringify([].map.call( document.querySelectorAll("nav[mapa-side] [mapa-side-row], " + "nav[mapa-side] ul[mapa-side-inside]"), function (row) { var box = row.getBoundingClientRect(); return [box.left, box.top, box.width, box.height].join(" "); }))` // IN5: with the cookie set, opened rows are on the server's HTML, and // mounting moves none of the sidebar's rows. func TestIN5OpenRowsAreOnTheFirstPaint(t *testing.T) { rig, store := browserRig(t, 0) ctx := context.Background() projects, err := store.CreateTeamPage(ctx, Bo, "Projects", "See.") if err != nil { t.Fatal(err) } if _, err := store.CreateTeamPage(ctx, Bo, "Alpha", "In: [[Projects]]\n\nText."); err != nil { t.Fatal(err) } viewer, _ := store.Viewer(ctx, Ana, nil) if err := store.SetPin(ctx, viewer, projects.ID, true); err != nil { t.Fatal(err) } drive(t, rig, onNewDocument(shiftRecorder), openCookie(rig, strings.TrimPrefix(mapa.PagePath(projects), "/p/"))) load := func(live bool) string { drive(t, rig, scripts(live), chromedp.EmulateViewport(1440, 900), chromedp.Navigate(rig.Origin+mapa.PagePath(projects)), chromedp.WaitVisible(`ul[mapa-side-inside]`)) if live { drive(t, rig, chromedp.WaitVisible(mounted)) } return read(t, rig, chromedp.Evaluate[string](sideBoxes)) } if before, after := load(false), load(true); before != after { t.Fatalf("the sidebar moved on mounting:\n%s\n%s", before, after) } shifts := evaluate(t, rig, `JSON.stringify(window.__shifts || [])`) if shifts != "[]" { t.Fatalf("layout shifts %s", shifts) } } // openCookie sets the mapa-open cookie. func openCookie(rig *harness.Rig, value string) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: "mapa-open", Value: value, URL: rig.Origin + "/"}) return err }) }