| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 14 | ) |
| 15 | |
| 16 | func TestWindowsGuestCannotFallBackToLinux(t *testing.T) { |
| 17 | s := testServer(t) |
| 18 | s.windowsGuest = &guestos.WindowsProfile{} |
| 19 | if _, err := s.guestControl(context.Background(), "exit 0", ""); err == nil { |
| 20 | t.Fatal("Windows ran Linux control") |
| 21 | } |
| 22 | if cmd := s.sshCommand(context.Background(), "-T"); cmd.Err == nil { |
| 23 | t.Fatal("Windows created Linux SSH command") |
| 24 | } |
| 25 | r := httptest.NewRequest(http.MethodPost, s.origin+"/api/start", strings.NewReader(`{"guest_system":"debian-13"}`)) |
| 26 | r.Header.Set("Origin", s.origin) |
| 27 | r.Header.Set("Content-Type", "application/json") |
| 28 | r.AddCookie(&http.Cookie{Name: s.cookieName(), Value: s.session}) |
| 29 | r.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 30 | response := httptest.NewRecorder() |
| 31 | s.handler().ServeHTTP(response, r) |
| 32 | if response.Code != http.StatusConflict || s.jobs.snapshot().ID != "" { |
| 33 | t.Fatalf("wrong system start: %d", response.Code) |
| 34 | } |
| 35 | for _, path := range []string{"/api/mobile/state"} { |
| 36 | response := request(s, http.MethodGet, path, s.origin, "", true) |
| 37 | if response.Code != http.StatusNotImplemented { |
| 38 | t.Fatalf("%s admitted: %d", path, response.Code) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | func TestWindowsGuestCapabilitiesReportMissingFeatures(t *testing.T) { |
| 43 | s := testServer(t) |
| 44 | s.windowsGuest = &guestos.WindowsProfile{} |
| 45 | s.ready = true |
| 46 | s.windowsState.Available = true |
| 47 | s.windowsState.ComponentsChecked = time.Now() |
| 48 | s.windowsState.Capabilities = []string{"clipboard", "terminal"} |
| 49 | s.windowsState.Components = map[string]windowsComponentState{"clipboard": {Installed: true, Ready: true}, "terminal": {Installed: true, Ready: true}} |
| 50 | w := windowsBrowserRequestForTest(s, http.MethodGet, "/api/status", nil) |
| 51 | var status struct { |
| 52 | System string `json:"guest_system"` |
| 53 | Capabilities struct { |
| 54 | Available map[string]bool `json:"available"` |
| 55 | Unavailable []string `json:"unavailable"` |
| 56 | Network string `json:"network"` |
| 57 | } `json:"guest_capabilities"` |
| 58 | } |
| 59 | if err := json.Unmarshal(w.Body.Bytes(), &status); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if status.System != guestos.Windows11 || !status.Capabilities.Available["terminal"] || status.Capabilities.Network != "offline" { |
| 63 | t.Fatalf("wrong status: %#v", status) |
| 64 | } |
| 65 | missing := map[string]bool{} |
| 66 | for _, name := range status.Capabilities.Unavailable { |
| 67 | missing[name] = true |
| 68 | } |
| 69 | for _, name := range []string{"projects", "browser", "network", "claude-cli", "claude-desktop"} { |
| 70 | if !missing[name] { |
| 71 | t.Fatalf("%s was reported supported", name) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestWindowsBrowserRegistrationRequiresInstalledRuntime(t *testing.T) { |
| 77 | s := testServer(t) |
| 78 | s.ready = true |
| 79 | s.windowsState.Available = true |
| 80 | s.windowsState.ComponentsChecked = time.Now() |
| 81 | s.windowsState.Capabilities = []string{"browser", "project-mount"} |
| 82 | available := s.windowsCapabilitiesLocked()["available"].(map[string]bool) |
| 83 | if available["browser"] || available["editor"] { |
| 84 | t.Fatal("registration alone advertised an installed browser/editor") |
| 85 | } |
| 86 | s.windowsState.Browser = windowsBrowserState{Installed: true, NetworkConfigured: true, EditorInstalled: true} |
| 87 | s.windowsState.Components = map[string]windowsComponentState{"browser": {Installed: true, Ready: true}, "editor": {Installed: true, Ready: true}, "project-mount": {Installed: true, Ready: true}} |
| 88 | available = s.windowsCapabilitiesLocked()["available"].(map[string]bool) |
| 89 | if !available["browser"] || !available["editor"] { |
| 90 | t.Fatal("installed runtime should be startable before its first window opens") |
| 91 | } |
| 92 | if s.windowsCapabilitiesLocked()["browser_running"] != false { |
| 93 | t.Fatal("an unopened browser was reported running") |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestWindowsGateUsesBoundInstanceDirectory(t *testing.T) { |
| 98 | s := testServer(t) |
| 99 | s.windowsGuest = &guestos.WindowsProfile{InstanceDirectory: t.TempDir()} |
| 100 | if s.instanceDir() != s.windowsGuest.InstanceDirectory { |
| 101 | t.Fatal("Windows gate lookup fell back to a Linux instance directory") |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestWindowsSharedStartupAndFavoritesRoutes(t *testing.T) { |
| 106 | s := testServer(t) |
| 107 | s.windowsGuest = &guestos.WindowsProfile{} |
| 108 | for _, path := range []string{"/api/host/git", "/api/favorites"} { |
| 109 | w := windowsBrowserRequestForTest(s, "GET", path, nil) |
| 110 | if w.Code != http.StatusOK { |
| 111 | t.Fatalf("shared Windows desktop GET %s: %d", path, w.Code) |
| 112 | } |
| 113 | if w := request(s, "GET", path, s.origin, "", true); w.Code != http.StatusForbidden { |
| 114 | t.Fatalf("untrusted viewer reached %s: %d", path, w.Code) |
| 115 | } |
| 116 | } |
| 117 | w := windowsBrowserRequestForTest(s, "POST", "/api/host/git", []byte(`{"confirm":false}`)) |
| 118 | if w.Code != http.StatusBadRequest { |
| 119 | t.Fatalf("host Git confirmation: %d", w.Code) |
| 120 | } |
| 121 | w = windowsBrowserRequestForTest(s, "POST", "/api/favorites", []byte(`{"action":"save","title":"Cage","url":"https://example.org/"}`)) |
| 122 | if w.Code != http.StatusOK { |
| 123 | t.Fatalf("save favorite: %d", w.Code) |
| 124 | } |
| 125 | list := windowsBrowserRequestForTest(s, "GET", "/api/favorites", nil) |
| 126 | if !strings.Contains(list.Body.String(), "https://example.org/") { |
| 127 | t.Fatal("Windows workbench did not restore the saved favorite") |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestWindowsColdStartWaitsForAgentButNotWrongIdentityOrLockedUser(t *testing.T) { |
| 132 | for _, result := range []error{nil, errWindowsLoginRequired, errors.New("wrong VM")} { |
| 133 | attempts := 0 |
| 134 | err := waitWindowsBoot(context.Background(), time.Millisecond, func(context.Context) error { |
| 135 | attempts++ |
| 136 | if attempts == 1 { |
| 137 | return errWindowsAgentPending |
| 138 | } |
| 139 | return result |
| 140 | }) |
| 141 | if err != result || attempts != 2 { |
| 142 | t.Fatalf("result=%v attempts=%d err=%v", result, attempts, err) |
| 143 | } |
| 144 | } |
| 145 | ctx, cancel := context.WithCancel(context.Background()) |
| 146 | cancel() |
| 147 | if err := waitWindowsBoot(ctx, time.Hour, func(context.Context) error { return errWindowsAgentPending }); !errors.Is(err, context.Canceled) { |
| 148 | t.Fatalf("canceled: %v", err) |
| 149 | } |
| 150 | } |