跳到主要内容

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

浏览文件

windows_projects_test.go

167 行 · 5.5 KB历史下载
1package workbench
2
3import (
4 "bufio"
5 "context"
6 "encoding/json"
7 "io"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "strings"
12 "testing"
13 "time"
14
15 "github.com/pkg/sftp"
16 "github.com/sylvinus/agent-vm/internal/guestos"
17)
18
19func TestWindowsProjectTransportIsLiveAndConfined(t *testing.T) {
20 ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
21 defer cancel()
22 folder := t.TempDir()
23 if err := os.WriteFile(filepath.Join(folder, "hello.txt"), []byte("live"), 0600); err != nil {
24 t.Fatal(err)
25 }
26 if err := os.Mkdir(filepath.Join(folder, ".git"), 0700); err != nil {
27 t.Fatal(err)
28 }
29 if err := os.WriteFile(filepath.Join(folder, ".git", "config"), []byte("protected"), 0600); err != nil {
30 t.Fatal(err)
31 }
32 receipt := filepath.Join(t.TempDir(), "complete")
33 cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestWindowsProjectRelayHelper$")
34 cmd.Env = append(os.Environ(), "CAGE_WINDOWS_PROJECT_RPC_TEST="+receipt)
35 binding := windowsProjectBinding{ID: strings.Repeat("a", 16), Grant: strings.Repeat("b", 64)}
36 share, err := startWindowsProjectTransport(cmd, folder, binding)
37 if err != nil {
38 t.Fatal(err)
39 }
40 defer share.close()
41 select {
42 case <-share.done:
43 case <-ctx.Done():
44 t.Fatal("project transport did not finish")
45 }
46 if data, err := os.ReadFile(receipt); err != nil || string(data) != "ok" {
47 t.Fatalf("SFTP guest assertions failed: %q %v", data, err)
48 }
49 if data, err := os.ReadFile(filepath.Join(folder, "saved.txt")); err != nil || string(data) != "atomic" {
50 t.Fatalf("atomic host save failed: %q %v", data, err)
51 }
52 if data, err := os.ReadFile(filepath.Join(folder, ".git", "config")); err != nil || string(data) != "git-write" {
53 t.Fatalf("Git metadata write did not reach host: %q %v", data, err)
54 }
55}
56func TestWindowsProjectRelayHelper(t *testing.T) {
57 receipt := os.Getenv("CAGE_WINDOWS_PROJECT_RPC_TEST")
58 if receipt == "" {
59 return
60 }
61 reader := bufio.NewReader(os.Stdin)
62 line, err := reader.ReadBytes('\n')
63 if err != nil {
64 os.Exit(2)
65 }
66 var request struct {
67 Action string `json:"action"`
68 ID string `json:"id"`
69 Grant string `json:"grant"`
70 }
71 if json.Unmarshal(line, &request) != nil || request.Action != "mount" || request.ID != strings.Repeat("a", 16) || request.Grant != strings.Repeat("b", 64) {
72 os.Exit(2)
73 }
74 if err = json.NewEncoder(os.Stdout).Encode(map[string]any{"started": true, "id": request.ID, "path": `C:\Users\Guest\Cage\Projects\` + request.ID}); err != nil {
75 os.Exit(2)
76 }
77 client, err := sftp.NewClientPipe(reader, os.Stdout)
78 if err != nil {
79 os.Exit(2)
80 }
81 f, err := client.Open("/hello.txt")
82 if err != nil {
83 os.Exit(2)
84 }
85 data, err := io.ReadAll(f)
86 f.Close()
87 if err != nil || string(data) != "live" {
88 os.Exit(2)
89 }
90 root, err := client.RealPath(".")
91 if err != nil || root != "/" {
92 os.Exit(2)
93 }
94 if err = exerciseProjectGitWrites(client, ""); err != nil {
95 os.Exit(2)
96 }
97 if f, err = client.Open("/../../host-private"); err == nil {
98 f.Close()
99 os.Exit(2)
100 }
101 f, err = client.Create("/atomic.tmp")
102 if err != nil {
103 os.Exit(2)
104 }
105 if _, err = f.Write([]byte("atomic")); err != nil {
106 os.Exit(2)
107 }
108 f.Close()
109 if err = client.PosixRename("/atomic.tmp", "/saved.txt"); err != nil {
110 os.Exit(2)
111 }
112 if err = os.WriteFile(receipt, []byte("ok"), 0600); err != nil {
113 os.Exit(2)
114 }
115 client.Close()
116 os.Exit(0)
117}
118
119func TestWindowsDefaultEnvironmentRequiresLiveShare(t *testing.T) {
120 s := testServer(t)
121 s.windowsGuest = &guestos.WindowsProfile{}
122 if err := os.MkdirAll(s.environmentDir(), 0700); err != nil {
123 t.Fatal(err)
124 }
125 if err := s.ensureProjectShare(s.environmentDir()); err == nil {
126 t.Fatal("default host environment became guest home without sharing")
127 }
128 binding := &windowsProjectBinding{ID: strings.Repeat("a", 16), Grant: strings.Repeat("b", 64), Path: `C:\Users\Guest\Cage\Projects\` + strings.Repeat("a", 16)}
129 closed := make(chan struct{})
130 share := &projectShare{done: closed, windowsProject: binding}
131 s.projectShares = map[string]*projectShare{s.environmentDir(): share}
132 if actual, err := s.windowsProjectForHost(s.environmentDir()); err != nil || actual == nil || actual.ID != binding.ID {
133 t.Fatalf("default share mapping missing: %v %v", actual, err)
134 }
135 s.windowsProjectHistory = map[string]string{binding.Path: s.environmentDir()}
136 body, _ := json.Marshal(map[string]any{"tabs": []any{map[string]any{"kind": "editor", "workspace": binding.Path}}, "downloads": []any{}})
137 mapped, err := s.browserStateForHost(body)
138 if err != nil {
139 t.Fatal(err)
140 }
141 var state struct {
142 Tabs []struct {
143 Workspace string `json:"workspace"`
144 } `json:"tabs"`
145 }
146 if err = json.Unmarshal(mapped, &state); err != nil || len(state.Tabs) != 1 || state.Tabs[0].Workspace != s.environmentDir() {
147 t.Fatalf("editor did not map to host share: %s %v", mapped, err)
148 }
149 close(closed)
150 if _, err := s.windowsProjectForHost(s.environmentDir()); err == nil {
151 t.Fatal("revoked default grant still usable")
152 }
153}
154
155func TestWindowsProjectReconnectKeepsWorkspaceAndChangesGrant(t *testing.T) {
156 profile, project := t.TempDir(), t.TempDir()
157 first, second := windowsProjectIdentity(profile, project), windowsProjectIdentity(profile, project)
158 if first.ID != second.ID || len(first.ID) != 16 || first.Grant == second.Grant || len(first.Grant) != 64 {
159 t.Fatalf("reconnect changed project identity or reused grant: %#v %#v", first, second)
160 }
161 if other := windowsProjectIdentity(t.TempDir(), project); other.ID == first.ID {
162 t.Fatal("different profile reused project identity")
163 }
164 if other := windowsProjectIdentity(profile, t.TempDir()); other.ID == first.ID {
165 t.Fatal("different project reused project identity")
166 }
167}