跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

windows_components_test.go

162 行 · 7.2 KB历史下载
1package workbench
2
3import (
4 "encoding/json"
5 "net/http"
6 "os"
7 "path/filepath"
8 "testing"
9 "time"
10
11 "github.com/google/uuid"
12 "github.com/sylvinus/agent-vm/internal/guestos"
13)
14
15func init() {
16 if os.Getenv("CAGE_TEST_WINDOWS_COMPONENT_RELAY") != "1" {
17 return
18 }
19 if len(os.Args) != 4 || os.Args[1] != "--relay-profile" || os.Args[3] != "status" {
20 os.Exit(2)
21 }
22 binding, err := guestos.LoadWindowsProfile(filepath.Dir(os.Args[2]))
23 if err != nil || binding == nil {
24 os.Exit(2)
25 }
26 vm := binding.VMID.String()
27 if os.Getenv("CAGE_TEST_WINDOWS_COMPONENT_BAD_IDENTITY") == "1" {
28 vm = uuid.NewString()
29 }
30 // This is an explicit transport fixture, not a Windows COM or package test.
31 report := windowsGuestState{Version: 1, OS: "windows", VMID: vm, SID: binding.GUIUserSID, Session: "component-fixture", Available: true,
32 Capabilities: []string{"cli", "desktop", "audio"}, Components: map[string]windowsComponentState{
33 "cli": {Installed: false, Ready: false, Reason: "cli_tools_missing_or_invalid", Dependencies: map[string]bool{"python": false}},
34 "desktop": {Installed: true, Ready: true}, "audio": {Supported: true, Installed: true, Ready: false, Reason: "audio_status_unavailable", Stage: "render_enumerate", NativeCode: "80004002"},
35 }}
36 _ = json.NewEncoder(os.Stdout).Encode(report)
37 os.Exit(0)
38}
39
40func TestWindowsStatusRefreshUsesFixedPairedStatusTransport(t *testing.T) {
41 s := testServer(t)
42 binding := guestos.WindowsProfile{Version: 1, VMID: uuid.New(), GUIUserSID: "S-1-5-21-1-2-3-1001", GUIUser: "Guest", InstanceDirectory: filepath.Join(s.profile, "unused-vm"), PairingFile: filepath.Join(s.profile, "unused-pair"), RelayExecutable: os.Args[0]}
43 if err := guestos.BindWindowsProfile(s.profile, binding); err != nil {
44 t.Fatal(err)
45 }
46 s.windowsGuest = &binding
47 s.ready = true
48 s.windowsState = windowsGuestState{Version: 1, OS: "windows", VMID: binding.VMID.String(), SID: binding.GUIUserSID, Session: "initial", Available: true}
49 t.Setenv("CAGE_TEST_WINDOWS_COMPONENT_RELAY", "1")
50 wait := func() {
51 t.Helper()
52 deadline := time.Now().Add(5 * time.Second)
53 for time.Now().Before(deadline) {
54 s.mu.Lock()
55 done := !s.windowsState.ComponentsRefreshing && !s.windowsState.ComponentsChecked.IsZero()
56 s.mu.Unlock()
57 if done {
58 return
59 }
60 time.Sleep(10 * time.Millisecond)
61 }
62 t.Fatal("fixed status refresh did not complete")
63 }
64 if w := windowsBrowserRequestForTest(s, http.MethodGet, "/api/status", nil); w.Code != http.StatusOK {
65 t.Fatalf("actual status route: %d", w.Code)
66 }
67 wait()
68 s.mu.Lock()
69 available := s.windowsCapabilitiesLocked()["available"].(map[string]bool)
70 session := s.windowsState.Session
71 s.mu.Unlock()
72 if !available["claude-desktop"] || available["claude-cli"] || available["audio"] || session != "component-fixture" {
73 t.Fatal("actual fixed status transport did not replace pending component data")
74 }
75 t.Setenv("CAGE_TEST_WINDOWS_COMPONENT_BAD_IDENTITY", "1")
76 s.mu.Lock()
77 s.windowsState.ComponentsChecked = time.Time{}
78 s.mu.Unlock()
79 if w := windowsBrowserRequestForTest(s, http.MethodGet, "/api/status", nil); w.Code != http.StatusOK {
80 t.Fatalf("actual refresh route: %d", w.Code)
81 }
82 wait()
83 s.mu.Lock()
84 defer s.mu.Unlock()
85 if s.windowsState.VMID != binding.VMID.String() || s.windowsState.Session != "component-fixture" || s.windowsState.Components["desktop"].Ready || s.windowsState.Components["desktop"].Reason != "agent_status_unavailable" {
86 t.Fatal("wrong VM status gained readiness or replaced authenticated identity")
87 }
88}
89
90func TestWindowsStatusSeparatesRegisteredFromReadyComponents(t *testing.T) {
91 s := testServer(t)
92 s.windowsGuest = &guestos.WindowsProfile{}
93 s.ready = true
94 s.windowsState.Available = true
95 s.windowsState.ComponentsChecked = time.Now()
96 s.windowsState.Capabilities = []string{"cli", "desktop", "audio", "terminal", "clipboard"}
97 s.windowsState.Components = map[string]windowsComponentState{
98 "cli": {Installed: false, Ready: false, Reason: "cli_tools_missing_or_invalid", Dependencies: map[string]bool{"python": false, "claude": true, "git_bash": true}},
99 "desktop": {Installed: false, Ready: false, Reason: "desktop_not_installed"},
100 "audio": {Supported: true, Installed: true, Ready: false, Reason: "audio_status_unavailable", Stage: "render_activate", NativeCode: "80040154"},
101 "terminal": {Installed: true, Ready: true}, "clipboard": {Installed: true, Ready: true},
102 }
103 w := windowsBrowserRequestForTest(s, http.MethodGet, "/api/status", nil)
104 var status struct {
105 Capabilities struct {
106 Registered map[string]bool `json:"registered"`
107 Installed map[string]bool `json:"installed"`
108 Available map[string]bool `json:"available"`
109 Components map[string]windowsComponentState `json:"components"`
110 Unavailable []string `json:"unavailable"`
111 } `json:"guest_capabilities"`
112 }
113 if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &status) != nil {
114 t.Fatalf("actual status handler: %d", w.Code)
115 }
116 for _, name := range []string{"cli", "desktop", "audio"} {
117 if !status.Capabilities.Registered[name] {
118 t.Fatalf("RPC registration lost: %s", name)
119 }
120 }
121 for _, name := range []string{"claude-cli", "claude-desktop", "audio", "desktop-file-chooser", "desktop-downloads", "phone-browser", "phone-desktop", "legacy-mobile-migration"} {
122 if status.Capabilities.Available[name] {
123 t.Fatalf("unready component advertised: %s", name)
124 }
125 found := false
126 for _, missing := range status.Capabilities.Unavailable {
127 if missing == name {
128 found = true
129 }
130 }
131 if !found {
132 t.Fatalf("missing capability omitted: %s", name)
133 }
134 }
135 if !status.Capabilities.Installed["audio"] || status.Capabilities.Installed["claude-cli"] || status.Capabilities.Installed["claude-desktop"] {
136 t.Fatal("installed prerequisites conflated with registration")
137 }
138 if status.Capabilities.Components["audio"].Stage != "render_activate" || status.Capabilities.Components["audio"].NativeCode != "80040154" || status.Capabilities.Components["cli"].Dependencies["python"] {
139 t.Fatal("safe diagnostic/dependency status lost")
140 }
141
142 s.windowsState.Components["desktop"] = windowsComponentState{Installed: true, Ready: true}
143 s.windowsState.Components["cli"] = windowsComponentState{Installed: true, Ready: true}
144 available := s.windowsCapabilitiesLocked()["available"].(map[string]bool)
145 if !available["claude-desktop"] || !available["claude-cli"] {
146 t.Fatal("verified installed components must remain usable before on-demand app start")
147 }
148 s.windowsState.Available = false
149 available = s.windowsCapabilitiesLocked()["available"].(map[string]bool)
150 if available["claude-cli"] || available["claude-desktop"] || available["terminal"] {
151 t.Fatal("locked GUI retained readiness")
152 }
153 s.windowsState.Available = true
154 s.windowsState.Capabilities = []string{"desktop"}
155 if s.windowsCapabilitiesLocked()["available"].(map[string]bool)["claude-cli"] || !s.windowsCapabilitiesLocked()["installed"].(map[string]bool)["claude-cli"] {
156 t.Fatal("newly installed CLI without registered handler must require agent reload")
157 }
158 s.windowsState.ComponentsChecked = time.Now().Add(-time.Minute)
159 if s.windowsCapabilitiesLocked()["available"].(map[string]bool)["claude-desktop"] {
160 t.Fatal("old component cache remained ready without active status polling")
161 }
162}