1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package hexaiaction
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"codeberg.org/snonux/hexai/internal/tmux"
)
func TestShouldRunInTmux_Preferences(t *testing.T) {
if shouldRunInTmux(false, true) { t.Fatal("expected false when no-tmux is set") }
if !shouldRunInTmux(true, false) { t.Fatal("expected true when -tmux is set") }
}
func TestShouldRunInTmux_Auto(t *testing.T) {
oldIsTTY := isTTYFn
oldAvail := tmuxAvailableFn
t.Cleanup(func() { isTTYFn = oldIsTTY; tmuxAvailableFn = oldAvail })
isTTYFn = func(_ uintptr) bool { return false }
tmuxAvailableFn = func() bool { return true }
if !shouldRunInTmux(false, false) { t.Fatal("expected true when not TTY and tmux available") }
isTTYFn = func(_ uintptr) bool { return true }
if shouldRunInTmux(false, false) { t.Fatal("expected false when TTY present") }
isTTYFn = func(_ uintptr) bool { return false }
tmuxAvailableFn = func() bool { return false }
if shouldRunInTmux(false, false) { t.Fatal("expected false when tmux unavailable") }
}
func TestPersistStdin_WritesFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "in.txt")
// Point stdin to content
src := filepath.Join(dir, "src.txt")
if err := os.WriteFile(src, []byte("hello world"), 0o600); err != nil { t.Fatalf("write src: %v", err) }
f, _ := os.Open(src)
defer f.Close()
if err := persistStdin(path, f); err != nil { t.Fatalf("persistStdin: %v", err) }
b, _ := os.ReadFile(path)
if string(b) != "hello world" { t.Fatalf("unexpected content %q", string(b)) }
}
func TestEchoThrough(t *testing.T) {
dir := t.TempDir()
in := filepath.Join(dir, "in.txt")
out := filepath.Join(dir, "out.txt")
_ = os.WriteFile(in, []byte("hello"), 0o600)
if err := echoThrough(in, out, os.Stdin, os.Stdout); err != nil { t.Fatalf("echoThrough: %v", err) }
b, _ := os.ReadFile(out)
if string(b) != "hello" { t.Fatalf("unexpected: %q", string(b)) }
}
func TestEchoThrough_StdinStdout(t *testing.T) {
// set stdin
rIn, wIn, _ := os.Pipe()
_, _ = wIn.Write([]byte("PIPE"))
_ = wIn.Close()
// capture stdout
r, w, _ := os.Pipe()
if err := echoThrough("", "", rIn, w); err != nil { t.Fatalf("echoThrough: %v", err) }
_ = w.Close()
data, _ := io.ReadAll(r)
if string(data) != "PIPE" { t.Fatalf("stdout: %q", string(data)) }
}
func TestRunInTmuxParent_Stubbed(t *testing.T) {
dir := t.TempDir()
// set stdin content
r, w, _ := os.Pipe()
_, _ = w.Write([]byte("input"))
_ = w.Close()
// capture stdout
rout, wout, _ := os.Pipe()
oldExec := osExecutableFn
oldSplit := splitRunFn
osExecutableFn = func() (string, error) { return "/bin/hexai-action", nil }
splitRunFn = func(opts tmux.SplitOpts, argv []string) error {
for i := 0; i < len(argv)-1; i++ {
if argv[i] == "-outfile" && i+1 < len(argv) {
_ = os.WriteFile(argv[i+1], []byte("OUT:"+strings.Join(argv, ",")), 0o600)
break
}
}
return nil
}
t.Cleanup(func() { osExecutableFn = oldExec; splitRunFn = oldSplit })
if err := runInTmuxParent(r, wout, "", "v", 33); err != nil { t.Fatalf("runInTmuxParent: %v", err) }
_ = wout.Close()
got, _ := io.ReadAll(rout)
if !strings.HasPrefix(string(got), "OUT:") { t.Fatalf("unexpected stdout: %q", string(got)) }
_ = dir
}
func TestRunInTmuxParent_ExecutableError(t *testing.T) {
old := osExecutableFn
osExecutableFn = func() (string, error) { return "", fmt.Errorf("no exe") }
t.Cleanup(func() { osExecutableFn = old })
r, w, _ := os.Pipe(); _, _ = w.Write([]byte("x")); _ = w.Close()
if err := runInTmuxParent(r, io.Discard, "", "v", 33); err == nil { t.Fatal("expected error from missing executable") }
}
func TestRunInTmuxParent_SplitError(t *testing.T) {
oldExec := osExecutableFn
osExecutableFn = func() (string, error) { return "/bin/hexai-action", nil }
oldSplit := splitRunFn
splitRunFn = func(_ tmux.SplitOpts, _ []string) error { return fmt.Errorf("split failed") }
t.Cleanup(func() { osExecutableFn = oldExec; splitRunFn = oldSplit })
r, w, _ := os.Pipe(); _, _ = w.Write([]byte("x")); _ = w.Close()
if err := runInTmuxParent(r, io.Discard, "", "v", 33); err == nil { t.Fatal("expected split error") }
}
func TestRunChild_StdoutAndOutfile(t *testing.T) {
// Outfile mode
dir := t.TempDir()
in := filepath.Join(dir, "in.txt")
out := filepath.Join(dir, "out.txt")
_ = os.WriteFile(in, []byte("sel"), 0o600)
oldRun := runFn
runFn = func(_ context.Context, _ io.Reader, w io.Writer, _ io.Writer) error { _, _ = io.WriteString(w, "RESULT"); return nil }
t.Cleanup(func(){ runFn = oldRun })
if err := runChild(context.Background(), in, out, io.Discard, io.Discard); err != nil { t.Fatalf("runChild: %v", err) }
b, _ := os.ReadFile(out)
if len(b) == 0 { t.Fatalf("expected some output") }
// Stdout mode
r, w, _ := os.Pipe()
if err := runChild(context.Background(), in, "", w, io.Discard); err != nil { t.Fatalf("runChild: %v", err) }
_ = w.Close(); buf, _ := io.ReadAll(r)
if len(buf) == 0 { t.Fatalf("expected stdout output") }
}
func TestWaitForFile_Timeout(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "nope")
if err := waitForFile(p, 10*time.Millisecond); err == nil { t.Fatal("expected timeout error") }
}
func TestOpenIO_InfileOutfile(t *testing.T) {
dir := t.TempDir()
in := filepath.Join(dir, "i"); out := filepath.Join(dir, "o")
_ = os.WriteFile(in, []byte("X"), 0o600)
r, w, ci, co, err := openIO(in, out)
if err != nil { t.Fatalf("openIO: %v", err) }
defer ci(); defer co()
if _, err := io.Copy(w, r); err != nil { t.Fatalf("copy: %v", err) }
b, _ := os.ReadFile(out)
if string(b) != "X" { t.Fatalf("got %q", string(b)) }
}
|