Plugins can add pages to the sidebar, VM tabs, Settings and dialogs. Their pages can now use host forms and navigation, VM and settings access, authorized file I/O, task progress, controlled HTTP requests, events and private storage. Plugin pages cannot change allow_ai_access, which stays the user's per-VM consent. Closing a page cancels its unfinished tasks, and running plugin tasks no longer block changing the data folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
5.0 KiB
Go
136 lines
5.0 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
func float64Ptr(value float64) *float64 { return &value }
|
|
|
|
func newPanelBridgeTestApp(t *testing.T, permissions ...string) *App {
|
|
t.Helper()
|
|
mgr, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: t.TempDir(), HostVersion: "0.2.12"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(mgr.Close)
|
|
def := plugin.PluginDef{
|
|
Manifest: plugin.Manifest{
|
|
Schema: 1, ID: "vendor.panel", Version: "1.0.0",
|
|
Name: plugin.LocalizedText{EnUS: "Test panel"},
|
|
Permissions: permissions,
|
|
Settings: &plugin.SettingsSchema{Scope: "both", Properties: map[string]plugin.SettingProperty{
|
|
"enabled": {Type: "boolean", Default: true},
|
|
"count": {Type: "integer", Minimum: float64Ptr(0), Maximum: float64Ptr(10)},
|
|
"mode": {Type: "string", Enum: []string{"dev", "prod"}, Default: "dev"},
|
|
"secret": {Type: "string", Secret: true},
|
|
"vm_only": {Type: "boolean", Scope: "vm"},
|
|
"required": {Type: "string", Required: true},
|
|
computerUseAccessSetting: {Type: "boolean"},
|
|
}},
|
|
Contributes: plugin.Contributes{
|
|
Panels: []plugin.PanelContribution{{ID: "main", Slot: "settings.plugin", Title: plugin.LocalizedText{EnUS: "Test panel"}}},
|
|
Views: []plugin.ViewContribution{{ID: "console", Location: "sidebar", Title: plugin.LocalizedText{EnUS: "Console"}, Entry: "index.html"}},
|
|
},
|
|
},
|
|
Source: plugin.SourceBuiltin, Desired: true,
|
|
}
|
|
if err := mgr.Registry().Register(def); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mgr.Registry().Reconcile(context.Background())
|
|
if !mgr.Registry().IsActive("vendor.panel") {
|
|
t.Fatal("test panel did not activate")
|
|
}
|
|
return &App{pluginMgr: mgr}
|
|
}
|
|
|
|
func TestPanelBridgeConfirmationAndCopy(t *testing.T) {
|
|
a := newPanelBridgeTestApp(t, "ui:notify", "ui:slot")
|
|
result, err := a.PanelBridgeCall("vendor.panel", "ui.confirm", map[string]any{
|
|
"title": "Publish", "message": "Publish this draft?",
|
|
"confirmLabel": "Publish", "cancelLabel": "Keep editing", "danger": true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
confirm, ok := result.(panelConfirmRequest)
|
|
if !ok {
|
|
t.Fatalf("confirmation result has type %T", result)
|
|
}
|
|
if confirm.Message != "Publish this draft?" || confirm.ConfirmLabel != "Publish" || !confirm.Danger {
|
|
t.Fatalf("unexpected confirmation: %+v", confirm)
|
|
}
|
|
result, err = a.PanelBridgeCall("vendor.panel", "ui.copy", map[string]any{"text": "first\nsecond"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
copyReq, ok := result.(panelCopyRequest)
|
|
if !ok || copyReq.Text != "first\nsecond" {
|
|
t.Fatalf("unexpected copy request: %#v", result)
|
|
}
|
|
result, err = a.PanelBridgeCall("vendor.panel", "ui.copy", map[string]any{"text": ""})
|
|
if err != nil || result.(panelCopyRequest).Text != "" {
|
|
t.Fatalf("empty clipboard text was rejected: %#v, %v", result, err)
|
|
}
|
|
if _, err := a.PanelBridgeCall("vendor.panel", "ui.copy", map[string]any{"text": 42}); err == nil {
|
|
t.Fatal("non-string clipboard input was accepted")
|
|
}
|
|
if _, err := a.PanelBridgeCall("vendor.panel", "ui.confirm", map[string]any{"message": "bad\x00message"}); err == nil {
|
|
t.Fatal("control character in confirmation was accepted")
|
|
}
|
|
}
|
|
|
|
func TestPanelBridgeStorage(t *testing.T) {
|
|
a := newPanelBridgeTestApp(t, "fs:plugin_data")
|
|
if _, err := a.PanelBridgeCall("vendor.panel", "storage.set", map[string]any{
|
|
"key": "draft", "value": map[string]any{"enabled": true},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := a.PanelBridgeCall("vendor.panel", "storage.get", map[string]any{"key": "draft"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := result.(map[string]any)
|
|
if got["found"] != true {
|
|
t.Fatalf("stored value was not found: %#v", got)
|
|
}
|
|
raw := got["value"].(json.RawMessage)
|
|
var value map[string]any
|
|
if err := json.Unmarshal(raw, &value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(value, map[string]any{"enabled": true}) {
|
|
t.Fatalf("stored value = %#v", value)
|
|
}
|
|
result, err = a.PanelBridgeCall("vendor.panel", "storage.list", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if keys := result.(map[string]any)["keys"].([]string); !reflect.DeepEqual(keys, []string{"draft"}) {
|
|
t.Fatalf("storage keys = %v", keys)
|
|
}
|
|
result, err = a.PanelBridgeCall("vendor.panel", "storage.delete", map[string]any{"key": "draft"})
|
|
if err != nil || result.(map[string]any)["deleted"] != true {
|
|
t.Fatalf("delete result = %#v, err = %v", result, err)
|
|
}
|
|
}
|
|
|
|
func TestPanelBridgeRejectsUnavailableActions(t *testing.T) {
|
|
a := newPanelBridgeTestApp(t, "ui:slot")
|
|
if _, err := a.PanelBridgeCall("vendor.panel", "storage.set", map[string]any{"key": "draft", "value": true}); err == nil {
|
|
t.Fatal("storage write succeeded without fs:plugin_data")
|
|
}
|
|
if _, err := a.PanelBridgeCall("vendor.panel", "ui.openExternal", map[string]any{"url": "file:///C:/Windows/System32"}); err == nil {
|
|
t.Fatal("non-HTTPS external URL was accepted")
|
|
}
|
|
if _, err := a.PanelBridgeCall("vendor.panel", "file.pick", nil); err == nil {
|
|
t.Fatal("experimental file.pick became available without an implementation")
|
|
}
|
|
}
|