refactor: reduce cyclomatic complexity in template tests

This commit is contained in:
2026-07-05 23:27:11 +02:00
parent 137c6ab644
commit 217478c894
2 changed files with 33 additions and 22 deletions

View File

@@ -33,30 +33,34 @@ func TestManifestIconDimensions(t *testing.T) {
} }
for _, icon := range manifest.Icons { for _, icon := range manifest.Icons {
t.Run(icon.Sizes, func(t *testing.T) { t.Run(icon.Sizes, checkManifestIcon(icon))
width, height := parseIconSize(t, icon.Sizes) }
assetURL, err := url.Parse(icon.Src) }
if err != nil {
t.Fatalf("parse icon URL: %v", err)
}
if assetURL.Query().Get("v") == "" {
t.Fatalf("icon URL is not versioned: %q", icon.Src)
}
file, err := os.Open(filepath.Join("..", strings.TrimPrefix(assetURL.Path, "/"))) func checkManifestIcon(icon manifestIcon) func(*testing.T) {
if err != nil { return func(t *testing.T) {
t.Fatalf("open icon: %v", err) width, height := parseIconSize(t, icon.Sizes)
} assetURL, err := url.Parse(icon.Src)
defer file.Close() if err != nil {
t.Fatalf("parse icon URL: %v", err)
}
if assetURL.Query().Get("v") == "" {
t.Fatalf("icon URL is not versioned: %q", icon.Src)
}
config, err := png.DecodeConfig(file) file, err := os.Open(filepath.Join("..", strings.TrimPrefix(assetURL.Path, "/")))
if err != nil { if err != nil {
t.Fatalf("decode icon: %v", err) t.Fatalf("open icon: %v", err)
} }
if config.Width != width || config.Height != height { defer file.Close()
t.Fatalf("icon dimensions = %dx%d, manifest declares %s", config.Width, config.Height, icon.Sizes)
} config, err := png.DecodeConfig(file)
}) if err != nil {
t.Fatalf("decode icon: %v", err)
}
if config.Width != width || config.Height != height {
t.Fatalf("icon dimensions = %dx%d, manifest declares %s", config.Width, config.Height, icon.Sizes)
}
} }
} }

View File

@@ -78,6 +78,11 @@ func TestRenderUsesPurposeSizedBrandAssets(t *testing.T) {
t.Fatalf("parse rendered html: %v", err) t.Fatalf("parse rendered html: %v", err)
} }
assertBrandAssetLinks(t, doc)
assertNavigationLogo(t, doc)
}
func assertBrandAssetLinks(t *testing.T, doc *goquery.Document) {
assets := map[string]string{ assets := map[string]string{
`link[rel="manifest"]`: "/static/assets/manifest.json", `link[rel="manifest"]`: "/static/assets/manifest.json",
`link[rel="icon"][sizes="32x32"]`: "/static/assets/favicon-32.png", `link[rel="icon"][sizes="32x32"]`: "/static/assets/favicon-32.png",
@@ -94,7 +99,9 @@ func TestRenderUsesPurposeSizedBrandAssets(t *testing.T) {
t.Fatalf("%s href = %q, want %q", selector, path, want) t.Fatalf("%s href = %q, want %q", selector, path, want)
} }
} }
}
func assertNavigationLogo(t *testing.T, doc *goquery.Document) {
logo := doc.Find(`a[title="Home"] img`) logo := doc.Find(`a[title="Home"] img`)
if logo.Length() != 1 { if logo.Length() != 1 {
t.Fatalf("navigation logo count = %d, want 1", logo.Length()) t.Fatalf("navigation logo count = %d, want 1", logo.Length())