Skip to content
quarry Artifact [73072ebf6c]
Login

Artifact [73072ebf6c]

Artifact 73072ebf6c9a251f64813211e8c16acb4148bc0430982f13b42a11bdbbd80caa:


package theme

import (
	"strings"
	"testing"
)

// Every theme holds the floor quarry's own palette holds: each text
// colour at 4.5:1 on each ground it is drawn on.
func TestEveryThemeReadsOnItsGrounds(t *testing.T) {
	text := []string{"--fg", "--add", "--del", "--syn-kw", "--syn-str", "--syn-num",
		"--syn-fn", "--syn-type", "--syn-com", "--syn-builtin", "--syn-attr"}
	grounds := []string{"--code-block", "--add-row", "--del-row", "--add-mark", "--del-mark"}
	for _, th := range All {
		for _, g := range grounds {
			for _, c := range text {
				fg, bg := th.Value(c), th.Value(g)
				if r := Contrast(fg, bg); r < 4.5 {
					t.Errorf("FAIL: %s: %s %s on %s %s is %.2f:1", th.ID, c, fg, g, bg, r)
				}
			}
		}
		for _, c := range []string{"--fg", "--muted", "--accent"} {
			if r := Contrast(th.Value(c), th.Value("--bg")); r < 4.5 {
				t.Errorf("FAIL: %s: %s on the page is %.2f:1", th.ID, c, r)
			}
		}
	}
}

// A theme is chosen by id from a cookie and served at its own URL, so
// both must be unique and every theme must carry every token a sheet
// writes.
func TestThemesAreWholeAndDistinct(t *testing.T) {
	if len(All) < 60 {
		t.Fatalf("FAIL: %d themes, want hunk's full set", len(All))
	}
	ids, urls := map[string]bool{}, map[string]bool{}
	for _, th := range All {
		if ids[th.ID] {
			t.Errorf("FAIL: duplicate id %s", th.ID)
		}
		ids[th.ID] = true
		for _, u := range []string{SiteURL(th), CodeURL(th)} {
			if urls[u] {
				t.Errorf("FAIL: duplicate url %s", u)
			}
			urls[u] = true
		}
		for _, name := range CodeTokens {
			if th.Value(name) == "" {
				t.Errorf("FAIL: %s has no %s", th.ID, name)
			}
		}
	}
	if len(Assets()) != 2*(len(All)+len(Families)) {
		t.Errorf("FAIL: %d assets for %d themes and %d families", len(Assets()), len(All), len(Families))
	}
	if _, ok := ByID("gruvbox-dark-hard"); !ok {
		t.Error("FAIL: no gruvbox-dark-hard")
	}
}

// The sheets are what the browser reads: the site one on the root,
// above paletteCSS's own selectors; the code one on what draws code,
// with only the code tokens.
func TestSheetsTargetTheRightElements(t *testing.T) {
	th, _ := ByID("catppuccin-mocha")
	site, code := SiteCSS(th), CodeCSS(th)
	if !strings.HasPrefix(site, ":root:root{") || !strings.Contains(site, "--bg:") || !strings.Contains(site, "color-scheme:dark") {
		t.Errorf("FAIL: site sheet: %s", site)
	}
	if !strings.HasPrefix(code, codeSelector+"{") || strings.Contains(code, "--bg:") || strings.Contains(code, "--accent:") {
		t.Errorf("FAIL: code sheet: %s", code)
	}
	if !strings.Contains(code, "--syn-kw:") || !strings.Contains(code, "--add-row:") {
		t.Errorf("FAIL: code sheet lacks syntax or diff tokens: %s", code)
	}
}