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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
package tmuxedit
import (
"fmt"
"strings"
"testing"
)
func TestClaudeAgent_ExtractPrompt(t *testing.T) {
agent := newClaudeAgent()
tests := []struct {
name string
content string
want string
}{
{
name: "single line",
content: "──────\n❯ hello world\n──────",
want: "hello world",
},
{
name: "multi-line between rules",
content: "previous output\n" +
"──────────────\n" +
"❯ first line\n" +
"\n" +
"❯ second line\n" +
"\n" +
"❯ third line\n" +
"──────────────\n" +
" -- INSERT --",
want: "first line\nsecond line\nthird line",
},
{
name: "wrapped long line",
content: "──────────────\n" +
"❯ This is a really long prompt that wraps\n" +
" to a second line in the terminal\n" +
"──────────────\n" +
" -- INSERT --",
want: "This is a really long prompt that wraps\nto a second line in the terminal",
},
{
name: "ignores previous messages",
content: "──────────────\n" +
"❯ old user message\n" +
"──────────────\n" +
"assistant response here\n" +
"──────────────\n" +
"❯ current prompt\n" +
"──────────────\n" +
" -- INSERT --",
want: "current prompt",
},
{
name: "no match",
content: "no prompt here",
want: "",
},
{
name: "no section delimiters",
content: "❯ hello world",
want: "hello world",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := agent.ExtractPrompt(tt.content)
if got != tt.want {
t.Errorf("ExtractPrompt() = %q, want %q", got, tt.want)
}
})
}
}
func TestClaudeAgent_ClearInput(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
}
agent := newClaudeAgent()
err := agent.ClearInput("%3")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// "C-a C-k" (Emacs/readline style) should send each as separate send-keys call
want := []string{
"send:%3:C-a",
"send:%3:C-k",
}
if len(calls) != len(want) {
t.Fatalf("got %d calls, want %d: %v", len(calls), len(want), calls)
}
for i, w := range want {
if calls[i] != w {
t.Errorf("call[%d] = %q, want %q", i, calls[i], w)
}
}
}
func TestClaudeAgent_ExtractPrompt_EmptyPattern(t *testing.T) {
agent := &claudeAgent{baseAgent{promptPat: "", sectionPat: `^─{5,}`}}
got := agent.ExtractPrompt("──────\n❯ hello\n──────")
if got != "" {
t.Errorf("expected empty for empty pattern, got %q", got)
}
}
func TestClaudeAgent_ExtractPrompt_InvalidRegex(t *testing.T) {
agent := &claudeAgent{baseAgent{promptPat: "[invalid", sectionPat: `^─{5,}`}}
got := agent.ExtractPrompt("──────\n❯ hello\n──────")
if got != "" {
t.Errorf("expected empty for invalid regex, got %q", got)
}
}
func TestClaudeAgent_ExtractPrompt_ContinuationBreaksOnEmpty(t *testing.T) {
agent := newClaudeAgent()
// Empty line between prompt blocks should break continuation
content := "──────────────\n" +
"❯ first line\n" +
" continued\n" +
"\n" +
"unrelated text\n" +
"──────────────"
got := agent.ExtractPrompt(content)
want := "first line\ncontinued"
if got != want {
t.Errorf("ExtractPrompt() = %q, want %q", got, want)
}
}
func TestClaudeAgent_ClearInput_Disabled(t *testing.T) {
agent := &claudeAgent{baseAgent{clearFirst: false, clearKeys: "C-a C-k"}}
err := agent.ClearInput("%1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestClaudeAgent_ClearInput_EmptyKeys(t *testing.T) {
agent := &claudeAgent{baseAgent{clearFirst: true, clearKeys: ""}}
err := agent.ClearInput("%1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestClaudeAgent_ClearInput_Error(t *testing.T) {
noSleep(t)
oldSend := sendKeys
defer func() { sendKeys = oldSend }()
sendKeys = func(string, ...string) error {
return fmt.Errorf("send failed")
}
agent := newClaudeAgent()
err := agent.ClearInput("%1")
if err == nil {
t.Fatal("expected error from sendClearSequence failure")
}
}
func TestClaudeAgent_Detect(t *testing.T) {
agent := newClaudeAgent()
tests := []struct {
name string
content string
want bool
}{
{"prompt symbol", "❯ hello", true},
{"claude code banner", "claude code v1.0", true},
{"anthropic mention", "Powered by Anthropic", true},
{"no match", "some text", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := agent.Detect(tt.content); got != tt.want {
t.Errorf("Detect() = %v, want %v", got, tt.want)
}
})
}
}
|