| index aacdd19..824a76f 100644 |
| --- a/internal/fichastest/browser_rhythm_test.go |
| +++ b/internal/fichastest/browser_rhythm_test.go |
| @@ -3,9 +3,11 @@ |
| package fichastest |
| |
| import ( |
| - "cmp" |
| "context" |
| + "crypto/ed25519" |
| + "crypto/rand" |
| "fmt" |
| + "math" |
| "testing" |
| "time" |
| |
| @@ -25,28 +27,47 @@ import ( |
| // Name a region here only if it is a component's internals. |
| var rhythmSkip []string |
| |
| -// asWithoutScript puts the card back as it is drawn with scripts off: |
| -// grid.js marks the page live (data-fichas-live) and the card's Save |
| -// changes button goes, as each field saves on its own. Page scripts |
| -// cannot be switched off for the audit, which is itself a script. |
| -const asWithoutScript = `document.body.removeAttribute("data-fichas-live")` |
| +// 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, the card's own page, its History and |
| -// every form (a card's, a field's, a type's, the card layout), at a |
| -// desktop width and a phone's, with scripts on. 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. |
| +// 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 (b/ 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. |
| +// 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 |
| - rig := browserApp(t, func(ctx context.Context, d *db.DB, cfg fichas.Config) { |
| + // 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) |
| @@ -74,38 +95,46 @@ func TestTheScreensKeepTheRhythm(t *testing.T) { |
| }) |
| card := cardPath(id) |
| split := "/t/t_customer?sort=f_name&card=" + itoa(id) |
| - screens := []struct{ name, path, ready, prep string }{ |
| - {"the grid", "/t/t_customer?sort=f_name", `[fichas-grid] tbody tr`, ""}, |
| - {"the grid under a notice", "/t/t_customer?sort=f_name&saved=1", `[fichas-notice]`, ""}, |
| - {"a type with no cards", "/t/" + empty, `[rst-empty]`, ""}, |
| - {"the cards view", "/t/t_customer?sort=f_name&view=cards", `[fichas-card-grid]`, ""}, |
| - {"the card beside the list", split, `[fichas-pane] form[data-card-ready]`, ""}, |
| - {"the card beside the list, as without script", split, `[fichas-pane] form[data-card-ready]`, asWithoutScript}, |
| - {"the card beside the list under a notice", split + "&saved=1", `[fichas-pane] form[data-card-ready]`, ""}, |
| - {"the card's page", card, `form[data-card-ready]`, ""}, |
| - {"the card's page, as without script", card, `form[data-card-ready]`, asWithoutScript}, |
| - {"the card's History", card + "/history", `[fichas-history]`, ""}, |
| - {"the card's edit form", card + "/edit", `form[fichas-form]`, ""}, |
| - {"Delete, confirmed", card + "/delete", `[fichas-appbar]`, ""}, |
| - {"a new card", "/cards/new?type=t_customer", `form[fichas-form]`, ""}, |
| - {"the fields", "/t/t_customer/fields", `[fichas-grid]`, ""}, |
| - {"a new field", "/t/t_customer/fields/new", `form[fichas-form]`, ""}, |
| - {"a field's edit form", "/fields/" + fichas.FieldStage + "/edit", `form[fichas-form]`, ""}, |
| - {"a new type", "/types/new", `form[fichas-form]`, ""}, |
| - {"the card layout", "/t/t_customer/layout", `[fichas-layout-editor]`, ""}, |
| + // 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 { |
| - ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| - var found []rhythm.Finding |
| - err := chromedp.Do(ctx, |
| - viewport(width), |
| - chromedp.Navigate(rig.Origin+s.path), |
| - chromedp.WaitVisible(chromedp.CSS(s.ready)), |
| - chromedp.Evaluate[chromedp.Void](cmp.Or(s.prep, "0")), |
| - harness.Into(&found, chromedp.Evaluate[[]rhythm.Finding]( |
| - rhythm.Expression("main", rhythm.Options{Skip: rhythmSkip})))) |
| - cancel() |
| + found, err := auditScreen(rig, width, s) |
| if err != nil { |
| t.Fatalf("%s at %dpx: %v", s.name, width, err) |
| } |
| @@ -115,3 +144,86 @@ func TestTheScreensKeepTheRhythm(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// 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) |
| +} |