//go:build browser package fichastest import ( "context" "crypto/ed25519" "crypto/rand" "fmt" "math" "testing" "time" "amadan.net/rastrillo/fiesta/check/rhythm" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" fichas "amadan.net/oficina/fichas/internal/fichas" ) // rhythmSkip is what the audit does not read, and it is nothing. The // grid's canvas (its rows, cells and editors) is the one region whose // spacing is not the page's, and the audit already reads a table as one // block, measured against its neighbours but never walked into; an // editor or picker floats (position: absolute), which it passes over. // Name a region here only if it is a component's internals. var rhythmSkip []string // contractFlow is any well-formed flow: the contract's first two phases // (choose a customer, choose its fields) are GETs a signed-in person // reaches with one. Its last two need a signed release, so the audit // reads the phases a person sees first. const contractFlow = "00000000-0000-4000-8000-000000000000" // rhythmScreen is one state of one screen: the path, what to wait for // once it is drawn, whether page scripts run, and an optional step in // the page (open Advanced, submit a form) and what to wait for after it. type rhythmScreen struct { name, path, ready string noScript bool act, then string } // TestTheScreensKeepTheRhythm: Fiesta's rhythm audit (Paul, 2026-11-22: // "Can we somehow put something in place to avoid weird spacing issues // like this in future?") over Fichas' own screens: the grid, the cards // view, the card beside the list and on its own page (with scripts and // without), its History, every form (a card's, a field's, a type's, the // card layout, the contract's first two phases) with Advanced open, the // choices box shown and the server's errors drawn, at a desktop width // and a phone's. Every gap between two blocks is a step of the scale, no // text rests on a control, and every heading sits closer to its content // than to what comes before. // // It failed on main before the rhythm swap (95 findings, 2026-11-22): // the card's heading, meta line, fields and call log were 14, 28 and // 10px apart, and the grid's help line sat 11.2px over the count. func TestTheScreensKeepTheRhythm(t *testing.T) { var id int64 var empty string // The contract's release is configured, so its pages are mounted. _, key, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatal(err) } release := func(cfg *fichas.Config) { cfg.ContractRelease = &fichas.ContractReleaseConfig{Destination: "https://docs.example.com", Importer: TestOwner, Team: "test-team", Key: key} } rig := browserAppWith(t, release, func(ctx context.Context, d *db.DB, cfg fichas.Config) { m, err := fichas.EnsureMember(ctx, d.G, cfg.Sealer, TestOwner) if err != nil { t.Fatalf("member: %v", err) } ct, err := fichas.CreateType(ctx, d.G, cfg.Sealer, fichas.Person(m.ID), "Supplier", "Suppliers") if err != nil { t.Fatalf("type: %v", err) } empty = ct.Key for i := 0; i < 3; i++ { c, err := fichas.CreateCard(ctx, d.G, cfg.Sealer, fichas.TypeCustomer, fichas.Person(m.ID), map[string]string{fichas.FieldName: fmt.Sprintf("Card %03d", i), fichas.FieldStage: "Lead", fichas.FieldEmail: fmt.Sprintf("c%d@example.com", i), fichas.FieldPhone: "+353 1 555 0142"}) if err != nil { t.Fatalf("seed: %v", err) } if i == 0 { id = c.ID if _, err := fichas.AddEntry(ctx, d.G, cfg.Sealer, c.ID, fichas.Person(m.ID), fichas.TypeCallLog, "manual", map[string]string{fichas.FieldBody: "Asked for the renewal quote by Friday."}); err != nil { t.Fatalf("seed entry: %v", err) } } } }) card := cardPath(id) split := "/t/t_customer?sort=f_name&card=" + itoa(id) // submit sends a form past the browser's own checks, so the server's // errors are what is drawn. submit := func(form string) string { return `HTMLFormElement.prototype.submit.call(document.querySelector(` + "`" + form + "`" + `))` } screens := []rhythmScreen{ {name: "the grid", path: "/t/t_customer?sort=f_name", ready: `[fichas-grid][role="grid"] tbody tr`}, {name: "the grid under a notice", path: "/t/t_customer?sort=f_name&saved=1", ready: `[fichas-notice]`}, {name: "a type with no cards", path: "/t/" + empty, ready: `[rst-empty]`}, {name: "the cards view", path: "/t/t_customer?sort=f_name&view=cards", ready: `[fichas-card-grid]`}, {name: "the card beside the list", path: split, ready: `[fichas-pane] form[data-card-ready]`}, {name: "the card beside the list, without script", path: split, ready: `[fichas-pane] [fichas-card-save]`, noScript: true}, {name: "the card beside the list under a notice", path: split + "&saved=1", ready: `[fichas-pane] form[data-card-ready]`}, {name: "the card's page", path: card, ready: `form[data-card-ready]`}, {name: "the card's page, without script", path: card, ready: `[fichas-card-save]`, noScript: true}, {name: "the card's History", path: card + "/history", ready: `[fichas-history]`}, {name: "the card's edit form", path: card + "/edit", ready: `form[fichas-form]`}, {name: "the card's edit form, without script", path: card + "/edit", ready: `form[fichas-form]`, noScript: true}, {name: "Delete, confirmed", path: card + "/delete", ready: `[fichas-appbar]`}, {name: "a new card", path: "/cards/new?type=t_customer", ready: `form[fichas-form]`}, {name: "a new card, refused", path: "/cards/new?type=t_customer", ready: `form[fichas-form]`, act: submit(`form[fichas-form]`), then: `[rst-field-error]`}, {name: "the fields", path: "/t/t_customer/fields", ready: `[fichas-grid]`}, {name: "a new field", path: "/t/t_customer/fields/new", ready: `form[fichas-form]`}, {name: "a new field, a Choice, Advanced open", path: "/t/t_customer/fields/new", ready: `form[fichas-form]`, act: `document.querySelector('input[name="kind"][value="enum"]').checked = true; document.querySelector('[fichas-advanced]').open = true`, then: `[fichas-choice-options] textarea`}, {name: "a new field, refused", path: "/t/t_customer/fields/new", ready: `form[fichas-form]`, act: submit(`form[fichas-form]`), then: `[rst-field-error]`}, {name: "a field's edit form", path: "/fields/" + fichas.FieldStage + "/edit", ready: `form[fichas-form]`}, {name: "a new type", path: "/types/new", ready: `form[fichas-form]`}, {name: "a new type, refused", path: "/types/new", ready: `form[fichas-form]`, act: submit(`form[fichas-form]`), then: `[rst-field-error]`}, {name: "the card layout", path: "/t/t_customer/layout", ready: `[fichas-layout-editor]`}, {name: "a contract: choose a customer", path: "/contracts/prepare?flow=" + contractFlow, ready: `[rst-box]`}, {name: "a contract: choose its fields", path: "/contracts/prepare?flow=" + contractFlow + "&card=" + itoa(id), ready: `[rst-box] form`}, } for _, width := range []int64{1440, 390} { for _, s := range screens { found, err := auditScreen(rig, width, s) if err != nil { t.Fatalf("%s at %dpx: %v", s.name, width, err) } if len(found) > 0 { t.Errorf("%s at %dpx: %s", s.name, width, rhythm.Report(found)) } } } } // auditScreen draws one screen state and reads the audit over main. // Scripts are switched back on afterwards, whatever happened. func auditScreen(rig *harness.Rig, width int64, s rhythmScreen) ([]rhythm.Finding, error) { ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) defer cancel() defer rig.Run(scriptsOn(true)) steps := []chromedp.Action[chromedp.Void]{scriptsOn(!s.noScript), viewport(width), chromedp.Navigate(rig.Origin + s.path)} if s.noScript { // No page script, so no script-made marker: wait on the server's // markup, through the DOM domain. steps = append(steps, chromedp.WaitReady(chromedp.CSS(s.ready))) } else { steps = append(steps, chromedp.WaitVisible(chromedp.CSS(s.ready))) } if s.act != "" { steps = append(steps, chromedp.Evaluate[chromedp.Void](s.act), chromedp.WaitVisible(chromedp.CSS(s.then))) } var found []rhythm.Finding steps = append(steps, harness.Into(&found, chromedp.Evaluate[[]rhythm.Finding]( rhythm.Expression("main", rhythm.Options{Skip: rhythmSkip})))) return found, chromedp.Do(ctx, steps...) } // fieldGaps is the space between each two neighbouring fields (rst-field // or a conflict's fieldset) of a form, and the scale's --rst-sp-3 in px. const fieldGaps = `(() => { const form = document.querySelector('form[fichas-form]'); const root = parseFloat(getComputedStyle(document.documentElement).fontSize); const step = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--rst-sp-3')) * root; const field = e => e.hasAttribute('rst-field') || e.hasAttribute('fichas-conflict'); const kids = [...form.children].filter(e => e.getBoundingClientRect().height > 0); const gaps = []; for (let i = 1; i < kids.length; i++) { if (field(kids[i - 1]) && field(kids[i])) gaps.push(kids[i].getBoundingClientRect().top - kids[i - 1].getBoundingClientRect().bottom); } return {step, gaps}; })()` // TestTheFormsFieldsAreAStepApart: the rhythm audit cannot see a gap that // is on the scale but the wrong step. The edit form once wrote each // field's hidden "was" input between it and the field before, which hid // that field from the stack's rule: its fields sat at the stack's 8px // floor, where the new-card form's sit at --rst-sp-3 (Fable, the rhythm // swap's review, M1). Every two fields, and a conflict's fieldset beside // a field, are --rst-sp-3 apart, on the edit form, after a conflict, and // on the new-card form. func TestTheFormsFieldsAreAStepApart(t *testing.T) { s := seedCards(t, 1) type spacing struct { Step float64 `json:"step"` Gaps []float64 `json:"gaps"` } check := func(name string, got spacing) { t.Helper() if len(got.Gaps) < 4 { t.Fatalf("%s: %d gaps between fields; this test would pass vacuously", name, len(got.Gaps)) } for i, g := range got.Gaps { if math.Abs(g-got.Step) > 0.5 { t.Errorf("%s: fields %d and %d are %.1fpx apart, want --rst-sp-3, %.1fpx", name, i, i+1, g, got.Step) } } } id := s.ids[0] for _, path := range []string{cardPath(id) + "/edit", "/cards/new?type=t_customer"} { var got spacing s.rig.Run(wide(), chromedp.Navigate(s.rig.Origin+path), chromedp.WaitVisible(`form[fichas-form]`), harness.Into(&got, chromedp.Evaluate[spacing](fieldGaps))) check(path, got) } // A conflict: a teammate changes City under the open edit form, and // Save draws theirs and mine as a fieldset among the fields. var got spacing s.rig.Run(wide(), chromedp.Navigate(s.rig.Origin+cardPath(id)+"/edit"), chromedp.WaitVisible(`#`+s.city)) s.teammate(t, id, s.city, "Galway", "") s.rig.Run(chromedp.SetValue(`#`+s.city, "Cork"), chromedp.Evaluate[chromedp.Void](`HTMLFormElement.prototype.submit.call(document.querySelector('form[fichas-form]'))`), chromedp.WaitVisible(`[fichas-conflict]`), harness.Into(&got, chromedp.Evaluate[spacing](fieldGaps))) check("the edit form after a conflict", got) }