| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/sylvinus/agent-vm/internal/bashref" |
| 10 | "github.com/sylvinus/agent-vm/internal/paths" |
| 11 | "github.com/sylvinus/agent-vm/internal/vm" |
| 12 | "github.com/sylvinus/agent-vm/internal/vmname" |
| 13 | ) |
| 14 | |
| 15 | func TestCodePortsHostBash(t *testing.T) { |
| 16 | for _, n := range []string{"proj-0000270f", "proj-ffffffff", "proj-00000000", "noHash", strings.Repeat("Long", 20) + "-abcdef12"} { |
| 17 | var ps []string |
| 18 | for _, p := range codePorts(n) { |
| 19 | ps = append(ps, strconv.Itoa(p)) |
| 20 | } |
| 21 | r := bashref.Run(t, "", nil, "_agent_vm_code_ports", n) |
| 22 | if strings.Join(ps, " ")+" " != r.Stdout { |
| 23 | t.Errorf("codePorts(%q) = %v, bash %q", n, ps, r.Stdout) |
| 24 | } |
| 25 | r = bashref.Run(t, "", nil, "_agent_vm_code_host", n) |
| 26 | if codeHost(n)+"\n" != r.Stdout { |
| 27 | t.Errorf("codeHost(%q) = %q, bash %q", n, codeHost(n), r.Stdout) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestCode(t *testing.T) { |
| 33 | se := newStartEnv(t) |
| 34 | se.base() |
| 35 | name := vmname.Name(se.proj) |
| 36 | answer := "missing\n" |
| 37 | var started []string |
| 38 | se.fake.ShellFunc = func(n string, o vm.ShellOpts) int { |
| 39 | if len(o.Args) > 2 && o.Args[2] == codePrep { |
| 40 | fmt.Fprint(o.Stdout, answer) |
| 41 | return 0 |
| 42 | } |
| 43 | if strings.Contains(strings.Join(o.Args, " "), "code-server --config") { |
| 44 | started = o.Args |
| 45 | return 0 |
| 46 | } |
| 47 | return se.g.shell(n, o) |
| 48 | } |
| 49 | if se.run("code") != 1 || !strings.Contains(se.out(), "code-server is not installed in VM") { |
| 50 | t.Errorf("missing: %s", se.out()) |
| 51 | } |
| 52 | ports := codePorts(name) |
| 53 | answer = fmt.Sprintf("config=/home/u/.config/code-server/agent-vm-h.yaml\npassword=s3cret\nlistening=%d\n", ports[0]) |
| 54 | if se.run("code") != 0 { |
| 55 | t.Fatalf("code: %s", se.out()) |
| 56 | } |
| 57 | want := fmt.Sprintf("Address: http://%s:%d/", codeHost(name), ports[1]) |
| 58 | if !strings.Contains(se.out(), want) || !strings.Contains(se.out(), "Password: s3cret") { |
| 59 | t.Errorf("box: %s", se.out()) |
| 60 | } |
| 61 | args := strings.Join(started, " ") |
| 62 | for _, w := range []string{"--bind-addr 127.0.0.1:" + strconv.Itoa(ports[1]), "--cookie-suffix " + name, "--app-name " + name, "--disable-proxy", |
| 63 | "--config /home/u/.config/code-server/agent-vm-h.yaml", "--disable-telemetry", "--disable-update-check", "--disable-workspace-trust", |
| 64 | "--disable-getting-started-override", "--link-protection-trusted-domains https://claude.com/cai/oauth", |
| 65 | "--link-protection-trusted-domains https://platform.claude.com/oauth", "--vscode-option disable-experiments", |
| 66 | "--vscode-option disable-extension=GitHub.copilot-chat", "VSCODE_PROXY_URI= code-server", paths.Guest(se.proj)} { |
| 67 | if !strings.Contains(args, w) { |
| 68 | t.Errorf("missing %q in %s", w, args) |
| 69 | } |
| 70 | } |
| 71 | answer = "config=/c.yaml\npassword=x\nrunning=23456\n" |
| 72 | started = nil |
| 73 | if se.run("code") != 0 || !strings.Contains(se.out(), "already runs, from another") || !strings.Contains(se.out(), ":23456/") || started != nil { |
| 74 | t.Errorf("running: %s", se.out()) |
| 75 | } |
| 76 | answer = "config=/c.yaml\npassword=a\033]0;x\007\n" |
| 77 | if se.run("code") != 1 || !strings.Contains(se.out(), "could not read the editor's password") { |
| 78 | t.Errorf("control characters: %s", se.out()) |
| 79 | } |
| 80 | if se.run("code", "x") != 1 || !strings.Contains(se.out(), "unknown argument for code: x") { |
| 81 | t.Errorf("argument: %s", se.out()) |
| 82 | } |
| 83 | } |