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, "`
_ = 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)
}
})
}