oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package browsertest
4
5import (
6 "testing"
7
8 "github.com/chromedp/chromedp"
9
10 "amadan.net/rastrillo/rastrillo/harness"
11)
12
13// settingsLayoutJS describes what is out of place in Settings, "" if
14// nothing: its sections in order under their headings; every row one
15// height, its name on the left and its control on the right, centred on
16// the row; Appearance's pill as tall as a button; signing out last, its
17// explanation right under it.
18const settingsLayoutJS = `(() => {
19 const problems = [];
20 const heads = [...document.querySelectorAll(".settings .group h3")].map(h => h.textContent).join(", ");
21 if (heads !== "You, This Base, Ask Base") problems.push("headings: " + heads);
22 const rows = [...document.querySelectorAll(".settings .rows li")];
23 const names = rows.map(li => li.querySelector(".who").firstChild.textContent).join(", ");
24 if (names !== "Signed in as, Appearance, Passkeys, Base name, Connection") problems.push("rows: " + names);
25 const heights = new Set(rows.map(li => Math.round(li.getBoundingClientRect().height)));
26 if (heights.size !== 1) problems.push("rows of heights " + [...heights].join(", "));
27 const btn = document.querySelector(".settings .rows .btn.ghost:not([hidden])").getBoundingClientRect();
28 const pill = document.querySelector("form.segmented").getBoundingClientRect();
29 if (Math.round(pill.height) !== Math.round(btn.height)) problems.push("Appearance is " + pill.height + "px tall, a button " + btn.height);
30 for (const li of rows) {
31 const r = li.getBoundingClientRect(), who = li.querySelector(".who").getBoundingClientRect();
32 const mid = (r.top + r.bottom) / 2;
33 const control = [...li.children].find(c => c !== li.querySelector(".who") && c.checkVisibility());
34 if (Math.abs((who.top + who.bottom) / 2 - mid) > 1.5) problems.push(li.textContent.slice(0, 20) + ": name not centred");
35 if (!control) continue;
36 const c = control.getBoundingClientRect();
37 if (Math.abs((c.top + c.bottom) / 2 - mid) > 1.5) problems.push(li.textContent.slice(0, 20) + ": control not centred");
38 if (Math.abs(c.right - r.right) > 1) problems.push(li.textContent.slice(0, 20) + ": control " + (r.right - c.right) + "px off the right");
39 if (c.left < who.right) problems.push(li.textContent.slice(0, 20) + ": control overlaps the name");
40 }
41 const last = [...document.querySelector(".settings").children].pop();
42 if (!last.matches(".signout") || !last.querySelector('form[action="/auth/signout"]')) problems.push("signing out is not last");
43 const out = last.querySelector("button").getBoundingClientRect(), note = last.querySelector(".note").getBoundingClientRect();
44 if (note.top < out.bottom || note.top - out.bottom > 16 || Math.abs(note.left - out.left) > 1) problems.push("the explanation is not under Sign out");
45 return problems.join("; ");
46})()`
47
48// Settings is in tidy sections at a phone's width and a laptop's, light
49// and dark. With BASE_BROWSER_ARTIFACTS set it saves each.
50func TestSettingsLayout(t *testing.T) {
51 rig, _, _ := askBaseOn(t, nil, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3)))
52 for _, v := range []struct {
53 name string
54 view chromedp.Action[chromedp.Void]
55 }{
56 {"390", chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))},
57 {"1280", chromedp.EmulateViewport(1280, 800)},
58 } {
59 for _, scheme := range []string{"light", "dark"} {
60 drive(t, rig, v.view, reducedMotion(scheme, true), chromedp.Navigate(rig.Origin+"/settings"), chromedp.WaitVisible(chromedp.CSS(`form.segmented`)))
61 // The add-a-passkey button shows once its script has looked.
62 drive(t, rig, chromedp.Poll[chromedp.Void](`!document.querySelector("[data-passkey-add]").hidden`))
63 noOverflow(t, rig, "Settings at "+v.name)
64 if got := harness.Evaluate[string](rig, settingsLayoutJS); got != "" {
65 t.Errorf("Settings at %s, %s: %s", v.name, scheme, got)
66 }
67 if got := harness.Evaluate[string](rig, closeJS); got != "" {
68 t.Errorf("Settings' close at %s: %s", v.name, got)
69 }
70 mobileShot(t, rig, "settings-"+v.name+"-"+scheme)
71 }
72 }
73 // The Store and People pages' close, on a phone.
74 drive(t, rig, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3)), reducedMotion("dark", true))
75 for _, p := range []string{"store", "people"} {
76 drive(t, rig, chromedp.Navigate(rig.Origin+"/"+p), chromedp.WaitVisible(chromedp.CSS(`.crumb a.close`)))
77 mobileShot(t, rig, p+"-390-dark")
78 }
79}
80