| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | agentvm "github.com/sylvinus/agent-vm" |
| 13 | "github.com/sylvinus/agent-vm/internal/host" |
| 14 | "github.com/sylvinus/agent-vm/internal/state" |
| 15 | "github.com/sylvinus/agent-vm/internal/vm" |
| 16 | "github.com/sylvinus/agent-vm/internal/vm/vmtest" |
| 17 | "github.com/sylvinus/agent-vm/internal/vmname" |
| 18 | ) |
| 19 | |
| 20 | |
| 21 | |
| 22 | func setupRun(t *testing.T, args ...string) (*testEnv, int, []string) { |
| 23 | t.Helper() |
| 24 | te := newTestEnv(t, vmtest.New()) |
| 25 | host.CPUs = func() int { return 64 } |
| 26 | host.MemGiB = func() int { return 256 } |
| 27 | var stdin []string |
| 28 | te.fake.ShellFunc = func(name string, o vm.ShellOpts) int { |
| 29 | b, _ := io.ReadAll(o.Stdin) |
| 30 | stdin = append(stdin, string(b)) |
| 31 | return 0 |
| 32 | } |
| 33 | code := te.setupBase(append([]string{"setup"}, args...)) |
| 34 | return te, code, stdin |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | func (te *testEnv) setupBase(args []string) int { |
| 40 | te.stdout.Reset() |
| 41 | te.stderr.Reset() |
| 42 | te.fake.Calls = nil |
| 43 | saved := skipPrereqs |
| 44 | skipPrereqs = true |
| 45 | defer func() { skipPrereqs = saved }() |
| 46 | return te.main(context.Background(), args) |
| 47 | } |
| 48 | |
| 49 | func exportsOf(t *testing.T, stdin []string) map[string]string { |
| 50 | t.Helper() |
| 51 | if len(stdin) == 0 || !strings.HasSuffix(stdin[0], agentvm.SetupScript) { |
| 52 | t.Fatalf("the setup script was not run: %q", stdin) |
| 53 | } |
| 54 | m := map[string]string{} |
| 55 | for _, l := range strings.Split(strings.TrimSuffix(stdin[0], agentvm.SetupScript), "\n") { |
| 56 | if k, v, ok := strings.Cut(strings.TrimPrefix(l, "export AGENT_VM_INSTALL_"), "="); ok { |
| 57 | m[k] = v |
| 58 | } |
| 59 | } |
| 60 | return m |
| 61 | } |
| 62 | |
| 63 | func on(m map[string]string) string { |
| 64 | var out []string |
| 65 | for _, n := range software { |
| 66 | if m[strings.ToUpper(strings.ReplaceAll(n, "-", "_"))] == "1" { |
| 67 | out = append(out, n) |
| 68 | } |
| 69 | } |
| 70 | return strings.Join(out, ",") |
| 71 | } |
| 72 | |
| 73 | func TestSetupPreinstall(t *testing.T) { |
| 74 | for _, c := range []struct { |
| 75 | args []string |
| 76 | want string |
| 77 | }{ |
| 78 | {[]string{"--preinstall=default"}, "python,node,docker,chromium,gh,claude,opencode,codex,vibe,mcp-chrome"}, |
| 79 | {[]string{"--preinstall", "--disk", "20"}, "python,node,docker,chromium,gh,claude,opencode,codex,vibe,mcp-chrome"}, |
| 80 | {[]string{"--preinstall=none"}, ""}, |
| 81 | {[]string{"--preinstall=all"}, strings.Join(software, ",")}, |
| 82 | {[]string{"--preinstall=python, docker,claude"}, "python,docker,claude"}, |
| 83 | {[]string{"--preinstall=code-opencode"}, "opencode,code-server,code-opencode"}, |
| 84 | {[]string{"--preinstall=code-vibe"}, "code-server,code-vibe"}, |
| 85 | |
| 86 | {[]string{"--preinstall=codex"}, "node,codex"}, |
| 87 | } { |
| 88 | te, code, stdin := setupRun(t, c.args...) |
| 89 | if code != 0 { |
| 90 | t.Fatalf("%q: %d %s", c.args, code, te.out()) |
| 91 | } |
| 92 | if got := on(exportsOf(t, stdin)); got != c.want { |
| 93 | t.Errorf("%q: %s, want %s", c.args, got, c.want) |
| 94 | } |
| 95 | if c.want == "node,codex" && !strings.Contains(te.out(), "Enabling Node.js because Codex CLI requires npm.") { |
| 96 | t.Errorf("node not said: %s", te.out()) |
| 97 | } |
| 98 | } |
| 99 | te, code, _ := setupRun(t, "--preinstall=Claude") |
| 100 | if code != exitUsage || !strings.Contains(te.out(), "Unknown preinstall name: Claude (names are lowercase)") { |
| 101 | t.Errorf("unknown name: %s", te.out()) |
| 102 | } |
| 103 | te, code, _ = setupRun(t, "--reset") |
| 104 | if code != exitUsage || !strings.Contains(te.out(), "Unknown option: --reset") { |
| 105 | t.Errorf("--reset: %s", te.out()) |
| 106 | } |
| 107 | for _, args := range [][]string{{"--rm"}, {"--scratch"}, {"--ssh-port=2222"}, {"--disk", "10G"}, {"--disk"}, {"--unsafe-writable-git"}, {"--unsafe-disable-security-prompts"}} { |
| 108 | if te, code, _ = setupRun(t, args...); code != exitUsage || te.fake.CallLog() != "" { |
| 109 | t.Errorf("%q: %d %s", args, code, te.out()) |
| 110 | } |
| 111 | } |
| 112 | te, code, _ = setupRun(t, "--help") |
| 113 | if code != 0 || te.stdout.String() != setupHelp { |
| 114 | t.Error("--help") |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | func wizardRun(t *testing.T, answers ...string) (*testEnv, []string) { |
| 120 | t.Helper() |
| 121 | te := newTestEnv(t, vmtest.New(), answers...) |
| 122 | host.CPUs = func() int { return 64 } |
| 123 | host.MemGiB = func() int { return 256 } |
| 124 | var stdin []string |
| 125 | te.fake.ShellFunc = func(name string, o vm.ShellOpts) int { |
| 126 | b, _ := io.ReadAll(o.Stdin) |
| 127 | stdin = append(stdin, string(b)) |
| 128 | return 0 |
| 129 | } |
| 130 | if code := te.setupBase([]string{"setup"}); code != 0 { |
| 131 | t.Fatalf("%d %s", code, te.out()) |
| 132 | } |
| 133 | return te, stdin |
| 134 | } |
| 135 | |
| 136 | func TestSetupWizard(t *testing.T) { |
| 137 | |
| 138 | te, stdin := wizardRun(t) |
| 139 | if got := on(exportsOf(t, stdin)); got != strings.Join(defaultSoftware, ",") { |
| 140 | t.Errorf("default: %s", got) |
| 141 | } |
| 142 | if b := te.fake.VMs[vmname.Template]; b.CPUs != 1 || b.Memory != 3<<30 || b.Disk != 10<<30 { |
| 143 | t.Errorf("default resources: %+v", b) |
| 144 | } |
| 145 | if !strings.Contains(te.out(), " Resources: CPUs: 1, Memory: 3 GiB, Disk: 10 GiB\n") { |
| 146 | t.Errorf("resources not shown: %s", te.out()) |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | te, stdin = wizardRun(t, |
| 152 | "n", |
| 153 | "", "n", "y", "n", "", |
| 154 | "y", "2", |
| 155 | "n", "y", "n", "y", "", |
| 156 | "n", "y", "", "", |
| 157 | "n", "15", "", "3") |
| 158 | if got := on(exportsOf(t, stdin)); got != "node,ruby,chromium,code-server,code-claude,code-codex,mcp-chrome" { |
| 159 | t.Errorf("picked: %s", got) |
| 160 | } |
| 161 | if b := te.fake.VMs[vmname.Template]; b.CPUs != 3 || b.Memory != 4<<30 || b.Disk != 15<<30 { |
| 162 | t.Errorf("resources: %+v", b) |
| 163 | } |
| 164 | if !strings.Contains(te.out(), "Node.js 24: yes (Chrome DevTools MCP uses npx)") || !strings.Contains(te.out(), " agent-vm code\n") || |
| 165 | strings.Contains(te.out(), " agent-vm claude\n") { |
| 166 | t.Errorf("output: %s", te.out()) |
| 167 | } |
| 168 | |
| 169 | |
| 170 | te, stdin = wizardRun(t, |
| 171 | "n", |
| 172 | "n", "y", "n", "n", "", |
| 173 | "y", "2", |
| 174 | "n", "n", "n", |
| 175 | "n", "n", "", "", "", |
| 176 | "", |
| 177 | ) |
| 178 | if got := on(exportsOf(t, stdin)); got != "opencode,code-server,code-opencode" { |
| 179 | t.Errorf("opencode extension: %s", got) |
| 180 | } |
| 181 | if !strings.Contains(te.out(), "OpenCode's extension needs its command line: it stays installed.") { |
| 182 | t.Errorf("note not shown: %s", te.out()) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | func TestSetupWizardMemory(t *testing.T) { |
| 189 | te := newTestEnv(t, vmtest.New(), "n", "", "", "", "", "", "y") |
| 190 | host.CPUs = func() int { return 64 } |
| 191 | host.MemGiB = func() int { return 256 } |
| 192 | if te.setupBase([]string{"setup", "--memory", "2"}) != 0 { |
| 193 | t.Fatal(te.out()) |
| 194 | } |
| 195 | if b := te.fake.VMs[vmname.Template]; b.Memory != 2<<30 || !strings.Contains(te.out(), "code-server (VS Code") { |
| 196 | t.Errorf("--memory with code-server: %+v %s", b, te.out()) |
| 197 | } |
| 198 | |
| 199 | te, code, stdin := setupRun(t) |
| 200 | if code != 0 || strings.Contains(te.out(), "setup wizard") || on(exportsOf(t, stdin)) != strings.Join(defaultSoftware, ",") { |
| 201 | t.Errorf("no terminal: %d %s", code, te.out()) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func TestSetupFailures(t *testing.T) { |
| 206 | for _, c := range []struct{ op, want string }{ |
| 207 | {"create", "Error: Failed to create base VM. Full log: "}, |
| 208 | {"start", "Error: Failed to start base VM. Full log: "}, |
| 209 | {"shell", "Error: Setup script failed. Full log: "}, |
| 210 | } { |
| 211 | te := newTestEnv(t, vmtest.New()) |
| 212 | te.fake.FailOn = map[string]error{c.op: errors.New("boom")} |
| 213 | if te.setupBase([]string{"setup", "--preinstall=none"}) != 1 || !strings.Contains(te.out(), c.want+te.state.Path("setup.log")) { |
| 214 | t.Errorf("%s: %s", c.op, te.out()) |
| 215 | } |
| 216 | if log, _ := os.ReadFile(te.state.Path("setup.log")); !strings.Contains(string(log), "boom") { |
| 217 | t.Errorf("%s: log %q", c.op, log) |
| 218 | } |
| 219 | if _, err := os.Stat(te.state.Path(state.BaseVersion)); err == nil { |
| 220 | t.Errorf("%s: ready marker", c.op) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | te := newTestEnv(t, vmtest.New()) |
| 226 | te.fake.Sticky[building] = true |
| 227 | if te.setupBase([]string{"setup", "--preinstall=none"}) != 1 || !strings.Contains(te.out(), "the new base VM is set up but did not stop") { |
| 228 | t.Errorf("running base: %s", te.out()) |
| 229 | } |
| 230 | if _, err := os.Stat(te.state.Path(state.BaseVersion)); err == nil { |
| 231 | t.Error("a running base was marked ready") |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | |
| 236 | func TestSetupExistingNote(t *testing.T) { |
| 237 | const note = "Existing VMs were not updated" |
| 238 | te, _, _ := setupRun(t, "--preinstall=none") |
| 239 | if strings.Contains(te.out(), note) { |
| 240 | t.Errorf("no VM, but: %s", te.out()) |
| 241 | } |
| 242 | te.fake.VMs["proj-deadbeef"] = &vm.Instance{Name: "proj-deadbeef", Status: vm.Stopped} |
| 243 | if te.setupBase([]string{"setup", "--preinstall=none"}) != 0 || !strings.Contains(te.out(), note) { |
| 244 | t.Errorf("with a VM: %s", te.out()) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | func TestSetupBase(t *testing.T) { |
| 249 | te, code, stdin := setupRun(t, "--preinstall=none", "--disk", "12", "--memory=5", "--cpus", "2") |
| 250 | if code != 0 { |
| 251 | t.Fatalf("%d %s", code, te.out()) |
| 252 | } |
| 253 | base := te.fake.VMs[vmname.Template] |
| 254 | if base == nil || base.Status != vm.Stopped || base.CPUs != 2 || base.Memory != 5<<30 || base.Disk != 12<<30 || len(base.Mounts) != 0 { |
| 255 | t.Fatalf("base: %+v", base) |
| 256 | } |
| 257 | if !strings.HasPrefix(te.fake.CallLog(), "delete base-building\ncreate base-building template:debian-13\nstart base-building\nshell base-building bash -l\nstop base-building\ndelete base\nclone base-building base\ndelete base-building") { |
| 258 | t.Errorf("calls:\n%s", te.fake.CallLog()) |
| 259 | } |
| 260 | if len(stdin) != 1 { |
| 261 | t.Errorf("guest stdin: %q", stdin) |
| 262 | } |
| 263 | if te.state.Read(state.BaseBuiltBy) == "" || te.state.Read(state.BaseVersion) == "" { |
| 264 | t.Error("markers not written") |
| 265 | } |
| 266 | if !strings.Contains(te.out(), "Base VM ready.") || strings.Contains(te.out(), "agent-vm claude") { |
| 267 | t.Errorf("output: %s", te.out()) |
| 268 | } |
| 269 | |
| 270 | |
| 271 | os.WriteFile(te.state.Path("setup.sh"), []byte("echo x\r\n"), 0o644) |
| 272 | var got []string |
| 273 | te.fake.ShellFunc = func(name string, o vm.ShellOpts) int { |
| 274 | b, _ := io.ReadAll(o.Stdin) |
| 275 | got = append(got, strings.Join(o.Args, " ")+": "+string(b)) |
| 276 | if o.Args[0] == "zsh" { |
| 277 | return 1 |
| 278 | } |
| 279 | return 0 |
| 280 | } |
| 281 | if te.setupBase([]string{"setup", "--preinstall=none"}) != 1 || !strings.Contains(te.out(), "Custom setup script failed") { |
| 282 | t.Errorf("failing setup.sh: %s", te.out()) |
| 283 | } |
| 284 | if len(got) != 2 || got[1] != "zsh -l: echo x\n" { |
| 285 | t.Errorf("setup.sh: %q", got) |
| 286 | } |
| 287 | |
| 288 | if _, err := os.Stat(te.state.Path(state.BaseVersion)); err != nil || te.fake.VMs[vmname.Template] == nil { |
| 289 | t.Error("a failed setup took the base from before") |
| 290 | } |
| 291 | |
| 292 | cfg := filepath.Join(LimaHome(te.state), "_config") |
| 293 | os.MkdirAll(cfg, 0o755) |
| 294 | os.WriteFile(filepath.Join(cfg, "override.yaml"), []byte("mounts: []\n"), 0o644) |
| 295 | te.fake.Calls = nil |
| 296 | if te.setupBase([]string{"setup", "--preinstall=none"}) != 1 || !strings.Contains(te.out(), "override.yaml adds to every VM's config") || te.fake.CallLog() != "" { |
| 297 | t.Errorf("override.yaml: %s\n%s", te.out(), te.fake.CallLog()) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | func TestCageCustomSetupUsesBashRegardlessOfGuestLoginShell(t *testing.T) { |
| 302 | te, code, _ := setupRun(t, "--preinstall=none") |
| 303 | if code != 0 { |
| 304 | t.Fatal(te.out()) |
| 305 | } |
| 306 | t.Setenv("CLAUDE_CAGE_CONFIG", filepath.Join(t.TempDir(), "config.json")) |
| 307 | if err := os.WriteFile(te.state.Path("setup.sh"), []byte("set -euo pipefail\nlocal_marker=ready\n"), 0600); err != nil { |
| 308 | t.Fatal(err) |
| 309 | } |
| 310 | var commands []string |
| 311 | te.fake.ShellFunc = func(_ string, options vm.ShellOpts) int { |
| 312 | _, _ = io.Copy(io.Discard, options.Stdin) |
| 313 | commands = append(commands, strings.Join(options.Args, " ")) |
| 314 | return 0 |
| 315 | } |
| 316 | if result := te.setupBase([]string{"setup", "--preinstall=none"}); result != 0 { |
| 317 | t.Fatalf("%d: %s", result, te.out()) |
| 318 | } |
| 319 | if len(commands) != 2 || commands[1] != "bash -l" { |
| 320 | t.Fatalf("Cage script ran in the guest login shell: %v", commands) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | func TestCageSetupResumesExistingBuild(t *testing.T) { |
| 325 | t.Setenv("CLAUDE_CAGE_CONFIG", filepath.Join(t.TempDir(), "config.json")) |
| 326 | te := newTestEnv(t, vmtest.New()) |
| 327 | te.fake.VMs[building] = &vm.Instance{Name: building, Status: vm.Stopped, CPUs: 2, Memory: 4 << 30, Disk: 24 << 30} |
| 328 | if code := te.setupBase([]string{"setup", "--preinstall=none"}); code != 0 { |
| 329 | t.Fatalf("resume failed: %s", te.out()) |
| 330 | } |
| 331 | log := te.fake.CallLog() |
| 332 | if strings.Contains(log, "create base-building") || !strings.Contains(log, "clone base-building base") { |
| 333 | t.Fatalf("partial build was recreated instead of resumed: %s", log) |
| 334 | } |
| 335 | } |