oficina / fichas Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package fichastest
4
5import (
6 "context"
7 "crypto/ed25519"
8 "crypto/rand"
9 "fmt"
10 "math"
11 "testing"
12 "time"
13
14 "amadan.net/rastrillo/fiesta/check/rhythm"
15 "amadan.net/rastrillo/rastrillo/db"
16 "amadan.net/rastrillo/rastrillo/harness"
17 "github.com/chromedp/chromedp"
18
19 fichas "amadan.net/oficina/fichas/internal/fichas"
20)
21
22// rhythmSkip is what the audit does not read, and it is nothing. The
23// grid's canvas (its rows, cells and editors) is the one region whose
24// spacing is not the page's, and the audit already reads a table as one
25// block, measured against its neighbours but never walked into; an
26// editor or picker floats (position: absolute), which it passes over.
27// Name a region here only if it is a component's internals.
28var rhythmSkip []string
29
30// contractFlow is any well-formed flow: the contract's first two phases
31// (choose a customer, choose its fields) are GETs a signed-in person
32// reaches with one. Its last two need a signed release, so the audit
33// reads the phases a person sees first.
34const contractFlow = "00000000-0000-4000-8000-000000000000"
35
36// rhythmScreen is one state of one screen: the path, what to wait for
37// once it is drawn, whether page scripts run, and an optional step in
38// the page (open Advanced, submit a form) and what to wait for after it.
39type rhythmScreen struct {
40 name, path, ready string
41 noScript bool
42 act, then string
43}
44
45// TestTheScreensKeepTheRhythm: Fiesta's rhythm audit (Paul, 2026-11-22:
46// "Can we somehow put something in place to avoid weird spacing issues
47// like this in future?") over Fichas' own screens: the grid, the cards
48// view, the card beside the list and on its own page (with scripts and
49// without), its History, every form (a card's, a field's, a type's, the
50// card layout, the contract's first two phases) with Advanced open, the
51// choices box shown and the server's errors drawn, at a desktop width
52// and a phone's. Every gap between two blocks is a step of the scale, no
53// text rests on a control, and every heading sits closer to its content
54// than to what comes before.
55//
56// It failed on main before the rhythm swap (95 findings, 2026-11-22):
57// the card's heading, meta line, fields and call log were 14, 28 and
58// 10px apart, and the grid's help line sat 11.2px over the count.
59func TestTheScreensKeepTheRhythm(t *testing.T) {
60 var id int64
61 var empty string
62 // The contract's release is configured, so its pages are mounted.
63 _, key, err := ed25519.GenerateKey(rand.Reader)
64 if err != nil {
65 t.Fatal(err)
66 }
67 release := func(cfg *fichas.Config) {
68 cfg.ContractRelease = &fichas.ContractReleaseConfig{Destination: "https://docs.example.com", Importer: TestOwner, Team: "test-team", Key: key}
69 }
70 rig := browserAppWith(t, release, func(ctx context.Context, d *db.DB, cfg fichas.Config) {
71 m, err := fichas.EnsureMember(ctx, d.G, cfg.Sealer, TestOwner)
72 if err != nil {
73 t.Fatalf("member: %v", err)
74 }
75 ct, err := fichas.CreateType(ctx, d.G, cfg.Sealer, fichas.Person(m.ID), "Supplier", "Suppliers")
76 if err != nil {
77 t.Fatalf("type: %v", err)
78 }
79 empty = ct.Key
80 for i := 0; i < 3; i++ {
81 c, err := fichas.CreateCard(ctx, d.G, cfg.Sealer, fichas.TypeCustomer, fichas.Person(m.ID),
82 map[string]string{fichas.FieldName: fmt.Sprintf("Card %03d", i), fichas.FieldStage: "Lead",
83 fichas.FieldEmail: fmt.Sprintf("c%d@example.com", i), fichas.FieldPhone: "+353 1 555 0142"})
84 if err != nil {
85 t.Fatalf("seed: %v", err)
86 }
87 if i == 0 {
88 id = c.ID
89 if _, err := fichas.AddEntry(ctx, d.G, cfg.Sealer, c.ID, fichas.Person(m.ID), fichas.TypeCallLog, "manual",
90 map[string]string{fichas.FieldBody: "Asked for the renewal quote by Friday."}); err != nil {
91 t.Fatalf("seed entry: %v", err)
92 }
93 }
94 }
95 })
96 card := cardPath(id)
97 split := "/t/t_customer?sort=f_name&card=" + itoa(id)
98 // submit sends a form past the browser's own checks, so the server's
99 // errors are what is drawn.
100 submit := func(form string) string {
101 return `HTMLFormElement.prototype.submit.call(document.querySelector(` + "`" + form + "`" + `))`
102 }
103 screens := []rhythmScreen{
104 {name: "the grid", path: "/t/t_customer?sort=f_name", ready: `[fichas-grid][role="grid"] tbody tr`},
105 {name: "the grid under a notice", path: "/t/t_customer?sort=f_name&saved=1", ready: `[fichas-notice]`},
106 {name: "a type with no cards", path: "/t/" + empty, ready: `[rst-empty]`},
107 {name: "the cards view", path: "/t/t_customer?sort=f_name&view=cards", ready: `[fichas-card-grid]`},
108 {name: "the card beside the list", path: split, ready: `[fichas-pane] form[data-card-ready]`},
109 {name: "the card beside the list, without script", path: split, ready: `[fichas-pane] [fichas-card-save]`, noScript: true},
110 {name: "the card beside the list under a notice", path: split + "&saved=1", ready: `[fichas-pane] form[data-card-ready]`},
111 {name: "the card's page", path: card, ready: `form[data-card-ready]`},
112 {name: "the card's page, without script", path: card, ready: `[fichas-card-save]`, noScript: true},
113 {name: "the card's History", path: card + "/history", ready: `[fichas-history]`},
114 {name: "the card's edit form", path: card + "/edit", ready: `form[fichas-form]`},
115 {name: "the card's edit form, without script", path: card + "/edit", ready: `form[fichas-form]`, noScript: true},
116 {name: "Delete, confirmed", path: card + "/delete", ready: `[fichas-appbar]`},
117 {name: "a new card", path: "/cards/new?type=t_customer", ready: `form[fichas-form]`},
118 {name: "a new card, refused", path: "/cards/new?type=t_customer", ready: `form[fichas-form]`,
119 act: submit(`form[fichas-form]`), then: `[rst-field-error]`},
120 {name: "the fields", path: "/t/t_customer/fields", ready: `[fichas-grid]`},
121 {name: "a new field", path: "/t/t_customer/fields/new", ready: `form[fichas-form]`},
122 {name: "a new field, a Choice, Advanced open", path: "/t/t_customer/fields/new", ready: `form[fichas-form]`,
123 act: `document.querySelector('input[name="kind"][value="enum"]').checked = true; document.querySelector('[fichas-advanced]').open = true`,
124 then: `[fichas-choice-options] textarea`},
125 {name: "a new field, refused", path: "/t/t_customer/fields/new", ready: `form[fichas-form]`,
126 act: submit(`form[fichas-form]`), then: `[rst-field-error]`},
127 {name: "a field's edit form", path: "/fields/" + fichas.FieldStage + "/edit", ready: `form[fichas-form]`},
128 {name: "a new type", path: "/types/new", ready: `form[fichas-form]`},
129 {name: "a new type, refused", path: "/types/new", ready: `form[fichas-form]`,
130 act: submit(`form[fichas-form]`), then: `[rst-field-error]`},
131 {name: "the card layout", path: "/t/t_customer/layout", ready: `[fichas-layout-editor]`},
132 {name: "a contract: choose a customer", path: "/contracts/prepare?flow=" + contractFlow, ready: `[rst-box]`},
133 {name: "a contract: choose its fields", path: "/contracts/prepare?flow=" + contractFlow + "&card=" + itoa(id), ready: `[rst-box] form`},
134 }
135 for _, width := range []int64{1440, 390} {
136 for _, s := range screens {
137 found, err := auditScreen(rig, width, s)
138 if err != nil {
139 t.Fatalf("%s at %dpx: %v", s.name, width, err)
140 }
141 if len(found) > 0 {
142 t.Errorf("%s at %dpx: %s", s.name, width, rhythm.Report(found))
143 }
144 }
145 }
146}
147
148// auditScreen draws one screen state and reads the audit over main.
149// Scripts are switched back on afterwards, whatever happened.
150func auditScreen(rig *harness.Rig, width int64, s rhythmScreen) ([]rhythm.Finding, error) {
151 ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second)
152 defer cancel()
153 defer rig.Run(scriptsOn(true))
154 steps := []chromedp.Action[chromedp.Void]{scriptsOn(!s.noScript), viewport(width), chromedp.Navigate(rig.Origin + s.path)}
155 if s.noScript {
156 // No page script, so no script-made marker: wait on the server's
157 // markup, through the DOM domain.
158 steps = append(steps, chromedp.WaitReady(chromedp.CSS(s.ready)))
159 } else {
160 steps = append(steps, chromedp.WaitVisible(chromedp.CSS(s.ready)))
161 }
162 if s.act != "" {
163 steps = append(steps, chromedp.Evaluate[chromedp.Void](s.act), chromedp.WaitVisible(chromedp.CSS(s.then)))
164 }
165 var found []rhythm.Finding
166 steps = append(steps, harness.Into(&found, chromedp.Evaluate[[]rhythm.Finding](
167 rhythm.Expression("main", rhythm.Options{Skip: rhythmSkip}))))
168 return found, chromedp.Do(ctx, steps...)
169}
170
171// fieldGaps is the space between each two neighbouring fields (rst-field
172// or a conflict's fieldset) of a form, and the scale's --rst-sp-3 in px.
173const fieldGaps = `(() => {
174 const form = document.querySelector('form[fichas-form]');
175 const root = parseFloat(getComputedStyle(document.documentElement).fontSize);
176 const step = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--rst-sp-3')) * root;
177 const field = e => e.hasAttribute('rst-field') || e.hasAttribute('fichas-conflict');
178 const kids = [...form.children].filter(e => e.getBoundingClientRect().height > 0);
179 const gaps = [];
180 for (let i = 1; i < kids.length; i++) {
181 if (field(kids[i - 1]) && field(kids[i])) gaps.push(kids[i].getBoundingClientRect().top - kids[i - 1].getBoundingClientRect().bottom);
182 }
183 return {step, gaps};
184})()`
185
186// TestTheFormsFieldsAreAStepApart: the rhythm audit cannot see a gap that
187// is on the scale but the wrong step. The edit form once wrote each
188// field's hidden "was" input between it and the field before, which hid
189// that field from the stack's rule: its fields sat at the stack's 8px
190// floor, where the new-card form's sit at --rst-sp-3 (Fable, the rhythm
191// swap's review, M1). Every two fields, and a conflict's fieldset beside
192// a field, are --rst-sp-3 apart, on the edit form, after a conflict, and
193// on the new-card form.
194func TestTheFormsFieldsAreAStepApart(t *testing.T) {
195 s := seedCards(t, 1)
196 type spacing struct {
197 Step float64 `json:"step"`
198 Gaps []float64 `json:"gaps"`
199 }
200 check := func(name string, got spacing) {
201 t.Helper()
202 if len(got.Gaps) < 4 {
203 t.Fatalf("%s: %d gaps between fields; this test would pass vacuously", name, len(got.Gaps))
204 }
205 for i, g := range got.Gaps {
206 if math.Abs(g-got.Step) > 0.5 {
207 t.Errorf("%s: fields %d and %d are %.1fpx apart, want --rst-sp-3, %.1fpx", name, i, i+1, g, got.Step)
208 }
209 }
210 }
211 id := s.ids[0]
212 for _, path := range []string{cardPath(id) + "/edit", "/cards/new?type=t_customer"} {
213 var got spacing
214 s.rig.Run(wide(), chromedp.Navigate(s.rig.Origin+path), chromedp.WaitVisible(`form[fichas-form]`),
215 harness.Into(&got, chromedp.Evaluate[spacing](fieldGaps)))
216 check(path, got)
217 }
218
219 // A conflict: a teammate changes City under the open edit form, and
220 // Save draws theirs and mine as a fieldset among the fields.
221 var got spacing
222 s.rig.Run(wide(), chromedp.Navigate(s.rig.Origin+cardPath(id)+"/edit"), chromedp.WaitVisible(`#`+s.city))
223 s.teammate(t, id, s.city, "Galway", "")
224 s.rig.Run(chromedp.SetValue(`#`+s.city, "Cork"),
225 chromedp.Evaluate[chromedp.Void](`HTMLFormElement.prototype.submit.call(document.querySelector('form[fichas-form]'))`),
226 chromedp.WaitVisible(`[fichas-conflict]`),
227 harness.Into(&got, chromedp.Evaluate[spacing](fieldGaps)))
228 check("the edit form after a conflict", got)
229}
230