跳到主要内容

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

浏览文件

window_test.go

63 行 · 2.0 KB历史下载
1package ui
2
3import (
4 "bytes"
5 "fmt"
6 "strings"
7 "testing"
8)
9
10// A 40-column terminal gets a 40-column box; never under 30, nor over 72.
11func TestBoxWidth(t *testing.T) {
12 for width, want := range map[int]int{40: 40, 20: 30, 200: 72} {
13 var b bytes.Buffer
14 u := &UI{Stderr: &b, Width: width}
15 u.Box("Title", strings.Repeat("word ", 40))
16 for _, l := range strings.Split(strings.TrimSuffix(b.String(), "\n"), "\n") {
17 if len(l) > want {
18 t.Errorf("width %d: line of %d: %q", width, len(l), l)
19 }
20 }
21 if last := strings.Split(strings.TrimSuffix(b.String(), "\n"), "\n"); len(last[len(last)-1]) != want {
22 t.Errorf("width %d: bottom rule %q", width, last[len(last)-1])
23 }
24 }
25}
26
27// On a terminal: the last lines only, at most height, each cut to the width,
28// Lima's msg= kept, a progress bar's last redraw; cleared at Close. The log
29// gets everything, as written.
30func TestWindow(t *testing.T) {
31 var out, log bytes.Buffer
32 w := &Window{out: &out, log: &log, tty: true, width: 20, height: 3}
33 var in strings.Builder
34 for i := 1; i <= 5; i++ {
35 fmt.Fprintf(&in, "line %d\n", i)
36 }
37 in.WriteString(`time="2026-10-04T10:00:00Z" level=info msg="Downloading the image"` + "\n")
38 in.WriteString("10%\r50%\r100%\n")
39 in.WriteString(strings.Repeat("x", 40) + "\n")
40 in.WriteString("\x1b[31mred\x1b[0m\tend")
41 w.Write([]byte(in.String()))
42 if strings.Join(w.lines, "|") != "Downloading the ima|100%|"+strings.Repeat("x", 19) {
43 t.Errorf("window: %q", w.lines)
44 }
45 if log.String() != in.String() {
46 t.Errorf("log: %q", log.String())
47 }
48 w.Close()
49 if !strings.HasSuffix(out.String(), "\033[3A\r\033[J") || w.drawn != 0 {
50 t.Errorf("not cleared: %q", out.String()[max(len(out.String())-20, 0):])
51 }
52 if strings.Join(w.lines, "|") != "100%|"+strings.Repeat("x", 19)+"|red end" {
53 t.Errorf("last line: %q", w.lines)
54 }
55 // Not a terminal: every line as is.
56 out.Reset()
57 w = &Window{out: &out, log: &log, width: 20, height: 3}
58 w.Write([]byte("a\nb\n"))
59 w.Close()
60 if out.String() != "a\nb\n" {
61 t.Errorf("no terminal: %q", out.String())
62 }
63}