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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
package tmuxedit
import (
"fmt"
"log"
"strings"
"testing"
"codeberg.org/snonux/hexai/internal/appconfig"
)
func TestRunWithConfig_HappyPath(t *testing.T) {
noSleep(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 Aider agent detected; aider uses "> prompt" pattern
capturePane = func(paneID string) (string, error) {
return "aider v0.50\n> fix the bug", 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 keys, then the full edited text (both lines)
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) {
noSleep(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 "aider v0.50\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{
FeatureConfig: appconfig.FeatureConfig{
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 TestLogPaneLines_WithDebugLog(t *testing.T) {
// Set up debugLog to a buffer to cover the logging branch
var buf strings.Builder
oldDebugLog := debugLog
debugLog = log.New(&buf, "", 0)
defer func() { debugLog = oldDebugLog }()
// Content with box-drawing and arrow characters triggers logging
content := "normal line\n│ box line\n→ arrow line\nplain"
logPaneLines(content)
output := buf.String()
if !strings.Contains(output, "box line") {
t.Errorf("expected log of box-drawing line, got: %s", output)
}
if !strings.Contains(output, "arrow line") {
t.Errorf("expected log of arrow line, got: %s", output)
}
}
func TestLogPaneLines_WithoutDebugLog(t *testing.T) {
// When debugLog is nil, logPaneLines should not panic
oldDebugLog := debugLog
debugLog = nil
defer func() { debugLog = oldDebugLog }()
logPaneLines("│ test line\n→ arrow")
// No panic means pass
}
func TestRunWithConfig_ClearInputError(t *testing.T) {
noSleep(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
}
// Use Aider (clearFirst=true, clearKeys="C-u") so ClearInput is exercised
capturePane = func(string) (string, error) {
return "aider v0.50\n> fix the bug", nil
}
openEditorPopup = func(string, string, string) (string, error) {
return "new text", nil
}
sendKeys = func(string, ...string) error {
return fmt.Errorf("clear input failed")
}
cfg := appconfig.App{}
err := runWithConfig(Options{}, cfg)
if err == nil || !strings.Contains(err.Error(), "clear input failed") {
t.Errorf("expected clear input error, got: %v", err)
}
}
func TestRunWithConfig_SendTextError(t *testing.T) {
noSleep(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
}
// Use generic agent (no clear) so ClearInput succeeds
capturePane = func(string) (string, error) {
return "some unknown pane content", nil
}
openEditorPopup = func(string, string, string) (string, error) {
return "new text", nil
}
callCount := 0
sendKeys = func(string, ...string) error {
callCount++
// Fail on text send (generic agent has no clear)
return fmt.Errorf("send text failed")
}
cfg := appconfig.App{}
err := runWithConfig(Options{}, cfg)
if err == nil || !strings.Contains(err.Error(), "send text failed") {
t.Errorf("expected send text 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")
}
}
|