package plugin import ( "context" "encoding/json" "errors" "sync" "testing" ) type stubInProcessEndpoint struct { mu sync.Mutex calls []string notifications []string onCall func(ctx context.Context, method string, params any, out any) error } func (s *stubInProcessEndpoint) Call(ctx context.Context, method string, params any, out any) error { s.mu.Lock() s.calls = append(s.calls, method) fn := s.onCall s.mu.Unlock() if fn != nil { return fn(ctx, method, params, out) } return nil } func (s *stubInProcessEndpoint) Notify(method string, _ any) { s.mu.Lock() s.notifications = append(s.notifications, method) s.mu.Unlock() } func TestInProcessEndpointLifecycleAndRouting(t *testing.T) { dataRoot := t.TempDir() var ( mu sync.Mutex hostCalledWith string ) mgr, err := NewManager(ManagerOptions{ DataRoot: dataRoot, HostVersion: "0.2.12", HostHandlers: func(pluginID string) map[string]RPCRequestHandler { return map[string]RPCRequestHandler{ "host/test.echo": func(_ context.Context, raw json.RawMessage) (any, error) { var req struct { Text string `json:"text"` } if err := json.Unmarshal(raw, &req); err != nil { return nil, err } mu.Lock() hostCalledWith = pluginID + ":" + req.Text mu.Unlock() return map[string]any{"echo": req.Text}, nil }, } }, }) if err != nil { t.Fatalf("NewManager: %v", err) } var notifiedTitle string mgr.Broker().SetNotifier(func(_ string, title, _ string) { notifiedTitle = title }) ep := &stubInProcessEndpoint{ onCall: func(_ context.Context, method string, params any, out any) error { if method != "demo/ping" { return errors.New("unexpected method") } b, _ := json.Marshal(map[string]any{"ok": true, "got": params}) return json.Unmarshal(b, out) }, } const pid = "builtin.demo.inprocess" if err := mgr.Registry().Register(PluginDef{ Manifest: Manifest{ Schema: 1, ID: pid, Version: "0.1.0", Name: LocalizedText{EnUS: "Demo In-Process"}, Permissions: []string{"ui:notify"}, }, Source: SourceBuiltin, Trust: TrustOfficial, Desired: true, Activate: func(_ context.Context, sc *Scope) error { sc.Registry().RegisterInProcessEndpoint(sc, ep) return nil }, }); err != nil { t.Fatalf("Register: %v", err) } mgr.Registry().Reconcile(context.Background()) // CallSidecar routes to active in-process endpoint. var pingOut struct { OK bool `json:"ok"` } if err := mgr.CallSidecar(context.Background(), pid, "demo/ping", map[string]any{"n": 1}, &pingOut); err != nil { t.Fatalf("CallSidecar: %v", err) } if !pingOut.OK { t.Fatalf("expected ok=true, got %+v", pingOut) } // NotifySidecar and NotifyRunningSidecars deliver to active in-process endpoint. mgr.NotifySidecar(pid, "download/progress", map[string]any{"id": "dl-1"}) mgr.NotifyRunningSidecars("console/userTakeover", map[string]any{"vm": "mac"}) ep.mu.Lock() gotNotifs := append([]string(nil), ep.notifications...) ep.mu.Unlock() if len(gotNotifs) != 2 || gotNotifs[0] != "download/progress" || gotNotifs[1] != "console/userTakeover" { t.Fatalf("unexpected notifications: %v", gotNotifs) } // CallHostAPI dispatches host/notify via broker and custom hostHandlers. if err := mgr.CallHostAPI(context.Background(), pid, "host/notify", map[string]any{"title": "Hi", "body": "Ready"}, nil); err != nil { t.Fatalf("CallHostAPI host/notify: %v", err) } if notifiedTitle != "Hi" { t.Fatalf("expected broker notification Hi, got %q", notifiedTitle) } var echoOut struct { Echo string `json:"echo"` } if err := mgr.CallHostAPI(context.Background(), pid, "host/test.echo", map[string]any{"text": "hello"}, &echoOut); err != nil { t.Fatalf("CallHostAPI host/test.echo: %v", err) } if echoOut.Echo != "hello" { t.Fatalf("expected echo=hello, got %+v", echoOut) } mu.Lock() called := hostCalledWith mu.Unlock() if called != pid+":hello" { t.Fatalf("unexpected host handler call: %q", called) } // Disabling removes endpoint with zero residue and blocks CallHostAPI. if err := mgr.Disable(context.Background(), pid); err != nil { t.Fatalf("Disable: %v", err) } if mgr.Registry().InProcessEndpoint(pid) != nil { t.Fatal("expected in-process endpoint to be removed on disable") } if err := mgr.CallSidecar(context.Background(), pid, "demo/ping", nil, &pingOut); err == nil { t.Fatal("expected CallSidecar to fail after disable") } if err := mgr.CallHostAPI(context.Background(), pid, "host/test.echo", map[string]any{"text": "blocked"}, &echoOut); err == nil { t.Fatal("expected CallHostAPI to fail when plugin is disabled") } } func TestAutoInstallersPreferBuiltinFirst(t *testing.T) { reg := NewRegistry(nil) if err := reg.Register(PluginDef{ Manifest: Manifest{ Schema: 1, ID: "aaa.thirdparty.autoinstall", Version: "1.0.0", Name: LocalizedText{EnUS: "Third Party"}, Contributes: Contributes{ AutoInstallers: []AutoInstallerContribution{{ ID: "thirdparty", Title: LocalizedText{EnUS: "Third Party"}, GuestFamilies: []string{"macos"}, }}, }, }, Source: SourceLocal, Trust: TrustCommunity, Desired: true, }); err != nil { t.Fatalf("register third-party: %v", err) } if err := reg.Register(PluginDef{ Manifest: Manifest{ Schema: 1, ID: "builtin.macos.autoinstall", Version: "1.0.0", Name: LocalizedText{EnUS: "Built-in Auto-Install"}, Contributes: Contributes{ AutoInstallers: []AutoInstallerContribution{{ ID: "macos-local", Title: LocalizedText{EnUS: "Built-in Auto-Install"}, GuestFamilies: []string{"macos"}, }}, }, }, Source: SourceBuiltin, Trust: TrustOfficial, Desired: true, }); err != nil { t.Fatalf("register builtin: %v", err) } reg.Reconcile(context.Background()) list := reg.AutoInstallersForFamily("macos") if len(list) != 2 { t.Fatalf("expected 2 providers, got %d", len(list)) } if list[0].PluginID != "builtin.macos.autoinstall" { t.Fatalf("expected builtin provider first, got %q", list[0].PluginID) } }