package builtin import ( "context" "slices" "testing" "github.com/Nwflower/reims-go/internal/config" "github.com/Nwflower/reims-go/internal/plugin" ) func coreTestCallbacks(t *testing.T, settings map[string]any) HostCallbacks { t.Helper() root := t.TempDir() return HostCallbacks{ GetConfig: func() config.Config { return config.Config{DataRoot: root} }, GetSetting: func(pluginID, _ string, key string) (any, bool) { if pluginID != IDCore { return nil, false } v, ok := settings[key] return v, ok }, } } func TestCoreIsSystemAndHoldsDriverAndTheme(t *testing.T) { reg := plugin.NewRegistry(nil) // desiredFn says "off" for everything: the core must ignore it. if err := RegisterAll(reg, coreTestCallbacks(t, nil), func(string, bool) bool { return false }); err != nil { t.Fatal(err) } reg.Reconcile(context.Background()) if !reg.IsActive(IDCore) { t.Fatal("core must be active even when state says off") } info, ok := reg.Info(IDCore) if !ok || !info.System || info.Removable { t.Fatalf("core must be a non-removable system plugin, got %+v", info) } if err := reg.SetDesired(context.Background(), IDCore, false); err == nil { t.Fatal("disabling the core must fail") } if !reg.IsActive(IDCore) { t.Fatal("core went inactive after a refused disable") } if _, ok := reg.GuestDriver(IDDriverReimsMacOS); !ok { t.Fatal("core must register the reims macOS driver under its existing driver ID") } if !slices.ContainsFunc(reg.Themes(), func(th plugin.ThemeContribution) bool { return th.ID == IDThemeDefault }) { t.Fatal("core must contribute the default theme under its existing theme ID") } for _, id := range RetiredIDs { if _, ok := reg.Info(id); ok { t.Errorf("retired plugin %s is still registered", id) } } } func dispatchCoreStart(t *testing.T, settings map[string]any, facts map[string]any, caps []string) plugin.StartIntent { t.Helper() reg := plugin.NewRegistry(nil) if err := RegisterAll(reg, coreTestCallbacks(t, settings), nil); err != nil { t.Fatal(err) } reg.Reconcile(context.Background()) intents, err := reg.Hooks().DispatchBeforeStart(context.Background(), plugin.BeforeStartPayload{ VMName: "mac1", VMDir: t.TempDir(), DriverID: IDDriverReimsMacOS, RuntimeCaps: caps, HostFacts: facts, }) if err != nil { t.Fatal(err) } var core *plugin.StartIntent for i := range intents { if intents[i].PluginID == IDCore { if core != nil { t.Fatal("core returned more than one start intent") } core = &intents[i] } } if core == nil { t.Fatalf("no core start intent in %+v", intents) } return *core } func argKeys(in plugin.StartIntent) []string { var keys []string for _, a := range in.Args { keys = append(keys, a.Key) } return keys } var whpxHostFacts = map[string]any{ "host.needs_cold_reboot": false, "host.whpx_irqchip": true, "host.whpx_nested_virt": false, } func TestCoreStartStepsInOneIntent(t *testing.T) { in := dispatchCoreStart(t, nil, whpxHostFacts, nil) if !slices.Equal(argKeys(in), []string{keyKernelIrqchip}) { t.Fatalf("want only the kernel-irqchip arg, got %v", argKeys(in)) } for _, k := range []string{keyOfflineHelper, keyWin10ColdReboot} { if !slices.Contains(in.ClearKeys, k) { t.Errorf("unneeded step %s should clear its key, got ClearKeys %v", k, in.ClearKeys) } } // A runtime that tolerates the missing property needs no fallback. in = dispatchCoreStart(t, nil, whpxHostFacts, []string{"nested-virt-nonfatal"}) if len(in.Args) != 0 || !slices.Contains(in.ClearKeys, keyKernelIrqchip) { t.Fatalf("nested-virt-nonfatal runtime: want kernel-irqchip cleared, got args %v clear %v", argKeys(in), in.ClearKeys) } } func TestCoreWorkaroundSettings(t *testing.T) { off := map[string]any{SettingWHPXFallback: false, SettingWin10ColdReboot: false} in := dispatchCoreStart(t, off, whpxHostFacts, nil) if len(in.Args) != 0 || !slices.Contains(in.ClearKeys, keyKernelIrqchip) { t.Fatalf("WHPX setting off: want kernel-irqchip cleared, got args %v clear %v", argKeys(in), in.ClearKeys) } win10 := map[string]any{"host.needs_cold_reboot": true} in = dispatchCoreStart(t, nil, win10, nil) if !slices.Contains(argKeys(in), keyWin10ColdReboot) || len(in.PostStart) != 1 || in.PostStart[0].Kind != "qmp.watch_cold_reboot" { t.Fatalf("Win10 host: want cold-reboot args and watcher, got args %v post %+v", argKeys(in), in.PostStart) } in = dispatchCoreStart(t, off, win10, nil) if slices.Contains(argKeys(in), keyWin10ColdReboot) || len(in.PostStart) != 0 || !slices.Contains(in.ClearKeys, keyWin10ColdReboot) { t.Fatalf("cold-reboot setting off: want no args or watcher, got args %v post %+v clear %v", argKeys(in), in.PostStart, in.ClearKeys) } } func TestMigrateRetiredCarriesWorkaroundChoices(t *testing.T) { root := t.TempDir() store := plugin.NewStateStore(plugin.NewPluginsLayout(root).Root) if err := store.Save(&plugin.StateFile{Plugins: map[string]*plugin.PluginRecord{ "builtin.compat.whpx-fallback": {ID: "builtin.compat.whpx-fallback", Enabled: false}, "builtin.prep.guest-lang": {ID: "builtin.prep.guest-lang", Enabled: false}, IDThemeDefault: {ID: IDThemeDefault, Enabled: true}, IDClipboard: {ID: IDClipboard, Enabled: false}, }}); err != nil { t.Fatal(err) } mgr, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: root, HostVersion: "0.0.0"}) if err != nil { t.Fatal(err) } defer mgr.Close() if err := RegisterAll(mgr.Registry(), coreTestCallbacks(t, nil), mgr.DesiredFor); err != nil { t.Fatal(err) } // Before migration the CLI still honours the old switch. if CoreSettingOnInState(plugin.ReadState(root), SettingWHPXFallback) { t.Fatal("unmigrated whpx-fallback=off must read as off") } if err := MigrateRetired(mgr); err != nil { t.Fatal(err) } st := plugin.ReadState(root) for _, id := range RetiredIDs { if _, ok := st.Plugins[id]; ok { t.Errorf("retired record %s left in state.json", id) } } if rec := st.Plugins[IDClipboard]; rec == nil || rec.Enabled { t.Error("migration must not touch records of plugins that still exist") } vals := mgr.GetSettings(IDCore, "", false) if CoreSettingOn(vals, SettingWHPXFallback) { t.Error("a switched-off whpx-fallback plugin must become whpx_fallback=false") } if !CoreSettingOn(vals, SettingWin10ColdReboot) { t.Error("cold reboot had no record and must stay on") } if CoreSettingOnInState(st, SettingWHPXFallback) || !CoreSettingOnInState(st, SettingWin10ColdReboot) { t.Error("state snapshot must read the migrated core settings") } if err := MigrateRetired(mgr); err != nil { t.Fatalf("second migration: %v", err) } }