package app import ( "context" "strings" "testing" "time" "github.com/Nwflower/reims-go/internal/config" "github.com/Nwflower/reims-go/internal/guesttools" "github.com/Nwflower/reims-go/internal/i18n" "github.com/Nwflower/reims-go/internal/plugin" "github.com/Nwflower/reims-go/internal/plugin/builtin" ) // Switching the Guest Tools plugin off must stop the bridge for real: feature // calls get an error instead of quietly starting a new server. func TestGuestToolsBridgeFollowsPluginSwitch(t *testing.T) { dataRoot := t.TempDir() mgr, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: dataRoot, HostVersion: "0.0.0"}) if err != nil { t.Fatal(err) } defer mgr.Close() cfg := config.Default() cfg.DataRoot = dataRoot cfg.Locale = i18n.EnUS a := &App{cfg: cfg, pluginMgr: mgr} err = mgr.Registry().Register(plugin.PluginDef{ Manifest: plugin.Manifest{Schema: 1, ID: builtin.IDGuestTools, Version: "0.0.0"}, Source: plugin.SourceBuiltin, Trust: plugin.TrustOfficial, Desired: false, Activate: func(_ context.Context, sc *plugin.Scope) error { srv := guesttools.NewTransportServer(nil) if _, err := srv.Start(-1); err != nil { return err } sc.Provide(builtin.ServiceGuestToolsTransport, "1.0.0", srv) sc.OnDispose(func() { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() _ = srv.Close(ctx) }) return nil }, }) if err != nil { t.Fatal(err) } mgr.Registry().Reconcile(context.Background()) if _, _, err := a.ensureGuestToolsBridge(); err == nil || !strings.Contains(err.Error(), "turned off") { t.Fatalf("plugin off: want a turned-off error, got %v", err) } if a.guestTools != nil { t.Fatal("plugin off: a feature call started the Guest Tools server") } if err := mgr.Registry().SetDesired(context.Background(), builtin.IDGuestTools, true); err != nil { t.Fatal(err) } srv, port, err := a.ensureGuestToolsBridge() if err != nil || srv == nil || port <= 0 { t.Fatalf("plugin on: want the plugin's server, got srv=%v port=%d err=%v", srv, port, err) } if err := mgr.Registry().SetDesired(context.Background(), builtin.IDGuestTools, false); err != nil { t.Fatal(err) } if a.guestToolsEnabled() { t.Fatal("plugin switched back off: Guest Tools still reported as available") } }