diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-07 11:26:10 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-07 11:26:10 +0300 |
| commit | 8889949ad3851bfbf36ff5b73128286d67c88201 (patch) | |
| tree | 0f515ae6ee3da898dea113799c09e943f3e3f8fb /cmd | |
| parent | 7c0266e94378f6121719939c6d53915eb72eed3e (diff) | |
tiding up
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/internal/hexai-action/main.go | 196 | ||||
| -rw-r--r-- | cmd/internal/hexai-action/main_test.go | 357 |
2 files changed, 523 insertions, 30 deletions
diff --git a/cmd/internal/hexai-action/main.go b/cmd/internal/hexai-action/main.go index 8bcc3cd..b8ba524 100644 --- a/cmd/internal/hexai-action/main.go +++ b/cmd/internal/hexai-action/main.go @@ -6,24 +6,62 @@ import ( "fmt" "io" "os" + "path/filepath" + "time" "codeberg.org/snonux/hexai/internal/hexaiaction" + "codeberg.org/snonux/hexai/internal/tmux" + "golang.org/x/term" ) func main() { infile := flag.String("infile", "", "Read input from this file instead of stdin") outfile := flag.String("outfile", "", "Write output to this file instead of stdout") + // Tmux/UI flags + forceTmux := flag.Bool("tmux", false, "Force running the UI in a tmux split-pane (auto if not set)") + noTmux := flag.Bool("no-tmux", false, "Disable tmux mode even if available") + uiChild := flag.Bool("ui-child", false, "INTERNAL: run interactive UI and write to -outfile atomically") + tmuxTarget := flag.String("tmux-target", "", "tmux split target (advanced)") + tmuxSplit := flag.String("tmux-split", "v", "tmux split orientation: v or h") + tmuxPercent := flag.Int("tmux-percent", 33, "tmux split size percentage (1-100)") flag.Parse() - in, out, closeIn, closeOut, err := openIO(*infile, *outfile) - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) + // Child mode: run TUI and write atomically to -outfile + if *uiChild { + if err := runChild(*infile, *outfile); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + return + } + + // Parent mode: decide inline vs tmux + if shouldRunInTmux(*forceTmux, *noTmux) { + if err := runInTmuxParent(*tmuxTarget, *tmuxSplit, *tmuxPercent); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + return } - defer closeIn() - defer closeOut() - if err := hexaiaction.Run(context.Background(), in, out, os.Stderr); err != nil { + // Inline path: only if we have a TTY for UI; otherwise echo input + if isTTY(os.Stdout.Fd()) && isTTY(os.Stdin.Fd()) { + in, out, closeIn, closeOut, err := openIO(*infile, *outfile) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + defer closeIn() + defer closeOut() + if err := hexaiactionRun(context.Background(), in, out, os.Stderr); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + return + } + + // Fallback: no TTY and tmux not available; echo input to output + if err := echoThrough(*infile, *outfile); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } @@ -54,3 +92,147 @@ func openIO(infile, outfile string) (io.Reader, io.Writer, func(), func(), error } return in, out, closeIn, closeOut, nil } + +// runChild runs the interactive flow and writes the final output atomically to outfile. +var hexaiactionRun = hexaiaction.Run + +func runChild(infile, outfile string) error { + if outfile == "" { + // No atomic handoff needed; just run normally to stdout + in, out, closeIn, closeOut, err := openIO(infile, "") + if err != nil { + return err + } + defer closeIn() + defer closeOut() + return hexaiactionRun(context.Background(), in, out, os.Stderr) + } + tmp := outfile + ".tmp" + in, out, closeIn, closeOut, err := openIO(infile, tmp) + if err != nil { + return err + } + defer closeIn() + if err := hexaiactionRun(context.Background(), in, out, os.Stderr); err != nil { + // On error, try to echo input to tmp to avoid blocking + closeOut() + if copyErr := echoThrough(infile, tmp); copyErr != nil { + return fmt.Errorf("hexai-action child: %v; echo failed: %v", err, copyErr) + } + } else { + closeOut() + } + return os.Rename(tmp, outfile) +} + +var isTTYFn = isTTY +var tmuxAvailableFn = tmux.Available +var splitRunFn = tmux.SplitRun +var osExecutableFn = os.Executable + +func shouldRunInTmux(forceTmux, noTmux bool) bool { + if noTmux { + return false + } + if forceTmux { + return true + } + // Auto: prefer tmux when stdio are not TTYs (Helix :pipe scenario) + if !(isTTYFn(os.Stdin.Fd()) && isTTYFn(os.Stdout.Fd())) && tmuxAvailableFn() { + return true + } + return false +} + +func isTTY(fd uintptr) bool { return term.IsTerminal(int(fd)) } + +func runInTmuxParent(target, split string, percent int) error { + // Prepare temp files + dir, err := os.MkdirTemp("", "hexai-action-") + if err != nil { + return err + } + defer func() { _ = os.RemoveAll(dir) }() + inPath := filepath.Join(dir, "input.txt") + outPath := filepath.Join(dir, "reply.txt") + // Read stdin and persist to inPath + if err := persistStdin(inPath); err != nil { + return err + } + // Build child argv + exe, err := osExecutableFn() + if err != nil { + return err + } + argv := []string{exe, "-ui-child", "-infile", inPath, "-outfile", outPath} + // Spawn tmux split + opts := tmux.SplitOpts{Target: target, Vertical: split != "h", Percent: percent} + if err := splitRunFn(opts, argv); err != nil { + return err + } + // Wait for outfile to appear + if err := waitForFile(outPath, 60*time.Second); err != nil { + return err + } + // Print to stdout + return catFileToStdout(outPath) +} + +func persistStdin(path string) error { + f, err := os.Create(path) + if err != nil { + return err + } + defer func() { _ = f.Close() }() + if _, err := io.Copy(f, os.Stdin); err != nil { + return err + } + return f.Sync() +} + +func waitForFile(path string, timeout time.Duration) error { + deadline := time.Now().Add(timeout) + for { + if _, err := os.Stat(path); err == nil { + return nil + } + if time.Now().After(deadline) { + return fmt.Errorf("hexai-action: timeout waiting for reply file") + } + time.Sleep(200 * time.Millisecond) + } +} + +func catFileToStdout(path string) error { + f, err := os.Open(path) + if err != nil { + return err + } + defer func() { _ = f.Close() }() + _, err = io.Copy(os.Stdout, f) + return err +} + +func echoThrough(infile, outfile string) error { + // Read from infile or stdin and write to outfile or stdout + var in io.Reader = os.Stdin + var out io.Writer = os.Stdout + if infile != "" { + f, err := os.Open(infile) + if err != nil { + return err + } + defer func() { _ = f.Close() }() + in = f + } + if outfile != "" { + f, err := os.Create(outfile) + if err != nil { + return err + } + defer func() { _ = f.Close() }() + out = f + } + _, err := io.Copy(out, in) + return err +} diff --git a/cmd/internal/hexai-action/main_test.go b/cmd/internal/hexai-action/main_test.go index 9603826..16bb2ed 100644 --- a/cmd/internal/hexai-action/main_test.go +++ b/cmd/internal/hexai-action/main_test.go @@ -1,44 +1,355 @@ package main import ( + "context" + "fmt" "io" "os" "path/filepath" + "strings" "testing" + + "codeberg.org/snonux/hexai/internal/tmux" ) -// TestOpenIO_InOutFiles verifies that openIO opens the specified files -// and that writing via the returned writer persists to disk. -func TestOpenIO_InOutFiles(t *testing.T) { +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() - inPath := filepath.Join(dir, "in.txt") - outPath := filepath.Join(dir, "out.txt") + 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) + } +} - // Prepare input file - want := "hello world" - if err := os.WriteFile(inPath, []byte(want), 0o600); err != nil { - t.Fatalf("write infile: %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() }() - in, out, cin, cout, err := openIO(inPath, outPath) - if err != nil { - t.Fatalf("openIO: %v", err) + // 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 cin() - defer cout() + defer func() { osExecutableFn = oldExec; splitRunFn = oldSplit; hexaiactionRun = oldRun }() - // Copy through to simulate main's behavior - if _, err := io.Copy(out.(io.Writer), in); err != nil { - t.Fatalf("copy: %v", err) + 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)) + } +} - // Verify outfile content - got, err := os.ReadFile(outPath) - if err != nil { - t.Fatalf("read outfile: %v", err) +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 } - if string(got) != want { - t.Fatalf("mismatch: got %q want %q", string(got), want) + 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") + } +} |
