package action import ( "context" "errors" "fmt" "testing" "time" pwin "zerorigin/internal/platform/windows" ) // restoreDesktopFakes 在测试结束时恢复平台函数,避免影响其他用例。 func restoreDesktopFakes(t *testing.T, com func(int32) error, taskView func(int32, time.Duration) error, count func() (int, error)) { t.Helper() oldCOM, oldTaskView, oldCount := switchDesktopCOM, switchDesktopViaTaskView, virtualDesktopCount t.Cleanup(func() { switchDesktopCOM = oldCOM switchDesktopViaTaskView = oldTaskView virtualDesktopCount = oldCount }) switchDesktopCOM = com switchDesktopViaTaskView = taskView virtualDesktopCount = count } // TestSwitchDesktopNoTaskViewFallbackOnSingleDesktop 验证单桌面时 COM 返回 // ErrNotEnoughDesktops,不得回退任务视图(否则任务视图会留在前台)。 func TestSwitchDesktopNoTaskViewFallbackOnSingleDesktop(t *testing.T) { taskViewCalls := 0 restoreDesktopFakes(t, func(int32) error { return fmt.Errorf("%w (found 1)", pwin.ErrNotEnoughDesktops) }, func(int32, time.Duration) error { taskViewCalls++; return nil }, nil, ) err := (&switchDesktopAction{}).Execute(context.Background(), map[string]any{ "mode": "right", "method": "com", "taskview_delay_ms": 0, }) if !errors.Is(err, pwin.ErrNotEnoughDesktops) { t.Fatalf("Execute error = %v, want ErrNotEnoughDesktops", err) } if taskViewCalls != 0 { t.Fatalf("task view fallback called %d times, want 0 on single desktop", taskViewCalls) } } // TestSwitchDesktopCOMErrorFallsBackToTaskView 验证普通 COM 错误仍回退 // 任务视图,并透传方向与等待时长。 func TestSwitchDesktopCOMErrorFallsBackToTaskView(t *testing.T) { gotDir := int32(0) gotDelay := time.Duration(0) restoreDesktopFakes(t, func(int32) error { return errors.New("com unavailable") }, func(dir int32, delay time.Duration) error { gotDir, gotDelay = dir, delay return nil }, nil, ) err := (&switchDesktopAction{}).Execute(context.Background(), map[string]any{ "mode": "left", "method": "com", "taskview_delay_ms": 500, }) if err != nil { t.Fatalf("Execute error = %v, want nil (fallback succeeds)", err) } if gotDir != -1 || gotDelay != 500*time.Millisecond { t.Fatalf("fallback got dir=%d delay=%v, want -1/500ms", gotDir, gotDelay) } } // TestSwitchDesktopTaskViewGuardSingleDesktop 验证显式 taskview 方式在 // 单桌面时提前失败,不打开任务视图。 func TestSwitchDesktopTaskViewGuardSingleDesktop(t *testing.T) { comCalls, taskViewCalls := 0, 0 restoreDesktopFakes(t, func(int32) error { comCalls++; return nil }, func(int32, time.Duration) error { taskViewCalls++; return nil }, func() (int, error) { return 1, nil }, ) err := (&switchDesktopAction{}).Execute(context.Background(), map[string]any{ "mode": "right", "method": "taskview", }) if !errors.Is(err, pwin.ErrNotEnoughDesktops) { t.Fatalf("Execute error = %v, want ErrNotEnoughDesktops", err) } if comCalls != 0 || taskViewCalls != 0 { t.Fatalf("switches called (com=%d taskview=%d), want none", comCalls, taskViewCalls) } } // TestSwitchDesktopTaskViewCountFailureProceeds 验证数量查询失败不阻断 // taskview 方式(保持旧行为,失败由动作本身报错)。 func TestSwitchDesktopTaskViewCountFailureProceeds(t *testing.T) { gotDelay := time.Duration(0) restoreDesktopFakes(t, func(int32) error { return nil }, func(_ int32, delay time.Duration) error { gotDelay = delay; return nil }, func() (int, error) { return 0, errors.New("query failed") }, ) err := (&switchDesktopAction{}).Execute(context.Background(), map[string]any{ "mode": "right", "method": "taskview", "taskview_delay_ms": 900, }) if err != nil { t.Fatalf("Execute error = %v, want nil", err) } if gotDelay != 900*time.Millisecond { t.Fatalf("delay = %v, want 900ms", gotDelay) } }