vitrine

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 leads with the
// computed :root override and still contains the embedded stylesheet after it.
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%.
	want := ":root { --accent: hsl(351 70% 35%); }\n"
	if !strings.HasPrefix(body, want) {
		t.Errorf("stylesheet does not start with theme override %q; head: %q", want, body[:min(len(body), 120)])
	}
	if !strings.Contains(body, "--paper") {
		t.Errorf("embedded stylesheet missing after 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.6 KiB · 8921cae64f