跳到主要内容

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

浏览文件

project_remove_sessions_test.go

73 行 · 2.5 KB历史下载
1//go:build !windows
2
3package workbench
4
5import (
6 "encoding/json"
7 "io"
8 "net/http"
9 "os"
10 "path/filepath"
11 "slices"
12 "strings"
13 "testing"
14
15 "al.essio.dev/pkg/shellescape"
16)
17
18func TestProjectRemovalRefreshesExternalSessions(t *testing.T) {
19 s, id, project := metadataFixture(t)
20 s.terminals.sessions = nil
21 home, _ := filepath.EvalSymlinks(t.TempDir())
22 bin := filepath.Join(home, "bin")
23 if err := os.Mkdir(bin, 0700); err != nil {
24 t.Fatal(err)
25 }
26 const cli = "#!/bin/sh\n[ \"$#\" -eq 2 ] && [ \"$1\" = agents ] && [ \"$2\" = --json ] || exit 2\ncat \"$HOME/agents.json\"\n"
27 if err := os.WriteFile(filepath.Join(bin, "claude"), []byte(cli), 0700); err != nil {
28 t.Fatal(err)
29 }
30 if err := os.WriteFile(filepath.Join(home, ".bash_profile"), []byte("export PATH="+shellescape.Quote(bin)+":/usr/bin:/bin\n"), 0600); err != nil {
31 t.Fatal(err)
32 }
33 mobileTestSSH(t, s, home, []string{"HOME=" + home, "PATH=" + bin + ":/usr/bin:/bin", "CLAUDE_CONFIG_DIR=" + filepath.Join(home, ".claude")})
34 record, err := s.mobileCLI.record(s.profile, id)
35 if err != nil {
36 t.Fatal(err)
37 }
38 history := filepath.Join(home, ".claude", "projects", "fixture", record.CLIID+".jsonl")
39 if err := os.MkdirAll(filepath.Dir(history), 0700); err != nil {
40 t.Fatal(err)
41 }
42 row, _ := json.Marshal(map[string]any{"cwd": guestPath(project), "type": "user", "message": map[string]string{"content": "keep history"}})
43 if err := os.WriteFile(history, append(row, '\n'), 0600); err != nil {
44 t.Fatal(err)
45 }
46 registry := filepath.Join(home, "agents.json")
47 if err := writePrivate(registry, []map[string]any{{"sessionId": record.CLIID, "pid": os.Getpid()}}); err != nil {
48 t.Fatal(err)
49 }
50 s.ready = true
51 s.browserControl = browserClientForTest(t, s, browserTransport(func(r *http.Request) (*http.Response, error) {
52 return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(`{"tabs":[]}`))}, nil
53 }))
54 removeProjectRequest(t, s, project, 409)
55 if !s.mobileCLI.running[id] {
56 t.Fatal("removal did not refresh the previously empty process cache")
57 }
58 if err := os.WriteFile(registry, []byte("invalid JSON"), 0600); err != nil {
59 t.Fatal(err)
60 }
61 removeProjectRequest(t, s, project, 409)
62 if err := writePrivate(registry, []any{}); err != nil {
63 t.Fatal(err)
64 }
65 removeProjectRequest(t, s, project, 200)
66 paths, err := s.projectPaths()
67 if err != nil || slices.Contains(paths, project) {
68 t.Fatal("finished session blocked removal", err)
69 }
70 if _, err := os.Stat(history); err != nil {
71 t.Fatal("guest history changed", err)
72 }
73}