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
3// Help → Colophon and Help → Fonts in a real Chromium (RASTRILLO_CHROME):
4//
5// CGO_ENABLED=0 go test -tags browser -run Colophon ./internal/docs/
6package docs_test
7
8import (
9 "context"
10 "fmt"
11 "net/http"
12 "strings"
13 "testing"
14 "time"
15
16 "amadan.net/rastrillo/rastrillo"
17 "amadan.net/rastrillo/rastrillo/harness"
18 "github.com/chromedp/cdproto/emulation"
19 "github.com/chromedp/chromedp"
20
21 "github.com/carlosframework/docs/internal/docs"
22)
23
24// The pages reflow at 320px (WCAG 1.4.10), every link is a 24px target
25// on its smaller axis (2.5.8, no spacing exception), they work with
26// scripts off, and they never load a font: the chrome's system face is
27// all they use, so nothing on them can flash.
28func TestColophonPagesReflowAndNeedNoScriptOrFont(t *testing.T) {
29 // Served through rastrillo's production middleware, as cmd/docs
30 // serves it (ServeOptions: the locales and catalog among it), so the
31 // page measured is the one people get, in English, not catalog keys.
32 rig := harness.New(t, func(origin string) http.Handler {
33 app, err := docs.NewForTestWith(t, docs.Config{Origin: origin})
34 if err != nil {
35 t.Fatalf("NewForTestWith: %v", err)
36 }
37 served, done, err := rastrillo.Handler(docs.ServeOptions(rastrillo.Options{}, app))
38 if err != nil {
39 t.Fatalf("rastrillo.Handler: %v", err)
40 }
41 if done != nil {
42 t.Cleanup(func() { _ = done() })
43 }
44 return served
45 })
46 run := func(what string, actions ...chromedp.Action[chromedp.Void]) {
47 t.Helper()
48 ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second)
49 defer cancel()
50 if err := chromedp.Do(ctx, actions...); err != nil {
51 t.Fatalf("%s: %v", what, err)
52 }
53 }
54 type measure struct {
55 Overflow int
56 Small []string
57 Entries int
58 Loaded int
59 Family string
60 HasMain bool
61 Headings int
62 Wrapped []string
63 Keys []string
64 }
65 const script = `(() => {
66 const main = document.querySelector('[estilo-colophon]');
67 const small = [...(main ? main.querySelectorAll('a') : [])]
68 .filter(a => a.getClientRects().length > 0)
69 .filter(a => { const r = a.getBoundingClientRect(); return Math.min(r.width, r.height) < 24 })
70 .map(a => a.textContent.trim() + ' ' + Math.round(a.getBoundingClientRect().height) + 'px');
71 return {
72 // The page's own content only. The bar is estilo's chrome, held
73 // by its own checks; here its labels are catalog keys (no locale
74 // middleware), and the signed-out "docs.chrome.signin" is wider
75 // at 320px than "Sign in" ever is.
76 Overflow: main ? Math.max(0, ...[...main.querySelectorAll('*')].map(e => Math.ceil(e.getBoundingClientRect().right - innerWidth)), main.scrollWidth - main.clientWidth) : 0,
77 Small: small,
78 Entries: document.querySelectorAll('[estilo-colophon-entry]').length,
79 Loaded: [...document.fonts].filter(f => f.status === 'loaded').length,
80 Family: main ? getComputedStyle(main.querySelector('h1')).fontFamily : '',
81 HasMain: !!main,
82 // A catalog key is docs.<area>.<name>, with no slash, colon or
83 // hyphen around it, so a hostname such as docs.oficina.works in
84 // page text is not one.
85 Keys: (document.body.innerText.match(/(?<![\w.\/:-])docs\.(?:app|chrome|colophon|help|error|signin|signout|list|jumpto)\.[a-z][a-z.]*[a-z](?![\w\/:-])/g) || []).slice(0, 5),
86 Headings: document.querySelectorAll('h1').length,
87 // A label is one line: taller than its own line-height means
88 // flex squeezed it and it broke mid-word ("Ver/si/on").
89 Wrapped: [...document.querySelectorAll('[estilo-colophon] dt')]
90 .filter(e => e.getBoundingClientRect().height > parseFloat(getComputedStyle(e).lineHeight) * 1.5)
91 .map(e => e.textContent.trim() + ' ' + Math.round(e.getBoundingClientRect().width) + 'px wide'),
92 };
93 })()`
94 c := docs.Colophon()
95 for _, scripts := range []bool{true, false} {
96 run("scripts", command(emulation.SetScriptExecutionDisabled, emulation.SetScriptExecutionDisabledParams{Value: !scripts}))
97 for _, width := range []int64{320, 1200} {
98 run("width", command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: width, Height: 900, DeviceScaleFactor: 1}))
99 for _, path := range []string{"/help/colophon", "/help/colophon/carlito", "/help/colophon/prosemirror", "/help/fonts"} {
100 var m measure
101 run(path, chromedp.Navigate(rig.Origin+path), chromedp.WaitReady(chromedp.CSS("[estilo-colophon]")), chromedp.Sleep(150*time.Millisecond), evalInto(&m, script))
102 where := fmt.Sprintf("%s at %dpx, scripts %v", path, width, scripts)
103 if !m.HasMain || m.Headings != 1 {
104 t.Fatalf("%s: the page did not render (%+v)", where, m)
105 }
106 if len(m.Keys) > 0 {
107 t.Errorf("%s: the page shows catalog keys, not its words: %v", where, m.Keys)
108 }
109 if m.Overflow > 0 {
110 t.Errorf("%s: scrolls sideways by %dpx", where, m.Overflow)
111 }
112 if len(m.Wrapped) > 0 {
113 t.Errorf("%s: labels broken over lines: %s", where, strings.Join(m.Wrapped, "; "))
114 }
115 if len(m.Small) > 0 {
116 t.Errorf("%s: links under 24px: %s", where, strings.Join(m.Small, "; "))
117 }
118 if m.Loaded != 0 {
119 t.Errorf("%s: %d font faces loaded; the chrome uses the system face only", where, m.Loaded)
120 }
121 if !strings.Contains(m.Family, "system-ui") {
122 t.Errorf("%s: the heading is set in %q, not the system UI face", where, m.Family)
123 }
124 if path == "/help/colophon" && m.Entries != c.Count()["total"] {
125 t.Errorf("%s: %d entries shown, %d in the Colophon", where, m.Entries, c.Count()["total"])
126 }
127 }
128 }
129 }
130}
131