跳到主要内容

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

浏览文件

guest_test.go

136 行 · 4.6 KB历史下载
1package cli
2
3import (
4 "errors"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "github.com/sylvinus/agent-vm/internal/vm"
12)
13
14// localGuest runs what the VM would, on this machine: the project is at its
15// own path, as the share puts it, and the guest has its own home.
16func localGuest(t *testing.T, se *startEnv) (home string, ran *[][]string) {
17 t.Helper()
18 for _, sh := range []string{"sh", "zsh", "awk"} {
19 if _, err := exec.LookPath(sh); err != nil {
20 t.Skip("no " + sh)
21 }
22 }
23 home = t.TempDir()
24 var calls [][]string
25 se.fake.ShellFunc = func(name string, o vm.ShellOpts) int {
26 calls = append(calls, append([]string{o.Workdir}, o.Args...))
27 cmd := exec.Command(o.Args[0], o.Args[1:]...)
28 cmd.Dir = o.Workdir
29 if cmd.Dir == "" {
30 cmd.Dir = home
31 }
32 cmd.Env = []string{"HOME=" + home, "PATH=" + os.Getenv("PATH"), "ZDOTDIR=" + home}
33 cmd.Stdin, cmd.Stdout, cmd.Stderr = o.Stdin, o.Stdout, o.Stderr
34 err := cmd.Run()
35 var ee *exec.ExitError
36 if errors.As(err, &ee) {
37 return ee.ExitCode()
38 }
39 if err != nil {
40 t.Fatal(err)
41 }
42 return 0
43 }
44 return home, &calls
45}
46
47// The probe, run as the VM runs it: the env pushed private, the project's
48// own after the shared one, CRs dropped; nothing left in the project.
49func TestGuestProbe(t *testing.T) {
50 se := newStartEnv(t)
51 se.base()
52 home, _ := localGuest(t, se)
53 os.WriteFile(se.state.Path("env"), []byte("TOKEN='x'\n"), 0o600)
54 os.WriteFile(filepath.Join(se.proj, ".agent-vm.env"), []byte("P=1\r\n"), 0o600)
55 if code := se.run("run", "true"); code != 0 {
56 t.Fatalf("run: %d %s", code, se.out())
57 }
58 fi, err := os.Stat(filepath.Join(home, ".agent-vm.env"))
59 if err != nil || fi.Mode().Perm() != 0o600 {
60 t.Fatalf("guest env: %v %v", fi, err)
61 }
62 if b, _ := os.ReadFile(filepath.Join(home, ".agent-vm.env")); string(b) != "TOKEN='x'\nP=1\n" {
63 t.Errorf("guest env: %q", b)
64 }
65 ents, _ := os.ReadDir(se.proj)
66 for _, e := range ents {
67 if strings.Contains(e.Name(), "probe") {
68 t.Errorf("left in the project: %s", e.Name())
69 }
70 }
71 // Removed on the host: emptied in the guest.
72 os.Remove(se.state.Path("env"))
73 os.Remove(filepath.Join(se.proj, ".agent-vm.env"))
74 se.run("run", "true")
75 if b, err := os.ReadFile(filepath.Join(home, ".agent-vm.env")); err != nil || len(b) != 0 {
76 t.Errorf("guest env after removal: %q %v", b, err)
77 }
78}
79
80// ~/.agent-vm/runtime.sh: piped whole into the VM, CRs dropped, under the
81// shell its shebang names.
82func TestGuestUserRuntime(t *testing.T) {
83 se := newStartEnv(t)
84 se.base()
85 localGuest(t, se)
86 os.WriteFile(se.state.Path("runtime.sh"), []byte("#!/bin/bash -e\r\necho \"bash=${BASH_VERSION:+yes}\"\r\n"), 0o644)
87 if code := se.run("run", "true"); code != 0 || !strings.Contains(se.out(), "Running user runtime setup...") || !strings.Contains(se.out(), "bash=yes\n") {
88 t.Errorf("runtime.sh: %d %q", code, se.out())
89 }
90}
91
92// `run`'s wrapper: a missing command is 127 and said so, a file without +x
93// is env's 126, an assignment goes to the command.
94func TestGuestRun(t *testing.T) {
95 se := newStartEnv(t)
96 se.base()
97 localGuest(t, se)
98 if code := se.run("run", "no-such-command-x"); code != 127 || !strings.Contains(se.out(), "agent-vm: no-such-command-x is not installed in this VM.") {
99 t.Errorf("missing: %d %s", code, se.out())
100 }
101 os.WriteFile(filepath.Join(se.proj, "s.sh"), []byte("#!/bin/sh\necho hi\n"), 0o644)
102 if code := se.run("run", "./s.sh"); code != 126 {
103 t.Errorf("no +x: %d %s", code, se.out())
104 }
105 if code := se.run("run", "V=x", "sh", "-c", "echo v=$V"); code != 0 || !strings.Contains(se.out(), "v=x") {
106 t.Errorf("assignment: %d %s", code, se.out())
107 }
108}
109
110// A project runtime script that is a link out of the project runs in the
111// VM, by its path there: the host never reads it (the agent can retarget
112// the link at any host file).
113func TestGuestRuntimeLink(t *testing.T) {
114 se := newStartEnv(t)
115 se.base()
116 _, ran := localGuest(t, se)
117 outside := filepath.Join(t.TempDir(), "rt.sh")
118 os.WriteFile(outside, []byte("#!/bin/sh\necho from-runtime\n"), 0o644)
119 os.Symlink(outside, filepath.Join(se.proj, ".agent-vm.runtime.sh"))
120 if code := se.run("run", "true"); code != 0 || !strings.Contains(se.out(), "Running project runtime setup...") {
121 t.Fatalf("run: %d %s", code, se.out())
122 }
123 inVM := false
124 for _, c := range *ran {
125 last := c[len(c)-1]
126 if last == filepath.Join(se.proj, ".agent-vm.runtime.sh") && c[1] == "zsh" {
127 inVM = true
128 }
129 if strings.Contains(strings.Join(c, " "), " -s") && strings.HasPrefix(c[len(c)-1], "exec ") {
130 t.Errorf("piped from the host: %q", c)
131 }
132 }
133 if !inVM || !strings.Contains(se.out(), "from-runtime") {
134 t.Errorf("not run in the VM by its path: %q\n%s", *ran, se.out())
135 }
136}