跳到主要内容

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

浏览文件

info_test.go

109 行 · 3.8 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/paths"
12 "github.com/sylvinus/agent-vm/internal/state"
13 "github.com/sylvinus/agent-vm/internal/vm"
14 "github.com/sylvinus/agent-vm/internal/vm/vmtest"
15 "github.com/sylvinus/agent-vm/internal/vmname"
16)
17
18func infoMap(t *testing.T, te *testEnv, args ...string) map[string]string {
19 t.Helper()
20 if code := te.run(append([]string{"info"}, args...)...); code != 0 {
21 t.Fatalf("info: %d %s", code, te.out())
22 }
23 m := map[string]string{}
24 for _, l := range strings.Split(strings.TrimSuffix(te.stdout.String(), "\n"), "\n") {
25 k, v, ok := strings.Cut(l, "=")
26 if !ok {
27 t.Fatalf("not key=value: %q", l)
28 }
29 m[k] = v
30 }
31 return m
32}
33
34func TestInfo(t *testing.T) {
35 if _, err := exec.LookPath("git"); err != nil {
36 t.Skip("no git")
37 }
38 home := t.TempDir()
39 t.Setenv("HOME", home)
40 // Go reads the home from USERPROFILE on Windows, not HOME.
41 t.Setenv("USERPROFILE", home)
42 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
43 os.Unsetenv("GIT_CONFIG_GLOBAL")
44 os.Unsetenv("XDG_CONFIG_HOME")
45 // On Windows the temporary folder is under LOCALAPPDATA, which no project may be in.
46 t.Setenv("APPDATA", "")
47 t.Setenv("LOCALAPPDATA", "")
48 os.WriteFile(filepath.Join(home, ".gitconfig"), []byte("[safe]\n\tbareRepository = explicit\n"), 0o644)
49 // Host-spelled, as AbsDir spells info's folders.
50 proj := paths.Host(filepath.Join(home, "proj"))
51 os.MkdirAll(proj, 0o755)
52 t.Chdir(proj)
53 t.Setenv("PWD", proj)
54 name := vmname.Name(proj)
55 t.Setenv("AGENT_VM_UNSAFE_WRITABLE_GIT", "")
56
57 te := newTestEnv(t, vmtest.New())
58 m := infoMap(t, te)
59 for k, want := range map[string]string{
60 "template": "base", "dir": proj, "vm_name": name, "project_env": proj + "/.agent-vm.env",
61 "base_exists": "0", "vm_exists": "0", "vm_running": "0", "vm_stale": "unknown", "ssh_host": "lima-" + name,
62 "ssh_config": "unknown", "git_protected": "1", "security_questions": "none", "state_dir": string(te.state),
63 } {
64 if m[k] != want {
65 t.Errorf("no VM: %s=%q, want %q", k, m[k], want)
66 }
67 }
68
69 // The base, usable once its marker is there; the VM running, from an
70 // older base.
71 te.fake.VMs[vmname.Template] = &vm.Instance{Name: vmname.Template, Status: vm.Stopped}
72 te.fake.VMs[name] = &vm.Instance{Name: name, Status: vm.Running, SSHConfig: "/x/ssh.config"}
73 if infoMap(t, te)["base_exists"] != "0" {
74 t.Error("a base without its marker counted")
75 }
76 os.WriteFile(te.state.Path(state.BaseVersion), []byte("2\n"), 0o644)
77 os.WriteFile(te.state.Marker(state.VersionOf, name), []byte("1\n"), 0o644)
78 m = infoMap(t, te)
79 if m["base_exists"] != "1" || m["vm_exists"] != "1" || m["vm_running"] != "1" || m["vm_stale"] != "1" || m["ssh_config"] != "/x/ssh.config" {
80 t.Errorf("with VMs: %v", m)
81 }
82 os.WriteFile(te.state.Marker(state.VersionOf, name), []byte("2\n"), 0o644)
83 if infoMap(t, te)["vm_stale"] != "0" {
84 t.Error("an up to date VM said stale")
85 }
86
87 // What a start would ask.
88 exec.Command("git", "-C", proj, "init", "-q").Run()
89 exec.Command("git", "-C", proj, "config", "core.hooksPath", ".").Run()
90 exec.Command("git", "-C", proj, "config", "core.fsmonitor", "./mon.sh").Run()
91 os.WriteFile(filepath.Join(home, ".gitconfig"), nil, 0o644)
92 if q := infoMap(t, te)["security_questions"]; q != "hooks,git-config,bare-repo" {
93 t.Errorf("questions: %q", q)
94 }
95 t.Setenv("AGENT_VM_UNSAFE_WRITABLE_GIT", "1")
96 if m := infoMap(t, te); m["git_protected"] != "0" || m["security_questions"] != "bare-repo" {
97 t.Errorf("opted out: %v", m)
98 }
99
100 // A backend that cannot be asked: unknown, never a confident 0.
101 te.fake.Err = errors.New("broken")
102 m = infoMap(t, te)
103 if m["base_exists"] != "unknown" || m["vm_exists"] != "unknown" || m["vm_running"] != "unknown" {
104 t.Errorf("failing backend: %v", m)
105 }
106 if te.run("info", "/no/such/dir") != 1 || !strings.Contains(te.out(), "no such directory") {
107 t.Errorf("missing dir: %s", te.out())
108 }
109}