Files
Reims-Go/internal/plugin/icon_test.go
T
千川白浪 e0ff794b0e feat: add experimental macOS desktop support on Apple Silicon (#207)
* feat: add experimental Apple Silicon support with Nivfold

Integrate the standalone Nivfold protocol 1 runner; tested against Nivfold f436fb6. The Windows Reims vGPU runtime remains independent.

* fix: open QQ invites and VM SSH on macOS

* fix: move VM, View and Help to the macOS menu bar

* feat(macos): add resumable Apple restore image downloads

Expose the compatible restore image in the VM wizard and Images page, validate downloads before caching, and fix macOS memory reporting and plugin SVG rendering.

* fix(macos): localize the native application menu

* fix(macos): address native desktop review findings
2026-12-06 00:47:07 +08:00

99 lines
3.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)
}
assertPluginIconNamespace(t, got.SVG)
}
func TestResolveBuiltinClipboardIcon(t *testing.T) {
// This uses the outline and currentColor attributes of built-in icons, not
// an inline HTML SVG where the browser may recover from duplicate attributes.
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"><rect x="8" y="3" width="8" height="4" rx="1"/></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 := `<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)
}
})
}