oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package mapatest
4
5import (
6 "context"
7 "encoding/json"
8 "testing"
9 "time"
10
11 "amadan.net/rastrillo/fiesta/check/rhythm"
12 "amadan.net/rastrillo/rastrillo/harness"
13 "github.com/chromedp/chromedp"
14
15 "amadan.net/oficina/mapa/internal/mapa"
16)
17
18// Every signed-in screen keeps the spacing scale's rhythm (level-up
19// build note 89): Fiesta's audit over the page column, at a desktop and
20// a phone width, scripts off and on. Between two blocks every gap is a
21// step of the scale, no text sits closer than --rst-sp-2 to a control,
22// and every heading is closer to its own content than to the block
23// before it.
24
25// rhythmSkip is what the audit neither measures nor walks into: the
26// page's body, the editor's document, whose own typographic rhythm in
27// em (prose.css) is held by gates L1 to L5. The audit stops at a
28// skipped block's edges too, so pageGaps measures them. The Links
29// panel needs no skip: it is fixed, and the audit passes over fixed
30// boxes.
31var rhythmSkip = []string{"[mapa-body]"}
32
33func TestEveryScreenKeepsTheRhythm(t *testing.T) {
34 rig, store := browserRig(t, 0)
35 page, err := store.CreateTeamPage(context.Background(), Bo, "Short",
36 "One line, linking [[Acme]].")
37 if err != nil {
38 t.Fatal(err)
39 }
40 path := mapa.PagePath(page)
41 drive(t, rig, chromedp.Navigate(rig.Origin+path+"/history"),
42 chromedp.WaitReady("a[mapa-ver]"))
43 version := read(t, rig, chromedp.Evaluate[string](
44 `document.querySelector("a[mapa-ver]").getAttribute("href")`))
45 screens := []string{"/", "/d/2026-11-10", "/d/2026-11-10/edit", path,
46 path + "/edit", path + "/history", version, path + "/rename",
47 path + "/delete", path + "/look", path + "/links",
48 path + "/inside", path + "/blocks", "/new", "/new?title=Acme",
49 "/pages", "/scratch", "/scratch/new", "/search", "/search?q=line",
50 "/search?q=nothing", "/settings", "/deleted", "/help/markdown",
51 "/help/open-source"}
52 for _, live := range []bool{false, true} {
53 for _, width := range []int64{1280, 390} {
54 for _, screen := range screens {
55 drive(t, rig, scripts(live), chromedp.EmulateViewport(width,
56 800), chromedp.Navigate(rig.Origin+screen),
57 chromedp.WaitReady("[mapa-doc]"))
58 stage := screen + " at " + itoa(int(width)) + "px, scripts "
59 if live {
60 stage += "on"
61 } else {
62 stage += "off"
63 }
64 auditRhythm(t, rig, stage, rhythmSkip)
65 checkPageGaps(t, rig, stage)
66 }
67 }
68 }
69}
70
71// pageGaps is what the audit cannot see: the gaps either side of the
72// page's body, which it skips, and the Links panel's place, which a
73// stack's step would move (a margin moves a fixed box). It answers each
74// problem as a sentence.
75const pageGaps = `JSON.stringify((() => {
76 const doc = document.querySelector("[mapa-doc]")
77 const out = []
78 if (!doc) return ["no page column"]
79 const panel = doc.querySelector(":scope > aside[mapa-links]")
80 if (panel && getComputedStyle(panel).display !== "none" &&
81 getComputedStyle(panel).marginTop !== "0px")
82 out.push("the Links panel has a " + getComputedStyle(panel).marginTop +
83 " margin; its place is its insets")
84 const body = doc.querySelector(":scope > article[mapa-body]")
85 if (!body || body.getBoundingClientRect().height === 0) return out
86 const root = getComputedStyle(document.documentElement)
87 const rem = parseFloat(root.fontSize) || 16
88 const steps = [0]
89 for (let n = 1; n <= 6; n++) {
90 const v = root.getPropertyValue("--rst-sp-" + n).trim()
91 steps.push(v.endsWith("rem") ? parseFloat(v) * rem : parseFloat(v))
92 }
93 const inFlow = el => {
94 const s = getComputedStyle(el)
95 return el.tagName !== "SCRIPT" && s.display !== "none" &&
96 s.position !== "fixed" && s.position !== "absolute" &&
97 el.getBoundingClientRect().height > 0
98 }
99 const near = (el, way) => {
100 for (let at = el[way]; at; at = at[way]) if (inFlow(at)) return at
101 return null
102 }
103 const check = (gap, what) => {
104 if (!steps.some(step => Math.abs(gap - step) <= 0.5))
105 out.push(what + " is " + Math.round(gap * 10) / 10 +
106 "px, not a step of the scale")
107 }
108 const box = body.getBoundingClientRect()
109 const above = near(body, "previousElementSibling")
110 if (above) check(box.top - above.getBoundingClientRect().bottom,
111 "the gap above the page's body")
112 const below = near(body, "nextElementSibling")
113 if (below) check(below.getBoundingClientRect().top - box.bottom,
114 "the gap under the page's body")
115 return out
116})())`
117
118func checkPageGaps(t *testing.T, rig *harness.Rig, stage string) {
119 t.Helper()
120 var found []string
121 readJSON(t, rig, pageGaps, &found)
122 for _, problem := range found {
123 t.Errorf("%s: %s", stage, problem)
124 }
125}
126
127// auditRhythm runs Fiesta's audit over the page column and fails the
128// test on each finding, naming the stage. A read that lands while the
129// page's own refresh replaces it is tried again.
130func auditRhythm(t *testing.T, rig *harness.Rig, stage string,
131 skip []string) {
132 t.Helper()
133 expression := "JSON.stringify(" + rhythm.Expression("[mapa-doc]",
134 rhythm.Options{Skip: skip}) + ")"
135 var answer string
136 var err error
137 for try := 0; ; try++ {
138 ctx, cancel := context.WithTimeout(rig.Context(), 10*time.Second)
139 answer, err = chromedp.Run(ctx, chromedp.Evaluate[string](expression))
140 cancel()
141 if err == nil || try == 2 {
142 break
143 }
144 }
145 if err != nil {
146 t.Fatalf("%s: the rhythm audit did not run: %v", stage, err)
147 }
148 found := decodeFindings(t, stage, answer)
149 if len(found) > 0 {
150 t.Errorf("%s: %s", stage, rhythm.Report(found))
151 }
152}
153
154func decodeFindings(t *testing.T, stage, answer string) []rhythm.Finding {
155 t.Helper()
156 var found []rhythm.Finding
157 if err := json.Unmarshal([]byte(answer), &found); err != nil {
158 t.Fatalf("%s: the rhythm audit answered %q: %v", stage, answer, err)
159 }
160 return found
161}
162