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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
package tmuxedit
import (
"fmt"
"strings"
"testing"
"codeberg.org/snonux/hexai/internal/appconfig"
)
func TestRunWithConfig_HappyPath(t *testing.T) {
// Save and restore all seams
oldCapture := capturePane
oldSendKeys := sendKeys
oldEditorPopup := openEditorPopup
oldRunCmd := runCommand
defer func() {
capturePane = oldCapture
sendKeys = oldSendKeys
openEditorPopup = oldEditorPopup
runCommand = oldRunCmd
}()
// Mock: pane resolution via tmux query
runCommand = func(name string, args ...string) ([]byte, error) {
if name == "tmux" && args[0] == "display-message" {
return []byte("%5"), nil
}
return nil, nil
}
// Mock: capture pane content with Claude Code agent detected
capturePane = func(paneID string) (string, error) {
return "claude code v1.0\n────\n❯ fix the bug\n────", nil
}
// Mock: editor popup returns modified text
openEditorPopup = func(initial, w, h string) (string, error) {
if initial != "fix the bug" {
t.Errorf("initial = %q, want 'fix the bug'", initial)
}
if w != "80%" || h != "80%" {
t.Errorf("dimensions = %sx%s, want 80%%x80%%", w, h)
}
return "fix the bug\nalso refactor the module", nil
}
// Track send-keys calls
var sent []string
sendKeys = func(paneID string, keys ...string) error {
sent = append(sent, strings.Join(keys, ","))
return nil
}
cfg := appconfig.App{}
err := runWithConfig(Options{}, cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should have sent: clear (C-u), then the full edited text (both lines)
// since deduplicateText returns the complete text whenever anything changed.
if len(sent) < 2 {
t.Fatalf("expected at least 2 send calls (clear + text), got %d: %v", len(sent), sent)
}
allSent := strings.Join(sent, "|")
if !strings.Contains(allSent, "fix the bug") {
t.Errorf("expected 'fix the bug' in sent calls: %v", sent)
}
if !strings.Contains(allSent, "also refactor the module") {
t.Errorf("expected 'also refactor the module' in sent calls: %v", sent)
}
}
func TestRunWithConfig_ExplicitAgent(t *testing.T) {
oldCapture := capturePane
oldSendKeys := sendKeys
oldEditorPopup := openEditorPopup
oldRunCmd := runCommand
defer func() {
capturePane = oldCapture
sendKeys = oldSendKeys
openEditorPopup = oldEditorPopup
runCommand = oldRunCmd
}()
runCommand = func(name string, args ...string) ([]byte, error) {
return []byte("%1"), nil
}
capturePane = func(string) (string, error) {
return "some generic content\n> hello", nil
}
openEditorPopup = func(initial, w, h string) (string, error) {
// With cursor agent, prompt extraction uses │ pattern, so initial should be empty
if initial != "" {
t.Errorf("initial = %q, want empty (cursor agent doesn't match > pattern)", initial)
}
return "new prompt", nil
}
sendKeys = func(string, ...string) error { return nil }
cfg := appconfig.App{}
err := runWithConfig(Options{Agent: "cursor"}, cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestRunWithConfig_EditorEmpty(t *testing.T) {
oldCapture := capturePane
oldSendKeys := sendKeys
oldEditorPopup := openEditorPopup
oldRunCmd := runCommand
defer func() {
capturePane = oldCapture
sendKeys = oldSendKeys
openEditorPopup = oldEditorPopup
runCommand = oldRunCmd
}()
runCommand = func(name string, args ...string) ([]byte, error) {
return []byte("%1"), nil
}
capturePane = func(string) (string, error) {
return "claude code\n❯ ", nil
}
openEditorPopup = func(string, string, string) (string, error) {
return "", nil // user saved empty file
}
sendKeys = func(string, ...string) error {
t.Fatal("sendKeys should not be called when editor returns empty")
return nil
}
cfg := appconfig.App{}
err := runWithConfig(Options{}, cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestRunWithConfig_CustomDimensions(t *testing.T) {
oldCapture := capturePane
oldSendKeys := sendKeys
oldEditorPopup := openEditorPopup
oldRunCmd := runCommand
defer func() {
capturePane = oldCapture
sendKeys = oldSendKeys
openEditorPopup = oldEditorPopup
runCommand = oldRunCmd
}()
runCommand = func(name string, args ...string) ([]byte, error) {
return []byte("%1"), nil
}
capturePane = func(string) (string, error) { return "", nil }
openEditorPopup = func(initial, w, h string) (string, error) {
if w != "90%" || h != "85%" {
t.Errorf("dimensions = %sx%s, want 90%%x85%%", w, h)
}
return "test", nil
}
sendKeys = func(string, ...string) error { return nil }
cfg := appconfig.App{
TmuxEditPopupWidth: "90%",
TmuxEditPopupHeight: "85%",
}
err := runWithConfig(Options{}, cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestPickAgent_ExplicitName(t *testing.T) {
agents := builtinAgents()
got := pickAgent("cursor", "Claude Code detected", agents)
if got.Name != "cursor" {
t.Errorf("pickAgent(cursor) = %q, want cursor (explicit name should win)", got.Name)
}
}
func TestPickAgent_AutoDetect(t *testing.T) {
agents := builtinAgents()
got := pickAgent("", "Amp by Sourcegraph", agents)
if got.Name != "amp" {
t.Errorf("pickAgent('', amp content) = %q, want amp", got.Name)
}
}
func TestShellQuote(t *testing.T) {
tests := []struct {
input string
want string
}{
{"simple", "'simple'"},
{"with space", "'with space'"},
{"it's", "'it'\\''s'"},
}
for _, tt := range tests {
got := shellQuote(tt.input)
if got != tt.want {
t.Errorf("shellQuote(%q) = %q, want %q", tt.input, got, tt.want)
}
}
}
func TestLaunchPopup_CommandArgs(t *testing.T) {
oldLaunch := launchPopup
defer func() { launchPopup = oldLaunch }()
var capturedArgs struct {
ed, path, w, h string
}
launchPopup = func(ed, path, w, h string) error {
capturedArgs.ed = ed
capturedArgs.path = path
capturedArgs.w = w
capturedArgs.h = h
return nil
}
err := launchPopup("vim", "/tmp/test.md", "90%", "85%")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if capturedArgs.ed != "vim" {
t.Errorf("ed = %q, want vim", capturedArgs.ed)
}
if capturedArgs.w != "90%" || capturedArgs.h != "85%" {
t.Errorf("dimensions = %sx%s, want 90%%x85%%", capturedArgs.w, capturedArgs.h)
}
}
func TestLaunchPopup_NoDimensions(t *testing.T) {
oldLaunch := launchPopup
defer func() { launchPopup = oldLaunch }()
var capturedArgs struct {
w, h string
}
launchPopup = func(ed, path, w, h string) error {
capturedArgs.w = w
capturedArgs.h = h
return nil
}
err := launchPopup("nano", "/tmp/f.md", "", "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if capturedArgs.w != "" || capturedArgs.h != "" {
t.Errorf("expected empty dimensions, got %qx%q", capturedArgs.w, capturedArgs.h)
}
}
func TestRunWithConfig_CaptureError(t *testing.T) {
oldCapture := capturePane
oldRunCmd := runCommand
defer func() {
capturePane = oldCapture
runCommand = oldRunCmd
}()
runCommand = func(name string, args ...string) ([]byte, error) {
return []byte("%1"), nil
}
capturePane = func(string) (string, error) {
return "", fmt.Errorf("capture failed")
}
cfg := appconfig.App{}
err := runWithConfig(Options{Pane: "%1"}, cfg)
if err == nil || !strings.Contains(err.Error(), "capture failed") {
t.Errorf("expected capture error, got: %v", err)
}
}
func TestRunWithConfig_EditorError(t *testing.T) {
oldCapture := capturePane
oldEditorPopup := openEditorPopup
oldRunCmd := runCommand
defer func() {
capturePane = oldCapture
openEditorPopup = oldEditorPopup
runCommand = oldRunCmd
}()
runCommand = func(name string, args ...string) ([]byte, error) {
return []byte("%1"), nil
}
capturePane = func(string) (string, error) {
return "some content", nil
}
openEditorPopup = func(string, string, string) (string, error) {
return "", fmt.Errorf("editor crashed")
}
cfg := appconfig.App{}
err := runWithConfig(Options{Pane: "%1"}, cfg)
if err == nil || !strings.Contains(err.Error(), "editor crashed") {
t.Errorf("expected editor error, got: %v", err)
}
}
func TestRunWithConfig_PaneResolveError(t *testing.T) {
oldRunCmd := runCommand
defer func() { runCommand = oldRunCmd }()
runCommand = func(string, ...string) ([]byte, error) {
return nil, fmt.Errorf("tmux unavailable")
}
t.Setenv("HEXAI_TMUX_PANE", "")
cfg := appconfig.App{}
err := runWithConfig(Options{}, cfg)
if err == nil {
t.Fatal("expected error for pane resolution failure")
}
}
|