返回 DeepSeek-Reasonix
clipboard_wsl_test.go
根目录 / internal / cli / clipboard_wsl_test.go
1 package cli
2
3 import (
4 "context"
5 "io"
6 "strings"
7 "testing"
8 )
9
10 func TestWSLClipboardCommandPreservesUTF8Text(t *testing.T) {
11 t.Setenv("PATH", t.TempDir())
12 t.Setenv("WAYLAND_DISPLAY", "")
13 for _, text := range []string{"中文测试", "hello", "你好 🚀"} {
14 t.Run(text, func(t *testing.T) {
15 cmd := newWSLClipboardCommand(context.Background(), text)
16 if got := cmd.Args[0]; got != "powershell.exe" {
17 t.Fatalf("command = %q, want powershell.exe", got)
18 }
19 script := cmd.Args[len(cmd.Args)-1]
20 if !strings.Contains(script, "[Text.UTF8Encoding]::new($false)") {
21 t.Fatalf("PowerShell script does not explicitly decode UTF-8: %q", script)
22 }
23 if !strings.Contains(script, "Set-Clipboard") {
24 t.Fatalf("PowerShell script does not use Set-Clipboard: %q", script)
25 }
26 got, err := io.ReadAll(cmd.Stdin)
27 if err != nil {
28 t.Fatalf("read command stdin: %v", err)
29 }
30 if string(got) != text {
31 t.Fatalf("clipboard bridge stdin = %q, want %q", got, text)
32 }
33 })
34 }
35 }
36
37 func TestIsWSLFor(t *testing.T) {
38 tests := []struct {
39 name string
40 goos string
41 values map[string]string
42 want bool
43 }{
44 {name: "windows ignores WSL variables", goos: "windows", values: map[string]string{"WSL_DISTRO_NAME": "Ubuntu"}, want: false},
45 {name: "linux without WSL", goos: "linux", values: map[string]string{}, want: false},
46 {name: "WSL distro", goos: "linux", values: map[string]string{"WSL_DISTRO_NAME": "Ubuntu"}, want: true},
47 {name: "WSL interop", goos: "linux", values: map[string]string{"WSL_INTEROP": "/run/WSL/8_interop"}, want: true},
48 }
49 for _, tt := range tests {
50 t.Run(tt.name, func(t *testing.T) {
51 getenv := func(key string) string { return tt.values[key] }
52 if got := isWSLFor(tt.goos, getenv); got != tt.want {
53 t.Fatalf("isWSLFor(%q) = %v, want %v", tt.goos, got, tt.want)
54 }
55 })
56 }
57 }
58
58 lines GO