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 {
t.Run(icon.Sizes, func(t *testing.T) {
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)
}
t.Run(icon.Sizes, checkManifestIcon(icon))
}
}
file, err := os.Open(filepath.Join("..", strings.TrimPrefix(assetURL.Path, "/")))
if err != nil {
t.Fatalf("open icon: %v", err)
}
defer file.Close()
func checkManifestIcon(icon manifestIcon) func(*testing.T) {
return func(t *testing.T) {
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)
}
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)
}
})
file, err := os.Open(filepath.Join("..", strings.TrimPrefix(assetURL.Path, "/")))
if err != nil {
t.Fatalf("open icon: %v", err)
}
defer file.Close()
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)
}
assertBrandAssetLinks(t, doc)
assertNavigationLogo(t, doc)
}
func assertBrandAssetLinks(t *testing.T, doc *goquery.Document) {
assets := map[string]string{
`link[rel="manifest"]`: "/static/assets/manifest.json",
`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)
}
}
}
func assertNavigationLogo(t *testing.T, doc *goquery.Document) {
logo := doc.Find(`a[title="Home"] img`)
if logo.Length() != 1 {
t.Fatalf("navigation logo count = %d, want 1", logo.Length())