oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package docs_test
4
5import (
6 "context"
7 "net/http"
8 "net/http/httptest"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/estilo.git/googleimport"
14 "amadan.net/rastrillo/fiesta/check/rhythm"
15 "amadan.net/rastrillo/rastrillo"
16 "amadan.net/rastrillo/rastrillo/harness"
17 "github.com/chromedp/cdproto/emulation"
18 "github.com/chromedp/chromedp"
19
20 "github.com/carlosframework/docs/internal/docs"
21)
22
23// TestEveryScreenKeepsTheRhythm: each of Docs' own screens, read by
24// Fiesta's rhythm audit (Paul, 2026-11-22: "Can we somehow put
25// something in place to avoid weird spacing issues like this in
26// future?"). Every gap between two of a page's blocks is a step of the
27// spacing scale, no text rests on a control, and every heading sits
28// closer to its own content than to what comes before it, at a desktop
29// width and a phone's.
30//
31// The audit reads around what is not the page's to space: the
32// document itself (the editor's canvas and the published reader's
33// body), set at the document's own measures in inches and ems, and the
34// editor's formatting bar, a toolbar with its own density.
35//
36// CGO_ENABLED=0 go test -tags browser -run Rhythm ./internal/docs/
37func TestEveryScreenKeepsTheRhythm(t *testing.T) {
38 var app, google *docs.App
39 var serving http.Handler
40 rig := harness.New(t, func(origin string) http.Handler {
41 // Served as a deployed instance serves it, so the pages carry
42 // the English catalog: the audit measures real words, which
43 // wrap where the keys would not. Two instances on one origin:
44 // one as most are, with a member and a guest the member shared
45 // a document with, and one with Google Drive import configured,
46 // which draws the import page's chooser.
47 serve := func(cfg docs.Config) (*docs.App, http.Handler) {
48 cfg.Origin = origin
49 a, err := docs.NewForTestWith(t, cfg)
50 if err != nil {
51 t.Fatalf("NewForTestWith: %v", err)
52 }
53 served, done, err := rastrillo.Handler(docs.ServeOptions(rastrillo.Options{}, a))
54 if err != nil {
55 t.Fatalf("rastrillo.Handler: %v", err)
56 }
57 if done != nil {
58 t.Cleanup(func() { _ = done() })
59 }
60 return a, served
61 }
62 app, serving = serve(docs.Config{AllowedAddresses: "rhythm@example.com"})
63 var googleServed http.Handler
64 google, googleServed = serve(docs.Config{AllowedAddresses: "rhythm@example.com", GoogleImport: googleimport.Config{
65 ClientID: "123456789-client.apps.googleusercontent.com", APIKey: "AIzaPublicBrowserKey123456789", AppID: "123456789"}})
66 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
67 if r.URL.Query().Get("instance") == "google" || strings.HasPrefix(r.Header.Get("Referer"), origin+"/import?instance=google") {
68 googleServed.ServeHTTP(w, r)
69 return
70 }
71 serving.ServeHTTP(w, r)
72 })
73 })
74 cookies := signInCookies(t, app, "rhythm@example.com")
75 post := func(path, body string) *httptest.ResponseRecorder {
76 t.Helper()
77 req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
78 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
79 req.Header.Set("Sec-Fetch-Site", "same-origin")
80 for _, c := range cookies {
81 req.AddCookie(c)
82 }
83 rec := httptest.NewRecorder()
84 app.Handler().ServeHTTP(rec, req)
85 if rec.Code != http.StatusSeeOther {
86 t.Fatalf("POST %s: %d", path, rec.Code)
87 }
88 return rec
89 }
90 sqid := strings.TrimPrefix(post("/documents", "title=Quarterly+plan").Header().Get("Location"), "/d/")
91
92 // Each screen is loaded once, at a desktop width, and read there and
93 // again at a phone's: the published link shows only in the render
94 // that follows the publish, so a second load would not show it.
95 // ready is a selector only that screen draws; the browser must still
96 // be on path when it is read (a lost session lands on the sign-in
97 // door, whose rhythm is not this page's); and main must draw at
98 // least blocks blocks, so a page that drew nothing cannot pass by
99 // having nothing to measure.
100 audit := func(name, path, ready string, blocks int, skip []string, after ...chromedp.Action[chromedp.Void]) {
101 t.Helper()
102 for i, width := range []int64{1280, 390} {
103 ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second)
104 var found []rhythm.Finding
105 var at string
106 var drawn int
107 actions := []chromedp.Action[chromedp.Void]{
108 command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: width, Height: 900, DeviceScaleFactor: 1}),
109 }
110 if i == 0 {
111 actions = append(actions, chromedp.Navigate(rig.Origin+path))
112 }
113 actions = append(actions,
114 chromedp.WaitVisible(chromedp.CSS(ready)),
115 evalInto(&at, `location.pathname + location.search`),
116 evalInto(&drawn, `document.querySelectorAll('main *').length`),
117 evalInto(&found, rhythm.Expression("main", rhythm.Options{Skip: skip})))
118 actions = append(actions, after...)
119 err := chromedp.Do(ctx, actions...)
120 cancel()
121 if err != nil {
122 t.Fatalf("%s at %dpx: %v", name, width, err)
123 }
124 if at != path {
125 t.Fatalf("%s: the browser is at %s, not %s", name, at, path)
126 }
127 if drawn < blocks {
128 t.Fatalf("%s at %dpx: main holds %d elements, want at least %d", name, width, drawn, blocks)
129 }
130 if len(found) > 0 {
131 t.Errorf("%s at %dpx: %s", name, width, rhythm.Report(found))
132 }
133 }
134 }
135 signInAs := func(cookies []*http.Cookie) {
136 t.Helper()
137 set := []chromedp.Action[chromedp.Void]{chromedp.Navigate(rig.Origin + "/signin")}
138 for _, c := range cookies {
139 set = append(set, setCookie(rig.Origin, c))
140 }
141 rig.Run(set...)
142 }
143
144 // Signed out: the door every visit starts on (estilo's card).
145 audit("the sign-in door", "/signin", `input[name="address"]`, 3, nil)
146 audit("the fonts help", "/help/fonts", `[docs-font-twins]`, 8, nil)
147
148 signInAs(cookies)
149 audit("the list", "/", `[docs-shelf]`, 8, nil)
150 audit("a search with no results", "/?q=nothing-matches-this", `[rst-empty]`, 8, nil)
151 audit("the editor", "/d/"+sqid, `[contenteditable="true"]`, 8, []string{"[docs-bar]", "[docs-canvas]"})
152 audit("the share page", "/d/"+sqid+"/share", `[docs-share-form] input[name="address"]`, 12, nil)
153 // The page about to go is drawn tipped by 3°, so its box is wider
154 // and taller than the sheet a reader sees: the audit reads around it.
155 audit("delete", "/d/"+sqid+"/delete", `[docs-delete]`, 6, []string{"[docs-delete-sheet]"})
156 // This instance does not seal content, so the import page draws its
157 // refusal above the form: the error state.
158 audit("import, refused", "/import", `[docs-import-error]`, 6, nil)
159
160 // Published: the link shows only in the render that follows the
161 // publish, so it is read once, and the reader is the page it opens.
162 post("/d/"+sqid+"/share/link", "on=1")
163 var link string
164 audit("the share page with its link", "/d/"+sqid+"/share", `[docs-share-url-input]`, 12, nil,
165 evalInto(&link, `document.querySelector('[docs-share-url-input]')?.value ?? ""`))
166 if link == "" {
167 t.Fatal("the published share page shows no link")
168 }
169 audit("the published reader", strings.TrimPrefix(link, rig.Origin), `[docs-public]`, 3, []string{"[docs-public-body]"})
170
171 // A guest: somebody the member shared a document with, who is not on
172 // the instance's list, so the list's header has no New or Import.
173 post("/d/"+sqid+"/share", "address=guest%40example.com&role=reader")
174 signInAs(signInCookies(t, app, "guest@example.com"))
175 var guestCreates bool
176 audit("a guest's list", "/", `[docs-list]`, 6, nil,
177 evalInto(&guestCreates, `!!document.querySelector('[docs-import-link], [docs-create-form]')`))
178 if guestCreates {
179 t.Fatal("the guest's list offers New or Import: this audited a member's header")
180 }
181
182 // Google Drive import configured, on its own instance.
183 signInAs(signInCookies(t, google, "rhythm@example.com"))
184 audit("import, with Google Drive", "/import?instance=google", `[estilo-google-import]`, 8, nil)
185}
186