diff options
Diffstat (limited to 'internal/hexaicli')
| -rw-r--r-- | internal/hexaicli/run.go | 8 | ||||
| -rw-r--r-- | internal/hexaicli/run_test.go | 13 |
2 files changed, 18 insertions, 3 deletions
diff --git a/internal/hexaicli/run.go b/internal/hexaicli/run.go index 823dcaa..11e8938 100644 --- a/internal/hexaicli/run.go +++ b/internal/hexaicli/run.go @@ -3,7 +3,6 @@ package hexaicli import ( - "bufio" "context" "fmt" "io" @@ -78,8 +77,11 @@ func RunWithClient(ctx context.Context, args []string, stdin io.Reader, stdout, func readInput(stdin io.Reader, args []string) (string, error) { var stdinData string if fi, err := os.Stdin.Stat(); err == nil && (fi.Mode()&os.ModeCharDevice) == 0 { - b, _ := io.ReadAll(bufio.NewReader(stdin)) - stdinData = strings.TrimSpace(string(b)) + data, readErr := io.ReadAll(stdin) + if readErr != nil { + return "", fmt.Errorf("hexai: failed to read stdin: %w", readErr) + } + stdinData = strings.TrimSpace(string(data)) } argData := strings.TrimSpace(strings.Join(args, " ")) switch { diff --git a/internal/hexaicli/run_test.go b/internal/hexaicli/run_test.go index d192850..a4184f6 100644 --- a/internal/hexaicli/run_test.go +++ b/internal/hexaicli/run_test.go @@ -12,6 +12,10 @@ import ( "codeberg.org/snonux/hexai/internal/llm" ) +type failingReader struct{ err error } + +func (f failingReader) Read([]byte) (int, error) { return 0, f.err } + func TestReadInput_Combinations(t *testing.T) { // stdin + arg restore, f := setStdin(t, "from-stdin") @@ -41,6 +45,15 @@ func TestReadInput_Combinations(t *testing.T) { } } +func TestReadInput_PropagatesStdinError(t *testing.T) { + restore, _ := setStdin(t, "ignored") + defer restore() + bad := failingReader{err: io.ErrUnexpectedEOF} + if _, err := readInput(bad, nil); err == nil || !strings.Contains(err.Error(), "failed to read stdin") { + t.Fatalf("expected stdin read error, got %v", err) + } +} + func TestBuildMessages_Explain(t *testing.T) { msgs := buildMessages("please explain this") if len(msgs) != 2 || msgs[0].Role != "system" || !strings.Contains(strings.ToLower(msgs[0].Content), "explanation") { |
