跳到主要内容

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

浏览文件

appearance_test.go

190 行 · 6.5 KB历史下载
1package workbench
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "net/http/httptest"
8 "os"
9 "path/filepath"
10 "reflect"
11 "strings"
12 "testing"
13)
14
15func TestCodexAppearanceImportsOnlyAppearance(t *testing.T) {
16 dir := t.TempDir()
17 path := filepath.Join(dir, "config.toml")
18 data := `model = "unrelated"
19[desktop]
20sansFontSize = 16
21codeFontSize = 13
22appearanceLightCodeThemeId = "solarized"
23appearanceDarkCodeThemeId = "ayu"
24[desktop.appearanceLightChromeTheme]
25accent = "#b58900"
26surface = "#fdf6e3"
27ink = "#657b83"
28contrast = 45
29opaqueWindows = false
30[desktop.appearanceDarkChromeTheme]
31accent = "#e6b450"
32surface = "#10141c"
33ink = "#bfbdb6"
34contrast = 60
35opaqueWindows = true
36[desktop.appearanceDarkChromeTheme.fonts]
37code = "Menlo"
38`
39 if err := os.WriteFile(path, []byte(data), 0600); err != nil {
40 t.Fatal(err)
41 }
42 if err := os.WriteFile(filepath.Join(dir, ".codex-global-state.json"), []byte(`{"reduced-motion-preference":"on","irrelevant":"secret"}`), 0600); err != nil {
43 t.Fatal(err)
44 }
45 a, err := readCodexAppearance(path)
46 if err != nil {
47 t.Fatal(err)
48 }
49 if a.Mode != "system" || a.Light.Preset != "solarized" || a.Dark.Surface != "#10141c" || a.UIFontSize != 16 || a.CodeFontSize != 13 || a.Dark.Fonts.Code != "Menlo" || a.ReducedMotion != "on" || !a.FontSmoothing {
50 t.Fatalf("unexpected appearance: %+v", a)
51 }
52 b, _ := json.Marshal(a)
53 if bytes.Contains(b, []byte("secret")) || bytes.Contains(b, []byte("unrelated")) {
54 t.Fatal("non-appearance data leaked")
55 }
56}
57
58func TestAppearanceSaveDoesNotChangeEnvironmentAndRejectsBadInput(t *testing.T) {
59 s := &Server{profile: t.TempDir(), preferences: Preferences{Workspace: "/project"}, jobs: jobManager{state: Job{Running: true, Kind: "start"}}}
60 a := defaultAppearance()
61 a.Mode = "dark"
62 a.UIFontSize = 16
63 b, _ := json.Marshal(a)
64 w := httptest.NewRecorder()
65 req := httptest.NewRequest("POST", "/api/appearance", bytes.NewReader(b))
66 req.Header.Set("Content-Type", "application/json")
67 s.saveAppearance(w, req)
68 if w.Code != 200 {
69 t.Fatal(w.Body.String())
70 }
71 if !s.jobs.snapshot().Running || s.jobs.snapshot().Kind != "start" || s.preferences.Workspace != "/project" {
72 t.Fatal("appearance changed environment state")
73 }
74 w = httptest.NewRecorder()
75 s.getAppearance(w, httptest.NewRequest("GET", "/api/appearance", nil))
76 var saved Appearance
77 if err := json.Unmarshal(w.Body.Bytes(), &saved); err != nil || !reflect.DeepEqual(saved, a) {
78 t.Fatalf("appearance did not round trip: %s", w.Body.String())
79 }
80 a.UIFontSize = 400
81 b, _ = json.Marshal(a)
82 w = httptest.NewRecorder()
83 req = httptest.NewRequest("POST", "/api/appearance", bytes.NewReader(b))
84 req.Header.Set("Content-Type", "application/json")
85 s.saveAppearance(w, req)
86 if w.Code == 200 {
87 t.Fatal("invalid appearance accepted")
88 }
89 stored, _ := os.ReadFile(filepath.Join(s.profile, "appearance.json"))
90 if bytes.Contains(stored, []byte("400")) {
91 t.Fatal("invalid settings replaced saved appearance")
92 }
93}
94
95func TestAppearanceCustomThemesPersistAndInvalidUpdatesKeepFile(t *testing.T) {
96 s := &Server{profile: t.TempDir()}
97 a := defaultAppearance()
98 a.Light.Colors = map[string]string{"chrome": "#123456", "buttonInk": "#abcdef"}
99 a.Dark.Terminal = map[string]string{"background": "#112233", "brightRed": "#aabbcc"}
100 a.CustomThemes = []SavedAppearanceTheme{{ID: "my-theme", Name: "我的配色", Light: a.Light, Dark: a.Dark}}
101 save := func(a Appearance) *httptest.ResponseRecorder {
102 b, _ := json.Marshal(a)
103 req := httptest.NewRequest("POST", "/api/appearance", bytes.NewReader(b))
104 req.Header.Set("Content-Type", "application/json")
105 w := httptest.NewRecorder()
106 s.saveAppearance(w, req)
107 return w
108 }
109 if w := save(a); w.Code != 200 {
110 t.Fatal(w.Body.String())
111 }
112 original, err := os.ReadFile(filepath.Join(s.profile, "appearance.json"))
113 if err != nil {
114 t.Fatal(err)
115 }
116 restarted := &Server{profile: s.profile}
117 w := httptest.NewRecorder()
118 restarted.getAppearance(w, httptest.NewRequest("GET", "/api/appearance", nil))
119 var loaded Appearance
120 if err := json.Unmarshal(w.Body.Bytes(), &loaded); err != nil || !reflect.DeepEqual(loaded, a) {
121 t.Fatalf("lost saved themes: %s", w.Body.String())
122 }
123 for _, mutate := range []func(*Appearance){
124 func(a *Appearance) { a.Light.Colors["chrome"] = "red" },
125 func(a *Appearance) { a.Dark.Terminal["unknown"] = "#ffffff" },
126 func(a *Appearance) { a.CustomThemes[0].Name = " " },
127 func(a *Appearance) { a.CustomThemes = append(a.CustomThemes, a.CustomThemes[0]) },
128 func(a *Appearance) { a.CustomThemes[0].Dark.Colors = map[string]string{"line": "#abc"} },
129 } {
130 var invalid Appearance
131 if err := json.Unmarshal(original, &invalid); err != nil {
132 t.Fatal(err)
133 }
134 mutate(&invalid)
135 if w := save(invalid); w.Code == 200 {
136 t.Fatal("invalid theme accepted")
137 }
138 stored, _ := os.ReadFile(filepath.Join(s.profile, "appearance.json"))
139 if !bytes.Equal(stored, original) {
140 t.Fatal("invalid input replaced valid theme file")
141 }
142 }
143 // The library can exceed the old 64 KB appearance request limit.
144 for i := 1; i < 50; i++ {
145 item := SavedAppearanceTheme{ID: fmt.Sprintf("theme-%d", i), Name: "配色", Light: a.Light, Dark: a.Dark}
146 item.Light.Fonts.UI = strings.Repeat("a", 200)
147 item.Light.Fonts.Code = strings.Repeat("b", 200)
148 item.Dark.Fonts = item.Light.Fonts
149 for _, theme := range []*AppearanceTheme{&item.Light, &item.Dark} {
150 theme.Colors = make(map[string]string)
151 theme.Terminal = make(map[string]string)
152 for _, key := range appearanceColorKeys {
153 theme.Colors[key] = "#123456"
154 }
155 for _, key := range terminalColorKeys {
156 theme.Terminal[key] = "#abcdef"
157 }
158 }
159 a.CustomThemes = append(a.CustomThemes, item)
160 }
161 b, _ := json.Marshal(a)
162 if len(b) <= 64<<10 {
163 t.Fatal("fixture must exceed old request limit")
164 }
165 if w := save(a); w.Code != 200 {
166 t.Fatal(w.Body.String())
167 }
168 a.CustomThemes = append(a.CustomThemes, SavedAppearanceTheme{ID: "overflow", Name: "超限", Light: a.Light, Dark: a.Dark})
169 if w := save(a); w.Code == 200 {
170 t.Fatal("library limit not enforced")
171 }
172}
173
174func TestAppearanceLegacyFileKeepsDerivedDefaults(t *testing.T) {
175 s := &Server{profile: t.TempDir()}
176 old := defaultAppearance()
177 b, _ := json.Marshal(old)
178 if err := os.WriteFile(filepath.Join(s.profile, "appearance.json"), b, 0600); err != nil {
179 t.Fatal(err)
180 }
181 w := httptest.NewRecorder()
182 s.getAppearance(w, httptest.NewRequest("GET", "/api/appearance", nil))
183 if w.Code != 200 {
184 t.Fatal(w.Body.String())
185 }
186 var loaded Appearance
187 if err := json.Unmarshal(w.Body.Bytes(), &loaded); err != nil || !reflect.DeepEqual(loaded, old) {
188 t.Fatalf("legacy appearance lost: %s", w.Body.String())
189 }
190}