跳到主要内容

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

浏览文件

claude_desktop_resize_unix_test.go

131 行 · 5.0 KB历史下载
1//go:build !windows
2
3package workbench
4
5import (
6 "os"
7 "os/exec"
8 "path/filepath"
9 "runtime"
10 "strconv"
11 "strings"
12 "testing"
13 "time"
14)
15
16func TestDesktopResizeScriptAppliesTheViewersDensity(t *testing.T) {
17 bash, err := exec.LookPath("bash")
18 if runtime.GOOS != "linux" || err != nil {
19 t.Skip("the script identifies its settings manager through /proc")
20 }
21 // go test already executes its test binary from this filesystem. Both
22 // TMPDIR and /dev/shm can be mounted noexec independently of the Go build directory.
23 executable, err := os.Executable()
24 if err != nil {
25 t.Fatal(err)
26 }
27 root, err := os.MkdirTemp(filepath.Dir(executable), "cage-resize-")
28 if err != nil {
29 t.Fatal(err)
30 }
31 t.Cleanup(func() { os.RemoveAll(root) })
32 desktop, phone, bin := filepath.Join(root, "desktop"), filepath.Join(root, "phone"), filepath.Join(root, "bin")
33 for _, directory := range []string{desktop, phone, bin} {
34 if err := os.Mkdir(directory, 0o700); err != nil {
35 t.Fatal(err)
36 }
37 }
38 write := func(path, content string, mode os.FileMode) {
39 t.Helper()
40 if err := os.WriteFile(path, []byte(content), mode); err != nil {
41 t.Fatal(err)
42 }
43 }
44 read := func(path string) string {
45 data, _ := os.ReadFile(path)
46 return string(data)
47 }
48 t.Setenv("CAGE_RESIZE_FIXTURE", root)
49 write(filepath.Join(bin, "xrandr"), "#!/bin/sh\necho \"$*\" >> \"$CAGE_RESIZE_FIXTURE/modes\"\n", 0o700)
50 // The script signals a process named xsettingsd.
51 write(filepath.Join(bin, "xsettingsd"), "#!/bin/sh\ntrap 'echo reload >> \"$CAGE_RESIZE_FIXTURE/reloads\"' HUP\nwhile :; do sleep 1 & wait $!; done\n", 0o700)
52 resize := func(scale string) string {
53 t.Helper()
54 script := strings.NewReplacer("/run/cage-desktop", desktop, "/run/cage-phone-desktop", phone).Replace(desktopResizeScript(1000, 800, scale))
55 cmd := exec.Command(bash, "--noprofile", "--norc", "-c", script)
56 cmd.Env = append(os.Environ(), "PATH="+bin+":"+os.Getenv("PATH"))
57 output, err := cmd.CombinedOutput()
58 if err != nil {
59 t.Fatalf("resize %q: %v\n%s", scale, err, output)
60 }
61 return string(output)
62 }
63 reloads := func(want int) {
64 t.Helper()
65 deadline := time.Now().Add(3 * time.Second)
66 for strings.Count(read(filepath.Join(root, "reloads")), "reload") < want && time.Now().Before(deadline) {
67 time.Sleep(20 * time.Millisecond)
68 }
69 // A reload that should not happen would arrive within this pause.
70 time.Sleep(150 * time.Millisecond)
71 if got := strings.Count(read(filepath.Join(root, "reloads")), "reload"); got != want {
72 t.Fatalf("settings reloads: %d, want %d", got, want)
73 }
74 }
75
76 // A fixed-density session keeps its density and reports none.
77 if output := resize("1.25"); output != "" || read(filepath.Join(desktop, "desktop-size")) != "1000 800 \n" || read(filepath.Join(desktop, "xsettings.conf")) != "" {
78 t.Fatalf("fixed density changed: %q", output)
79 }
80 if !strings.Contains(read(filepath.Join(root, "modes")), "--output VNC-0 --mode cage-1000x800") {
81 t.Fatal("size not applied")
82 }
83
84 manager := exec.Command(filepath.Join(bin, "xsettingsd"))
85 if err := manager.Start(); err != nil {
86 t.Fatal(err)
87 }
88 t.Cleanup(func() { _ = manager.Process.Kill(); _, _ = manager.Process.Wait() })
89 for attempt := 0; read("/proc/"+strconv.Itoa(manager.Process.Pid)+"/comm") != "xsettingsd\n"; attempt++ {
90 if attempt == 100 {
91 t.Skip("the stand-in manager is not visible under its name")
92 }
93 time.Sleep(10 * time.Millisecond)
94 }
95 // The trap is installed shortly after the shell reports its name.
96 time.Sleep(200 * time.Millisecond)
97 write(filepath.Join(desktop, "xsettings.pid"), strconv.Itoa(manager.Process.Pid)+"\n", 0o600)
98 write(filepath.Join(desktop, "base-display-scale"), "2\n", 0o600)
99 write(filepath.Join(desktop, "display-scale"), "2\n", 0o600)
100
101 if output := resize("1.25"); output != "scale 1.25\n" {
102 t.Fatalf("applied density not reported: %q", output)
103 }
104 if conf := read(filepath.Join(desktop, "xsettings.conf")); !strings.HasPrefix(conf, "Xft/DPI 122880\n") || !strings.Contains(conf, "Gdk/UnscaledDPI 122880\n") {
105 t.Fatalf("settings: %q", conf)
106 }
107 if read(filepath.Join(desktop, "display-scale")) != "1.25\n" || read(filepath.Join(desktop, "desktop-size")) != "1000 800 1.25\n" {
108 t.Fatal("density not recorded")
109 }
110 reloads(1)
111 // An unchanged density leaves the application alone.
112 resize("1.25")
113 reloads(1)
114
115 // A viewer that names no density gets the configured one back.
116 if output := resize(""); output != "" || !strings.HasPrefix(read(filepath.Join(desktop, "xsettings.conf")), "Xft/DPI 196608\n") {
117 t.Fatalf("configured density not restored: %q", output)
118 }
119 reloads(2)
120
121 // While a phone holds the display the request is kept for its release.
122 write(filepath.Join(phone, "layout.json"), "{}", 0o600)
123 modes := read(filepath.Join(root, "modes"))
124 if output := resize("1"); output != "scale 1\n" || read(filepath.Join(desktop, "desktop-size")) != "1000 800 1\n" {
125 t.Fatalf("deferred request: %q", output)
126 }
127 if read(filepath.Join(desktop, "display-scale")) != "2\n" || read(filepath.Join(root, "modes")) != modes {
128 t.Fatal("a held display was changed")
129 }
130 reloads(2)
131}