跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

desktop_test.go

137 行 · 3.8 KB历史下载
1package desktop
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "path/filepath"
8 "runtime"
9 "testing"
10 "time"
11)
12
13// fake puts a shell script named name first in PATH, and nothing else that could show a dialog.
14func fake(t *testing.T, name, script string) {
15 t.Helper()
16 dir := t.TempDir()
17 if script != "" {
18 if err := os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"+script+"\n"), 0o755); err != nil {
19 t.Fatal(err)
20 }
21 }
22 // /usr/bin and /bin for sh and sleep: no dialog tool lives there in the test environment.
23 t.Setenv("PATH", dir+":/usr/bin:/bin")
24}
25
26type want struct{ allow, ok bool }
27
28func TestAskLinux(t *testing.T) {
29 if runtime.GOOS != "linux" {
30 t.Skip("linux")
31 }
32 for _, tool := range []struct {
33 name string
34 deny int // the exit status of a Deny
35 display string
36 }{
37 {"zenity", 1, "cannot open display"},
38 {"kdialog", 1, "could not connect to display"},
39 {"kdialog", 2, "cannot connect to X server"},
40 } {
41 for _, c := range []struct {
42 name string
43 script string
44 env string
45 want want
46 }{
47 {"allow", "exit 0", ":0", want{true, true}},
48 {"deny", fmt.Sprintf("exit %d", tool.deny), ":0", want{false, true}},
49 {"no display tool", "echo '" + tool.display + "' >&2; exit 1", ":0", want{false, false}},
50 {"crash", "kill -SEGV $$", ":0", want{false, false}},
51 {"no display variable", "exit 0", "", want{false, false}},
52 {"tool missing", "", ":0", want{false, false}},
53 } {
54 t.Run(tool.name+"/"+c.name, func(t *testing.T) {
55 t.Setenv("DISPLAY", c.env)
56 t.Setenv("WAYLAND_DISPLAY", "")
57 fake(t, tool.name, c.script)
58 allow, ok := Ask(context.Background(), "t", "m")
59 if (want{allow, ok}) != c.want {
60 t.Errorf("got allow=%v ok=%v, want %v", allow, ok, c.want)
61 }
62 })
63 }
64 }
65}
66
67// zenity that cannot show a dialog falls back to kdialog.
68func TestAskLinuxFallback(t *testing.T) {
69 if runtime.GOOS != "linux" {
70 t.Skip("linux")
71 }
72 t.Setenv("DISPLAY", ":0")
73 t.Setenv("WAYLAND_DISPLAY", "")
74 fake(t, "zenity", "echo 'cannot open display' >&2; exit 1")
75 dir := filepath.SplitList(os.Getenv("PATH"))[0]
76 if err := os.WriteFile(filepath.Join(dir, "kdialog"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
77 t.Fatal(err)
78 }
79 if allow, ok := Ask(context.Background(), "t", "m"); !allow || !ok {
80 t.Errorf("got allow=%v ok=%v", allow, ok)
81 }
82}
83
84// The dialog is up when the context ends: an answer, a refusal.
85func TestAskTimeout(t *testing.T) {
86 var tool string
87 switch runtime.GOOS {
88 case "linux":
89 tool = "zenity"
90 t.Setenv("DISPLAY", ":0")
91 t.Setenv("WAYLAND_DISPLAY", "")
92 case "darwin":
93 tool = "osascript"
94 default:
95 t.Skip("no dialog")
96 }
97 fake(t, tool, "exec sleep 30")
98 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
99 defer cancel()
100 start := time.Now()
101 allow, ok := Ask(ctx, "t", "m")
102 if allow || !ok {
103 t.Errorf("got allow=%v ok=%v", allow, ok)
104 }
105 if time.Since(start) > 10*time.Second {
106 t.Error("did not stop with the context")
107 }
108}
109
110func TestAskDarwin(t *testing.T) {
111 if runtime.GOOS != "darwin" {
112 t.Skip("macOS")
113 }
114 for _, c := range []struct {
115 name string
116 script string
117 want want
118 }{
119 {"allow", "echo 'button returned:Allow'", want{true, true}},
120 {"deny button", "echo 'button returned:Deny'", want{false, true}},
121 {"closed", "echo 'execution error: User canceled. (-128)' >&2; exit 1", want{false, true}},
122 {"no session", "echo 'execution error: Application isn'\\''t running. (-600)' >&2; exit 1", want{false, false}},
123 {"missing", "", want{false, false}},
124 } {
125 t.Run(c.name, func(t *testing.T) {
126 fake(t, "osascript", c.script)
127 if c.script == "" {
128 // The real osascript lives in /usr/bin and would show a dialog.
129 t.Setenv("PATH", t.TempDir())
130 }
131 allow, ok := Ask(context.Background(), "t", "m")
132 if (want{allow, ok}) != c.want {
133 t.Errorf("got allow=%v ok=%v, want %v", allow, ok, c.want)
134 }
135 })
136 }
137}