//go:build browser // Fiesta's rhythm audit over Memoria's screens, in a real Chromium. Run // with `make browser`. // // Paul, 2026-11-22: "Can we somehow put something in place to avoid // weird spacing issues like this in future?", then "Release and start // the rollout". `make ci` runs the stylesheet half (go tool fiesta // spacing); this is the half only a laid-out page can answer: every gap // between two blocks is a step of the scale, no text rests on a control, // and every heading sits closer to its own content than to what comes // before it. // // The audit does not walk into an element carrying an rst- name other // than rst-page, rst-stack and rst-section, because those are Fiesta's // components, spaced by Fiesta. Memoria's own regions are rst- names too // (rst-home, rst-paper, rst-fact-box, rst-ai-answer, …), so to the audit // each would be one opaque block. Each page is therefore read from main // AND from each of those regions it shows, as roots of their own: a // root is always walked. Fiesta's own components inside them (a callout, // a table, a list row) are still Fiesta's. // // One skip: the Colophon's list ([estilo-credits]), estilo's template // and stylesheet, whose h1 and list keep the browser's margins (18.8px); // that is estilo's to put on a stack. The door's card and the connect // pages are estilo's too, and opaque here by their estilo- and rst- // names without a skip; estilo audits its own card (6238a47). // // Screens are drawn in the states that change their blocks: the front // page with recent searches, a fact box, ?explain=facts, an AI answer, // no results, a document, its delete question, Add content (and with // its outcome flash), Sources and Notion unconnected and then connected // with an import behind them and suite apps in each breaker state, a // correction, Reports with a row, the 👎 page, the Colophon, a 404, and // team mode's partial lists. Not the upload tray: it is fixed in the // corner, out of the page's flow. // // Written against origin/main (5d9034d) first, its static/ and // templates/ under this branch's go.mod: 72 findings on 26 of the 34 // screen-widths, 67 of them signed in (68 off-scale, such as the h1's // browser margin of 18.8px, 28px above the home lists and 17.4px under // the results count; and 4 headings no closer to their content than to // the block before them, the Add content h1 among them) and 5 on the // door (estilo before 6238a47: 22px under the brand). It passes on the // stack. MUTATION-TESTED 2026-11-22, each reverted: rst-stack taken off //
→ 16 findings on a document, Add content, Sources and Notion at // both widths (an h1 back on its browser margin, 18.8px); [rst-ai-by]'s // margin put back to 0.4rem → "6.4px between blocks" in [rst-ai-answer] // at both widths, which only the region roots can see. (A hand margin on // a stack's child, [rst-home-lists] given back 1.75rem, changes nothing: // the stack's rule outweighs it, which is the point of the stack.) package memoriatest import ( "context" "fmt" "io" "log/slog" "net/http" "net/url" "path/filepath" "regexp" "testing" "time" "amadan.net/rastrillo/fiesta/check/rhythm" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/runtime" "github.com/chromedp/chromedp" memoria "amadan.net/oficina/memoria/internal/memoria" "amadan.net/oficina/memoria/internal/memoria/notion" "amadan.net/oficina/memoria/internal/memoria/provider" "amadan.net/oficina/memoria/internal/memoria/sibling" ) // quickAI answers at once, with a paragraph and a list, so the answer // card is drawn with both of its shapes. type quickAI struct{} func (quickAI) ExtractAddresses(context.Context, []string) ([]provider.Pointer, error) { return []provider.Pointer{}, nil } func (quickAI) Answer(context.Context, string, []string) ([]provider.Sentence, error) { return []provider.Sentence{ {Text: "The kitchen rota is pinned by the kettle.", Cite: []int{0}}, {Text: "- Mondays: Paul", Cite: []int{0}}, {Text: "- Tuesdays: Doc", Cite: []int{0}}, }, nil } // rhythmRegions are Memoria's own regions that hold blocks, each audited // as a root of its own where the page shows one (see above). var rhythmRegions = []string{ "[rst-home]", "[rst-home-lists] > section", "[rst-refresh=\"results\"]", "[rst-answer]", "[rst-ai-answer]", "[rst-fact-box]", "[rst-paper]", "[rst-correct]", "[rst-correct-form]", "[rst-dropzone]", "[rst-sources] > li", "[rst-report]", "[rst-page-footer]", } // rhythmSkip are the regions whose spacing is not the page's. var rhythmSkip = []string{ // The Colophon is estilo's template and estilo's stylesheet // (estilo/credits); its h1 and list keep the browser's margins, which // is estilo's to put on its stack. "[estilo-credits]", } // auditRhythm reads the page the browser is on from main and from each // of Memoria's regions it shows, and returns every finding. func auditRhythm(ctx context.Context) ([]rhythm.Finding, error) { var all []rhythm.Finding for _, root := range append([]string{"main"}, rhythmRegions...) { var present bool if err := chromedp.Do(ctx, evalInto(&present, fmt.Sprintf(`!!document.querySelector(%q)`, root))); err != nil { return nil, err } if !present { continue } var found []rhythm.Finding if err := chromedp.Do(ctx, evalInto(&found, rhythm.Expression(root, rhythm.Options{Skip: rhythmSkip}))); err != nil { return nil, fmt.Errorf("audit %s: %w", root, err) } for i := range found { found[i].Message = root + ": " + found[i].Message } all = append(all, found...) } return all, nil } // settle waits for the page's fonts, so the audit measures the type the // reader sees and not the fallback's. var settle = evalOnly(`document.fonts.ready.then(() => true)`, func(p *runtime.EvaluateParams) { p.AwaitPromise = new(true) }) // TestEveryScreenKeepsTheRhythm is the audit over every signed-in // screen, at a desktop width and a phone's. func TestEveryScreenKeepsTheRhythm(t *testing.T) { var app interface { Store() *memoria.Store UseFakeSource(memoria.SourceClient, func(context.Context, string) (string, error)) UseSiblings(memoria.Custody) PreviewUnknownStanding() } rig := harness.New(t, func(origin string) http.Handler { logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) if err != nil { t.Fatalf("db.Open: %v", err) } t.Cleanup(func() { d.Close() }) mux, a, err := memoria.AppForTest(d, origin, testDEK(t), logger) if err != nil { t.Fatalf("AppForTest: %v", err) } app = a a.UseFakeModel(quickAI{}) for _, doc := range [][2]string{ {"Office", "## Office Address\n\nThe office phone number is: +353 (0)1 547 7854\n\n" + "We also have the following unused phone numbers: +353 (0)1 598 1234\n\nOffice VAT: IE3384527RH\n\n" + "### Directions\n\n- Take the Luas to Abbey Street\n- Walk north\n - Past the Spire\n\n> Ring the bell twice.\n\n" + "| Floor | Team |\n|---|---|\n| 1 | Sales |\n| 2 | Ops |\n"}, {"Kitchen", "## Rota\n\nThe kitchen rota is pinned by the kettle."}, {"Accounts", "## Contact\n\nAccounts email: accounts@tito-example.ie"}, } { if _, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{Source: "upload", Key: doc[0], Title: doc[0], Body: doc[1]}); err != nil { t.Fatal(err) } } return mux }) run := func(what string, actions ...chromedp.Action[chromedp.Void]) { t.Helper() ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) defer cancel() if err := chromedp.Do(ctx, actions...); err != nil { t.Fatalf("%s: %v", what, err) } } // A document's page, its delete question, a correction (so Reports // has a row) and a 👎 (so the feedback page exists). var at string run("find a document", chromedp.Navigate(rig.Origin+"/documents"), chromedp.WaitVisible(chromedp.CSS(`a[href^="/documents/"]`)), attributeInto(chromedp.CSS(`a[href^="/documents/"]`), "href", &at, nil), ) doc := regexp.MustCompile(`^/documents/\d+$`).FindString(at) if doc == "" { t.Fatalf("the documents list links no document (got %q)", at) } phone := "/facts/correct?q=" + url.QueryEscape("office phone number") run("report a correction", chromedp.Navigate(rig.Origin+phone), chromedp.WaitVisible(chromedp.CSS(`input[name="choice"]`)), chromedp.Click(chromedp.CSS(`input[name="choice"]`)), chromedp.Submit(chromedp.CSS(`input[name="choice"]`)), chromedp.WaitVisible(chromedp.CSS(`input[type="search"]`)), ) var feedback string run("rate an answer down", chromedp.Navigate(rig.Origin+"/?q="+url.QueryEscape("kitchen rota")), chromedp.WaitVisible(chromedp.CSS(`[rst-ai-feedback] button[value="down"]`)), chromedp.Click(chromedp.CSS(`[rst-ai-feedback] button[value="down"]`)), chromedp.WaitVisible(chromedp.CSS(`textarea[name="note"]`)), locationInto(&feedback), ) if u, err := url.Parse(feedback); err == nil { feedback = u.Path } audit := func(path string, width int64, actions ...chromedp.Action[chromedp.Void]) { t.Helper() ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) defer cancel() err := chromedp.Do(ctx, append([]chromedp.Action[chromedp.Void]{chromedp.EmulateViewport(width, 900)}, append(actions, settle)...)...) var found []rhythm.Finding if err == nil { found, err = auditRhythm(ctx) } if err != nil { t.Fatalf("%s at %dpx: %v", path, width, err) } if len(found) > 0 { t.Errorf("%s at %dpx: %s", path, width, rhythm.Report(found)) } } visit := func(path, ready string, width int64) { t.Helper() audit(path, width, chromedp.Navigate(rig.Origin+path), chromedp.WaitVisible(chromedp.CSS(ready))) } screens := []struct{ path, ready string }{ {"/", `[rst-home]`}, {"/?q=" + url.QueryEscape("office phone number"), `[rst-fact-box]`}, {"/?q=" + url.QueryEscape("office phone number") + "&explain=facts", `[rst-fact-explain]`}, {"/?q=" + url.QueryEscape("kitchen rota"), `[rst-ai-answer]`}, {"/?q=" + url.QueryEscape("zebra crossings"), `[rst-search-empty]`}, {"/documents", `[rst-docs]`}, {doc, `[rst-paper]`}, {doc + "/delete", `form[action$="/delete"]`}, {"/add", `textarea[name="body"]`}, {"/connectors", `main h1`}, {"/connectors/notion", `input[name="token"]`}, {phone, `input[name="choice"]`}, {"/reports", `[rst-report]`}, {feedback, `textarea[name="note"]`}, {"/help/open-source", `main h1`}, {"/no-such-page", `main`}, } for _, width := range []int64{1280, 390} { for _, s := range screens { visit(s.path, s.ready, width) } // Add content with its outcome: the flash under the form. audit("/add, stored", width, chromedp.Navigate(rig.Origin+"/add"), chromedp.WaitVisible(chromedp.CSS(`textarea[name="body"]`)), chromedp.SendKeys(chromedp.CSS(`textarea[name="body"]`), "The fire drill is on Thursdays."), chromedp.Click(chromedp.CSS(`[rst-actions] button`)), chromedp.WaitVisible(chromedp.CSS(`main [rst-callout]`)), ) } // Sources and Notion connected, with an import behind them, and the // suite apps in the breaker's three states (tools/drive's seed). seedRhythmConnector(t, app) seedRhythmSiblings(t, app) for _, width := range []int64{1280, 390} { visit("/connectors/notion", `[rst-facts]`, width) visit("/connectors", `[rst-source-name]`, width) } // Team mode with the reader's standing unknown: the list pages are // partial, with the notice. app.PreviewUnknownStanding() for _, width := range []int64{1280, 390} { for _, path := range []string{"/", "/documents", "/reports", "/?q=" + url.QueryEscape("office phone number")} { visit(path, `main [rst-callout]`, width) } } } // rhythmSource is a two-page stand-in for Notion. type rhythmSource struct{} func (rhythmSource) Roots(context.Context) (roots, orphans []notion.Ref, err error) { return []notion.Ref{{ID: "handbook", Title: "Handbook", EditedTime: "t1", Kind: notion.KindPage}}, nil, nil } func (rhythmSource) Fetch(_ context.Context, ref notion.Ref) (*notion.Content, error) { c := ¬ion.Content{Markdown: "## " + ref.Title + "\n\nImported for the rhythm audit.", UnreadBlocks: 2} if ref.ID == "handbook" { c.Children = []notion.Ref{{ID: "onboarding", Title: "Onboarding", Path: []string{"Handbook"}, EditedTime: "t1", Kind: notion.KindPage}} } return c, nil } // seedRhythmConnector connects Notion and runs one import to the end. func seedRhythmConnector(t *testing.T, app interface { Store() *memoria.Store UseFakeSource(memoria.SourceClient, func(context.Context, string) (string, error)) }) { t.Helper() ctx := context.Background() app.UseFakeSource(rhythmSource{}, func(context.Context, string) (string, error) { return "rhythm-workspace", nil }) if err := app.Store().SaveConnector(ctx, "notion", "ntn_rhythm_token", "rhythm-workspace"); err != nil { t.Fatal(err) } c, err := app.Store().Connector(ctx, "notion") if err != nil { t.Fatal(err) } run, err := app.Store().StartRun(ctx, c.ID) if err != nil { t.Fatal(err) } memoria.NewSyncer(app.Store(), slog.New(slog.NewTextHandler(io.Discard, nil))).RunWith(ctx, rhythmSource{}, c.ID, run.ID) } // seedRhythmSiblings puts three suite apps on Sources, quiet, firing and // cannot-determine, as tools/drive does. func seedRhythmSiblings(t *testing.T, app interface { Store() *memoria.Store UseSiblings(memoria.Custody) }) { t.Helper() ctx := context.Background() custody, err := memoria.NewSiblings("rhythm", nil, []sibling.Sibling{ {Name: "Docs", BaseURL: "https://rhythm.docs.oficina.works", Token: "rhythm"}, {Name: "Sheets", BaseURL: "https://rhythm.sheets.oficina.works", Token: "rhythm"}, {Name: "Calendar", BaseURL: "https://rhythm.calendar.oficina.works", Token: "rhythm"}, }) if err != nil { t.Fatal(err) } app.UseSiblings(custody) store, now := app.Store(), time.Now().UTC() for app, since := range map[string]time.Time{"Docs": now.AddDate(0, 0, -30), "Sheets": now.AddDate(0, 0, -30), "Calendar": now.AddDate(0, 0, -2)} { if err := store.StartCounting(ctx, app, since); err != nil { t.Fatal(err) } } for _, who := range []string{"ana@example.com", "ben@example.com", "cleo@example.com"} { store.SeedTallyForDrive(ctx, "Sheets", who, now.AddDate(0, 0, -3), 5, 0) store.SeedTallyForDrive(ctx, "Sheets", who, now, 0, 4) } } // TestTheDoorKeepsTheRhythm: the signed-out pages, drawn through the // production App with sign-in wired: the door and /signed-out. func TestTheDoorKeepsTheRhythm(t *testing.T) { rig := harness.New(t, func(origin string) http.Handler { logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) if err != nil { t.Fatalf("db.Open: %v", err) } t.Cleanup(func() { d.Close() }) mux, a, err := memoria.App(d, origin, testDEK(t), logger) if err != nil { t.Fatalf("App: %v", err) } au, err := memoria.NewAuth(d.Writer(), memoria.AuthEnv{ Origin: origin, InstanceKey: "rhythm-test-instance-key", Owners: []string{dialTestOwner}, Mailer: &stubMailer{}, }, logger, a) if err != nil { t.Fatalf("NewAuth: %v", err) } a.UseAuth(au) a.SetOwners([]string{dialTestOwner}) a.MountAuth(mux, origin) return mux }) for _, width := range []int64{1280, 390} { for _, path := range []string{"/signin", "/signed-out"} { ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) err := chromedp.Do(ctx, chromedp.EmulateViewport(width, 900), chromedp.Navigate(rig.Origin+path), chromedp.WaitVisible(chromedp.CSS(`main`)), settle, ) var found []rhythm.Finding if err == nil { found, err = auditRhythm(ctx) } cancel() if err != nil { t.Fatalf("%s at %dpx: %v", path, width, err) } if len(found) > 0 { t.Errorf("%s at %dpx: %s", path, width, rhythm.Report(found)) } } } }