package app import ( "context" "errors" "io" "strings" "testing" "github.com/Nwflower/reims-go/internal/config" "github.com/Nwflower/reims-go/internal/driver" "github.com/Nwflower/reims-go/internal/host" "github.com/Nwflower/reims-go/internal/i18n" "github.com/Nwflower/reims-go/internal/lifecycle" "github.com/Nwflower/reims-go/internal/plugin" "github.com/Nwflower/reims-go/internal/testutil/fakedriver" ) const cloneSourceSettingPlugin = "test.clone.agent" // newCloneSettingsEnv builds a host whose MCP clone reads its settings from a // source VM. The plugin declares the same login_password/username/ // allow_ai_access schema as reims.agent, scoped to the VM. func newCloneSettingsEnv(t *testing.T) (*App, *plugin.Manager, driver.Driver) { t.Helper() cfg := config.Default() cfg.DataRoot = t.TempDir() mgr, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: cfg.DataRoot, HostVersion: "0.0.0"}) if err != nil { t.Fatal(err) } t.Cleanup(mgr.Close) if err := mgr.Registry().Register(plugin.PluginDef{ Manifest: plugin.Manifest{ ID: cloneSourceSettingPlugin, Version: "1.0.0", Name: plugin.LocalizedText{EnUS: cloneSourceSettingPlugin}, Settings: &plugin.SettingsSchema{ Scope: "vm", Properties: map[string]plugin.SettingProperty{ "allow_ai_access": {Type: "boolean"}, "username": {Type: "string"}, "login_password": {Type: "string", Secret: true}, }, }, }, Source: plugin.SourceBuiltin, Desired: true, Activate: func(_ context.Context, _ *plugin.Scope) error { return nil }, }); err != nil { t.Fatal(err) } mgr.Registry().Reconcile(context.Background()) h := fakedriver.NewHarness(t) drv := h.Add("hosted", "test.clone.driver", "test.driver.clone") app := &App{ cfg: cfg, pluginMgr: mgr, transitions: map[string]string{}, pendingChanges: map[string][]PendingChange{}, pollWake: make(chan struct{}, 1), diskPaths: map[string]cachedDiskPath{}, procSamples: map[string]host.ProcessSample{}, driverRouter: h.Router, vmStateTable: h.States, lifecycleSvc: lifecycle.New(h.Registry, h.Router, nil), } return app, mgr, drv } func createVMs(t *testing.T, drv driver.Driver, names ...string) { t.Helper() for _, name := range names { if err := drv.Create(context.Background(), driver.CreateRequest{DriverID: drv.ID(), Name: name}, io.Discard, io.Discard); err != nil { t.Fatal(err) } } } func TestCloneSecretFallsBackToAuthorizedSource(t *testing.T) { app, mgr, drv := newCloneSettingsEnv(t) createVMs(t, drv, "source", "__agent-clone") sourceKey := driver.NewVMRef(drv.ID(), "source").Key() cloneKey := driver.NewVMRef(drv.ID(), "__agent-clone").Key() if err := mgr.SetSettings(cloneSourceSettingPlugin, sourceKey, map[string]any{ "allow_ai_access": true, "username": "macuser", "login_password": "secret-from-source", }); err != nil { t.Fatal(err) } cloneVM, err := app.resolveVMInfo(drv.ID(), "__agent-clone") if err != nil { t.Fatal(err) } if err := writeComputerUseOwnership(cloneVM.Directory, cloneSourceSettingPlugin, cloneKey, sourceKey); err != nil { t.Fatal(err) } got, err := app.computerUseSecretSetting(mgr, cloneSourceSettingPlugin, cloneVM, "login_password") if err != nil { t.Fatalf("clone should borrow the source password: %v", err) } if got != "secret-from-source" { t.Fatalf("clone secret = %q, want the source value", got) } if user := app.computerUseSSHUser(mgr, cloneSourceSettingPlugin, cloneVM); user != "macuser" { t.Fatalf("clone SSH user = %q, want the source user", user) } // The clone's own value wins over the source. if err := mgr.SetSettings(cloneSourceSettingPlugin, cloneKey, map[string]any{ "allow_ai_access": true, "username": "cloneuser", "login_password": "clone-own-password", }); err != nil { t.Fatal(err) } got, err = app.computerUseSecretSetting(mgr, cloneSourceSettingPlugin, cloneVM, "login_password") if err != nil || got != "clone-own-password" { t.Fatalf("clone own secret = %q (%v), want its own value", got, err) } if user := app.computerUseSSHUser(mgr, cloneSourceSettingPlugin, cloneVM); user != "cloneuser" { t.Fatalf("clone SSH user = %q, want its own value", user) } // Clearing the clone's own password and revoking access on the source // removes the fallback again. if err := mgr.SetSettings(cloneSourceSettingPlugin, cloneKey, map[string]any{"login_password": ""}); err != nil { t.Fatal(err) } if err := mgr.SetSettings(cloneSourceSettingPlugin, sourceKey, map[string]any{"allow_ai_access": false}); err != nil { t.Fatal(err) } if _, err := app.computerUseSecretSetting(mgr, cloneSourceSettingPlugin, cloneVM, "login_password"); !errors.Is(err, plugin.ErrSecretEmpty) { t.Fatalf("revoked source should leave no fallback, got %v", err) } if user := app.computerUseSSHUser(mgr, cloneSourceSettingPlugin, cloneVM); user != "cloneuser" { t.Fatalf("clone keeps its own SSH user after source revoke, got %q", user) } } func TestCloneSecretWithoutSourceStaysEmpty(t *testing.T) { app, mgr, drv := newCloneSettingsEnv(t) createVMs(t, drv, "__agent-orphan") cloneVM, err := app.resolveVMInfo(drv.ID(), "__agent-orphan") if err != nil { t.Fatal(err) } // A marker with no source (the lost-marker repair path) has no fallback. if err := writeComputerUseOwnership(cloneVM.Directory, cloneSourceSettingPlugin, cloneVM.Ref().Key(), ""); err != nil { t.Fatal(err) } if _, err := app.computerUseSecretSetting(mgr, cloneSourceSettingPlugin, cloneVM, "login_password"); !errors.Is(err, plugin.ErrSecretEmpty) { t.Fatalf("clone without a source should have no fallback, got %v", err) } } func TestSecretMissingMessageLocalized(t *testing.T) { const key = "err.computer_use.secret_missing" for _, loc := range []i18n.Locale{i18n.ZhCN, i18n.EnUS} { msg := strings.TrimSpace(i18n.M(loc, key, nil)) if msg == "" || msg == key { t.Fatalf("%s %q did not resolve", loc, key) } } }