跳到主要内容

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

浏览文件

desktop_status_test.go

108 行 · 3.1 KB历史下载
1package workbench
2
3import (
4 "context"
5 "errors"
6 "sync/atomic"
7 "testing"
8 "time"
9)
10
11func TestDesktopStatusSharesReadsAndRefreshesAfterAction(t *testing.T) {
12 var cache desktopStatusCache
13 var calls atomic.Int32
14 started, release := make(chan struct{}), make(chan struct{})
15 load := func(ctx context.Context) ([]byte, error) {
16 if calls.Add(1) == 1 {
17 close(started)
18 select {
19 case <-release:
20 case <-ctx.Done():
21 return nil, ctx.Err()
22 }
23 }
24 return []byte(`{"running":true}`), nil
25 }
26 first := make(chan error, 1)
27 ctx, cancel := context.WithCancel(t.Context())
28 go func() { _, err := cache.read(ctx, t.Context(), load); first <- err }()
29 <-started
30 cancel()
31 if err := <-first; !errors.Is(err, context.Canceled) {
32 t.Fatalf("cancelled waiter: %v", err)
33 }
34 close(release)
35 if _, err := cache.read(t.Context(), t.Context(), load); err != nil {
36 t.Fatal(err)
37 }
38 if calls.Load() != 1 {
39 t.Fatalf("shared status read executed %d times", calls.Load())
40 }
41 cache.invalidate()
42 if _, err := cache.read(t.Context(), t.Context(), load); err != nil {
43 t.Fatal(err)
44 }
45 if calls.Load() != 2 {
46 t.Fatalf("action did not refresh state: %d", calls.Load())
47 }
48}
49
50func TestDesktopStatusExpiresWithoutAnAction(t *testing.T) {
51 var cache desktopStatusCache
52 load := func(context.Context) ([]byte, error) { return []byte(`{"running":true}`), nil }
53 if _, err := cache.read(t.Context(), t.Context(), load); err != nil {
54 t.Fatal(err)
55 }
56 time.Sleep(2100 * time.Millisecond)
57 got, err := cache.read(t.Context(), t.Context(), func(context.Context) ([]byte, error) { return []byte(`{"running":false}`), nil })
58 if err != nil || string(got) != `{"running":false}` {
59 t.Fatalf("external app stop remained stale: %s %v", got, err)
60 }
61}
62
63func TestDesktopStatusDoesNotCacheFailedProbe(t *testing.T) {
64 var cache desktopStatusCache
65 var calls int
66 load := func(context.Context) ([]byte, error) {
67 calls++
68 if calls == 1 {
69 return nil, errors.New("guest disconnected")
70 }
71 return []byte(`{"running":true}`), nil
72 }
73 if _, err := cache.read(t.Context(), t.Context(), load); err == nil {
74 t.Fatal("lost guest was reported as healthy")
75 }
76 if _, err := cache.read(t.Context(), t.Context(), load); err != nil {
77 t.Fatal(err)
78 }
79 if calls != 2 {
80 t.Fatalf("failure prevented recovery: %d", calls)
81 }
82}
83
84func TestDesktopStatusOldProbeCannotOverwriteAction(t *testing.T) {
85 var cache desktopStatusCache
86 started, release := make(chan struct{}), make(chan struct{})
87 done := make(chan struct{})
88 go func() {
89 defer close(done)
90 _, _ = cache.read(t.Context(), t.Context(), func(context.Context) ([]byte, error) {
91 close(started)
92 <-release
93 return []byte(`{"running":false}`), nil
94 })
95 }()
96 <-started
97 cache.invalidate()
98 load := func(context.Context) ([]byte, error) { return []byte(`{"running":true}`), nil }
99 if _, err := cache.read(t.Context(), t.Context(), load); err != nil {
100 t.Fatal(err)
101 }
102 close(release)
103 <-done
104 got, err := cache.read(t.Context(), t.Context(), func(context.Context) ([]byte, error) { t.Error("new state lost from cache"); return nil, nil })
105 if err != nil || string(got) != `{"running":true}` {
106 t.Fatalf("stale probe replaced new state: %s %v", got, err)
107 }
108}