package fichastest import ( "os" "path/filepath" "regexp" "sort" "strings" "testing" "amadan.net/oficina/fichas/internal/fichas/icons" ) var ( tagRe = regexp.MustCompile(`<[a-zA-Z][^>]*>`) attrRe = regexp.MustCompile(`\s((?:rst|fichas)-[a-z0-9-]+)`) selectorRe = regexp.MustCompile(`\[((?:rst|fichas)-[a-z0-9-]+)`) iconRe = regexp.MustCompile(`\{\{-?\s*icon\s+"([a-z0-9-]+)"`) ) func read(t *testing.T, path string) string { t.Helper() b, err := os.ReadFile(path) if err != nil { t.Fatalf("reading %s: %v", path, err) } return string(b) } // TestTemplatesUseOnlyDefinedAttributes: every rst-* or fichas-* attribute a // template writes must have a rule in one of the app's stylesheets. // // The first index shipped rst-stack and rst-muted, which rastrillo did not // have. With no rule behind them the browser's defaults showed through — a // bullet point and an underlined purple link — and nothing failed. Only // attributes in THIS app's templates are checked: what rastrillo's partials // emit is the framework's business, and some of those are script hooks. func TestTemplatesUseOnlyDefinedAttributes(t *testing.T) { defined := map[string]bool{} sheets, _ := filepath.Glob("../fichas/static/*.css") brand, _ := filepath.Glob("../fichas/static/brand/*.css") for _, css := range append(sheets, brand...) { for _, m := range selectorRe.FindAllStringSubmatch(read(t, css), -1) { defined[m[1]] = true } } if !defined["rst-btn"] || !defined["fichas-grid"] { t.Fatal("the stylesheets were not found or not parsed; this test would pass vacuously") } templates, _ := filepath.Glob("../fichas/templates/*.html") if len(templates) == 0 { t.Fatal("no templates found; this test would pass vacuously") } var bad []string for _, f := range templates { for _, tag := range tagRe.FindAllString(read(t, f), -1) { for _, m := range attrRe.FindAllStringSubmatch(tag, -1) { if !defined[m[1]] { bad = append(bad, filepath.Base(f)+": "+m[1]) } } } } sort.Strings(bad) for _, b := range bad { t.Errorf("%s has no rule in any stylesheet — an invented attribute renders as browser defaults", b) } } // TestTemplateIconsExist: icons.Icon returns "" for a slug it does not know, // so a missing icon renders as nothing. Six of the first seven the mockups // used were missing, and nothing noticed until a screenshot. func TestTemplateIconsExist(t *testing.T) { templates, _ := filepath.Glob("../fichas/templates/*.html") var missing []string for _, f := range templates { for _, m := range iconRe.FindAllStringSubmatch(read(t, f), -1) { if icons.Icon(m[1]) == "" { missing = append(missing, filepath.Base(f)+": "+m[1]) } } } if len(missing) > 0 { t.Errorf("icon slugs not in internal/fichas/icons:\n %s", strings.Join(missing, "\n ")) } }