package main import ( "os" "path/filepath" "testing" ) func TestCheckTemplColorsFlagsHardcodedColorsAndInlineStyles(t *testing.T) { root := t.TempDir() file := filepath.Join(root, "sample.templ") content := `package sample templ Example() {
} ` if err := os.WriteFile(file, []byte(content), 0644); err != nil { t.Fatalf("write sample templ: %v", err) } violations := checkTemplColors(root) if len(violations) < 5 { t.Fatalf("checkTemplColors() returned %d violations, want at least 5: %#v", len(violations), violations) } rules := map[string]bool{} for _, violation := range violations { rules[violation.rule] = true } for _, rule := range []string{ "no-hardcoded-tw-color", "no-hex-in-class", "no-tw-arbitrary-hex", "no-inline-hex-style", "no-inline-rgb-style", } { if !rules[rule] { t.Fatalf("missing rule %q in violations: %#v", rule, violations) } } } func TestCheckTemplColorsAllowsSemanticTokensAndCSSVariables(t *testing.T) { root := t.TempDir() file := filepath.Join(root, "sample.templ") content := `package sample templ Example() {
} ` if err := os.WriteFile(file, []byte(content), 0644); err != nil { t.Fatalf("write sample templ: %v", err) } violations := checkTemplColors(root) if len(violations) != 0 { t.Fatalf("checkTemplColors() returned %d violations, want 0: %#v", len(violations), violations) } } func TestCheckNinjaTplColorsFlagsHardcodedColors(t *testing.T) { root := t.TempDir() tplDir := filepath.Join(root, "templates", "blocks") if err := os.MkdirAll(tplDir, 0755); err != nil { t.Fatalf("mkdir: %v", err) } file := filepath.Join(tplDir, "hero.ninjatpl") content := `

{{ headline }}

{{ subtext }}

{{ primary_cta }}
hardcoded hex
badge named color
` if err := os.WriteFile(file, []byte(content), 0644); err != nil { t.Fatalf("write ninjatpl: %v", err) } violations := checkNinjaTplColors(root) if len(violations) < 6 { t.Fatalf("checkNinjaTplColors() returned %d violations, want at least 6: %#v", len(violations), violations) } rules := map[string]bool{} for _, v := range violations { rules[v.rule] = true } for _, rule := range []string{ "no-hardcoded-hsl", "no-tw-arbitrary-color", "no-inline-hex", "no-hardcoded-tw-color", } { if !rules[rule] { t.Fatalf("missing rule %q in violations: %#v", rule, violations) } } } func TestCheckNinjaTplColorsAllowsCSSVariablesAndNeutralOverlays(t *testing.T) { root := t.TempDir() tplDir := filepath.Join(root, "templates", "blocks") if err := os.MkdirAll(tplDir, 0755); err != nil { t.Fatalf("mkdir: %v", err) } file := filepath.Join(tplDir, "hero.ninjatpl") content := `

{{ headline }}

{{ subtext }}

{{ cta }}
dark overlay
light overlay
badge
` if err := os.WriteFile(file, []byte(content), 0644); err != nil { t.Fatalf("write ninjatpl: %v", err) } violations := checkNinjaTplColors(root) if len(violations) != 0 { t.Fatalf("checkNinjaTplColors() returned %d violations, want 0: %#v", len(violations), violations) } }