summaryrefslogtreecommitdiff
path: root/internal/hexaiaction/run_more_test.go
blob: 67ffa96164ba94dd86437f0800bcf32aace5a51f (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
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
package hexaiaction

import (
	"bytes"
	"context"
	"log"
	"os"
	"strings"
	"testing"

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

// Covers the early error path in Run when no API key is available for the default provider.
func TestRun_MissingAPIKey(t *testing.T) {
	// Ensure no provider API keys in env
	for _, k := range []string{"HEXAI_OPENAI_API_KEY", "OPENAI_API_KEY"} {
		t.Setenv(k, "")
	}
	// Provide minimal stdin to get past empty input check (if reached)
	in := bytes.NewBufferString("some selection text")
	var out bytes.Buffer
	var errBuf bytes.Buffer
	// Expect an error due to missing OPENAI_API_KEY (default provider is openai)
	if err := Run(context.Background(), in, &out, &errBuf); err == nil {
		t.Fatal("expected error when API key is missing")
	}
	_ = os.Stderr
}

func TestRun_NoInput_IsActionable(t *testing.T) {
	runner := NewRunner()
	runner.loadConfig = func(context.Context, *log.Logger) appconfig.App { return appconfig.Load(nil) }
	runner.newClient = func(appconfig.App) (actionClient, error) { return llmFake{}, nil }
	runner.chooseAction = func(appconfig.App) (actionChoice, error) {
		return actionChoice{kind: ActionSkip}, nil
	}
	err := runner.Run(context.Background(), bytes.NewBufferString(""), &bytes.Buffer{}, &bytes.Buffer{})
	if err == nil {
		t.Fatal("expected actionable no-input error")
	}
	if !strings.Contains(err.Error(), "pipe the selected text or pane contents into hexai-tmux-action") {
		t.Fatalf("expected actionable guidance, got %q", err.Error())
	}
}

type stubChatDoer struct {
	calls int
	msgs  [][]llm.Message
}

func (s *stubChatDoer) Chat(ctx context.Context, msgs []llm.Message, opts ...llm.RequestOption) (string, error) {
	s.calls++
	s.msgs = append(s.msgs, msgs)
	return "ok", nil
}

func (s *stubChatDoer) DefaultModel() string { return "stub" }

func TestHandleDiagnosticsActionInvokesLLM(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS", "0")
	parts := InputParts{Diagnostics: []string{"warn1"}, Selection: "code"}
	client := &stubChatDoer{}
	cfg := appconfig.Load(nil)
	if _, err := handleDiagnosticsAction(context.Background(), parts, &cfg, client); err != nil {
		t.Fatalf("handleDiagnosticsAction: %v", err)
	}
	if client.calls != 1 {
		t.Fatalf("expected 1 chat call, got %d", client.calls)
	}
	found := false
	for _, msg := range client.msgs[0] {
		if msg.Role == "user" && strings.Contains(msg.Content, "warn1") {
			found = true
		}
	}
	if !found {
		t.Fatalf("expected diagnostics content in message: %#v", client.msgs[0])
	}
}

func TestHandleSimplifyActionPassesSelection(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS", "0")
	parts := InputParts{Selection: "value := 1"}
	client := &stubChatDoer{}
	cfg := appconfig.Load(nil)
	if _, err := handleSimplifyAction(context.Background(), parts, &cfg, client); err != nil {
		t.Fatalf("handleSimplifyAction: %v", err)
	}
	if client.calls != 1 {
		t.Fatalf("expected single chat invocation, got %d", client.calls)
	}
	seen := false
	for _, msg := range client.msgs[0] {
		if msg.Role == "user" && strings.Contains(msg.Content, "value := 1") {
			seen = true
		}
	}
	if !seen {
		t.Fatalf("expected selection echoed in prompt: %#v", client.msgs[0])
	}
}

func TestHandleCustomActionUsesProvidedCustom(t *testing.T) {
	t.Setenv("HEXAI_TMUX_STATUS", "0")
	sel := appconfig.CustomAction{ID: "custom", Title: "Do", Instruction: "do it"}
	parts := InputParts{Selection: "text"}
	client := &stubChatDoer{}
	cfg := appconfig.Load(nil)
	if _, err := handleCustomAction(context.Background(), parts, &cfg, client, &sel); err != nil {
		t.Fatalf("handleCustomAction: %v", err)
	}
	if client.calls != 1 {
		t.Fatalf("expected custom action to invoke chat, got %d calls", client.calls)
	}
}