package plugin
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestValidatePluginIcon(t *testing.T) {
validSVG := ``
if err := ValidatePluginIcon(nil, false); err != nil {
t.Fatalf("nil icon should be valid: %v", err)
}
if err := ValidatePluginIcon(&PluginIcon{Tone: "sky", SVG: "assets/icon.svg"}, false); err != nil {
t.Fatalf("external icon path should be valid: %v", err)
}
if err := ValidatePluginIcon(&PluginIcon{Tone: "indigo", SVG: validSVG}, true); err != nil {
t.Fatalf("builtin inline svg should be valid: %v", err)
}
if err := ValidatePluginIcon(&PluginIcon{Tone: "magenta"}, false); err == nil {
t.Fatal("expected invalid tone error")
}
if err := ValidatePluginIcon(&PluginIcon{Tone: "sky", SVG: validSVG}, false); err == nil {
t.Fatal("expected external inline svg error")
}
if err := ValidatePluginIcon(&PluginIcon{Tone: "sky", SVG: "icon.png"}, false); err == nil {
t.Fatal("expected non-svg extension error")
}
if err := ValidatePluginIcon(&PluginIcon{Tone: "sky", SVG: ``}, true); err == nil {
t.Fatal("expected active svg content error")
}
}
func TestResolvePluginIconFromPackageDir(t *testing.T) {
dir := t.TempDir()
svg := ``
if err := os.WriteFile(filepath.Join(dir, "icon.svg"), []byte(svg), 0o644); err != nil {
t.Fatal(err)
}
got := ResolvePluginIcon(&PluginIcon{Tone: "TEAL", SVG: "icon.svg"}, dir, false)
if got == nil {
t.Fatal("expected resolved icon")
}
if got.Tone != "teal" {
t.Fatalf("tone = %q, want teal", got.Tone)
}
if !strings.HasPrefix(got.SVG, "`
got := ResolvePluginIcon(&PluginIcon{Tone: "sky", SVG: svg}, "", true)
if got == nil || !strings.Contains(got.SVG, `stroke="currentColor"`) {
t.Fatalf("unexpected resolved builtin icon: %+v", got)
}
assertPluginIconNamespace(t, got.SVG)
}
func assertPluginIconNamespace(t *testing.T, svg string) {
t.Helper()
root, _, _ := strings.Cut(svg, ">")
if strings.Count(root, `xmlns="`) != 1 || !strings.Contains(root, `xmlns="http://www.w3.org/2000/svg"`) {
t.Fatalf("standalone SVG must declare the root namespace exactly once: %s", root)
}
}
func TestScanBlocksOversizedAndMalformedIconSVG(t *testing.T) {
manifest := `{"schema":1,"id":"test.icon.check","version":"1.0.0","name":"Icon","description":"Icon","icon":{"tone":"sky","svg":"icon.svg"}}`
t.Run("oversized", func(t *testing.T) {
dir := t.TempDir()
_ = os.WriteFile(filepath.Join(dir, "plugin.json"), []byte(manifest), 0o644)
big := ``
_ = os.WriteFile(filepath.Join(dir, "icon.svg"), []byte(big), 0o644)
rep := ScanDirectory(dir, "0.2.12", ScanOptions{})
if !rep.HardBlocked {
t.Fatalf("expected oversized icon.svg to be hard-blocked, got passed=%v", rep.Passed)
}
})
t.Run("malformed", func(t *testing.T) {
dir := t.TempDir()
_ = os.WriteFile(filepath.Join(dir, "plugin.json"), []byte(manifest), 0o644)
_ = os.WriteFile(filepath.Join(dir, "icon.svg"), []byte("not an svg"), 0o644)
rep := ScanDirectory(dir, "0.2.12", ScanOptions{})
if !rep.HardBlocked {
t.Fatalf("expected malformed icon.svg to be hard-blocked, got passed=%v", rep.Passed)
}
})
}