跳到主要内容

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

浏览文件

proxy_pause_test.go

167 行 · 5.4 KB历史下载
1package workbench
2
3import (
4 "context"
5 "net/http"
6 "os"
7 "path/filepath"
8 "sync"
9 "testing"
10 "time"
11
12 "github.com/sylvinus/agent-vm/internal/cage"
13 "github.com/sylvinus/agent-vm/internal/managedproxy"
14)
15
16// Keep the real request, job, source persistence and gate-control paths. Only
17// external subscription traffic and the installed proxy process are replaced.
18func pendingProxyApply(t *testing.T) (*Server, *cage.EgressGate, <-chan struct{}, func()) {
19 t.Helper()
20 s, gate := interfaceMonitorServer(t)
21 p, err := managedproxy.Load(s.profile)
22 if err != nil {
23 t.Fatal(err)
24 }
25 p.Sources[0].URL = "https://subscription.example/nodes"
26 p.Sources[0].Interval = 1
27 p.Sources[0].Updated = time.Now().Add(-2 * time.Hour)
28 p.Sources[0].Attempted = p.Sources[0].Updated
29 if err := p.Save(s.profile); err != nil {
30 t.Fatal(err)
31 }
32 validate, start, fetch := validateProxyCore, startProxyCore, fetchProxySource
33 t.Cleanup(func() { validateProxyCore, startProxyCore, fetchProxySource = validate, start, fetch })
34 validateProxyCore = func(_ context.Context, profile string, candidate managedproxy.Store) error {
35 _, err := candidate.Runtime(profile)
36 return err
37 }
38 fetchProxySource = func(_ context.Context, _ *http.Client, _ string) ([]byte, string, error) {
39 return []byte(`proxies: [{name: qa, type: socks5, server: 127.0.0.1, port: 1, password: updated-test-value}]`), "", nil
40 }
41 started, release := make(chan struct{}), make(chan struct{})
42 var once sync.Once
43 finish := func() { once.Do(func() { close(release) }) }
44 startProxyCore = func(ctx context.Context, _ string, _ managedproxy.Store) error {
45 close(started)
46 select {
47 case <-release:
48 return nil
49 case <-ctx.Done():
50 return ctx.Err()
51 }
52 }
53 t.Cleanup(func() { finish(); s.op.Lock(); s.op.Unlock() })
54 return s, gate, started, finish
55}
56
57func awaitProxyStart(t *testing.T, started <-chan struct{}) {
58 t.Helper()
59 select {
60 case <-started:
61 case <-time.After(3 * time.Second):
62 t.Fatal("proxy application did not reach core startup")
63 }
64}
65
66func pauseProxyNetwork(t *testing.T, s *Server) {
67 t.Helper()
68 if w := request(s, "POST", "/api/safety/close", s.origin, "{}", true); w.Code != 200 {
69 t.Fatalf("pause rejected: %d %s", w.Code, w.Body.String())
70 }
71}
72
73func assertProxyStayedPaused(t *testing.T, s *Server, gate *cage.EgressGate) {
74 t.Helper()
75 s.op.Lock()
76 defer s.op.Unlock()
77 s.mu.Lock()
78 paused, job := s.networkPaused, s.jobs.snapshot()
79 s.mu.Unlock()
80 if job.Running || job.Error != "" {
81 t.Fatalf("proxy update did not complete: %+v", job)
82 }
83 if !paused || gate.Status().Mode != cage.GateClosed {
84 t.Fatal("proxy update revoked manual pause")
85 }
86 p, err := managedproxy.Load(s.profile)
87 if err != nil || p.Revision == "test-revision" {
88 t.Fatal("proxy update did not persist a new revision", err)
89 }
90 cfg, err := loadConfig(s.configPath)
91 if err != nil || gate.Status().Configuration != cfg.Fingerprint() {
92 t.Fatal("paused gate was not synchronized to the applied proxy", err)
93 }
94}
95
96func TestAutomaticSubscriptionUpdatePreservesManualPause(t *testing.T) {
97 for _, pauseBefore := range []bool{true, false} {
98 name := "paused before automatic refresh"
99 if !pauseBefore {
100 name = "paused while automatic refresh applies"
101 }
102 t.Run(name, func(t *testing.T) {
103 s, gate, started, finish := pendingProxyApply(t)
104 if pauseBefore {
105 pauseProxyNetwork(t, s)
106 }
107 s.op.Lock()
108 s.refreshProxySubscriptions()
109 s.op.Unlock()
110 awaitProxyStart(t, started)
111 if !pauseBefore {
112 pauseProxyNetwork(t, s)
113 }
114 finish()
115 assertProxyStayedPaused(t, s, gate)
116 p, _ := managedproxy.Load(s.profile)
117 if p.Sources[0].Nodes[0].Config["password"] != "updated-test-value" {
118 t.Fatal("automatic refresh did not consume the new subscription")
119 }
120 })
121 }
122}
123
124func TestManualProxySelectionHonorsLaterPause(t *testing.T) {
125 s, gate, started, finish := pendingProxyApply(t)
126 pauseProxyNetwork(t, s)
127 p, _ := managedproxy.Load(s.profile)
128 if w := request(s, "POST", "/api/proxy/select", s.origin, `{"node_id":"`+p.Selected+`"}`, true); w.Code != 200 {
129 t.Fatalf("selection rejected: %d %s", w.Code, w.Body.String())
130 }
131 awaitProxyStart(t, started)
132 pauseProxyNetwork(t, s)
133 finish()
134 assertProxyStayedPaused(t, s, gate)
135 reloaded := reloadedIntentServer(t, s)
136 if err := reloaded.restoreNetworkIntent(); err != nil || !reloaded.networkPaused {
137 t.Fatal("later manual pause was not durable", err)
138 }
139}
140
141func TestManualProxySelectionMayResumeExistingPause(t *testing.T) {
142 s, _, started, finish := pendingProxyApply(t)
143 pauseProxyNetwork(t, s)
144 // No VM is running in this case, so selecting a node may prepare networking
145 // without trying to start a real guest as part of this unit test.
146 path := filepath.Join(s.profile, "state", "lima", cage.InstanceName(s.profile, s.environmentDir()), "cage-running.json")
147 if err := os.Remove(path); err != nil {
148 t.Fatal(err)
149 }
150 p, _ := managedproxy.Load(s.profile)
151 if w := request(s, "POST", "/api/proxy/select", s.origin, `{"node_id":"`+p.Selected+`"}`, true); w.Code != 200 {
152 t.Fatalf("selection rejected: %d %s", w.Code, w.Body.String())
153 }
154 awaitProxyStart(t, started)
155 finish()
156 s.op.Lock()
157 defer s.op.Unlock()
158 s.mu.Lock()
159 defer s.mu.Unlock()
160 if s.jobs.snapshot().Running || s.jobs.snapshot().Error != "" || s.networkPaused {
161 t.Fatalf("explicit selection failed to resume the existing pause: %+v", s.jobs.snapshot())
162 }
163 reloaded := reloadedIntentServer(t, s)
164 if err := reloaded.restoreNetworkIntent(); err != nil || reloaded.networkPaused {
165 t.Fatal("explicit node resume was not durable", err)
166 }
167}