跳到主要内容

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

浏览文件

macos_input_contract_test.go

127 行 · 4.5 KB历史下载
1package workbench
2
3import (
4 "bufio"
5 "bytes"
6 "encoding/json"
7 "net/http"
8 "net/http/httptest"
9 "strings"
10 "testing"
11 "time"
12
13 "github.com/gorilla/websocket"
14 "golang.org/x/crypto/ssh"
15)
16
17func TestMacOSWindowInputContract(t *testing.T) {
18 x, y, button, down := 0.2, 0.3, 2, true
19 target := macOSWindowRequest{PID: 42, Window: 7, Geometry: "geometry"}
20 pointer := target
21 pointer.Action, pointer.Phase, pointer.X, pointer.Y = "pointer", "down", &x, &y
22 pointer.Button, pointer.Buttons, pointer.Clicks = &button, 2, 2
23 if !validMacOSWindowRequest(pointer) {
24 t.Fatal("targeted right double-click was rejected")
25 }
26 pointer.Phase, pointer.Button = "move", nil
27 if !validMacOSWindowRequest(pointer) {
28 t.Fatal("targeted drag move was rejected")
29 }
30 pointer.Geometry = ""
31 if validMacOSWindowRequest(pointer) {
32 t.Fatal("pointer without captured geometry was accepted")
33 }
34 key := target
35 key.Action, key.Key, key.Code, key.Down = "key", "/", "Slash", &down
36 if !validMacOSWindowRequest(key) {
37 t.Fatal("physical key transition was rejected")
38 }
39 key.Down = nil
40 if validMacOSWindowRequest(key) {
41 t.Fatal("physical key without a transition was accepted")
42 }
43 ime := target
44 ime.Action, ime.Mode, ime.Context, ime.Session = "ime", "begin", strings.Repeat("a", 32), strings.Repeat("b", 32)
45 if !validMacOSWindowRequest(ime) {
46 t.Fatal("targeted composition was rejected")
47 }
48 ime.Mode, ime.Sequence = "commit", -1
49 if validMacOSWindowRequest(ime) {
50 t.Fatal("negative composition sequence was accepted")
51 }
52 if !validMacOSWindowRequest(macOSWindowRequest{Action: "input-reset"}) || !validMacOSWindowRequest(macOSWindowRequest{Action: "status"}) {
53 t.Fatal("connection cleanup or existing status request was rejected")
54 }
55}
56
57func TestMacOSWindowInputRoutesPreserveCompositionSequence(t *testing.T) {
58 guest := newMacWorkspaceGuest(t, func(channel ssh.Channel, command string) (uint32, bool) {
59 if !strings.Contains(command, "window.sock") {
60 return 0, false
61 }
62 reader := bufio.NewReader(channel)
63 for {
64 var input macOSWindowRequest
65 if json.NewDecoder(reader).Decode(&input) != nil {
66 return 0, true
67 }
68 if json.NewEncoder(channel).Encode(input) != nil || !input.KeepAlive {
69 return 0, true
70 }
71 }
72 })
73 s := guest.s
74 server := httptest.NewServer(http.HandlerFunc(s.macOSWindowConnection))
75 defer server.Close()
76 s.origin = server.URL
77 ws, _, err := websocket.DefaultDialer.Dial("ws"+strings.TrimPrefix(server.URL, "http"), http.Header{"Origin": {server.URL}})
78 if err != nil {
79 t.Fatal(err)
80 }
81 defer ws.Close()
82 for sequence, mode := range []string{"begin", "update", "commit"} {
83 input := macOSWindowRequest{Action: "ime", PID: 42, Window: 7, Geometry: "geometry", Mode: mode,
84 Context: strings.Repeat("a", 32), Session: strings.Repeat("b", 32), Sequence: sequence,
85 Project: "ungranted", PhoneToken: "ungranted", Path: "ungranted", Transfer: "ungranted", IDs: []string{"ungranted"}, KeepAlive: true, Wait: true, Binary: true}
86 if mode == "commit" {
87 input.Text = strings.Repeat("\x01", 8192)
88 }
89 if err := ws.WriteJSON(map[string]any{"id": sequence + 1, "request": input}); err != nil {
90 t.Fatal(err)
91 }
92 var response struct {
93 ID int `json:"id"`
94 Result macOSWindowRequest `json:"result"`
95 }
96 _ = ws.SetReadDeadline(time.Now().Add(5 * time.Second))
97 if err := ws.ReadJSON(&response); err != nil {
98 t.Fatal(err)
99 }
100 check := func(actual macOSWindowRequest, keepAlive bool) {
101 t.Helper()
102 if actual.Sequence != sequence || actual.Session != input.Session || actual.Text != input.Text || actual.Geometry != input.Geometry {
103 t.Fatalf("composition changed in transit for %s, sequence %d", mode, actual.Sequence)
104 }
105 if actual.Project != "" || actual.Path != "" || actual.PhoneToken != "" || actual.Transfer != "" || actual.IDs != nil || actual.Wait || actual.Binary || actual.KeepAlive != keepAlive {
106 t.Fatalf("public input retained privileged fields for %s", mode)
107 }
108 }
109 check(response.Result, true)
110 data, err := json.Marshal(input)
111 if err != nil {
112 t.Fatal(err)
113 }
114 recorder := httptest.NewRecorder()
115 request := httptest.NewRequest(http.MethodPost, "/api/macos/claude/window", bytes.NewReader(data))
116 request.Header.Set("Content-Type", "application/json")
117 s.macOSClaudeWindow(recorder, request)
118 if recorder.Code != http.StatusOK {
119 t.Fatalf("HTTP input returned %d: %s", recorder.Code, recorder.Body.String())
120 }
121 var actual macOSWindowRequest
122 if err := json.Unmarshal(recorder.Body.Bytes(), &actual); err != nil {
123 t.Fatal(err)
124 }
125 check(actual, false)
126 }
127}