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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"codeberg.org/snonux/hexai/internal/tmux"
)
func TestShouldRunInTmux_Preferences(t *testing.T) {
// no-tmux overrides
if shouldRunInTmux(false, true) {
t.Fatal("expected false when no-tmux is set")
}
// force tmux overrides
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 })
// Simulate Helix :pipe (no TTY) and tmux available
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")
}
// Simulate TTY present: prefer inline
isTTYFn = func(_ uintptr) bool { return true }
if shouldRunInTmux(false, false) {
t.Fatal("expected false when TTY present")
}
// Simulate tmux not available
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 os.Stdin to a temp file with 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, err := os.Open(src)
if err != nil { t.Fatalf("open src: %v", err) }
old := os.Stdin
os.Stdin = f
t.Cleanup(func() { os.Stdin = old; _ = f.Close() })
if err := persistStdin(path); err != nil {
t.Fatalf("persistStdin error: %v", err)
}
b, err := os.ReadFile(path)
if err != nil { t.Fatalf("read out: %v", err) }
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")
if err := os.WriteFile(in, []byte("hello"), 0o600); err != nil {
t.Fatalf("write in: %v", err)
}
if err := echoThrough(in, out); 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()
oldIn := os.Stdin
os.Stdin = rIn
defer func() { os.Stdin = oldIn; _ = rIn.Close() }()
// capture stdout
r, w, _ := os.Pipe()
oldOut := os.Stdout
os.Stdout = w
defer func() { os.Stdout = oldOut; _ = r.Close(); _ = w.Close() }()
if err := echoThrough("", ""); err != nil { t.Fatalf("echoThrough: %v", err) }
_ = w.Close()
data, _ := io.ReadAll(r)
if string(data) != "PIPE" {
t.Fatalf("stdout: %q", string(data))
}
}
func TestWaitForFile(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "x")
go func() {
// create shortly after
f, _ := os.Create(p)
defer f.Close()
f.WriteString("ok")
}()
if err := waitForFile(p, 2_000_000_000); err != nil { // 2s
t.Fatalf("waitForFile: %v", err)
}
}
func TestCatFileToStdout(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "f")
if err := os.WriteFile(p, []byte("abc"), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
// capture stdout
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
defer func() { os.Stdout = old; _ = r.Close(); _ = w.Close() }()
if err := catFileToStdout(p); err != nil {
t.Fatalf("catFileToStdout: %v", err)
}
_ = w.Close()
buf, _ := io.ReadAll(r)
if string(buf) != "abc" {
t.Fatalf("stdout = %q", string(buf))
}
}
func TestRunInTmuxParent_Stubbed(t *testing.T) {
dir := t.TempDir()
// set stdin content
src := filepath.Join(dir, "stdin.txt")
_ = os.WriteFile(src, []byte("input"), 0o600)
f, _ := os.Open(src)
oldStdin := os.Stdin
os.Stdin = f
defer func() { os.Stdin = oldStdin; _ = f.Close() }()
// capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
defer func() { os.Stdout = oldStdout; _ = r.Close(); _ = w.Close() }()
// stub seams
oldExec := osExecutableFn
oldSplit := splitRunFn
oldRun := hexaiactionRun
osExecutableFn = func() (string, error) { return "/bin/hexai-action", nil }
splitRunFn = func(opts tmux.SplitOpts, argv []string) error {
// find -outfile path and write content to simulate child
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
}
// Ensure child mode won't try to run the real TUI if invoked in tests.
hexaiactionRun = func(_ context.Context, _ io.Reader, w io.Writer, _ io.Writer) error {
_, _ = io.WriteString(w, "child-stub")
return nil
}
defer func() { osExecutableFn = oldExec; splitRunFn = oldSplit; hexaiactionRun = oldRun }()
if err := runInTmuxParent("", "v", 33); err != nil {
t.Fatalf("runInTmuxParent: %v", err)
}
_ = w.Close()
got, _ := io.ReadAll(r)
if !strings.HasPrefix(string(got), "OUT:") {
t.Fatalf("unexpected stdout: %q", string(got))
}
}
func TestRunChild_StubbedOutfile(t *testing.T) {
dir := t.TempDir()
in := filepath.Join(dir, "in.txt")
out := filepath.Join(dir, "out.txt")
_ = os.WriteFile(in, []byte("sel"), 0o600)
old := hexaiactionRun
hexaiactionRun = func(_ context.Context, _ io.Reader, w io.Writer, _ io.Writer) error {
_, _ = io.WriteString(w, "RESULT")
return nil
}
defer func() { hexaiactionRun = old }()
if err := runChild(in, out); err != nil {
t.Fatalf("runChild: %v", err)
}
b, _ := os.ReadFile(out)
if string(b) != "RESULT" {
t.Fatalf("unexpected outfile: %q", string(b))
}
}
func TestRunChild_StubbedStdout(t *testing.T) {
dir := t.TempDir()
in := filepath.Join(dir, "in.txt")
_ = os.WriteFile(in, []byte("sel"), 0o600)
// capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
defer func() { os.Stdout = oldStdout; _ = r.Close(); _ = w.Close() }()
old := hexaiactionRun
hexaiactionRun = func(_ context.Context, _ io.Reader, w io.Writer, _ io.Writer) error {
_, _ = io.WriteString(w, "STDOUT-RESULT")
return nil
}
defer func() { hexaiactionRun = old }()
if err := runChild(in, ""); err != nil {
t.Fatalf("runChild: %v", err)
}
_ = w.Close()
data, _ := io.ReadAll(r)
if string(data) != "STDOUT-RESULT" {
t.Fatalf("stdout: %q", string(data))
}
}
func TestRunChild_ErrorFallback(t *testing.T) {
dir := t.TempDir()
in := filepath.Join(dir, "in.txt")
out := filepath.Join(dir, "out.txt")
_ = os.WriteFile(in, []byte("INPUT"), 0o600)
old := hexaiactionRun
hexaiactionRun = func(_ context.Context, _ io.Reader, _ io.Writer, _ io.Writer) error {
return fmt.Errorf("boom")
}
defer func() { hexaiactionRun = old }()
if err := runChild(in, out); err != nil {
t.Fatalf("runChild: %v", err)
}
b, _ := os.ReadFile(out)
if string(b) != "INPUT" {
t.Fatalf("expected fallback echo, got %q", string(b))
}
}
func TestWaitForFile_Timeout(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "nope")
if err := waitForFile(p, 10_000_000); err == nil { // 10ms
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)) }
}
func TestRunInTmuxParent_ExecutableError(t *testing.T) {
old := osExecutableFn
osExecutableFn = func() (string, error) { return "", fmt.Errorf("no exe") }
defer func() { osExecutableFn = old }()
// set stdin content
r, w, _ := os.Pipe()
_, _ = w.Write([]byte("x"))
_ = w.Close()
oldIn := os.Stdin
os.Stdin = r
defer func() { os.Stdin = oldIn; _ = r.Close() }()
if err := runInTmuxParent("", "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") }
defer func() { osExecutableFn = oldExec; splitRunFn = oldSplit }()
// set stdin
r, w, _ := os.Pipe()
_, _ = w.Write([]byte("x"))
_ = w.Close()
oldIn := os.Stdin
os.Stdin = r
defer func() { os.Stdin = oldIn; _ = r.Close() }()
if err := runInTmuxParent("", "v", 33); err == nil {
t.Fatal("expected split error")
}
}
func TestEchoThrough_OutfileError(t *testing.T) {
dir := t.TempDir()
in := filepath.Join(dir, "i.txt")
_ = os.WriteFile(in, []byte("x"), 0o600)
// Outfile inside non-existent subdir -> Create should fail
out := filepath.Join(dir, "nope", "out.txt")
if err := echoThrough(in, out); err == nil {
t.Fatal("expected echoThrough outfile error")
}
}
func TestPersistStdin_Error(t *testing.T) {
// Parent directory missing -> Create should fail
dir := t.TempDir()
p := filepath.Join(dir, "missing", "x.txt")
// set stdin to something
r, w, _ := os.Pipe()
_, _ = w.Write([]byte("x"))
_ = w.Close()
old := os.Stdin
os.Stdin = r
defer func() { os.Stdin = old; _ = r.Close() }()
if err := persistStdin(p); err == nil {
t.Fatal("expected persistStdin error")
}
}
func TestCatFileToStdout_Error(t *testing.T) {
if err := catFileToStdout("/nonexistent/path/file.txt"); err == nil {
t.Fatal("expected error for missing file")
}
}
func TestOpenIO_Errors(t *testing.T) {
// Non-existent infile
if _, _, _, _, err := openIO("/definitely/missing/file.txt", ""); err == nil {
t.Fatal("expected infile error")
}
// Outfile in missing dir
dir := t.TempDir()
out := filepath.Join(dir, "nope", "x.txt")
if _, _, _, _, err := openIO("", out); err == nil {
t.Fatal("expected outfile error")
}
}
|