summaryrefslogtreecommitdiff
path: root/internal/tmuxedit/agent_test.go
blob: 1debfc4832a82f8a1806bc54621a324aebd051ee (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
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
package tmuxedit

import (
	"fmt"
	"strings"
	"testing"
)

func TestDetectAgent(t *testing.T) {
	agents := builtinAgents()
	tests := []struct {
		name    string
		content string
		want    string
	}{
		{"cursor box ui", "│ → type here │\n/ commands · @ files", "cursor"},
		// Cursor panes often show Claude model names; cursor's box UI must be detected first
		{"cursor not false claude", "Claude 4.5 Sonnet\n│ → test │\n/ commands · @ files", "cursor"},
		{"amp from banner", "Amp by Sourcegraph\n> ", "amp"},
		{"aider from banner", "aider v0.50\n> /help", "aider"},
		{"no match", "some random terminal output\n$ ", "generic"},
		{"empty content", "", "generic"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := detectAgent(tt.content, agents)
			if got.Name() != tt.want {
				t.Errorf("detectAgent() = %q, want %q", got.Name(), tt.want)
			}
		})
	}
}

func TestFindAgentByName(t *testing.T) {
	agents := builtinAgents()
	tests := []struct {
		name string
		want string
	}{
		{"CURSOR", "cursor"},
		{"amp", "amp"},
		{"nonexistent", "generic"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := findAgentByName(tt.name, agents)
			if got.Name() != tt.want {
				t.Errorf("findAgentByName(%q) = %q, want %q", tt.name, got.Name(), tt.want)
			}
		})
	}
}

func TestDetectAgent_InvalidRegex(t *testing.T) {
	agents := []Agent{
		&configAgent{baseAgent{name: "bad", detectPattern: "[invalid"}},
	}
	got := detectAgent("anything", agents)
	if got.Name() != "generic" {
		t.Errorf("expected generic fallback for invalid regex, got %q", got.Name())
	}
}

func TestGenericAgent(t *testing.T) {
	g := genericAgent()
	if g.Name() != "generic" {
		t.Errorf("Name = %q, want generic", g.Name())
	}
}

func TestBaseAgent_SendText_Empty(t *testing.T) {
	b := &baseAgent{newlineKeys: "S-Enter"}
	err := b.SendText("%1", "")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestBaseAgent_ClearInput_Disabled(t *testing.T) {
	b := &baseAgent{clearFirst: false, clearKeys: "C-u"}
	err := b.ClearInput("%1")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestBaseAgent_ClearInput_EmptyKeys(t *testing.T) {
	// clearFirst=true but no clearKeys should be a no-op
	b := &baseAgent{clearFirst: true, clearKeys: ""}
	err := b.ClearInput("%1")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestBaseAgent_ClearInput_Enabled(t *testing.T) {
	noSleep(t)
	var calls []string
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		calls = append(calls, fmt.Sprintf("send:%s:%s", paneID, strings.Join(keys, ",")))
		return nil
	}

	b := &baseAgent{clearFirst: true, clearKeys: "C-u"}
	err := b.ClearInput("%2")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(calls) != 1 || calls[0] != "send:%2:C-u" {
		t.Errorf("expected single C-u send call, got %v", calls)
	}
}

func TestBaseAgent_ClearInput_Error(t *testing.T) {
	noSleep(t)
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(string, ...string) error {
		return fmt.Errorf("send failed")
	}

	b := &baseAgent{clearFirst: true, clearKeys: "C-u"}
	err := b.ClearInput("%1")
	if err == nil {
		t.Fatal("expected error from sendClearSequence failure")
	}
}

func TestBaseAgent_ExtractPrompt_NoPattern(t *testing.T) {
	b := &baseAgent{}
	got := b.ExtractPrompt("some content")
	if got != "" {
		t.Errorf("expected empty, got %q", got)
	}
}

func TestBaseAgent_ExtractPrompt_InvalidRegex(t *testing.T) {
	b := &baseAgent{promptPat: "[invalid"}
	got := b.ExtractPrompt("> test")
	if got != "" {
		t.Errorf("expected empty for invalid regex, got %q", got)
	}
}

func TestConfigurable_Interface(t *testing.T) {
	// Verify that all agent types implement Configurable
	agents := builtinAgents()
	for _, a := range agents {
		c, ok := a.(Configurable)
		if !ok {
			t.Errorf("agent %q does not implement Configurable", a.Name())
			continue
		}
		base := c.Base()
		if base.name != a.Name() {
			t.Errorf("Base().name = %q, want %q", base.name, a.Name())
		}
	}
}