//go:build browser package mapatest import ( "context" "encoding/json" "testing" "time" "amadan.net/rastrillo/fiesta/check/rhythm" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "amadan.net/oficina/mapa/internal/mapa" ) // Every signed-in screen keeps the spacing scale's rhythm (level-up // build note 89): Fiesta's audit over the page column, at a desktop and // a phone width, scripts off and on. Between two blocks every gap is a // step of the scale, no text sits closer than --rst-sp-2 to a control, // and every heading is closer to its own content than to the block // before it. // rhythmSkip is what the audit neither measures nor walks into: the // page's body, the editor's document, whose own typographic rhythm in // em (prose.css) is held by gates L1 to L5. The audit stops at a // skipped block's edges too, so pageGaps measures them. The Links // panel needs no skip: it is fixed, and the audit passes over fixed // boxes. var rhythmSkip = []string{"[mapa-body]"} func TestEveryScreenKeepsTheRhythm(t *testing.T) { rig, store := browserRig(t, 0) page, err := store.CreateTeamPage(context.Background(), Bo, "Short", "One line, linking [[Acme]].") if err != nil { t.Fatal(err) } path := mapa.PagePath(page) drive(t, rig, chromedp.Navigate(rig.Origin+path+"/history"), chromedp.WaitReady("a[mapa-ver]")) version := read(t, rig, chromedp.Evaluate[string]( `document.querySelector("a[mapa-ver]").getAttribute("href")`)) screens := []string{"/", "/d/2026-11-10", "/d/2026-11-10/edit", path, path + "/edit", path + "/history", version, path + "/rename", path + "/delete", path + "/look", path + "/links", path + "/inside", path + "/blocks", "/new", "/new?title=Acme", "/pages", "/scratch", "/scratch/new", "/search", "/search?q=line", "/search?q=nothing", "/settings", "/deleted", "/help/markdown", "/help/open-source"} for _, live := range []bool{false, true} { for _, width := range []int64{1280, 390} { for _, screen := range screens { drive(t, rig, scripts(live), chromedp.EmulateViewport(width, 800), chromedp.Navigate(rig.Origin+screen), chromedp.WaitReady("[mapa-doc]")) stage := screen + " at " + itoa(int(width)) + "px, scripts " if live { stage += "on" } else { stage += "off" } auditRhythm(t, rig, stage, rhythmSkip) checkPageGaps(t, rig, stage) } } } } // pageGaps is what the audit cannot see: the gaps either side of the // page's body, which it skips, and the Links panel's place, which a // stack's step would move (a margin moves a fixed box). It answers each // problem as a sentence. const pageGaps = `JSON.stringify((() => { const doc = document.querySelector("[mapa-doc]") const out = [] if (!doc) return ["no page column"] const panel = doc.querySelector(":scope > aside[mapa-links]") if (panel && getComputedStyle(panel).display !== "none" && getComputedStyle(panel).marginTop !== "0px") out.push("the Links panel has a " + getComputedStyle(panel).marginTop + " margin; its place is its insets") const body = doc.querySelector(":scope > article[mapa-body]") if (!body || body.getBoundingClientRect().height === 0) return out const root = getComputedStyle(document.documentElement) const rem = parseFloat(root.fontSize) || 16 const steps = [0] for (let n = 1; n <= 6; n++) { const v = root.getPropertyValue("--rst-sp-" + n).trim() steps.push(v.endsWith("rem") ? parseFloat(v) * rem : parseFloat(v)) } const inFlow = el => { const s = getComputedStyle(el) return el.tagName !== "SCRIPT" && s.display !== "none" && s.position !== "fixed" && s.position !== "absolute" && el.getBoundingClientRect().height > 0 } const near = (el, way) => { for (let at = el[way]; at; at = at[way]) if (inFlow(at)) return at return null } const check = (gap, what) => { if (!steps.some(step => Math.abs(gap - step) <= 0.5)) out.push(what + " is " + Math.round(gap * 10) / 10 + "px, not a step of the scale") } const box = body.getBoundingClientRect() const above = near(body, "previousElementSibling") if (above) check(box.top - above.getBoundingClientRect().bottom, "the gap above the page's body") const below = near(body, "nextElementSibling") if (below) check(below.getBoundingClientRect().top - box.bottom, "the gap under the page's body") return out })())` func checkPageGaps(t *testing.T, rig *harness.Rig, stage string) { t.Helper() var found []string readJSON(t, rig, pageGaps, &found) for _, problem := range found { t.Errorf("%s: %s", stage, problem) } } // auditRhythm runs Fiesta's audit over the page column and fails the // test on each finding, naming the stage. A read that lands while the // page's own refresh replaces it is tried again. func auditRhythm(t *testing.T, rig *harness.Rig, stage string, skip []string) { t.Helper() expression := "JSON.stringify(" + rhythm.Expression("[mapa-doc]", rhythm.Options{Skip: skip}) + ")" var answer string var err error for try := 0; ; try++ { ctx, cancel := context.WithTimeout(rig.Context(), 10*time.Second) answer, err = chromedp.Run(ctx, chromedp.Evaluate[string](expression)) cancel() if err == nil || try == 2 { break } } if err != nil { t.Fatalf("%s: the rhythm audit did not run: %v", stage, err) } found := decodeFindings(t, stage, answer) if len(found) > 0 { t.Errorf("%s: %s", stage, rhythm.Report(found)) } } func decodeFindings(t *testing.T, stage, answer string) []rhythm.Finding { t.Helper() var found []rhythm.Finding if err := json.Unmarshal([]byte(answer), &found); err != nil { t.Fatalf("%s: the rhythm audit answered %q: %v", stage, answer, err) } return found }