package app import ( "context" "os" "runtime" "strings" "testing" "github.com/Nwflower/reims-go/internal/config" "github.com/Nwflower/reims-go/internal/driver" "github.com/Nwflower/reims-go/internal/i18n" vmlifecycle "github.com/Nwflower/reims-go/internal/lifecycle" "github.com/Nwflower/reims-go/internal/plugin" ) func TestReleaseVMThumbnailRemovesLock(t *testing.T) { const name = "thumbnail-release-test" t.Cleanup(func() { releaseVMThumbnail(name) }) first := vmThumbLock("", name) if got := vmThumbLock("", name); got != first { t.Fatal("same VM should share a thumbnail lock") } releaseVMThumbnail(name) thumbLocks.mu.Lock() remaining := len(thumbLocks.locks) thumbLocks.mu.Unlock() if remaining != 0 { t.Fatalf("thumbnail locks remain after release: %d", remaining) } if got := vmThumbLock("", name); got == first { t.Fatal("released lock was reused") } } // Every refusal carries a localized summary and a stable code. func TestToggleVMFullscreenLocalizesErrors(t *testing.T) { windowless := os.Getpid() for _, tt := range []struct { name, state, graphics string pid *int en, zh string }{ {"stopped", "stopped", "", nil, "VM Tahoe is not running [E_VM_NOT_RUNNING]", "虚拟机 Tahoe 未运行 [E_VM_NOT_RUNNING]"}, {"no process", "running", "reims-vgpu", nil, "VM Tahoe is not running [E_VM_NOT_RUNNING]", "虚拟机 Tahoe 未运行 [E_VM_NOT_RUNNING]"}, {"SVGA", "running", "vmware-svga", &windowless, "Press Ctrl+Alt+F in the QEMU window for Tahoe to toggle full screen [E_FULLSCREEN_VMWARE_SVGA]", "在 Tahoe 的 QEMU 窗口中按 Ctrl+Alt+F 切换全屏 [E_FULLSCREEN_VMWARE_SVGA]"}, {"missing window", "running", "reims-vgpu", &windowless, "Couldn’t find the display window for Tahoe [E_VM_WINDOW_NOT_FOUND]", "找不到 Tahoe 的显示窗口 [E_VM_WINDOW_NOT_FOUND]"}, } { if tt.name == "missing window" && runtime.GOOS != "windows" { continue // only the Windows host looks for windows } for locale, want := range map[string]string{"en-US": tt.en, "zh-CN": tt.zh} { t.Run(tt.name+"/"+locale, func(t *testing.T) { drv := &appSecondDriver{id: driver.BuiltinReimsMacOSDriverID, vms: []driver.VMInfo{{ Name: "Tahoe", DriverID: driver.BuiltinReimsMacOSDriverID, Directory: t.TempDir(), State: tt.state, PID: tt.pid, DriverData: map[string]any{"graphics": tt.graphics}, }}} reg := plugin.NewRegistry(nil) if err := reg.Register(plugin.PluginDef{ Manifest: plugin.Manifest{ID: "test.macos", Version: "1.0.0"}, Source: plugin.SourceBuiltin, Desired: true, Activate: func(_ context.Context, sc *plugin.Scope) error { reg.RegisterGuestDriver(sc, drv.id, drv) return nil }, }); err != nil { t.Fatal(err) } reg.Reconcile(t.Context()) a := &App{cfg: &config.Config{DataRoot: t.TempDir(), Locale: i18n.Locale(locale)}, lifecycleSvc: vmlifecycle.New(reg, driver.NewRouter(reg, nil), nil), transitions: map[string]string{}, pendingChanges: map[string][]PendingChange{}} got, err := a.ToggleVMFullscreen("", "Tahoe") if got || err == nil { t.Fatalf("toggle = (%v, %v), want an error", got, err) } if summary, _, _ := strings.Cut(err.Error(), "\n"); summary != want { t.Fatalf("summary = %q, want %q", summary, want) } }) } } }