package look_test
import (
"net/http/httptest"
"regexp"
"strings"
"testing"
"amadan.net/oficina/base/internal/look"
)
func renderTheme(t *testing.T, theme string) string {
t.Helper()
r, err := look.NewRenderer(testPages, nil)
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
if err := r.PageTheme(rec, 200, "hello", "you", theme); err != nil {
t.Fatal(err)
}
return rec.Body.String()
}
// A member's Appearance is drawn by the server: and the
// browser's own colours (color-scheme, theme-color) for that theme, so the
// page never flashes the other one and needs no script. Without one (and
// on pages with no member, Page) the page follows the device.
func TestPageTheme(t *testing.T) {
for theme, want := range map[string][]string{
"light": {``, ``, ``},
"dark": {``, ``, ``},
"": {``, ``,
``,
``},
"purple": {``, ``},
} {
body := renderTheme(t, theme)
for _, w := range want {
if !strings.Contains(body, w) {
t.Errorf("theme %q: no %s in:\n%s", theme, w, body)
}
}
if n := strings.Count(body, `name="theme-color"`); (theme == "light" || theme == "dark") && n != 1 {
t.Errorf("theme %q: %d theme-color tags", theme, n)
}
}
if body := render(t); !strings.Contains(body, ``) || strings.Contains(body, "data-theme") {
t.Errorf("Page forces a theme:\n%s", body)
}
}
// block returns the body of the first rule that starts with head, braces
// balanced, and where it starts and ends in css.
func block(t *testing.T, css, head string) (body string, from, to int) {
t.Helper()
from = strings.Index(css, head)
if from < 0 {
t.Fatalf("app.css has no %q", head)
}
depth := 0
for i := from + len(head) - 1; i < len(css); i++ {
switch css[i] {
case '{':
depth++
case '}':
depth--
if depth == 0 {
return css[from+len(head) : i], from, i + 1
}
}
}
t.Fatalf("%q never closes", head)
return "", 0, 0
}
var declRE = regexp.MustCompile(`(--[\w-]+|color-scheme):\s*([^;]+);`)
func decls(body string) map[string]string {
out := map[string]string{}
for _, m := range declRE.FindAllStringSubmatch(body, -1) {
out[m[1]] = strings.TrimSpace(m[2])
}
return out
}
var pairRE = regexp.MustCompile(`^light-dark\((.+?), ((?:rgba?\([^)]*\)|#\w+))\)$`)
// One set of tokens drives both themes. :root holds every token as a plain
// dark value, so a browser without light-dark() (Safari before 17.5) gets
// the dark theme whole. Where light-dark() works, @supports pairs each one
// with its light value and follows the device. data-theme="light" holds
// the light values plainly, so choosing Light works everywhere. Each pair's
// halves are the plain values, and every colour in app.css is a token.
func TestStylesheetThemes(t *testing.T) {
css := stylesheet(t)
rootBody, rootFrom, rootTo := block(t, css, "\n:root {")
lightBody, lightFrom, lightTo := block(t, css, `:root[data-theme="light"] {`)
supBody, supFrom, supTo := block(t, css, "@supports (color: light-dark(white, black)) {")
pairBody, _, _ := block(t, supBody, ":root {")
root, light, pairs := decls(rootBody), decls(lightBody), decls(pairBody)
if root["color-scheme"] != "dark" || light["color-scheme"] != "light" || pairs["color-scheme"] != "light dark" {
t.Errorf("color-scheme: :root %q, Light %q, @supports %q; want dark, light, light dark",
root["color-scheme"], light["color-scheme"], pairs["color-scheme"])
}
if !strings.Contains(css, `:root[data-theme="dark"] { color-scheme: dark; }`) {
t.Error(`no :root[data-theme="dark"] { color-scheme: dark; }`)
}
if !strings.Contains(supBody, "@media (prefers-color-scheme: light) {") {
t.Error("the device's light setting is not followed inside @supports")
}
if len(pairs) < 50 {
t.Fatalf("only %d paired tokens", len(pairs))
}
for name, v := range pairs {
if name == "color-scheme" {
continue
}
m := pairRE.FindStringSubmatch(v)
if m == nil {
t.Errorf("%s: %q is not light-dark(light, dark)", name, v)
continue
}
if root[name] != m[2] {
t.Errorf("%s: :root's plain value %q, want the pair's dark half %q", name, root[name], m[2])
}
if light[name] != m[1] {
t.Errorf("%s: Light's plain value %q, want the pair's light half %q", name, light[name], m[1])
}
}
for name, v := range root {
if strings.Contains(v, "light-dark(") {
t.Errorf(":root's %s uses light-dark(): %s", name, v)
}
}
comments := regexp.MustCompile(`(?s)/\*.*?\*/`)
if strings.Count(comments.ReplaceAllString(css, ""), "light-dark(") != strings.Count(comments.ReplaceAllString(supBody, ""), "light-dark(")+1 {
t.Error("light-dark() used outside the @supports block")
}
// No colour outside the token blocks.
rest := css[:rootFrom] + css[rootTo:lightFrom] + css[lightTo:supFrom] + css[supTo:]
colour := regexp.MustCompile(`#[0-9A-Fa-f]{3,8}\b|rgba?\(`)
for i, line := range strings.Split(rest, "\n") {
if colour.MatchString(line) {
t.Errorf("app.css has a colour outside the tokens (line %d of the rest): %s", i+1, strings.TrimSpace(line))
}
}
}
func stylesheet(t *testing.T) string {
t.Helper()
rec := httptest.NewRecorder()
look.Assets.Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/static/app.css", nil))
if rec.Code != 200 {
t.Fatalf("app.css: %d", rec.Code)
}
return rec.Body.String()
}