Files
Reims-Go/internal/app/thumbnail_flow_test.go
T

151 lines
5.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package app
import (
"context"
"errors"
"slices"
"testing"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/i18n"
"github.com/Nwflower/reims-go/internal/macos/reims"
)
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")
}
}
type testFullscreenClient struct {
detail reims.VMDetail
vms []reims.VMListItem
listCalls [][]reims.VMListItem
calls []string
}
func (c *testFullscreenClient) Show(_ context.Context, name string) (reims.VMDetail, error) {
c.calls = append(c.calls, "show "+name)
return c.detail, nil
}
func (c *testFullscreenClient) List(context.Context) ([]reims.VMListItem, error) {
idx := 0
for _, call := range c.calls {
if call == "list" {
idx++
}
}
c.calls = append(c.calls, "list")
if len(c.listCalls) > 0 {
if idx < len(c.listCalls) {
return c.listCalls[idx], nil
}
return c.listCalls[len(c.listCalls)-1], nil
}
return c.vms, nil
}
func TestToggleVMFullscreenUsesLiveVMState(t *testing.T) {
const name = "Tahoe"
oldPID, livePID, zeroPID, invalidPID := 301, 542, 0, -1
for _, tt := range []struct {
name string
state string
vms []reims.VMListItem
listCalls [][]reims.VMListItem
wantCalls []string
target uint32
}{
{"stopped at show", "stopped", []reims.VMListItem{{Name: name, State: "running", PID: &oldPID}}, nil, []string{"list", "show Tahoe"}, 0},
{"stopped before first list", "running", []reims.VMListItem{{Name: name, State: "stopped", PID: &oldPID}}, nil, []string{"list"}, 0},
{"missing from list", "running", []reims.VMListItem{{Name: "Other", State: "running", PID: &oldPID}}, nil, []string{"list"}, 0},
{"missing PID", "running", []reims.VMListItem{{Name: name, State: "running"}}, nil, []string{"list"}, 0},
{"zero PID", "running", []reims.VMListItem{{Name: name, State: "running", PID: &zeroPID}}, nil, []string{"list"}, 0},
{"negative PID", "running", []reims.VMListItem{{Name: name, State: "running", PID: &invalidPID}}, nil, []string{"list"}, 0},
{"restarted with different PID between show and list (e.g. switched to SVGA)", "running", nil, [][]reims.VMListItem{
{{Name: name, State: "running", PID: &oldPID}},
{{Name: name, State: "running", PID: &livePID}},
}, []string{"list", "show Tahoe", "list"}, 0},
{"live running VM with stable PID", "running", []reims.VMListItem{
{Name: "Other", State: "running", PID: &oldPID}, {Name: name, State: "running", PID: &livePID},
}, nil, []string{"list", "show Tahoe", "list"}, uint32(livePID)},
} {
t.Run(tt.name, func(t *testing.T) {
c := &testFullscreenClient{detail: reims.VMDetail{State: tt.state, Graphics: "reims-vgpu"}, vms: tt.vms, listCalls: tt.listCalls}
got, err := toggleVMFullscreen(context.Background(), name, i18n.EnUS, c, func(pid uint32) (bool, error) {
if pid != tt.target || tt.target == 0 {
t.Fatalf("toggled PID %d, want %d", pid, tt.target)
}
return true, nil
})
if !slices.Equal(c.calls, tt.wantCalls) {
t.Fatalf("client calls = %v, want %v", c.calls, tt.wantCalls)
}
if tt.target == 0 {
if got || err == nil || err.Error() != "VM Tahoe is not running [E_VM_NOT_RUNNING]" {
t.Fatalf("toggle = (%v, %v), want a not-running error", got, err)
}
} else if !got || err != nil {
t.Fatalf("toggle = (%v, %v), want (true, nil)", got, err)
}
})
}
}
func TestToggleVMFullscreenLocalizesErrors(t *testing.T) {
win32Error := errors.New("SetWindowPos: access denied")
pid := 542
for _, tt := range []struct {
name string
detail reims.VMDetail
hostError error
en, zh string
}{
{"stopped", reims.VMDetail{State: "stopped"}, nil,
"VM Tahoe is not running [E_VM_NOT_RUNNING]", "虚拟机 Tahoe 未运行 [E_VM_NOT_RUNNING]"},
{"SVGA", reims.VMDetail{State: "running", Graphics: "vmware-svga"}, nil,
"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", reims.VMDetail{State: "running", Graphics: "reims-vgpu"}, host.ErrDisplayWindowNotFound,
"Couldn’t find the display window for Tahoe [E_VM_WINDOW_NOT_FOUND]", "找不到 Tahoe 的显示窗口 [E_VM_WINDOW_NOT_FOUND]"},
{"Win32 failure", reims.VMDetail{State: "running", Graphics: "reims-vgpu"}, win32Error,
"Couldn’t toggle full screen for Tahoe [E_FULLSCREEN_FAILED]", "无法切换 Tahoe 的全屏状态 [E_FULLSCREEN_FAILED]"},
} {
for loc, summary := range map[i18n.Locale]string{i18n.EnUS: tt.en, i18n.ZhCN: tt.zh} {
t.Run(tt.name+"/"+string(loc), func(t *testing.T) {
c := &testFullscreenClient{detail: tt.detail, vms: []reims.VMListItem{{Name: "Tahoe", State: "running", PID: &pid}}}
got, err := toggleVMFullscreen(context.Background(), "Tahoe", loc, c, func(uint32) (bool, error) {
if tt.hostError == nil {
t.Fatal("toggled a stopped or SVGA VM")
}
return false, tt.hostError
})
want := summary
if tt.hostError != nil {
want += "\n[detail] " + tt.hostError.Error()
if !errors.Is(err, tt.hostError) {
t.Errorf("error lost original cause: %v", err)
}
}
if got || err == nil || err.Error() != want {
t.Fatalf("toggle = (%v, %v), want (false, %q)", got, err, want)
}
})
}
}
}