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

import (
	"testing"
)

func TestDetectAgent(t *testing.T) {
	agents := builtinAgents()
	tests := []struct {
		name    string
		content string
		want    string
	}{
		{"claude code prompt", "────\n❯ hello world\n────", "claude"},
		{"claude code banner", "claude code v1.2\n❯ ", "claude"},
		{"claude from anthropic", "Powered by Anthropic\n❯ ", "claude"},
		{"cursor box ui", "│ → type here │\n/ commands · @ files", "cursor"},
		{"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
	}{
		{"claude", "claude"},
		{"Claude", "claude"},
		{"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_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())
		}
	}
}