| 1 | package ui |
| 2 | |
| 3 | import ( |
| 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 | |
| 15 | const 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 | |
| 18 | func 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 | |
| 27 | func 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 | |
| 40 | func 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 | |
| 53 | func 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 | |
| 64 | func 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 | |
| 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 | } |