跳到主要内容

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

浏览文件

favorites_test.go

149 行 · 5.5 KB历史下载
1package workbench
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "github.com/sylvinus/agent-vm/internal/host"
8 "github.com/sylvinus/agent-vm/internal/mobilelink"
9 "os"
10 "path/filepath"
11 "sync"
12 "testing"
13)
14
15func TestFavoritesPersistenceAndFolders(t *testing.T) {
16 s := testServer(t)
17 post := func(body string) Favorites {
18 t.Helper()
19 w := request(s, "POST", "/api/favorites", s.origin, body, true)
20 if w.Code != 200 {
21 t.Fatalf("%d: %s", w.Code, w.Body.String())
22 }
23 var f Favorites
24 if err := json.Unmarshal(w.Body.Bytes(), &f); err != nil {
25 t.Fatal(err)
26 }
27 return f
28 }
29 f := post(`{"action":"save_folder","title":"开发"}`)
30 folder := f.Folders[0].ID
31 f = post(fmt.Sprintf(`{"action":"save","title":"文档","url":"https://example.com/docs","folder_id":%q}`, folder))
32 id := f.Items[0].ID
33 f = post(`{"action":"save","title":"文档新版","url":"https://example.com/docs"}`)
34 if len(f.Items) != 1 || f.Items[0].ID != id || f.Items[0].Title != "文档新版" {
35 t.Fatal("repeated save duplicated a favorite")
36 }
37 f = post(fmt.Sprintf(`{"action":"save","id":%q,"title":"已移动","url":"https://example.com/new","folder_id":%q}`, id, folder))
38 if f.Items[0].FolderID != folder {
39 t.Fatal("move failed")
40 }
41 post(fmt.Sprintf(`{"action":"save_folder","id":%q,"title":"工作"}`, folder))
42 // A new server instance reads the saved file without an active VM.
43 reopened := &Server{profile: s.profile}
44 f, err := reopened.readFavorites()
45 if err != nil || f.Folders[0].Title != "工作" || f.Items[0].URL != "https://example.com/new" {
46 t.Fatalf("persistence: %+v %v", f, err)
47 }
48 if host.CheckPrivateFile(filepath.Join(s.profile, "favorites.json")) != nil {
49 t.Fatal("favorites file must be private")
50 }
51 f = post(fmt.Sprintf(`{"action":"delete_folder","id":%q}`, folder))
52 if len(f.Folders) != 0 || len(f.Items) != 1 || f.Items[0].FolderID != "" {
53 t.Fatal("deleting folder must retain its favorites in unfiled")
54 }
55 f = post(fmt.Sprintf(`{"action":"delete","id":%q}`, id))
56 if len(f.Items) != 0 {
57 t.Fatal("delete failed")
58 }
59}
60
61func TestFavoritesBoundaryAndFailedWrites(t *testing.T) {
62 s := testServer(t)
63 if w := request(s, "GET", "/api/favorites", "", "", false); w.Code != 401 {
64 t.Fatal("unauthenticated favorites readable")
65 }
66 if w := request(s, "POST", "/api/favorites", "https://example.com", `{}`, true); w.Code != 403 {
67 t.Fatal("cross-origin mutation accepted")
68 }
69 for _, body := range []string{
70 `{"action":"save","title":"x","url":"javascript:alert(1)"}`,
71 `{"action":"save","title":"x","url":"file:///etc/passwd"}`,
72 `{"action":"save","title":"x","url":"https://user:[email protected]"}`,
73 `{"action":"save","title":"x","url":"https://example.com","folder_id":"missing"}`,
74 `{"action":"save_folder","title":" "}`,
75 } {
76 if w := request(s, "POST", "/api/favorites", s.origin, body, true); w.Code == 200 {
77 t.Fatalf("accepted invalid favorite: %s", body)
78 }
79 }
80 path := filepath.Join(s.profile, "favorites.json")
81 if err := os.WriteFile(path, []byte("broken"), 0600); err != nil {
82 t.Fatal(err)
83 }
84 w := request(s, "POST", "/api/favorites", s.origin, `{"action":"save_folder","title":"x"}`, true)
85 b, _ := os.ReadFile(path)
86 if w.Code == 200 || string(b) != "broken" {
87 t.Fatal("unreadable saved data was overwritten")
88 }
89}
90
91func TestConcurrentFavoritesKeepBothAdditions(t *testing.T) {
92 s := testServer(t)
93 var wg sync.WaitGroup
94 for i := 0; i < 8; i++ {
95 wg.Go(func() {
96 body := fmt.Sprintf(`{"action":"save","title":"Page %d","url":"https://example.com/%d"}`, i, i)
97 if w := request(s, "POST", "/api/favorites", s.origin, body, true); w.Code != 200 {
98 t.Errorf("save: %d", w.Code)
99 }
100 })
101 }
102 wg.Wait()
103 f, err := s.readFavorites()
104 if err != nil || len(f.Items) != 8 {
105 t.Fatalf("concurrent additions lost: %+v, %v", f, err)
106 }
107}
108
109// Exercise the desktop store through the same paired transport used by the native apps.
110func testSharedPhoneFavorites(t *testing.T, ctx context.Context, s *Server, phone *mobilelink.Client) {
111 t.Helper()
112 const path = "/api/mobile/browser/favorites"
113 call := func(method, body string) Favorites {
114 t.Helper()
115 reply, err := phone.Do(ctx, method, path, []byte(body))
116 if err != nil || reply.Status != 200 {
117 t.Fatalf("paired favorites: %+v %v", reply, err)
118 }
119 var f Favorites
120 if err := json.Unmarshal(reply.Body, &f); err != nil {
121 t.Fatal(err)
122 }
123 return f
124 }
125 if w := pairedHostRequest(s, "GET", path, ""); w.Code != 403 {
126 t.Fatal("unpaired connection could read favorites")
127 }
128 if w := pairedHostRequest(s, "POST", path, `{"action":"save_folder","title":"forbidden"}`); w.Code != 403 {
129 t.Fatal("unpaired connection could write favorites")
130 }
131 w := pairedHostRequest(s, "POST", "/api/favorites", `{"action":"save_folder","title":"Shared"}`)
132 if w.Code != 200 {
133 t.Fatal(w.Code)
134 }
135 initial := call("GET", "")
136 if len(initial.Folders) != 1 || initial.Folders[0].Title != "Shared" {
137 t.Fatal("phone missed desktop folder")
138 }
139 f := call("POST", fmt.Sprintf(`{"action":"save","title":"Docs","url":"https://example.com/docs","folder_id":%q}`, initial.Folders[0].ID))
140 var desktop Favorites
141 w = pairedHostRequest(s, "GET", "/api/favorites", "")
142 if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &desktop) != nil || len(desktop.Items) != 1 || len(f.Items) != 1 || f.Items[0] != desktop.Items[0] {
143 t.Fatal("desktop did not see phone bookmark")
144 }
145 w = pairedHostRequest(s, "POST", "/api/favorites", fmt.Sprintf(`{"action":"delete","id":%q}`, f.Items[0].ID))
146 if w.Code != 200 || len(call("GET", "").Items) != 0 {
147 t.Fatal("desktop deletion did not reach phone")
148 }
149}