| 1 | |
| 2 | |
| 3 | package workbench |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | |
| 21 | func TestMacOSGuestClaudeCode(t *testing.T) { |
| 22 | profile := os.Getenv("CAGE_TEST_MACOS_CLI_PROFILE") |
| 23 | if profile == "" { |
| 24 | t.Skip("set CAGE_TEST_MACOS_CLI_PROFILE to an explicitly selected test guest") |
| 25 | } |
| 26 | binding, err := guestos.LoadMacOSProfile(profile) |
| 27 | if err != nil || binding == nil { |
| 28 | t.Fatal("test guest binding unavailable") |
| 29 | } |
| 30 | s := testServer(t) |
| 31 | s.macosGuest = binding |
| 32 | for _, file := range []string{"macos-known-hosts", "state/lima/_config/user", "state/lima/mac/ssh.config", "state/lima/mac/cage-running.json"} { |
| 33 | dest := filepath.Join(s.profile, file) |
| 34 | if err := os.MkdirAll(filepath.Dir(dest), 0700); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if err := os.Symlink(filepath.Join(profile, file), dest); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | } |
| 41 | dir, err := filepath.EvalSymlinks(t.TempDir()) |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if err := writePrivate(filepath.Join(s.profile, "environment.json"), map[string]string{"directory": dir}); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | s.preferences.Workspace = dir |
| 49 | s.ready = true |
| 50 | call := func(method, route, body string, result any) { |
| 51 | t.Helper() |
| 52 | req := httptest.NewRequest(method, s.origin+route, strings.NewReader(body)) |
| 53 | req.Header.Set("Content-Type", "application/json") |
| 54 | req.Header.Set("Origin", s.origin) |
| 55 | req.AddCookie(&http.Cookie{Name: s.cookieName(), Value: s.session}) |
| 56 | req.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 57 | resp := httptest.NewRecorder() |
| 58 | s.handler().ServeHTTP(resp, req) |
| 59 | if resp.Code != 200 { |
| 60 | t.Fatalf("%s: %d %s", route, resp.Code, resp.Body.String()) |
| 61 | } |
| 62 | if err := json.Unmarshal(resp.Body.Bytes(), result); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | } |
| 66 | var tab struct { |
| 67 | ID string |
| 68 | Shared string `json:"cli_session_id"` |
| 69 | } |
| 70 | call("POST", "/api/terminals", `{"kind":"claude"}`, &tab) |
| 71 | if tab.ID == "" || !validCLIUUID(tab.Shared) { |
| 72 | t.Fatal("no shared terminal") |
| 73 | } |
| 74 | session := s.terminals.get(tab.ID) |
| 75 | defer func() { |
| 76 | s.terminals.close() |
| 77 | s.activities.Wait() |
| 78 | s.mobileCLI.mu.Lock() |
| 79 | rec, err := s.mobileCLI.record(s.profile, tab.Shared) |
| 80 | s.mobileCLI.mu.Unlock() |
| 81 | if err != nil || rec.Connection != session.connectionID { |
| 82 | t.Error("missing QA session binding") |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | ctx, cancel := context.WithTimeout(context.Background(), 16*time.Second) |
| 87 | defer cancel() |
| 88 | var ended struct{ OK bool } |
| 89 | for !ended.OK && ctx.Err() == nil { |
| 90 | err = s.runCLIChannel(ctx, "purge", map[string]string{"cli_id": rec.CLIID, "config": rec.GuestConfig, "workspace": rec.GuestWorkspace, "account": "default"}, &ended) |
| 91 | if !ended.OK { |
| 92 | time.Sleep(250 * time.Millisecond) |
| 93 | } |
| 94 | } |
| 95 | if !ended.OK { |
| 96 | t.Errorf("could not clean up this QA conversation: %v", err) |
| 97 | } |
| 98 | }() |
| 99 | deadline := time.Now().Add(40 * time.Second) |
| 100 | for { |
| 101 | session.mu.Lock() |
| 102 | output := cliStartupText(session.history) |
| 103 | session.mu.Unlock() |
| 104 | if strings.Contains(output, "UnabletoconnecttoAnthropic") { |
| 105 | t.Fatal("the test guest needs a currently authorized network connection") |
| 106 | } |
| 107 | if strings.Contains(output, "ClaudeCodev") { |
| 108 | break |
| 109 | } |
| 110 | if time.Now().After(deadline) { |
| 111 | t.Fatal("official native CLI did not present its interactive terminal") |
| 112 | } |
| 113 | time.Sleep(150 * time.Millisecond) |
| 114 | } |
| 115 | var index struct { |
| 116 | Sessions []mobileSummary |
| 117 | Notice string |
| 118 | } |
| 119 | call("GET", "/api/cli/sessions", "", &index) |
| 120 | found := false |
| 121 | for _, item := range index.Sessions { |
| 122 | if item.ID == tab.Shared { |
| 123 | found = true |
| 124 | } |
| 125 | } |
| 126 | if !found || index.Notice != "" { |
| 127 | t.Fatalf("CLI catalog did not rediscover the active conversation: %s", index.Notice) |
| 128 | } |
| 129 | var reopened mobileSession |
| 130 | call("POST", "/api/cli/sessions/"+tab.Shared+"/open", `{}`, &reopened) |
| 131 | if reopened.DesktopTerminalID != tab.ID { |
| 132 | t.Fatalf("reconnect created a different terminal") |
| 133 | } |
| 134 | if n := len(s.terminals.snapshot()); n != 1 { |
| 135 | t.Fatalf("reconnect published %d terminals", n) |
| 136 | } |
| 137 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 138 | defer cancel() |
| 139 | read, err := s.readClaudeGuestSnapshot(ctx, "default") |
| 140 | if err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | t.Logf("ordinary macOS account launched the official Claude Code terminal; active session rediscovered and reattached; native account probe succeeded (signed in: %t)", read.LoggedIn) |
| 144 | } |