跳到主要内容

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

浏览文件

migrate_test.go

129 行 · 4.5 KB历史下载
1package cli
2
3import (
4 "context"
5 "io"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "github.com/sylvinus/agent-vm/internal/state"
12 "github.com/sylvinus/agent-vm/internal/vm/vmtest"
13)
14
15// agent-vm takes every VM in its Lima home as its own (list, rm,
16// destroy-all): never the user's own Lima home, however spelled.
17func TestOwnLimaHome(t *testing.T) {
18 home := t.TempDir()
19 t.Setenv("HOME", home)
20 // Go reads the home from USERPROFILE on Windows, not HOME.
21 t.Setenv("USERPROFILE", home)
22 lima := t.TempDir()
23 t.Setenv("LIMA_HOME", lima)
24 t.Setenv("AGENT_VM_LIMA_HOME", lima+"/")
25 e, err := newApp(IO{Stdin: strings.NewReader(""), Stdout: io.Discard, Stderr: io.Discard})
26 if err != nil {
27 t.Fatal(err)
28 }
29 if _, err := e.backend(); err == nil || !strings.Contains(err.Error(), "your own Lima's home") {
30 t.Errorf("own home: %v", err)
31 }
32 // The LIMA_HOME agent-vm sets for Lima is not the user's: a second
33 // command in the same process (setup, then a start) is not refused.
34 os.Unsetenv("LIMA_HOME")
35 t.Setenv("AGENT_VM_LIMA_HOME", "")
36 for i := range 2 {
37 e, _ := newApp(IO{Stdin: strings.NewReader(""), Stdout: io.Discard, Stderr: io.Discard})
38 if _, err := e.backend(); err != nil {
39 t.Errorf("command %d: %v", i+1, err)
40 }
41 }
42}
43
44// doctor, info and list only look: they never move 0.2's VMs, which stops
45// running ones (an editor calls info unasked); a command that changes VMs
46// does.
47func TestMigrateNotFromReaders(t *testing.T) {
48 home := t.TempDir()
49 t.Setenv("HOME", home)
50 // Go reads the home from USERPROFILE on Windows, not HOME.
51 t.Setenv("USERPROFILE", home)
52 for _, args := range [][]string{{"doctor"}, {"info"}, {"list"}, {"stop", "x"}} {
53 te := newTestEnv(t, vmtest.New())
54 moved := false
55 te.migrate = func() { moved = true }
56 te.run(args...)
57 if want := args[0] == "stop"; moved != want {
58 t.Errorf("%s: moved %v", args[0], moved)
59 }
60 }
61}
62
63func TestMigrateOld(t *testing.T) {
64 old := t.TempDir()
65 now := filepath.Join(t.TempDir(), "lima")
66 // Already at the new place, under its new name: the move fails, and says
67 // so.
68 os.MkdirAll(filepath.Join(now, "p-12345678"), 0o755)
69 os.MkdirAll(filepath.Join(old, "agent-vm-p-12345678"), 0o755)
70 os.WriteFile(filepath.Join(old, "agent-vm-p-12345678", "lima.yaml"), []byte("{"), 0o644)
71 os.MkdirAll(filepath.Join(old, "theirs"), 0o755)
72 os.WriteFile(filepath.Join(old, "theirs", "lima.yaml"), []byte("{"), 0o644)
73
74 te := newTestEnv(t, vmtest.New(), "n\n")
75 os.MkdirAll(string(te.state), 0o755)
76 os.WriteFile(te.state.Marker(state.BuiltByOf, "agent-vm-p-12345678"), []byte("0.2.1\n"), 0o644)
77 te.migrateOld(context.Background(), old, now)
78 if out := te.out(); !strings.Contains(out, " agent-vm-p-12345678, as p-12345678\n") || strings.Contains(out, "theirs") || !strings.Contains(out, "Not moved") {
79 t.Errorf("declined:\n%s", out)
80 }
81 if _, err := os.Stat(te.state.Path(migratedMarker)); err == nil {
82 t.Error("declined, and marked as done")
83 }
84
85 st := te.state
86 te = newTestEnv(t, te.fake)
87 te.state = st
88 te.migrateOld(context.Background(), old, now)
89 if out := te.out(); !strings.Contains(out, "Error: could not move agent-vm-p-12345678") {
90 t.Errorf("no terminal, failed move:\n%s", out)
91 }
92 if _, err := os.Stat(te.state.Path(migratedMarker)); err == nil {
93 t.Error("a failed move was marked as done")
94 }
95
96 // Without a terminal it goes ahead; Lima reads a broken config as a
97 // broken VM, which moves all the same. Then it is done, once.
98 os.RemoveAll(filepath.Join(now, "p-12345678"))
99 te.stderr.Reset()
100 te.migrateOld(context.Background(), old, now)
101 if out := te.out(); !strings.HasSuffix(out, "Done: they keep their shares and settings, under their new names.\n") {
102 t.Errorf("no terminal:\n%s", out)
103 }
104 if _, err := os.Stat(filepath.Join(now, "p-12345678", "lima.yaml")); err != nil {
105 t.Error("not moved")
106 }
107 if te.state.ReadMarker(state.BuiltByOf, "p-12345678") != "0.2.1" {
108 t.Error("its markers did not follow")
109 }
110 if _, err := os.Stat(filepath.Join(old, "theirs", "lima.yaml")); err != nil {
111 t.Error("another VM was moved")
112 }
113 if _, err := os.Stat(te.state.Path(migratedMarker)); err != nil {
114 t.Error("not marked as done")
115 }
116 os.MkdirAll(filepath.Join(old, "agent-vm-q-12345678"), 0o755)
117 os.WriteFile(filepath.Join(old, "agent-vm-q-12345678", "lima.yaml"), []byte("{"), 0o644)
118 te.stderr.Reset()
119 te.migrateOld(context.Background(), old, now)
120 if te.out() != "" {
121 t.Errorf("after the marker: %q", te.out())
122 }
123 // Nothing of agent-vm's: nothing said.
124 te = newTestEnv(t, te.fake)
125 te.migrateOld(context.Background(), t.TempDir(), now)
126 if te.out() != "" {
127 t.Errorf("nothing to move: %q", te.out())
128 }
129}