跳到主要内容

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

浏览文件

mobile_cli_android_e2e_test.go

183 行 · 6.1 KB历史下载
1//go:build !windows
2
3package workbench
4
5import (
6 "context"
7 "crypto/rand"
8 "encoding/base64"
9 "encoding/json"
10 "errors"
11 "net"
12 "net/url"
13 "os"
14 "path/filepath"
15 "strings"
16 "testing"
17 "time"
18
19 "github.com/sylvinus/agent-vm/internal/host"
20 "github.com/sylvinus/agent-vm/internal/mobilelink"
21 "github.com/sylvinus/agent-vm/internal/remotelink"
22)
23
24// Opt-in host for the independently driven Android App. The driver imports
25// connection.json, checks host-pin.txt, sends "initial mobile prompt", waits
26// for approval-pending, approves Bash once, then sends "follow-up". It writes
27// ui-saw-continuation only after the WebView displays "isolated continuation".
28func TestMobileCLIAndroidApp(t *testing.T) {
29 handoff := os.Getenv("CAGE_TEST_ANDROID_HANDOFF")
30 if handoff == "" {
31 t.Skip("requires a running Android App and an isolated HTTPS relay")
32 }
33 info, err := os.Stat(handoff)
34 if err != nil || !filepath.IsAbs(handoff) || !info.IsDir() || info.Mode().Perm()&0077 != 0 {
35 t.Fatal("handoff must be an absolute private QA directory")
36 }
37 relayOrigin := os.Getenv("CAGE_TEST_ANDROID_RELAY")
38 u, err := url.Parse(relayOrigin)
39 if err != nil || u.Scheme != "https" || !net.ParseIP(u.Hostname()).IsLoopback() {
40 t.Fatal("this test requires a loopback HTTPS relay")
41 }
42 read := func(name string, private bool) string {
43 t.Helper()
44 file := os.Getenv(name)
45 if !filepath.IsAbs(file) || (private && host.CheckPrivateFile(file) != nil) {
46 t.Fatal("invalid QA file for " + name)
47 }
48 data, err := os.ReadFile(file)
49 if err != nil || len(data) == 0 || len(data) > 4096 {
50 t.Fatal("cannot read QA file for " + name)
51 }
52 return strings.TrimSpace(string(data))
53 }
54 write := func(name string, data []byte) {
55 t.Helper()
56 file, err := os.OpenFile(filepath.Join(handoff, name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
57 if err != nil {
58 t.Fatal(err)
59 }
60 _, err = file.Write(data)
61 if err = errors.Join(err, file.Close()); err != nil {
62 t.Fatal(err)
63 }
64 }
65 admin, phonePin := read("CAGE_TEST_ANDROID_ADMIN_FILE", true), read("CAGE_TEST_ANDROID_PIN_FILE", false)
66 s, bridge, project, desktop, resumed := newMobileCLIWorkbench(t)
67 ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
68 defer cancel()
69 id, err := remotelink.NewIdentity()
70 if err != nil {
71 t.Fatal(err)
72 }
73 var secret [32]byte
74 _, _ = rand.Read(secret[:])
75 lookup := base64.RawURLEncoding.EncodeToString(secret[:])
76 room, err := mobilelink.CreateDiscoverableRoom(ctx, relayOrigin, admin, lookup)
77 if err != nil {
78 t.Fatal(err)
79 }
80 pairing, err := json.Marshal(map[string]any{"version": 1, "relay": relayOrigin, "lookup_token": lookup, "host_pin": id.Pin})
81 if err != nil {
82 t.Fatal(err)
83 }
84 write("host-pin.txt", []byte(id.Pin+"\n"))
85 write("target-project.txt", []byte(project+"\n"))
86 write("connection.json", pairing)
87 t.Cleanup(func() { _ = os.Remove(filepath.Join(handoff, "connection.json")) })
88 clear(pairing)
89 done := make(chan struct{})
90 var serveErr error
91 go func() {
92 defer close(done)
93 for ctx.Err() == nil {
94 serveErr = mobilelink.ServeHost(ctx, relayOrigin, room.ID, room.HostToken, id, phonePin, bridge)
95 if ctx.Err() != nil || (serveErr != nil && !mobilelink.Retryable(serveErr)) {
96 return
97 }
98 room, serveErr = mobilelink.CreateDiscoverableRoom(ctx, relayOrigin, admin, lookup)
99 if serveErr != nil {
100 return
101 }
102 }
103 }()
104 defer func() { cancel(); <-done }()
105 t.Log("Android fixture ready; waiting for native UI actions")
106 tick := time.NewTicker(100 * time.Millisecond)
107 defer tick.Stop()
108 var sessionID string
109 pending, wrote, continued := false, false, false
110 for {
111 select {
112 case <-ctx.Done():
113 t.Fatal("timed out waiting for Android UI")
114 case <-done:
115 t.Fatalf("QA relay connection ended: %v", serveErr)
116 case <-tick.C:
117 }
118 list, err := s.mobileCLI.summaries(s.profile, map[string]bool{project: true}, false)
119 if err != nil {
120 t.Fatal(err)
121 }
122 if len(list) == 0 {
123 continue
124 }
125 if len(list) != 1 || (sessionID != "" && list[0].ID != sessionID) {
126 t.Fatal("Android actions must use the same single session")
127 }
128 sessionID = list[0].ID
129 s.mobileCLI.mu.Lock()
130 record, err := s.mobileCLI.record(s.profile, sessionID)
131 var view mobileSession
132 if err == nil {
133 view = record.snapshot()
134 }
135 s.mobileCLI.mu.Unlock()
136 if err != nil {
137 t.Fatal(err)
138 }
139 if len(view.Turns) == 0 {
140 continue
141 }
142 first := view.Turns[0]
143 if first.Prompt != "initial mobile prompt" || len(view.Turns) > 2 {
144 t.Fatal("unexpected Android prompts")
145 }
146 marker := filepath.Join(project, "mobile-approved.txt")
147 if !pending && first.Approval != nil {
148 if first.Approval.Tool != "Bash" || !strings.Contains(string(first.Approval.Input), "mobile-approved.txt") {
149 t.Fatal("unexpected pending tool request")
150 }
151 if _, err := os.Stat(marker); !errors.Is(err, os.ErrNotExist) {
152 t.Fatal("project marker existed before approval")
153 }
154 write("approval-pending", []byte("Bash pending; project marker absent\n"))
155 t.Log("Android prompt reached CLI; approval pending and marker absent")
156 pending = true
157 }
158 if !wrote && first.Status == "completed" {
159 body, err := os.ReadFile(marker)
160 if !pending || err != nil || string(body) != "approved" || first.Text != "isolated tool completed" {
161 t.Fatal("approved CLI turn did not produce expected file and reply")
162 }
163 t.Log("CLI wrote exact marker bytes after approval")
164 wrote = true
165 }
166 if !continued && len(view.Turns) == 2 && view.Turns[1].Status == "completed" {
167 if !wrote || view.Turns[1].Prompt != "follow-up" || view.Turns[1].Text != "isolated continuation" || !resumed.Load() || s.workspace() != desktop {
168 t.Fatal("resumed CLI history or original desktop project did not match")
169 }
170 write("host-verified", []byte("same session and CLI history confirmed\n"))
171 t.Log("second message retained CLI history and desktop project")
172 continued = true
173 }
174 if continued {
175 if data, err := os.ReadFile(filepath.Join(handoff, "ui-saw-continuation")); err == nil && strings.TrimSpace(string(data)) == "isolated continuation" {
176 return
177 }
178 }
179 if turn := view.Turns[len(view.Turns)-1]; turn.Status == "failed" || turn.Status == "interrupted" || turn.Status == "stopped" {
180 t.Fatalf("CLI turn ended in %s: %s", turn.Status, turn.Error)
181 }
182 }
183}