internal/httpd/theme_test.go

v1.32.0
gitbay/internal/httpd/theme_test.go history · blame · raw

64 lines · 2258 bytes

 1package httpd
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gitbay.org/gitbay/internal/web"
 8)
 9
10// The served stylesheet guards the dark media block against an explicit
11// light choice and repeats it, verbatim, for an explicit dark one (#232).
12func TestThemedCSSCarriesBothDarkBlocks(t *testing.T) {
13	css := string(themedCSS(web.StyleCSS))
14	const guarded = "@media (prefers-color-scheme: dark) {\n  :root:not([data-theme=\"light\"]) {"
15	const stamped = "\n:root[data-theme=\"dark\"] {"
16	gi, si := strings.Index(css, guarded), strings.Index(css, stamped)
17	if gi < 0 || si < 0 {
18		t.Fatalf("guarded %d stamped %d", gi, si)
19	}
20	body := func(from int) string {
21		rest := css[from:]
22		return rest[:strings.Index(rest, "\n  }\n}")]
23	}
24	guardedBody := body(gi + len(guarded))
25	rest := css[si+len(stamped):]
26	stampedBody := rest[:strings.Index(rest, "\n}")]
27	if guardedBody != stampedBody {
28		t.Fatalf("dark blocks differ:\n%s\n---\n%s", guardedBody, stampedBody)
29	}
30	if !strings.Contains(guardedBody, "--canvas: #101114;") {
31		t.Fatalf("dark block lacks the canvas token:\n%s", guardedBody)
32	}
33	if strings.Contains(css, "@media (prefers-color-scheme: dark) {\n  :root {") {
34		t.Fatal("the dark token block is served unguarded")
35	}
36	// The source keeps the plain block, which the token tests parse.
37	if !strings.Contains(string(web.StyleCSS), "@media (prefers-color-scheme: dark) {\n  :root {") {
38		t.Fatal("style.css no longer has the plain dark block")
39	}
40}
41
42// Each syntax palette is served four ways: under its media query unless
43// the other theme is stamped, and unconditionally under its own stamp.
44// chroma's LineLink rule, which set outline: none, is not served at all.
45func TestChromaPalettesAreScoped(t *testing.T) {
46	css := string(chromaCSS)
47	for _, want := range []string{
48		`:root:not([data-theme="dark"]) .chroma .na { color: #6f5a21 }`,
49		`:root:not([data-theme="light"]) .chroma `,
50		`:root[data-theme="light"] .chroma `,
51		`:root[data-theme="dark"] .chroma `,
52		`:root[data-theme="dark"] .bg `,
53	} {
54		if !strings.Contains(css, want) {
55			t.Errorf("lacks %s", want)
56		}
57	}
58	if strings.Contains(css, "lnlinks") {
59		t.Error("chroma's LineLink rule is served")
60	}
61	if strings.Contains(css, "\n.chroma .k ") {
62		t.Error("an unscoped palette rule is served")
63	}
64}