跳到主要内容

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

浏览文件

cli_test.go

156 行 · 6.5 KB历史下载
1package cli
2
3import (
4 "bytes"
5 "os"
6 "path/filepath"
7 "regexp"
8 "strings"
9 "testing"
10
11 "github.com/sylvinus/agent-vm/internal/bashref"
12 "github.com/sylvinus/agent-vm/internal/version"
13 "github.com/sylvinus/agent-vm/internal/vmname"
14)
15
16func run(t *testing.T, args ...string) bashref.Result {
17 t.Helper()
18 var stdout, stderr bytes.Buffer
19 code := Main(args, IO{Stdin: strings.NewReader(""), Stdout: &stdout, Stderr: &stderr})
20 return bashref.Result{Stdout: stdout.String(), Stderr: stderr.String(), Code: code}
21}
22
23// bashAgentVM runs 0.2's agent-vm with args, in dir, with a limactl on PATH
24// (it checks for one before most commands; Lima is built in here).
25func bashAgentVM(t *testing.T, dir string, args ...string) bashref.Result {
26 t.Helper()
27 bin := t.TempDir()
28 if err := os.WriteFile(filepath.Join(bin, "limactl"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
29 t.Fatal(err)
30 }
31 return bashref.Script(t, dir, []string{"PATH=" + bin + ":" + os.Getenv("PATH")}, `agent-vm "$@"`, args...)
32}
33
34// Same output and status as 0.2, with this build's version set to 0.2's.
35func TestMainBash(t *testing.T) {
36 saved := version.Version
37 t.Cleanup(func() { version.Version = saved })
38 version.Version = strings.TrimSpace(bashref.Script(t, "", nil, `echo "$AGENT_VM_VERSION"`).Stdout)
39
40 proj, _ := filepath.EvalSymlinks(t.TempDir())
41 t.Chdir(proj)
42 t.Setenv("PWD", proj)
43 cases := [][]string{
44 {"version"}, {"--version"}, {"-V"},
45 {"version", "--min", "0.1.0"}, {"version", "--min=0.2.1"}, {"version", "--min", "0.10.0"},
46 {"version", "--min"}, {"version", "--min", "x"}, {"version", "--min", ""}, {"version", "--max", "1"},
47 {"version", "--min", "1.2.3-rc.1"},
48 {"name"}, {"name", proj}, {"name", "."}, {"name", proj + "/missing"},
49 {"frobnicate"},
50 {"--disk"}, {"--disk", "10G", "claude"}, {"--disk=0", "shell"}, {"--memory", "-1"}, {"--ram=x"},
51 {"--cpus", "01"}, {"--ssh-port", "80"}, {"--ssh-port", "70000"}, {"--ssh-port=00"},
52 {"--readonly", "stop"}, {"--disk", "10", "--rm", "list"}, {"--rm", "version"}, {"--scratch", "help"},
53 {"--unsafe-writable-git=1", "name"}, {"--unsafe-disable-security-prompts", "doctor"},
54 }
55 // How to update depends on how each copy was installed.
56 update := regexp.MustCompile(`(?m)^ Update it: .*$`)
57 hash := regexp.MustCompile(`(?m)-[0-9a-f]{8}$`)
58 for _, args := range cases {
59 want := bashAgentVM(t, proj, args...)
60 got := run(t, args...)
61 want.Stderr = update.ReplaceAllString(want.Stderr, " Update it:")
62 got.Stderr = update.ReplaceAllString(got.Stderr, " Update it:")
63 // 0.3's names drop 0.2's prefix. The hash is of the folder bash had
64 // (TestNameBash compares hashes).
65 if args[0] == "name" {
66 want.Stdout = strings.TrimPrefix(want.Stdout, vmname.OldPrefix)
67 want.Stdout = hash.ReplaceAllString(want.Stdout, "-HASH")
68 got.Stdout = hash.ReplaceAllString(got.Stdout, "-HASH")
69 }
70 // 0.3 exits with exitUsage for a wrong command line, where 0.2 had 1.
71 if want.Code == 1 && got.Code == exitUsage {
72 want.Code = exitUsage
73 }
74 if got.Stdout != want.Stdout || got.Stderr != want.Stderr || got.Code != want.Code {
75 t.Errorf("agent-vm %q:\n go: %+v\n bash: %+v", args, got, want)
76 }
77 }
78}
79
80// The help is 0.2's, but for what Lima built in changed (CHANGELOG).
81func TestHelp(t *testing.T) {
82 want := bashref.Run(t, "", nil, "_agent_vm_help").Stdout
83 for _, args := range [][]string{{}, {"help"}, {"--help"}, {"-h"}} {
84 r := run(t, args...)
85 if r.Code != 0 || r.Stdout != helpText || r.Stderr != "" {
86 t.Errorf("%q: %+v", args, r)
87 }
88 }
89 strip := func(s string) string {
90 s = strings.Replace(s, "agent-vm rm agent-vm-old-name-1a2b3c4d ", "agent-vm rm old-name-1a2b3c4d ", 1)
91 // code-opencode is new.
92 s = strings.Replace(s, "(or code-opencode, code-codex", "(or code-codex", 1)
93 for _, cut := range []string{
94 "AGENT_VM_BIN_DIR). Run it from the clone:\n ./agent-vm.sh install\n",
95 "AGENT_VM_BIN_DIR)\n",
96 " ~/.agent-vm/network What the VMs may reach (above)\n",
97 " ~/.agent-vm/guarded More files to ask about (above)\n",
98 " licenses Print the licenses of agent-vm and of what is built into it\n",
99 } {
100 s = strings.Replace(s, cut, "", 1)
101 }
102 // Network isolation and guarded files are new.
103 if before, rest, ok := strings.Cut(s, "The VMs reach the internet"); ok {
104 _, after, _ := strings.Cut(rest, "Both files are read when a VM starts.\n\n")
105 s = before + after
106 }
107 before, _, _ := strings.Cut(s, "Every .git in the shared folders")
108 _, after, _ := strings.Cut(s, "A VM that already runs gets the warnings only")
109 return before + after
110 }
111 if strip(helpText) != strip(want) {
112 t.Error("the help differs from 0.2's beyond the Lima paragraphs and install")
113 }
114 if strings.Contains(helpText, "Lima build") || strings.Contains(helpText, "agent-vm.sh") {
115 t.Error("the help still offers a Lima build or the clone")
116 }
117}
118
119// A copy a package manager installed is updated by it: the installer's
120// would land ahead of it on the PATH.
121func TestUpdateCommand(t *testing.T) {
122 for exe, want := range map[string]string{
123 "/nix/store/abc-agent-vm-0.3.1/bin/agent-vm": "nix profile upgrade",
124 "/opt/homebrew/Cellar/agent-vm/0.3.1/bin/agent-vm": "brew upgrade agent-vm",
125 "/usr/bin/agent-vm": ".deb or .rpm",
126 "/home/u/.local/share/agent-vm/0.3.1/agent-vm": "install.sh | sh",
127 "/usr/local/bin/agent-vm": "install.sh | sh",
128 "": "install.sh | sh",
129 } {
130 if got := updateCommandFor(exe); !strings.Contains(got, want) {
131 t.Errorf("%q: %q", exe, got)
132 }
133 }
134}
135
136func TestSplit(t *testing.T) {
137 for _, c := range []struct {
138 in []string
139 cmd []string
140 tty bool
141 opts VMOpts
142 }{
143 {[]string{"docker", "run", "--rm", "x"}, []string{"docker", "run", "--rm", "x"}, false, newVMOpts()},
144 {[]string{"--rm", "--tty", "htop"}, []string{"htop"}, true, VMOpts{RM: true, SSHPort: -1}},
145 {[]string{"--disk=5", "--", "--rm"}, []string{"--rm"}, false, VMOpts{Disk: 5, SSHPort: -1}},
146 {[]string{"--ssh-port", "0", "--ram", "4", "x"}, []string{"x"}, false, VMOpts{Memory: 4, SSHPort: 0}},
147 {[]string{"--readonly", "--scratch", "--reset", "--unsafe-writable-git", "--unsafe-disable-security-prompts"}, []string{}, false,
148 VMOpts{ReadOnly: true, Scratch: true, Reset: true, UnsafeWritableGit: true, UnsafeNoPrompts: true, SSHPort: -1}},
149 } {
150 opts := newVMOpts()
151 cmd, tty, err := opts.split(c.in)
152 if err != nil || strings.Join(cmd, " ") != strings.Join(c.cmd, " ") || tty != c.tty || opts != c.opts {
153 t.Errorf("split(%q) = %q, %v, %+v, %v", c.in, cmd, tty, opts, err)
154 }
155 }
156}