-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscenarios_test.go
More file actions
779 lines (684 loc) · 25 KB
/
scenarios_test.go
File metadata and controls
779 lines (684 loc) · 25 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
package main
import (
"os/exec"
"strings"
"testing"
"time"
)
// ---- Channel / non-channel E2E chain tests ----
// TestScenario_ChannelDevServerWorkflow simulates a full dev server workflow
// in channel mode: start server, watch for readiness, curl it, kill it, then
// watch for the exit event — verifying both tool results and channel
// notifications arrive for each event.
func TestScenario_ChannelDevServerWorkflow(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t, "--channel")
sess := createSession(t, c, uniqueSession(t))
serverPane := sess["paneId"].(string)
// Split off a work pane.
var split map[string]any
c.callToolJSON(t, "split-pane", map[string]any{
"paneId": serverPane,
"direction": "horizontal",
}, &split)
workPane := split["paneId"].(string)
// Drain any notifications that arrived during setup.
drainNotifications(c)
// Start python HTTP server via start-and-watch; wait for pattern match.
var serverResult map[string]any
c.callToolJSON(t, "start-and-watch", map[string]any{
"paneId": serverPane,
"command": "python3 -m http.server 8766",
"pattern": "Serving HTTP",
"timeout": 20,
"triggers": "exit,error",
}, &serverResult)
// Tool result must report a pattern event.
event, _ := serverResult["event"].(string)
if !strings.Contains(event, "pattern") {
t.Fatalf("expected pattern event from start-and-watch, got %q", event)
}
// A channel notification must also have arrived.
notif := waitNotification(t, c, 5*time.Second)
metaMap, _ := notif["meta"].(map[string]any)
if metaMap == nil {
t.Fatalf("channel notification missing meta: %v", notif)
}
notifEvent, _ := metaMap["event"].(string)
if !strings.Contains(notifEvent, "pattern") {
t.Fatalf("channel notification event: expected pattern, got %q", notifEvent)
}
notifPane, _ := metaMap["paneId"].(string)
if notifPane != serverPane {
t.Fatalf("channel notification paneId: expected %q, got %q", serverPane, notifPane)
}
// Curl the server from the work pane.
var curlResult map[string]any
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": workPane,
"command": "curl -s http://localhost:8766",
}, &curlResult)
curlOutput, _ := curlResult["output"].(string)
if !strings.Contains(strings.ToLower(curlOutput), "html") &&
!strings.Contains(strings.ToLower(curlOutput), "directory") {
t.Fatalf("expected HTML from curl, got: %q", curlOutput)
}
// Kill the server with Ctrl-C.
exec.Command("tmux", "send-keys", "-t", serverPane, "C-c", "").Run() //nolint:errcheck
sleep(300 * time.Millisecond)
// Watch for exit on the server pane.
drainNotifications(c)
var exitResult map[string]any
c.callToolJSON(t, "watch-pane", map[string]any{
"paneId": serverPane,
"triggers": "exit,shell",
"timeout": 10,
}, &exitResult)
// Send exit to close the shell so the exit trigger can fire.
go exec.Command("tmux", "send-keys", "-t", serverPane, "exit", "Enter").Run() //nolint:errcheck
// Re-watch specifically for exit event.
drainNotifications(c)
var exitResult2 map[string]any
c.callToolJSON(t, "watch-pane", map[string]any{
"paneId": serverPane,
"triggers": "exit",
"timeout": 10,
}, &exitResult2)
exitEvent, _ := exitResult2["event"].(string)
if exitEvent != "exit" {
t.Logf("watch-pane exit result event: %q (may have been shell or other)", exitEvent)
}
// Wait for the channel notification for the exit event.
exitNotif := waitNotification(t, c, 5*time.Second)
exitMeta, _ := exitNotif["meta"].(map[string]any)
if exitMeta == nil {
t.Fatalf("exit channel notification missing meta: %v", exitNotif)
}
t.Logf("exit notification: event=%v paneId=%v", exitMeta["event"], exitMeta["paneId"])
}
// TestScenario_RegularDevServerWorkflow is the same workflow as
// TestScenario_ChannelDevServerWorkflow but in regular (non-channel) mode.
// It verifies that tool results match channel-mode behaviour and that NO
// channel notifications are emitted.
func TestScenario_RegularDevServerWorkflow(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t) // no --channel flag
sess := createSession(t, c, uniqueSession(t))
serverPane := sess["paneId"].(string)
// Split off a work pane.
var split map[string]any
c.callToolJSON(t, "split-pane", map[string]any{
"paneId": serverPane,
"direction": "horizontal",
}, &split)
workPane := split["paneId"].(string)
// Start python HTTP server and wait for readiness pattern.
var serverResult map[string]any
c.callToolJSON(t, "start-and-watch", map[string]any{
"paneId": serverPane,
"command": "python3 -m http.server 8767",
"pattern": "Serving HTTP",
"timeout": 20,
"triggers": "exit,error",
}, &serverResult)
event, _ := serverResult["event"].(string)
if !strings.Contains(event, "pattern") {
t.Fatalf("expected pattern event from start-and-watch, got %q", event)
}
// Curl the server.
var curlResult map[string]any
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": workPane,
"command": "curl -s http://localhost:8767",
}, &curlResult)
curlOutput, _ := curlResult["output"].(string)
if !strings.Contains(strings.ToLower(curlOutput), "html") &&
!strings.Contains(strings.ToLower(curlOutput), "directory") {
t.Fatalf("expected HTML from curl, got: %q", curlOutput)
}
// Kill server.
exec.Command("tmux", "send-keys", "-t", serverPane, "C-c", "").Run() //nolint:errcheck
// Verify no channel notifications arrived throughout the entire workflow.
select {
case notif := <-c.channelNotifications:
t.Fatalf("unexpected channel notification in regular mode: %v", notif)
default:
// Good — channel is empty.
}
}
// TestScenario_ChannelBuildMonitoring exercises build monitoring with channel
// notifications: a successful build that matches a pattern, and a failing build
// that fires an exit or error trigger.
func TestScenario_ChannelBuildMonitoring(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t, "--channel")
sess := createSession(t, c, uniqueSession(t))
paneID := sess["paneId"].(string)
drainNotifications(c)
// Successful build: pattern "Build complete" should match.
var successResult map[string]any
c.callToolJSON(t, "start-and-watch", map[string]any{
"paneId": paneID,
"command": "echo 'Building...' && sleep 0.5 && echo 'Build complete: 0 errors'",
"pattern": "Build complete",
"timeout": 15,
"triggers": "exit,error",
}, &successResult)
successEvent, _ := successResult["event"].(string)
if !strings.Contains(successEvent, "pattern") {
t.Fatalf("expected pattern event for successful build, got %q", successEvent)
}
// Verify channel notification carries the pattern match.
buildNotif := waitNotification(t, c, 5*time.Second)
buildMeta, _ := buildNotif["meta"].(map[string]any)
if buildMeta == nil {
t.Fatalf("build notification missing meta: %v", buildNotif)
}
buildNotifEvent, _ := buildMeta["event"].(string)
if !strings.Contains(buildNotifEvent, "pattern") {
t.Fatalf("build notification event: expected pattern, got %q", buildNotifEvent)
}
// Failing build: use a pattern that matches the error output so the test
// is not subject to timing between the poll interval and the fast command.
drainNotifications(c)
var failResult map[string]any
c.callToolJSON(t, "start-and-watch", map[string]any{
"paneId": paneID,
"command": "echo 'Building...' && sleep 0.3 && echo 'FATAL ERROR: compilation failed'",
"pattern": "FATAL ERROR",
"timeout": 15,
"triggers": "error,exit",
}, &failResult)
failEvent, _ := failResult["event"].(string)
if !strings.Contains(failEvent, "pattern") && failEvent != "exit" && failEvent != "error" {
t.Fatalf("expected pattern/exit/error event for failed build, got %q", failEvent)
}
// Verify channel notification reports the trigger.
failNotif := waitNotification(t, c, 5*time.Second)
failMeta, _ := failNotif["meta"].(map[string]any)
if failMeta == nil {
t.Fatalf("fail notification missing meta: %v", failNotif)
}
t.Logf("failed build notification: event=%v detail=%v", failMeta["event"], failMeta["detail"])
}
// TestScenario_ChannelMultiPaneMonitoring watches multiple panes simultaneously
// in channel mode and verifies that notifications arrive from distinct panes.
func TestScenario_ChannelMultiPaneMonitoring(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t, "--channel")
sess := createSession(t, c, uniqueSession(t))
paneA := sess["paneId"].(string)
// Create pane B.
var splitB map[string]any
c.callToolJSON(t, "split-pane", map[string]any{
"paneId": paneA,
"direction": "horizontal",
}, &splitB)
paneB := splitB["paneId"].(string)
// Create pane C.
var splitC map[string]any
c.callToolJSON(t, "split-pane", map[string]any{
"paneId": paneB,
"direction": "vertical",
}, &splitC)
paneC := splitC["paneId"].(string)
drainNotifications(c)
// Pane A: start-and-watch with a readiness message.
var resultA map[string]any
c.callToolJSON(t, "start-and-watch", map[string]any{
"paneId": paneA,
"command": "echo 'pane-a-ready'",
"pattern": "pane-a-ready",
"timeout": 10,
"triggers": "exit",
}, &resultA)
eventA, _ := resultA["event"].(string)
if !strings.Contains(eventA, "pattern") && eventA != "exit" {
t.Fatalf("pane A: expected pattern or exit event, got %q", eventA)
}
// Collect notification for pane A.
notifA := waitNotification(t, c, 5*time.Second)
metaA, _ := notifA["meta"].(map[string]any)
if metaA == nil {
t.Fatalf("pane A notification missing meta: %v", notifA)
}
// Pane B: execute-command (no blocking watch — just fire and forget).
var resultB map[string]any
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": paneB,
"command": "echo 'pane-b-work'",
}, &resultB)
outputB, _ := resultB["output"].(string)
if !strings.Contains(outputB, "pane-b-work") {
t.Fatalf("pane B: expected 'pane-b-work' in output, got: %q", outputB)
}
// Pane C: send exit in a goroutine so it doesn't race with watch-pane,
// then call watch-pane synchronously (it blocks until exit fires).
drainNotifications(c)
go func() {
sleep(600 * time.Millisecond)
exec.Command("tmux", "send-keys", "-t", paneC, "exit", "Enter").Run() //nolint:errcheck
}()
var resultC map[string]any
c.callToolJSON(t, "watch-pane", map[string]any{
"paneId": paneC,
"triggers": "exit",
"timeout": 15,
}, &resultC)
eventC, _ := resultC["event"].(string)
t.Logf("pane C watch event: %q", eventC)
// Wait for the channel notification from pane C.
notifC := waitNotification(t, c, 5*time.Second)
metaC, _ := notifC["meta"].(map[string]any)
if metaC == nil {
t.Fatalf("pane C notification missing meta: %v", notifC)
}
// The two notifications must come from different panes.
paneFromA, _ := metaA["paneId"].(string)
paneFromC, _ := metaC["paneId"].(string)
if paneFromA == paneFromC {
t.Fatalf("expected notifications from different panes, both reported %q", paneFromA)
}
t.Logf("pane A notification paneId=%q, pane C notification paneId=%q", paneFromA, paneFromC)
}
// TestScenario_NoChannelNotificationsInRegularMode is a comprehensive negative
// test that runs many operations in regular mode and asserts that zero channel
// notifications are ever emitted.
func TestScenario_NoChannelNotificationsInRegularMode(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t) // no --channel flag
sess := createSession(t, c, uniqueSession(t))
paneID := sess["paneId"].(string)
// execute-command.
var ecResult map[string]any
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": paneID,
"command": "echo hello",
}, &ecResult)
// start-and-watch with pattern.
var sawResult map[string]any
c.callToolJSON(t, "start-and-watch", map[string]any{
"paneId": paneID,
"command": "echo 'ready-marker'",
"pattern": "ready-marker",
"timeout": 10,
"triggers": "exit",
}, &sawResult)
// watch-pane with timeout (no trigger should fire quickly; use short timeout).
var wpResult map[string]any
c.callToolJSON(t, "watch-pane", map[string]any{
"paneId": paneID,
"triggers": "exit",
"timeout": 2,
}, &wpResult)
// The pane shell is still alive, so this will timeout — that is fine.
// Drain and assert empty.
var count int
for {
select {
case notif := <-c.channelNotifications:
count++
t.Logf("unexpected notification #%d: %v", count, notif)
default:
goto done
}
}
done:
if count > 0 {
t.Fatalf("expected zero channel notifications in regular mode, got %d", count)
}
}
// ---- helpers ----
// waitNotification blocks until a channel notification arrives or the timeout
// expires (fatal).
func waitNotification(t *testing.T, c *mcpClient, timeout time.Duration) map[string]any {
t.Helper()
select {
case n := <-c.channelNotifications:
return n
case <-time.After(timeout):
t.Fatalf("timed out waiting for channel notification after %v", timeout)
return nil
}
}
// drainNotifications discards all notifications currently buffered in the
// channel. Used to reset state between sub-steps.
func drainNotifications(c *mcpClient) {
for {
select {
case <-c.channelNotifications:
default:
return
}
}
}
// TestScenario_DevServerWorkflow simulates an agent starting a dev server,
// waiting for readiness, and running work in a second pane.
func TestScenario_DevServerWorkflow(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t)
sess := createSession(t, c, uniqueSession(t))
serverPane := sess["paneId"].(string)
// Split off a work pane.
var split map[string]any
c.callToolJSON(t, "split-pane", map[string]any{
"paneId": serverPane,
"direction": "horizontal",
}, &split)
workPane := split["paneId"].(string)
// Start python HTTP server via start-and-watch.
var serverResult map[string]any
c.callToolJSON(t, "start-and-watch", map[string]any{
"paneId": serverPane,
"command": "python3 -m http.server 8765",
"pattern": "Serving HTTP",
"timeout": 20,
}, &serverResult)
event, _ := serverResult["event"].(string)
if !strings.Contains(event, "pattern") {
t.Fatalf("expected pattern event from start-and-watch, got %q", event)
}
// Run curl in work pane.
var curlResult map[string]any
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": workPane,
"command": "curl -s http://localhost:8765",
}, &curlResult)
output, _ := curlResult["output"].(string)
// Python's http.server returns HTML directory listing.
if !strings.Contains(strings.ToLower(output), "html") &&
!strings.Contains(strings.ToLower(output), "directory") {
t.Fatalf("expected HTML from curl, got: %q", output)
}
// Kill server.
exec.Command("tmux", "send-keys", "-t", serverPane, "C-c", "").Run() //nolint:errcheck
}
// TestScenario_REPLSession simulates an agent opening a shell REPL and
// running multiple expressions across multiple run-in-repl calls.
func TestScenario_REPLSession(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t)
sess := createSession(t, c, uniqueSession(t))
paneID := sess["paneId"].(string)
// Start a lightweight sh REPL with a known prompt.
c.callToolJSON(t, "send-keys", map[string]any{
"paneId": paneID,
"keys": "env PS1='REPL> ' sh --norc --noprofile",
"enter": true,
}, &map[string]any{})
sleep(300 * time.Millisecond)
promptPattern := `^REPL>`
// Expression 1: set a variable.
var r1 map[string]any
c.callToolJSON(t, "run-in-repl", map[string]any{
"paneId": paneID,
"input": "x=42",
"promptPattern": promptPattern,
"timeout": 10,
}, &r1)
// Expression 2: evaluate x * 2 — should produce 84.
var r2 map[string]any
c.callToolJSON(t, "run-in-repl", map[string]any{
"paneId": paneID,
"input": "echo $((x * 2))",
"promptPattern": promptPattern,
"timeout": 10,
}, &r2)
output, _ := r2["output"].(string)
if !strings.Contains(output, "84") {
t.Fatalf("expected '84' in REPL output, got: %q", output)
}
}
// TestScenario_BuildMonitoring simulates an agent running builds and checking
// success/failure via exit code.
func TestScenario_BuildMonitoring(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t)
sess := createSession(t, c, uniqueSession(t))
paneID := sess["paneId"].(string)
// Successful build.
var success map[string]any
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": paneID,
"command": "echo 'Building...' && sleep 0.2 && echo 'Build complete: 0 errors'",
}, &success)
output, _ := success["output"].(string)
if !strings.Contains(output, "Build complete") {
t.Fatalf("expected 'Build complete' in output, got: %q", output)
}
exitCode, _ := success["exitCode"].(float64)
if int(exitCode) != 0 {
t.Fatalf("expected exitCode 0 for successful build, got %v", exitCode)
}
// Failed build.
var failure map[string]any
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": paneID,
"command": "echo 'Building...' && sleep 0.2 && echo 'FATAL: compilation failed' && exit 1",
}, &failure)
fOutput, _ := failure["output"].(string)
if !strings.Contains(fOutput, "FATAL") {
t.Fatalf("expected 'FATAL' in failure output, got: %q", fOutput)
}
fExitCode, _ := failure["exitCode"].(float64)
if int(fExitCode) != 1 {
t.Fatalf("expected exitCode 1 for failed build, got %v", fExitCode)
}
}
// TestScenario_CoachingDisplay simulates an agent using a split pane as a
// coaching display while running work in the main pane.
func TestScenario_CoachingDisplay(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t)
sess := createSession(t, c, uniqueSession(t))
mainPane := sess["paneId"].(string)
// Create display pane.
var split map[string]any
c.callToolJSON(t, "split-pane", map[string]any{
"paneId": mainPane,
"direction": "horizontal",
}, &split)
displayPane := split["paneId"].(string)
// Write first status to display.
c.callToolJSON(t, "write-to-display", map[string]any{
"paneId": displayPane,
"text": "Running tests...",
}, &map[string]any{})
// Run work in main pane.
var workResult map[string]any
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": mainPane,
"command": "echo 'test passed'",
}, &workResult)
workOutput, _ := workResult["output"].(string)
if !strings.Contains(workOutput, "test passed") {
t.Fatalf("expected 'test passed' in work output, got: %q", workOutput)
}
// Update display with result (clear=true).
c.callToolJSON(t, "write-to-display", map[string]any{
"paneId": displayPane,
"text": "Tests passed!",
"clear": true,
}, &map[string]any{})
sleep(400 * time.Millisecond)
// capture-pane on display — should contain "Tests passed".
// Strip all whitespace when comparing: terminal line-wrapping in narrow split
// panes can break words mid-character, so we compare without any whitespace.
displayText := c.callToolText(t, "capture-pane", map[string]any{"paneId": displayPane})
displayNoWS := strings.ReplaceAll(strings.ReplaceAll(displayText, "\n", ""), " ", "")
if !strings.Contains(displayNoWS, "Testspassed") {
t.Fatalf("expected 'Tests passed' in display pane, got:\n%s", displayText)
}
// capture-pane on main — should contain "test passed".
mainText := c.callToolText(t, "capture-pane", map[string]any{"paneId": mainPane})
if !strings.Contains(mainText, "test passed") {
t.Fatalf("expected 'test passed' in main pane, got:\n%s", mainText)
}
}
// TestScenario_MultiPaneOrchestration simulates creating multiple panes and
// running distinct commands in each.
func TestScenario_MultiPaneOrchestration(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t)
sess := createSession(t, c, uniqueSession(t))
paneA := sess["paneId"].(string)
windowID := sess["windowId"].(string)
// Split pane A horizontally → pane B.
var splitB map[string]any
c.callToolJSON(t, "split-pane", map[string]any{
"paneId": paneA,
"direction": "horizontal",
}, &splitB)
paneB := splitB["paneId"].(string)
// Split pane B vertically → pane C.
var splitC map[string]any
c.callToolJSON(t, "split-pane", map[string]any{
"paneId": paneB,
"direction": "vertical",
}, &splitC)
paneC := splitC["paneId"].(string)
// Verify we have 3 panes.
var panes []map[string]any
c.callToolJSON(t, "list-panes", map[string]any{"windowId": windowID}, &panes)
if len(panes) < 3 {
t.Fatalf("expected at least 3 panes, got %d", len(panes))
}
// Run different commands in each pane.
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": paneA,
"command": "echo pane-alpha",
}, &map[string]any{})
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": paneB,
"command": "echo pane-beta",
}, &map[string]any{})
c.callToolJSON(t, "execute-command", map[string]any{
"paneId": paneC,
"command": "echo pane-gamma",
}, &map[string]any{})
// capture-pane each and verify distinct content.
textA := c.callToolText(t, "capture-pane", map[string]any{"paneId": paneA})
textB := c.callToolText(t, "capture-pane", map[string]any{"paneId": paneB})
textC := c.callToolText(t, "capture-pane", map[string]any{"paneId": paneC})
if !strings.Contains(textA, "pane-alpha") {
t.Errorf("pane A: expected 'pane-alpha', got:\n%s", textA)
}
if !strings.Contains(textB, "pane-beta") {
t.Errorf("pane B: expected 'pane-beta', got:\n%s", textB)
}
if !strings.Contains(textC, "pane-gamma") {
t.Errorf("pane C: expected 'pane-gamma', got:\n%s", textC)
}
// pane-state on each — all should be alive.
for _, paneID := range []string{paneA, paneB, paneC} {
var state map[string]any
c.callToolJSON(t, "pane-state", map[string]any{"paneId": paneID}, &state)
if state["isAlive"] != true {
t.Errorf("pane %s: expected isAlive=true, got %v", paneID, state["isAlive"])
}
}
}
// TestScenario_NativeInputDetection simulates detecting when a process needs input.
func TestScenario_NativeInputDetection(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t)
sess := createSession(t, c, uniqueSession(t))
paneID := sess["paneId"].(string)
// 1. Shell at prompt — waitingForInput=true.
sleep(500 * time.Millisecond)
var idleState map[string]any
c.callToolJSON(t, "pane-state", map[string]any{"paneId": paneID}, &idleState)
if idleState["isAlive"] != true {
t.Fatalf("expected isAlive=true for idle shell")
}
t.Logf("idle: waitingForInput=%v foregroundCmd=%v", idleState["waitingForInput"], idleState["foregroundCmd"])
// 2. Start `cat` with no args — reads from stdin.
c.callToolJSON(t, "send-keys", map[string]any{
"paneId": paneID,
"keys": "cat",
"enter": true,
}, &map[string]any{})
sleep(700 * time.Millisecond)
var catState map[string]any
c.callToolJSON(t, "pane-state", map[string]any{"paneId": paneID}, &catState)
t.Logf("cat: waitingForInput=%v foregroundCmd=%v", catState["waitingForInput"], catState["foregroundCmd"])
if catState["isAlive"] != true {
t.Fatalf("expected isAlive=true while cat runs")
}
// 3. Send Ctrl-D (EOF) to cat using literal=false so tmux interprets "C-d".
c.callToolJSON(t, "send-keys", map[string]any{
"paneId": paneID,
"keys": "C-d",
"literal": false,
}, &map[string]any{})
sleep(700 * time.Millisecond)
// 4. Back to shell.
var shellState map[string]any
c.callToolJSON(t, "pane-state", map[string]any{"paneId": paneID}, &shellState)
t.Logf("after C-d: waitingForInput=%v foregroundCmd=%v", shellState["waitingForInput"], shellState["foregroundCmd"])
if shellState["isAlive"] != true {
t.Fatalf("expected isAlive=true after cat exits")
}
}
// TestScenario_ProcessLifecycle simulates tracking a full process lifecycle.
func TestScenario_ProcessLifecycle(t *testing.T) {
if testing.Short() {
t.Skip("requires tmux")
}
c := newMCPClient(t)
sess := createSession(t, c, uniqueSession(t))
paneID := sess["paneId"].(string)
// 1. Shell at prompt: isAlive=true.
sleep(400 * time.Millisecond)
var state1 map[string]any
c.callToolJSON(t, "pane-state", map[string]any{"paneId": paneID}, &state1)
if state1["isAlive"] != true {
t.Fatalf("step 1: expected isAlive=true, got %v", state1["isAlive"])
}
t.Logf("step 1 (idle): foregroundCmd=%v waitingForInput=%v", state1["foregroundCmd"], state1["waitingForInput"])
// 2. Run sleep 2: isAlive=true, foregroundCmd should be "sleep".
c.callToolJSON(t, "send-keys", map[string]any{
"paneId": paneID,
"keys": "sleep 2",
"enter": true,
}, &map[string]any{})
sleep(500 * time.Millisecond)
var state2 map[string]any
c.callToolJSON(t, "pane-state", map[string]any{"paneId": paneID}, &state2)
if state2["isAlive"] != true {
t.Fatalf("step 2: expected isAlive=true while sleep runs, got %v", state2["isAlive"])
}
cmd2, _ := state2["foregroundCmd"].(string)
t.Logf("step 2 (sleeping): foregroundCmd=%q waitingForInput=%v", cmd2, state2["waitingForInput"])
// foregroundCmd may be "sleep" or the shell depending on timing.
// 3. Wait for sleep to finish: isAlive=true, back to shell.
sleep(3 * time.Second)
var state3 map[string]any
c.callToolJSON(t, "pane-state", map[string]any{"paneId": paneID}, &state3)
if state3["isAlive"] != true {
t.Fatalf("step 3: expected isAlive=true after sleep, got %v", state3["isAlive"])
}
t.Logf("step 3 (after sleep): foregroundCmd=%v waitingForInput=%v", state3["foregroundCmd"], state3["waitingForInput"])
}