//go:build linux package tools import ( "os" "testing" ) func TestIsWayland(t *testing.T) { // 保存原始环境变量 origSession := os.Getenv("XDG_SESSION_TYPE") origWayland := os.Getenv("WAYLAND_DISPLAY") defer func() { os.Setenv("XDG_SESSION_TYPE", origSession) os.Setenv("WAYLAND_DISPLAY", origWayland) }() // 测试 X11 os.Setenv("XDG_SESSION_TYPE", "x11") os.Unsetenv("WAYLAND_DISPLAY") if isWayland() { t.Fatal("should not detect wayland on x11 session") } // 测试 Wayland (via XDG_SESSION_TYPE) os.Setenv("XDG_SESSION_TYPE", "wayland") os.Unsetenv("WAYLAND_DISPLAY") if !isWayland() { t.Fatal("should detect wayland via XDG_SESSION_TYPE") } // 测试 Wayland (via WAYLAND_DISPLAY) os.Setenv("XDG_SESSION_TYPE", "") os.Setenv("WAYLAND_DISPLAY", "wayland-0") if !isWayland() { t.Fatal("should detect wayland via WAYLAND_DISPLAY") } // 清除两个变量 os.Unsetenv("XDG_SESSION_TYPE") os.Unsetenv("WAYLAND_DISPLAY") if isWayland() { t.Fatal("should not detect wayland without env vars") } } func TestChordToYdotool(t *testing.T) { cases := []struct{ in, want string }{ {"ctrl+c", "ctrl+c"}, {"cmd+c", "meta+c"}, {"ctrl+alt+delete", "ctrl+alt+delete"}, {"pageup", "pageup"}, {"cmd+shift+t", "meta+shift+t"}, {"enter", "enter"}, {"space", "space"}, {"a", "a"}, {"f5", "f5"}, {"escape", "esc"}, } for _, c := range cases { mods, main, err := parseChord(c.in) if err != nil { t.Fatalf("%s: %v", c.in, err) } got, err := chordToYdotool(mods, main, c.in) if err != nil || got != c.want { t.Fatalf("%s: got %q (%v), want %q", c.in, got, err, c.want) } } } func TestHasBin(t *testing.T) { // ls 应该存在 if !hasBin("ls") { t.Fatal("ls should be found") } // 不存在的命令 if hasBin("nonexistent_binary_xyz_12345") { t.Fatal("nonexistent binary should not be found") } }