Files
Reims-Go/internal/plugin/icon_test.go
T
千川白浪 781f129fc1 feat(plugin): custom SVG icons, market stats, submit link, and native locale scaffold (#170)
* feat(plugin): custom SVG icons, market stats, submit link, and native locale scaffold

* chore(ci): allow spaces in PR model names and rename agent declaration to tool

* fix(plugin): validate icon.svg size and markup during static scan
2026-12-04 22:34:12 +08:00

79 lines
2.9 KiB
Go

package plugin
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestValidatePluginIcon(t *testing.T) {
validSVG := `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><circle cx="12" cy="12" r="8"/></svg>`
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: `<svg><script>alert(1)</script></svg>`}, true); err == nil {
t.Fatal("expected active svg content error")
}
}
func TestResolvePluginIconFromPackageDir(t *testing.T) {
dir := t.TempDir()
svg := `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="4" y="4" width="16" height="16"/></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, "<svg") || !strings.Contains(got.SVG, "<rect") {
t.Fatalf("unexpected resolved svg: %q", got.SVG)
}
}
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 := `<svg xmlns="http://www.w3.org/2000/svg"><!--` + strings.Repeat("a", maxPluginIconSVGBytes+64) + `--></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)
}
})
}