oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package look_test
2
3import (
4 "net/http/httptest"
5 "regexp"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/base/internal/look"
10)
11
12func renderTheme(t *testing.T, theme string) string {
13 t.Helper()
14 r, err := look.NewRenderer(testPages, nil)
15 if err != nil {
16 t.Fatal(err)
17 }
18 rec := httptest.NewRecorder()
19 if err := r.PageTheme(rec, 200, "hello", "you", theme); err != nil {
20 t.Fatal(err)
21 }
22 return rec.Body.String()
23}
24
25// A member's Appearance is drawn by the server: <html data-theme> and the
26// browser's own colours (color-scheme, theme-color) for that theme, so the
27// page never flashes the other one and needs no script. Without one (and
28// on pages with no member, Page) the page follows the device.
29func TestPageTheme(t *testing.T) {
30 for theme, want := range map[string][]string{
31 "light": {`<html lang="en" data-theme="light">`, `<meta name="color-scheme" content="light">`, `<meta name="theme-color" content="#F4F5FC">`},
32 "dark": {`<html lang="en" data-theme="dark">`, `<meta name="color-scheme" content="dark">`, `<meta name="theme-color" content="#1A1F45">`},
33 "": {`<html lang="en">`, `<meta name="color-scheme" content="light dark">`,
34 `<meta name="theme-color" content="#F4F5FC" media="(prefers-color-scheme: light)">`,
35 `<meta name="theme-color" content="#1A1F45" media="(prefers-color-scheme: dark)">`},
36 "purple": {`<html lang="en">`, `<meta name="color-scheme" content="light dark">`},
37 } {
38 body := renderTheme(t, theme)
39 for _, w := range want {
40 if !strings.Contains(body, w) {
41 t.Errorf("theme %q: no %s in:\n%s", theme, w, body)
42 }
43 }
44 if n := strings.Count(body, `name="theme-color"`); (theme == "light" || theme == "dark") && n != 1 {
45 t.Errorf("theme %q: %d theme-color tags", theme, n)
46 }
47 }
48 if body := render(t); !strings.Contains(body, `<html lang="en">`) || strings.Contains(body, "data-theme") {
49 t.Errorf("Page forces a theme:\n%s", body)
50 }
51}
52
53// block returns the body of the first rule that starts with head, braces
54// balanced, and where it starts and ends in css.
55func block(t *testing.T, css, head string) (body string, from, to int) {
56 t.Helper()
57 from = strings.Index(css, head)
58 if from < 0 {
59 t.Fatalf("app.css has no %q", head)
60 }
61 depth := 0
62 for i := from + len(head) - 1; i < len(css); i++ {
63 switch css[i] {
64 case '{':
65 depth++
66 case '}':
67 depth--
68 if depth == 0 {
69 return css[from+len(head) : i], from, i + 1
70 }
71 }
72 }
73 t.Fatalf("%q never closes", head)
74 return "", 0, 0
75}
76
77var declRE = regexp.MustCompile(`(--[\w-]+|color-scheme):\s*([^;]+);`)
78
79func decls(body string) map[string]string {
80 out := map[string]string{}
81 for _, m := range declRE.FindAllStringSubmatch(body, -1) {
82 out[m[1]] = strings.TrimSpace(m[2])
83 }
84 return out
85}
86
87var pairRE = regexp.MustCompile(`^light-dark\((.+?), ((?:rgba?\([^)]*\)|#\w+))\)$`)
88
89// One set of tokens drives both themes. :root holds every token as a plain
90// dark value, so a browser without light-dark() (Safari before 17.5) gets
91// the dark theme whole. Where light-dark() works, @supports pairs each one
92// with its light value and follows the device. data-theme="light" holds
93// the light values plainly, so choosing Light works everywhere. Each pair's
94// halves are the plain values, and every colour in app.css is a token.
95func TestStylesheetThemes(t *testing.T) {
96 css := stylesheet(t)
97 rootBody, rootFrom, rootTo := block(t, css, "\n:root {")
98 lightBody, lightFrom, lightTo := block(t, css, `:root[data-theme="light"] {`)
99 supBody, supFrom, supTo := block(t, css, "@supports (color: light-dark(white, black)) {")
100 pairBody, _, _ := block(t, supBody, ":root {")
101 root, light, pairs := decls(rootBody), decls(lightBody), decls(pairBody)
102
103 if root["color-scheme"] != "dark" || light["color-scheme"] != "light" || pairs["color-scheme"] != "light dark" {
104 t.Errorf("color-scheme: :root %q, Light %q, @supports %q; want dark, light, light dark",
105 root["color-scheme"], light["color-scheme"], pairs["color-scheme"])
106 }
107 if !strings.Contains(css, `:root[data-theme="dark"] { color-scheme: dark; }`) {
108 t.Error(`no :root[data-theme="dark"] { color-scheme: dark; }`)
109 }
110 if !strings.Contains(supBody, "@media (prefers-color-scheme: light) {") {
111 t.Error("the device's light setting is not followed inside @supports")
112 }
113 if len(pairs) < 50 {
114 t.Fatalf("only %d paired tokens", len(pairs))
115 }
116 for name, v := range pairs {
117 if name == "color-scheme" {
118 continue
119 }
120 m := pairRE.FindStringSubmatch(v)
121 if m == nil {
122 t.Errorf("%s: %q is not light-dark(light, dark)", name, v)
123 continue
124 }
125 if root[name] != m[2] {
126 t.Errorf("%s: :root's plain value %q, want the pair's dark half %q", name, root[name], m[2])
127 }
128 if light[name] != m[1] {
129 t.Errorf("%s: Light's plain value %q, want the pair's light half %q", name, light[name], m[1])
130 }
131 }
132 for name, v := range root {
133 if strings.Contains(v, "light-dark(") {
134 t.Errorf(":root's %s uses light-dark(): %s", name, v)
135 }
136 }
137 comments := regexp.MustCompile(`(?s)/\*.*?\*/`)
138 if strings.Count(comments.ReplaceAllString(css, ""), "light-dark(") != strings.Count(comments.ReplaceAllString(supBody, ""), "light-dark(")+1 {
139 t.Error("light-dark() used outside the @supports block")
140 }
141
142 // No colour outside the token blocks.
143 rest := css[:rootFrom] + css[rootTo:lightFrom] + css[lightTo:supFrom] + css[supTo:]
144 colour := regexp.MustCompile(`#[0-9A-Fa-f]{3,8}\b|rgba?\(`)
145 for i, line := range strings.Split(rest, "\n") {
146 if colour.MatchString(line) {
147 t.Errorf("app.css has a colour outside the tokens (line %d of the rest): %s", i+1, strings.TrimSpace(line))
148 }
149 }
150}
151
152func stylesheet(t *testing.T) string {
153 t.Helper()
154 rec := httptest.NewRecorder()
155 look.Assets.Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/static/app.css", nil))
156 if rec.Code != 200 {
157 t.Fatalf("app.css: %d", rec.Code)
158 }
159 return rec.Body.String()
160}
161