theme_test.go
package main
import (
"net/http"
"strings"
"testing"
)
// TestThemeHuePinned pins hue values for known seeds.
// These pins are load-bearing: a refactor that changes the hash or the modulo
// silently repaints every deployed host, and this test is what catches it.
func TestThemeHuePinned(t *testing.T) {
cases := map[string]int{
"langoustine": 180,
"vitrine": 72,
"alpha": 107,
"zebra": 95,
"": 61,
}
for seed, want := range cases {
if got := themeHue(seed); got != want {
t.Errorf("themeHue(%q) = %d, want %d", seed, got, want)
}
}
}
// TestThemeLightnessBand pins the contrast-driven lightness rule
// at the band edges and representative points; see themeLightness for the derivation.
func TestThemeLightnessBand(t *testing.T) {
cases := map[int]int{
0: 35, // reds: fine at 35%
34: 35, // last hue before the band
35: 26, // band start
60: 26, // pure yellow, the worst offender
180: 26, // cyan, still in the band
200: 26, // band end
201: 35, // blues onward: fine at 35%
240: 35,
}
for hue, want := range cases {
if got := themeLightness(hue); got != want {
t.Errorf("themeLightness(%d) = %d, want %d", hue, got, want)
}
}
}
func TestHostLabel(t *testing.T) {
cases := map[string]string{
"langoustine": "langoustine",
"langoustine.exe.xyz": "langoustine",
"a.b": "a",
}
for hostname, want := range cases {
if got := hostLabel(hostname); got != want {
t.Errorf("hostLabel(%q) = %q, want %q", hostname, got, want)
}
}
}
// TestStylesheetCarriesThemeAccent checks that /-/style.css ends with the
// computed :root override (light and dark), after the embedded stylesheet:
// at equal specificity the later declaration wins,
// so appending (not prepending) is what makes the theme accent take effect.
func TestStylesheetCarriesThemeAccent(t *testing.T) {
rec := get(t, newTestServer(t), "/-/style.css")
if rec.Code != http.StatusOK {
t.Fatalf("status = %d", rec.Code)
}
body := rec.Body.String()
// "testhost" is the seed newTestServer uses; it hashes to hue 351, lightness 35%/70%.
want := ":root { --accent: hsl(351 70% 35%); }\n" +
"@media (prefers-color-scheme: dark) { :root { --accent: hsl(351 70% 70%); } }\n"
if !strings.HasSuffix(body, want) {
t.Errorf("stylesheet does not end with theme override %q; tail: %q", want, body[max(0, len(body)-200):])
}
if !strings.Contains(body, "--paper") {
t.Errorf("embedded stylesheet missing before theme override")
}
}
// TestNavShowsThemeName checks that pages name the host theme beside the site name.
func TestNavShowsThemeName(t *testing.T) {
rec := get(t, newTestServer(t), "/")
if !strings.Contains(rec.Body.String(), `<span class="theme">@testhost</span>`) {
t.Errorf("nav missing theme name; body:\n%s", rec.Body.String())
}
}
theme_test.go · 2.8 KiB · 496af3b480