summaryrefslogtreecommitdiff
path: root/internal/hexaicli/editor_integration_test.go
blob: 784b83ccc2a351d8f0d9b80b90405f0e2e24bbda (plain)
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
package hexaicli

import (
	"bytes"
	"context"
	"testing"

	"codeberg.org/snonux/hexai/internal/appconfig"
	"codeberg.org/snonux/hexai/internal/llm"
)

type cliFake struct{}

func (cliFake) Chat(_ context.Context, _ []llm.Message, _ ...llm.RequestOption) (string, error) {
	return "OUT", nil
}
func (cliFake) Name() string         { return "fake" }
func (cliFake) DefaultModel() string { return "m" }
func (cliFake) CodeCompletion(context.Context, string, string, int, string, float64) ([]string, error) {
	return nil, nil
}

func TestRun_NoArgs_OpensEditor(t *testing.T) {
	runner := NewRunner()
	runner.newClient = func(_ appconfig.App) (llm.Client, error) { return cliFake{}, nil }
	runner.openEditor = func(context.Context, []byte) (string, error) { return "PROMPT", nil }

	// Provide stdin selection
	var stdout, stderr bytes.Buffer
	if err := runner.Run(context.Background(), nil, bytes.NewBufferString("SELECTION"), &stdout, &stderr); err != nil {
		t.Fatalf("Run: %v", err)
	}
	if stdout.String() == "" {
		t.Fatalf("expected some output")
	}
}

func TestRun_WithArgs_DoesNotOpenEditor(t *testing.T) {
	runner := NewRunner()
	runner.newClient = func(_ appconfig.App) (llm.Client, error) { return cliFake{}, nil }
	called := false
	runner.openEditor = func(context.Context, []byte) (string, error) {
		called = true
		return "", nil
	}
	var stdout, stderr bytes.Buffer
	if err := runner.Run(context.Background(), []string{"ARG"}, bytes.NewBufferString("SEL"), &stdout, &stderr); err != nil {
		t.Fatalf("Run: %v", err)
	}
	if called {
		t.Fatalf("editor should not be invoked when args provided")
	}
}