| 1 | //go:build browser |
| 2 | |
| 3 | // Fiesta's rhythm audit over Calls' own pages, in a real Chromium |
| 4 | // (test/browser/rhythm.mjs). `make browser` runs it. |
| 5 | // |
| 6 | // Paul, 2026-11-22: no ad-hoc spacing ("Can we somehow put something in |
| 7 | // place to avoid weird spacing issues like this in future?"), then |
| 8 | // "Release and start the rollout". `make ci` runs the stylesheet half |
| 9 | // (go tool fiesta spacing); this is the half only a laid-out page can |
| 10 | // answer: every gap between two blocks is a step of the scale, no text |
| 11 | // rests on a control, and every heading sits closer to its own content |
| 12 | // than to what comes before it. |
| 13 | // |
| 14 | // What it reads: the meetings list (empty, with upcoming and ended |
| 15 | // meetings, the ended list open, the new-meeting form refused), Settings |
| 16 | // (empty, with agents, saved, refused), a meeting's settings, the three |
| 17 | // confirm pages, the meeting and agent pages shown once, Share (with |
| 18 | // script and without), the holding screen (a member's with script, with |
| 19 | // its audio test open, with no camera or microphone to reach, and |
| 20 | // without script; a guest's), the guest's name page (and refused), the |
| 21 | // app hand-off page, Welcome, and the Colophon under Help. |
| 22 | // |
| 23 | // What it does not: the call itself (room.html), whose window of video, |
| 24 | // bar, PiP, tiles and speaking light is component geometry pinned to the |
| 25 | // Mac app's, not a page's flow; its spacing allowances say so in |
| 26 | // calls.css. The sign-in door is estilo's card and is audited in estilo |
| 27 | // (6238a47). The error page is one component, rastrillo's rst-error, |
| 28 | // which the audit reads as one block and which is Fiesta's to space. |
| 29 | // |
| 30 | // RHYTHM_SHOTS=<dir> keeps a screenshot of every state. |
| 31 | // |
| 32 | // Written against origin/main (9220b05) first, its static/ and |
| 33 | // templates/ under this branch's go.mod: see the commit for the count. |
| 34 | package callstest |
| 35 | |
| 36 | import ( |
| 37 | "bufio" |
| 38 | "bytes" |
| 39 | "context" |
| 40 | "encoding/json" |
| 41 | "io" |
| 42 | "net/http" |
| 43 | "net/url" |
| 44 | "os" |
| 45 | "os/exec" |
| 46 | "regexp" |
| 47 | "strconv" |
| 48 | "strings" |
| 49 | "testing" |
| 50 | "time" |
| 51 | |
| 52 | "amadan.net/rastrillo/fiesta/check/rhythm" |
| 53 | ) |
| 54 | |
| 55 | // rhythmSkip is what the audit does not read. The holding screen's |
| 56 | // camera preview is a <video>, one block the audit measures and never |
| 57 | // walks into, so nothing on the pages Calls draws needs naming. |
| 58 | var rhythmSkip []string |
| 59 | |
| 60 | // rhythmState is one state of one page: where to go, the page's own |
| 61 | // marker, who is looking, whether scripts run, and an optional step in |
| 62 | // the page (submit a form, open a disclosure) with what to wait for after |
| 63 | // it and the path it lands on. |
| 64 | type rhythmState struct { |
| 65 | Name string `json:"name"` |
| 66 | Path string `json:"path"` |
| 67 | Marker string `json:"marker"` |
| 68 | // Root is what the audit reads: main, unless main holds only one |
| 69 | // region of somebody else's whose insides are the page, as the |
| 70 | // Colophon's [estilo-credits] (estilo's template) is. |
| 71 | Root string `json:"root"` |
| 72 | As string `json:"as"` |
| 73 | NoScript bool `json:"noScript"` |
| 74 | Act string `json:"act,omitempty"` |
| 75 | Then string `json:"then,omitempty"` |
| 76 | After string `json:"after,omitempty"` |
| 77 | // NoRefresh takes the page's meta refresh out (rhythm.mjs). |
| 78 | NoRefresh bool `json:"noRefresh,omitempty"` |
| 79 | // NoMedia takes navigator.mediaDevices away before the page's |
| 80 | // scripts run, as an insecure context or an old browser has it. |
| 81 | NoMedia bool `json:"noMedia,omitempty"` |
| 82 | } |
| 83 | |
| 84 | // submitJS sends a form past the browser's own checks, so the server's |
| 85 | // answer (its errors, or the page after) is what is drawn. |
| 86 | func submitJS(form string) string { |
| 87 | return `HTMLFormElement.prototype.submit.call(document.querySelector(` + strconv.Quote(form) + `))` |
| 88 | } |
| 89 | |
| 90 | // fillAndSubmitJS sets one field, to value and the time so that each |
| 91 | // width's run makes a thing of its own, and sends the form. |
| 92 | func fillAndSubmitJS(form, field, value string) string { |
| 93 | return `(() => { const f = document.querySelector(` + strconv.Quote(form) + `); f.querySelector(` + strconv.Quote(field) + `).value = ` + |
| 94 | strconv.Quote(value+"-") + ` + Date.now(); f.removeAttribute('target'); HTMLFormElement.prototype.submit.call(f); })()` |
| 95 | } |
| 96 | |
| 97 | func TestEveryPageKeepsTheRhythm(t *testing.T) { |
| 98 | rig := newBrowserRig(t) |
| 99 | |
| 100 | // Before anything exists: the empty lists. |
| 101 | empty := []rhythmState{ |
| 102 | {Name: "the meetings, none yet", Path: "/", Marker: `main [rst-empty]`, As: "member"}, |
| 103 | {Name: "Settings, no agents", Path: "/settings", Marker: `main [rst-empty]`, As: "member"}, |
| 104 | } |
| 105 | found := rig.auditRhythm(t, empty) |
| 106 | |
| 107 | sqid, token := rig.createMeeting(t) |
| 108 | ended, _ := rig.createMeeting(t) |
| 109 | rig.post(t, "/meetings/"+ended+"/end", url.Values{}) |
| 110 | agentPage := rig.post(t, "/settings/agents", url.Values{"name": {"Diary"}}) |
| 111 | m := regexp.MustCompile(`/settings/agents/(\d+)/revoke`).FindSubmatch(rig.get(t, "/settings")) |
| 112 | if m == nil { |
| 113 | t.Fatalf("no agent to revoke on /settings after creating one:\n%s", agentPage) |
| 114 | } |
| 115 | agent := string(m[1]) |
| 116 | join := "/m/" + sqid + "/join" |
| 117 | states := []rhythmState{ |
| 118 | {Name: "the meetings", Path: "/", Marker: `.calls-meetings [rst-row]`, As: "member"}, |
| 119 | {Name: "the meetings, without script", Path: "/", Marker: `.calls-meetings [rst-row]`, As: "member", NoScript: true}, |
| 120 | {Name: "the meetings, the ended list open", Path: "/", Marker: `details.calls-ended[open]`, As: "member", |
| 121 | Act: `document.querySelector('details.calls-ended').open = true`}, |
| 122 | {Name: "a new meeting, refused", Path: "/", Marker: `[rst-field-error]`, As: "member", |
| 123 | Act: submitJS(`form[action="/meetings"]`), After: "/meetings"}, |
| 124 | {Name: "a new meeting's link, shown once", Path: "/", Marker: `.calls-link-input`, As: "member", |
| 125 | Act: fillAndSubmitJS(`form[action="/meetings"]`, `[name="title"]`, "Weekly"), After: "/meetings"}, |
| 126 | {Name: "Settings", Path: "/settings", Marker: `.calls-agents [rst-row]`, As: "member"}, |
| 127 | {Name: "Settings, saved", Path: "/settings?saved=1", Marker: `[rst-callout]`, As: "member"}, |
| 128 | {Name: "Settings, without script", Path: "/settings", Marker: `.calls-agents [rst-row]`, As: "member", NoScript: true}, |
| 129 | {Name: "a new agent, refused", Path: "/settings", Marker: `[rst-field-error]`, As: "member", |
| 130 | Act: submitJS(`form[action="/settings/agents"]`), After: "/settings/agents"}, |
| 131 | {Name: "an agent's token, shown once", Path: "/settings", Marker: `.calls-link-input`, As: "member", |
| 132 | Act: fillAndSubmitJS(`form[action="/settings/agents"]`, `[name="name"]`, "Ledger"), After: "/settings/agents"}, |
| 133 | {Name: "a meeting's settings", Path: "/meetings/" + sqid + "/settings", Marker: `form[action$="/settings"] [rst-form-actions]`, As: "member"}, |
| 134 | {Name: "a meeting's settings, saved", Path: "/meetings/" + sqid + "/settings?saved=1", Marker: `[rst-callout]`, As: "member"}, |
| 135 | {Name: "End meeting, confirmed", Path: "/meetings/" + sqid + "/end", Marker: `form[action="/meetings/` + sqid + `/end"]`, As: "member"}, |
| 136 | {Name: "Replace the link, confirmed", Path: "/meetings/" + sqid + "/links/replace", Marker: `form[action="/meetings/` + sqid + `/links"]`, As: "member"}, |
| 137 | {Name: "Revoke an agent, confirmed", Path: "/settings/agents/" + agent + "/revoke", Marker: `form[action="/settings/agents/` + agent + `/revoke"]`, As: "member"}, |
| 138 | {Name: "Share", Path: join, Marker: `#invitation-link`, As: "member", |
| 139 | Act: `(() => { const f = document.querySelector('form[action$="/share"]'); f.removeAttribute('target'); HTMLFormElement.prototype.submit.call(f); })()`, |
| 140 | After: "/m/" + sqid + "/share"}, |
| 141 | {Name: "the holding screen", Path: join, Marker: `#join:not([disabled])`, As: "member"}, |
| 142 | {Name: "Share, without script", Path: join, Marker: `#invitation-link`, As: "member", NoScript: true, |
| 143 | Act: `(() => { const f = document.querySelector('form[action$="/share"]'); f.removeAttribute('target'); HTMLFormElement.prototype.submit.call(f); })()`, |
| 144 | After: "/m/" + sqid + "/share"}, |
| 145 | {Name: "the holding screen, its audio test open", Path: join, Marker: `#audio-test[open]`, As: "member", |
| 146 | Then: `#join:not([disabled])`, Act: `document.querySelector('#audio-test').open = true`}, |
| 147 | {Name: "the holding screen, no camera or microphone to reach", Path: join, Marker: `#join:not([disabled])`, As: "member", NoMedia: true}, |
| 148 | {Name: "the holding screen, without script", Path: join, Marker: `.calls-holding-more`, As: "member", NoScript: true}, |
| 149 | {Name: "the guest's name page", Path: "/j/" + token, Marker: `form[action="/j/` + token + `/guest"]`}, |
| 150 | {Name: "the guest's name page, without script", Path: "/j/" + token, Marker: `form[action="/j/` + token + `/guest"]`, NoScript: true}, |
| 151 | {Name: "the guest's name, refused", Path: "/j/" + token, Marker: `[rst-field-error]`, |
| 152 | Act: submitJS(`form[action="/j/` + token + `/guest"]`), After: "/j/" + token + "/guest"}, |
| 153 | {Name: "a guest's holding screen", Path: "/j/" + token, Marker: `#join:not([disabled])`, |
| 154 | Act: fillAndSubmitJS(`form[action="/j/`+token+`/guest"]`, `[name="name"]`, "Ada"), After: join}, |
| 155 | {Name: "Join in the app", Path: "/m/" + sqid + "/app", Marker: `#app-offer`, As: "member", NoScript: true, NoRefresh: true}, |
| 156 | {Name: "Welcome", Path: "/welcome", Marker: `main h1`, As: "member"}, |
| 157 | {Name: "the Colophon", Path: "/help/open-source", Marker: `[estilo-credits]`, Root: `[estilo-credits]`, As: "member"}, |
| 158 | } |
| 159 | found += rig.auditRhythm(t, states) |
| 160 | t.Logf("%d finding(s) over %d states at two widths", found, len(empty)+len(states)) |
| 161 | } |
| 162 | |
| 163 | // auditRhythm runs rhythm.mjs over states at both widths, fails the |
| 164 | // test on every finding, and returns how many there were. |
| 165 | func (rig *browserRig) auditRhythm(t *testing.T, states []rhythmState) int { |
| 166 | t.Helper() |
| 167 | audits := map[string]string{} |
| 168 | for i := range states { |
| 169 | if states[i].Root == "" { |
| 170 | states[i].Root = "main" |
| 171 | } |
| 172 | audits[states[i].Root] = rhythm.Expression(states[i].Root, rhythm.Options{Skip: rhythmSkip}) |
| 173 | } |
| 174 | cfg, err := json.Marshal(map[string]any{ |
| 175 | "base": rig.origin, |
| 176 | "cookie": map[string]string{"name": rig.cookieName, "value": rig.cookieA}, |
| 177 | "audits": audits, |
| 178 | "states": states, |
| 179 | "shots": os.Getenv("RHYTHM_SHOTS"), |
| 180 | }) |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) |
| 185 | defer cancel() |
| 186 | cmd := exec.CommandContext(ctx, "node", "test/browser/rhythm.mjs") |
| 187 | cmd.Dir = repoRootForBrowser(t) |
| 188 | cmd.Stdin = bytes.NewReader(cfg) |
| 189 | var stderr bytes.Buffer |
| 190 | cmd.Stderr = &stderr |
| 191 | out, err := cmd.Output() |
| 192 | if err != nil { |
| 193 | t.Fatalf("node: %v\n%s", err, stderr.String()) |
| 194 | } |
| 195 | byName := map[string]rhythmState{} |
| 196 | for _, s := range states { |
| 197 | byName[s.Name] = s |
| 198 | } |
| 199 | seen, total := 0, 0 |
| 200 | sc := bufio.NewScanner(bytes.NewReader(out)) |
| 201 | sc.Buffer(make([]byte, 1<<20), 1<<22) |
| 202 | for sc.Scan() { |
| 203 | var got struct { |
| 204 | Name string `json:"name"` |
| 205 | Width int `json:"width"` |
| 206 | Status int `json:"status"` |
| 207 | Path string `json:"path"` |
| 208 | Marker bool `json:"marker"` |
| 209 | Scripted bool `json:"scripted"` |
| 210 | Stack bool `json:"stack"` |
| 211 | Blocks int `json:"blocks"` |
| 212 | Found []rhythm.Finding `json:"found"` |
| 213 | Error string `json:"error"` |
| 214 | Ms int `json:"ms"` |
| 215 | } |
| 216 | if err := json.Unmarshal(sc.Bytes(), &got); err != nil { |
| 217 | t.Fatalf("node said %q", sc.Text()) |
| 218 | } |
| 219 | seen++ |
| 220 | s, ok := byName[got.Name] |
| 221 | if !ok { |
| 222 | t.Fatalf("node audited a state nobody named: %q", got.Name) |
| 223 | } |
| 224 | where := got.Name + " at " + strconv.Itoa(got.Width) + "px" |
| 225 | if got.Ms > 5000 { |
| 226 | t.Logf("%s took %dms", where, got.Ms) |
| 227 | } |
| 228 | if got.Error != "" { |
| 229 | t.Errorf("%s: %s", where, got.Error) |
| 230 | continue |
| 231 | } |
| 232 | if s.Act == "" && got.Status != http.StatusOK { |
| 233 | t.Errorf("%s: status %d", where, got.Status) |
| 234 | } |
| 235 | wantPath := s.After |
| 236 | if wantPath == "" { |
| 237 | wantPath, _, _ = strings.Cut(s.Path, "?") |
| 238 | } |
| 239 | if got.Path != wantPath { |
| 240 | t.Errorf("%s: the browser is on %s, not %s", where, got.Path, wantPath) |
| 241 | continue |
| 242 | } |
| 243 | if !got.Marker { |
| 244 | t.Errorf("%s: the page's own marker is missing", where) |
| 245 | continue |
| 246 | } |
| 247 | if got.Scripted == s.NoScript { |
| 248 | t.Errorf("%s: page scripts ran = %v, want %v", where, got.Scripted, !s.NoScript) |
| 249 | } |
| 250 | if !got.Stack { |
| 251 | t.Errorf("%s: <main> is not a stack (rst-stack, a flex column)", where) |
| 252 | } |
| 253 | if got.Blocks < 2 { |
| 254 | t.Errorf("%s: the audit had %d block(s) under %s to measure; it would pass vacuously", where, got.Blocks, s.Root) |
| 255 | } |
| 256 | if len(got.Found) > 0 { |
| 257 | total += len(got.Found) |
| 258 | t.Errorf("%s: %s", where, rhythm.Report(got.Found)) |
| 259 | } |
| 260 | } |
| 261 | if seen != 2*len(states) { |
| 262 | t.Fatalf("node audited %d states, want %d\n%s", seen, 2*len(states), stderr.String()) |
| 263 | } |
| 264 | return total |
| 265 | } |
| 266 | |
| 267 | // post is member A sending a form, and the page it ends on. |
| 268 | func (rig *browserRig) post(t *testing.T, path string, form url.Values) []byte { |
| 269 | t.Helper() |
| 270 | req, err := http.NewRequest(http.MethodPost, rig.origin+path, strings.NewReader(form.Encode())) |
| 271 | if err != nil { |
| 272 | t.Fatal(err) |
| 273 | } |
| 274 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 275 | req.Header.Set("Origin", rig.origin) |
| 276 | req.AddCookie(&http.Cookie{Name: rig.cookieName, Value: rig.cookieA}) |
| 277 | return rig.do(t, req) |
| 278 | } |
| 279 | |
| 280 | // get is member A reading a page. |
| 281 | func (rig *browserRig) get(t *testing.T, path string) []byte { |
| 282 | t.Helper() |
| 283 | req, err := http.NewRequest(http.MethodGet, rig.origin+path, nil) |
| 284 | if err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | req.AddCookie(&http.Cookie{Name: rig.cookieName, Value: rig.cookieA}) |
| 288 | return rig.do(t, req) |
| 289 | } |
| 290 | |
| 291 | func (rig *browserRig) do(t *testing.T, req *http.Request) []byte { |
| 292 | t.Helper() |
| 293 | res, err := http.DefaultClient.Do(req) |
| 294 | if err != nil { |
| 295 | t.Fatalf("%s %s: %v", req.Method, req.URL.Path, err) |
| 296 | } |
| 297 | defer res.Body.Close() |
| 298 | body, _ := io.ReadAll(res.Body) |
| 299 | if res.StatusCode != http.StatusOK { |
| 300 | t.Fatalf("%s %s: %d\n%s", req.Method, req.URL.Path, res.StatusCode, body) |
| 301 | } |
| 302 | return body |
| 303 | } |
| 304 | |