跳到主要内容

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

浏览文件

stream_flood_test.go

97 行 · 2.8 KB历史下载
1package diagnostics
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/exec"
8 "strings"
9 "testing"
10 "time"
11)
12
13func TestStreamDrainsWarningFlood(t *testing.T) {
14 const flag = "CAGE_DIAGNOSTICS_FLOOD"
15 const count = 200000
16 const warning = "W: Tried to start delayed item package, but failed\n"
17 if os.Getenv(flag) == "1" {
18 chunk := strings.Repeat(warning, 1000)
19 for i := 0; i < count/1000; i++ {
20 fmt.Fprint(os.Stderr, chunk)
21 }
22 fmt.Fprintln(os.Stderr, "installation completed")
23 return
24 }
25 s := New(t.TempDir())
26 var visible strings.Builder
27 w := NewStream(s, "setup", "info", func(line string) { visible.WriteString(line) })
28 ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
29 defer cancel()
30 exe, err := os.Executable()
31 if err != nil {
32 t.Fatal(err)
33 }
34 cmd := exec.CommandContext(ctx, exe, "-test.run=^TestStreamDrainsWarningFlood$")
35 cmd.Env = append(os.Environ(), flag+"=1")
36 cmd.Stderr = w
37 start := time.Now()
38 if err := cmd.Run(); err != nil {
39 t.Fatal(err)
40 }
41 w.Close()
42 events := readEvents(t, s)
43 total := uint64(1)
44 for _, event := range events {
45 var repeats uint64
46 if _, err := fmt.Sscanf(event.Message, "[previous output repeated %d additional times]", &repeats); err == nil {
47 total += repeats
48 }
49 }
50 if total != count || len(events) > 25 || !strings.Contains(visible.String(), "installation completed") {
51 t.Fatalf("flood not coalesced or final output lost: count=%d events=%d", total, len(events))
52 }
53 if w.batchBytes != 0 || len(w.line) != 0 || len(visible.String()) > 4096 {
54 t.Fatal("output retained after drain")
55 }
56 t.Logf("drained %d warnings (%d bytes) in %s, %d events", count, count*len(warning), time.Since(start), len(events))
57}
58
59func TestStreamCoalescingKeepsProgressAndDistinctSecrets(t *testing.T) {
60 s := New(t.TempDir())
61 w := s.Stream("setup", "info")
62 input := "password=one\npassword=two\nCAGE_TOOL_BYTES pkg 0 10\nCAGE_TOOL_BYTES pkg 0 10\nlast\nlast\n"
63 w.Write([]byte(input))
64 w.Close()
65 events := readEvents(t, s)
66 if len(events) != 6 || !strings.Contains(events[5].Message, "1 additional times") {
67 t.Fatalf("unexpected events: %+v", events)
68 }
69 for _, e := range events {
70 if strings.Contains(e.Message, "one") || strings.Contains(e.Message, "two") {
71 t.Fatal("secret leaked")
72 }
73 }
74}
75
76func TestStreamBatchRotationPreservesEvents(t *testing.T) {
77 s := New(t.TempDir())
78 s.fileBytes = 1200
79 s.maxBytes = 100000
80 w := s.Stream("setup", "info")
81 var input strings.Builder
82 for i := 0; i < 200; i++ {
83 fmt.Fprintf(&input, "package %d installed\n", i)
84 }
85 w.Write([]byte(input.String()))
86 w.Close()
87 events := readEvents(t, s)
88 if len(events) != 200 {
89 t.Fatalf("batch rotation lost events: %d", len(events))
90 }
91 files, _ := s.files()
92 for _, f := range files {
93 if f.Size() > s.fileBytes {
94 t.Fatalf("file exceeded rotation bound: %s %d", f.Name(), f.Size())
95 }
96 }
97}