//go:build windows package tools import ( "context" "testing" ) // 这份用例只在 Windows 上编译,且**只有真的起得来 PowerShell 时才执行**: // 没有 PowerShell 的机器(Server Core、被裁掉的镜像)应当跳过,而不是把 // "跑不起来" 报成 "后端坏了"。可用性问题走 Available(),不在这里重述。 func TestRunScript(t *testing.T) { b := newGuiBackend() if err := b.Available(); err != nil { t.Skipf("PowerShell 不可用:%v", err) } w := b.(*windowsBackend) out, err := w.runScript("Write-Output 'hello'") if err != nil { t.Fatalf("runScript 失败:%v", err) } if out != "hello" { t.Fatalf("got %q, want hello", out) } } func TestRunScriptError(t *testing.T) { b := newGuiBackend() if err := b.Available(); err != nil { t.Skipf("PowerShell 不可用:%v", err) } if _, err := b.(*windowsBackend).runScript("throw 'test error'"); err == nil { t.Fatal("失败的脚本该返回错误") } } // 截图返回的是文件路径:这一条把"注入 user32 → 落到一个真实存在的 png"整条链 // 走完,是这套后端能用的最短证明。 func TestScreenshotWritesFile(t *testing.T) { b := newGuiBackend() if err := b.Available(); err != nil { t.Skipf("PowerShell 不可用:%v", err) } path, err := b.Screenshot(context.Background(), "") if err != nil { t.Fatalf("Screenshot 失败:%v", err) } if path == "" { t.Fatal("没有返回文件路径") } } func TestPSUser32Constant(t *testing.T) { if psUser32 == "" { t.Fatal("psUser32 should not be empty") } } func TestMouseConstants(t *testing.T) { if meLeftDown != "2" || meLeftUp != "4" { t.Fatal("left mouse constants incorrect") } if meRightDown != "8" || meRightUp != "16" { t.Fatal("right mouse constants incorrect") } if meWheel != "2048" || wheelNotch != "120" { t.Fatal("wheel constants incorrect") } }