| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "slices" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/google/uuid" |
| 15 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | func TestWindowsMobileNativePairingNoPromptE2E(t *testing.T) { |
| 22 | profile := os.Getenv("CAGE_WINDOWS_GUEST_QA_PROFILE") |
| 23 | if runtime.GOOS != "windows" || profile == "" || os.Getenv("CAGE_TEST_WINDOWS_MOBILE_NATIVE") != "1" { |
| 24 | t.Skip("requires an explicitly paired isolated native Windows guest") |
| 25 | } |
| 26 | binding, err := guestos.LoadWindowsProfile(profile) |
| 27 | if err != nil || binding == nil { |
| 28 | t.Fatalf("paired Windows QA profile: %v", err) |
| 29 | } |
| 30 | s := testServer(t) |
| 31 | if err := guestos.BindWindowsProfile(s.profile, *binding); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | s.windowsGuest = binding |
| 35 | s.ready = true |
| 36 | s.preferences = Preferences{Keyboard: "native", ClaudeTerminalCommand: "claude"} |
| 37 | s.lifetimeContext() |
| 38 | defer s.lifeCancel() |
| 39 | defer s.terminals.close() |
| 40 | defer s.closeProjectShares() |
| 41 | ctx, cancel := context.WithTimeout(s.lifetimeContext(), 20*time.Second) |
| 42 | defer cancel() |
| 43 | state, err := s.windowsStatus(ctx) |
| 44 | if err != nil || !slices.Contains(state.Capabilities, "cli") { |
| 45 | t.Fatalf("actual GUI/native CLI unavailable: %v", err) |
| 46 | } |
| 47 | for { |
| 48 | ready := state.Available |
| 49 | for _, name := range []string{"cli", "terminal", "project-mount"} { |
| 50 | component := state.Components[name] |
| 51 | ready = ready && component.Installed && component.Ready |
| 52 | } |
| 53 | if ready { |
| 54 | break |
| 55 | } |
| 56 | select { |
| 57 | case <-ctx.Done(): |
| 58 | t.Fatal("actual GUI native CLI/terminal/project components never became ready") |
| 59 | case <-time.After(time.Second): |
| 60 | } |
| 61 | fresh, err := readWindowsComponentStatus(ctx, s.profile) |
| 62 | if err != nil || fresh.Version != 1 || fresh.OS != "windows" || fresh.VMID != binding.VMID.String() || fresh.SID != binding.GUIUserSID || fresh.Session == "" { |
| 63 | t.Fatalf("actual native component identity/readiness: %v", err) |
| 64 | } |
| 65 | fresh.Running = state.Running |
| 66 | state = fresh |
| 67 | } |
| 68 | s.windowsState = state |
| 69 | workspace, err := filepath.EvalSymlinks(t.TempDir()) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if err := writePrivate(filepath.Join(s.profile, "environment.json"), map[string]string{"directory": workspace}); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | client, phoneCtx := pairedWindowsPhoneForTest(t, s) |
| 77 | creation := uuid.NewString() |
| 78 | encoded, _ := json.Marshal(map[string]any{"id": creation, "workspace": workspace}) |
| 79 | reply, err := client.Do(phoneCtx, http.MethodPost, "/api/mobile/cli/sessions", encoded) |
| 80 | if err != nil || reply.Status != http.StatusOK { |
| 81 | t.Fatalf("real phone/native ConPTY launch: %v %#v", err, reply) |
| 82 | } |
| 83 | var session mobileSession |
| 84 | if json.Unmarshal(reply.Body, &session) != nil || session.ID != creation || session.DesktopTerminalID == "" { |
| 85 | t.Fatal("phone did not receive the same desktop terminal") |
| 86 | } |
| 87 | phoneView, err := client.Do(phoneCtx, http.MethodGet, "/api/mobile/cli/sessions/"+creation, nil) |
| 88 | if err != nil || phoneView.Status != http.StatusOK { |
| 89 | t.Fatalf("native phone view: %v %#v", err, phoneView) |
| 90 | } |
| 91 | desktopView := windowsBrowserRequestForTest(s, http.MethodGet, "/api/cli/sessions/"+creation, nil) |
| 92 | var phone, desktop mobileSession |
| 93 | if desktopView.Code != http.StatusOK || json.Unmarshal(phoneView.Body, &phone) != nil || json.Unmarshal(desktopView.Body.Bytes(), &desktop) != nil || phone.ID != desktop.ID || phone.DesktopTerminalID != desktop.DesktopTerminalID { |
| 94 | t.Fatal("phone/desktop native execution identity differs") |
| 95 | } |
| 96 | t.Logf("actual shared execution opened; model-send readiness=%v (no prompt or login sent)", phone.CanSend != nil && *phone.CanSend) |
| 97 | |
| 98 | s.closeProjectShares() |
| 99 | denied, err := client.Do(phoneCtx, http.MethodGet, "/api/mobile/cli/sessions/"+creation, nil) |
| 100 | if err == nil && denied.Status == http.StatusOK { |
| 101 | t.Fatal("native revoked project remained readable") |
| 102 | } |
| 103 | } |