跳到主要内容

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

浏览文件

ui_test.go

90 行 · 3.0 KB历史下载
1package ui
2
3import (
4 "bytes"
5 "errors"
6 "io"
7 "os"
8 "strconv"
9 "strings"
10 "testing"
11
12 "github.com/sylvinus/agent-vm/internal/bashref"
13)
14
15const para = "An agent could write .git/config or .git/hooks in your projects, and git on this machine would run them, even when your editor or shell prompt calls git.\n\n brew unlink lima 2>/dev/null; brew install sylvinus/tap/lima-sylvinus with a long command line that must stay whole\nshort\n" +
16 "averyveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongword and then some more words after it\n"
17
18func TestWrapBash(t *testing.T) {
19 for w := 8; w <= 72; w++ {
20 r := bashref.Script(t, "", nil, `printf '%s' "$2" | _agent_vm_wrap "$1"`, strconv.Itoa(w), para)
21 if got := Wrap(para, w); got != r.Stdout {
22 t.Errorf("Wrap(%d):\n%s\nbash:\n%s", w, got, r.Stdout)
23 }
24 }
25}
26
27func TestBoxBash(t *testing.T) {
28 for _, title := range []string{"Lima cannot keep .git read-only", strings.Repeat("t", 80)} {
29 r := bashref.Script(t, "", nil, `_agent_vm_have_tty() { return 1; }; printf '%s' "$2" | _agent_vm_box "$1"`, title, para)
30 var b bytes.Buffer
31 u := &UI{Stderr: &b, Width: 72}
32 u.Box(title, para)
33 if b.String() != r.Stderr {
34 t.Errorf("Box(%q):\n%s\nbash:\n%s", title, b.String(), r.Stderr)
35 }
36 }
37}
38
39// A log's tail is shown without what could drive the terminal.
40func TestTail(t *testing.T) {
41 p := t.TempDir() + "/log"
42 lines := []string{"one", "two \033[31mred\033[0m", "title \033]0;pwned\007 set", "tab\there\r", "last"}
43 os.WriteFile(p, []byte(strings.Join(lines, "\n")+"\n"), 0o644)
44 got := Tail(p, 4)
45 if strings.ContainsAny(got, "\033\007\r") {
46 t.Errorf("control characters left: %q", got)
47 }
48 if got != "two red\ntitle ]0;pwned set\ntab\there\nlast\n" {
49 t.Errorf("Tail = %q", got)
50 }
51}
52
53func fakeTTY(answers ...string) func() (io.ReadCloser, error) {
54 return func() (io.ReadCloser, error) {
55 if len(answers) == 0 {
56 return io.NopCloser(strings.NewReader("")), nil
57 }
58 a := answers[0]
59 answers = answers[1:]
60 return io.NopCloser(strings.NewReader(a)), nil
61 }
62}
63
64func TestAsk(t *testing.T) {
65 var b bytes.Buffer
66 u := &UI{Stderr: &b, OpenTTY: fakeTTY("\n", "y\n", "no\n", "Yes\r\n")}
67 if !u.AskYN("Q?", true) || !u.AskYN("Q?", false) || u.AskYN("Q?", true) || !u.AskYN("Q?", false) {
68 t.Error("AskYN answers")
69 }
70 if b.String() != " Q? [Y/n]: Q? [y/N]: Q? [Y/n]: Q? [y/N]: " {
71 t.Errorf("prompts: %q", b.String())
72 }
73 // No terminal: the default, never a yes it was not given.
74 u = &UI{Stderr: io.Discard, OpenTTY: func() (io.ReadCloser, error) { return nil, errors.New("no tty") }}
75 if u.AskYN("Q?", false) || !u.AskYN("Q?", true) || u.CanAsk() {
76 t.Error("without a terminal")
77 }
78 b.Reset()
79 u = &UI{Stderr: &b, OpenTTY: fakeTTY("10G\n", "0\n", "7\n")}
80 if n := u.AskInt("Disk", 10, 0); n != 7 {
81 t.Errorf("AskInt = %d", n)
82 }
83 if !strings.Contains(b.String(), "(must be a positive integer, e.g. 10; got: 10G)") {
84 t.Errorf("AskInt said: %q", b.String())
85 }
86 u = &UI{Stderr: io.Discard, OpenTTY: fakeTTY("4\n", "\n")}
87 if n := u.AskChoice("Pick", 2, "a", "b", "c"); n != 2 {
88 t.Errorf("AskChoice = %d", n)
89 }
90}