From 1b1ceaf8b48d3d7b0899d674c6e90b5d8ff7b4ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Tue, 19 Aug 2025 19:58:23 -0700 Subject: [PATCH] test(icon): add test case to verify SVG minification with error --- internal/reader/icon/finder_test.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/internal/reader/icon/finder_test.go b/internal/reader/icon/finder_test.go index 80991a17..d30e69c6 100644 --- a/internal/reader/icon/finder_test.go +++ b/internal/reader/icon/finder_test.go @@ -194,16 +194,37 @@ func TestResizeInvalidImage(t *testing.T) { t.Fatalf("Tried to convert an invalid image") } } + func TestMinifySvg(t *testing.T) { data := []byte(``) want := []byte(``) + icon := model.Icon{Content: data, MimeType: "image/svg+xml"} + got := resizeIcon(&icon).Content + if !bytes.Equal(want, got) { + t.Fatalf("Didn't correctly minify the svg: got %s instead of %s", got, want) + } +} + +func TestMinifySvgWithError(t *testing.T) { + // Invalid SVG with malformed XML that should cause minification to fail + data := []byte(`<>unclosed`) + original := make([]byte, len(data)) + copy(original, data) icon := model.Icon{ Content: data, MimeType: "image/svg+xml", } - got := resizeIcon(&icon).Content - if !bytes.Equal(want, got) { - t.Fatalf("Didn't correctly minimize the svg: got %s instead of %s", got, want) + + result := resizeIcon(&icon) + + // When minification fails, the original content should be preserved + if !bytes.Equal(original, result.Content) { + t.Fatalf("Expected original content to be preserved on minification error, got %s instead of %s", result.Content, original) + } + + // MimeType should remain unchanged + if result.MimeType != "image/svg+xml" { + t.Fatalf("Expected MimeType to remain image/svg+xml, got %s", result.MimeType) } }