psmux 3.3.4

Terminal multiplexer for Windows - tmux alternative for PowerShell and Windows Terminal
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
# psmux Battle Test Suite
# Comprehensive testing of all psmux features: sessions, windows, panes, resize, kill, etc.

$ErrorActionPreference = "Continue"

# Colors and helpers
function Write-Pass { param($msg) Write-Host "[PASS] $msg" -ForegroundColor Green; $script:TestsPassed++ }
function Write-Fail { param($msg) Write-Host "[FAIL] $msg" -ForegroundColor Red; $script:TestsFailed++ }
function Write-Skip { param($msg) Write-Host "[SKIP] $msg" -ForegroundColor Yellow; $script:TestsSkipped++ }
function Write-Info { param($msg) Write-Host "[INFO] $msg" -ForegroundColor Cyan }
function Write-Test { param($msg) Write-Host "[TEST] $msg" -ForegroundColor White }
function Write-Section { param($msg) 
    Write-Host ""
    Write-Host "=" * 70 -ForegroundColor Magenta
    Write-Host "  $msg" -ForegroundColor Magenta
    Write-Host "=" * 70 -ForegroundColor Magenta
}

# Statistics
$script:TestsPassed = 0
$script:TestsFailed = 0
$script:TestsSkipped = 0

# Find psmux binary
$PSMUX = "$PSScriptRoot\..\target\release\psmux.exe"
if (-not (Test-Path $PSMUX)) {
    $PSMUX = "$PSScriptRoot\..\target\debug\psmux.exe"
}
if (-not (Test-Path $PSMUX)) {
    Write-Error "psmux binary not found. Please build the project first with: cargo build --release"
    exit 1
}

Write-Host ""
Write-Host "========================================================================" -ForegroundColor Cyan
Write-Host "               PSMUX BATTLE TEST SUITE                                " -ForegroundColor Cyan
Write-Host "               Comprehensive Feature Testing                          " -ForegroundColor Cyan
Write-Host "========================================================================" -ForegroundColor Cyan
Write-Host ""
Write-Info "Binary: $PSMUX"
Write-Info "Started: $(Get-Date)"
Write-Host ""

# Helper: Start a detached session safely
function Start-DetachedSession {
    param([string]$Name)
    
    # Kill any existing session with this name
    try { & $PSMUX kill-session -t $Name 2>&1 | Out-Null } catch {}
    Start-Sleep -Milliseconds 500
    
    # Start new detached session
    $proc = Start-Process -FilePath $PSMUX -ArgumentList "new-session", "-s", $Name, "-d" -PassThru -WindowStyle Hidden
    Start-Sleep -Milliseconds 1500
    
    # Verify session exists
    $result = & $PSMUX has-session -t $Name 2>&1
    if ($LASTEXITCODE -eq 0) {
        return $true
    }
    return $false
}

# Helper: Clean up session
function Stop-Session {
    param([string]$Name)
    try {
        & $PSMUX kill-session -t $Name 2>&1 | Out-Null
    } catch {}
    Start-Sleep -Milliseconds 300
}

# ============================================================================
# CLEANUP BEFORE TESTS
# ============================================================================
Write-Section "CLEANUP - Killing any existing test sessions"

$testSessions = @("battle_test", "test_session_1", "test_session_2", "test_session_3", 
                  "multi_test_1", "multi_test_2", "pane_test", "window_test", 
                  "resize_test", "kill_test", "stress_test", "rapid_test")

foreach ($session in $testSessions) {
    try { & $PSMUX kill-session -t $session 2>&1 | Out-Null } catch {}
}
Start-Sleep -Seconds 1
Write-Info "Cleanup complete"

# ============================================================================
# TEST CATEGORY 1: SESSION MANAGEMENT
# ============================================================================
Write-Section "SESSION MANAGEMENT TESTS"

# Test 1.1: Create a new session
Write-Test "Create new detached session"
if (Start-DetachedSession -Name "battle_test") {
    Write-Pass "Session 'battle_test' created successfully"
} else {
    Write-Fail "Failed to create session 'battle_test'"
}

# Test 1.2: List sessions
Write-Test "List sessions"
$sessions = & $PSMUX ls 2>&1
if ($sessions -match "battle_test") {
    Write-Pass "Session appears in list-sessions output"
} else {
    Write-Fail "Session not found in list: $sessions"
}

# Test 1.3: has-session check
Write-Test "has-session (existing)"
& $PSMUX has-session -t battle_test 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
    Write-Pass "has-session correctly identifies existing session"
} else {
    Write-Fail "has-session failed for existing session"
}

# Test 1.4: has-session for non-existent
Write-Test "has-session (non-existent)"
& $PSMUX has-session -t nonexistent_session_xyz 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
    Write-Pass "has-session correctly rejects non-existent session"
} else {
    Write-Fail "has-session incorrectly accepted non-existent session"
}

# Test 1.5: Create multiple sessions
Write-Test "Create multiple sessions simultaneously"
$created = 0
foreach ($i in 1..3) {
    if (Start-DetachedSession -Name "test_session_$i") {
        $created++
    }
}
if ($created -eq 3) {
    Write-Pass "Created 3 sessions successfully"
} else {
    Write-Fail "Only created $created/3 sessions"
}

# Test 1.6: Verify all sessions exist
Write-Test "Verify all sessions in list"
$sessions = & $PSMUX ls 2>&1
$found = 0
foreach ($i in 1..3) {
    if ($sessions -match "test_session_$i") { $found++ }
}
if ($found -eq 3) {
    Write-Pass "All 3 sessions appear in list"
} else {
    Write-Fail "Only $found/3 sessions found in list"
}

# ============================================================================
# TEST CATEGORY 2: WINDOW MANAGEMENT
# ============================================================================
Write-Section "WINDOW MANAGEMENT TESTS"

# Test 2.1: Create new windows
Write-Test "Create new windows in session"
& $PSMUX new-window -t battle_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
& $PSMUX new-window -t battle_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
& $PSMUX new-window -t battle_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
Write-Pass "Created 3 new windows"

# Test 2.2: List windows
Write-Test "List windows"
$windows = & $PSMUX list-windows -t battle_test 2>&1
if ($windows) {
    Write-Pass "list-windows returned output"
    Write-Info "Windows: $($windows -join ', ')"
} else {
    Write-Fail "list-windows returned empty"
}

# Test 2.3: Navigate windows with next-window
Write-Test "next-window navigation"
$success = $true
foreach ($i in 1..5) {
    & $PSMUX next-window -t battle_test 2>&1 | Out-Null
    if ($LASTEXITCODE -ne 0) { $success = $false }
    Start-Sleep -Milliseconds 100
}
if ($success) {
    Write-Pass "next-window navigation works"
} else {
    Write-Fail "next-window navigation had errors"
}

# Test 2.4: Navigate windows with previous-window
Write-Test "previous-window navigation"
$success = $true
foreach ($i in 1..5) {
    & $PSMUX previous-window -t battle_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 100
}
Write-Pass "previous-window navigation executed"

# Test 2.5: last-window
Write-Test "last-window"
& $PSMUX last-window -t battle_test 2>&1 | Out-Null
Write-Pass "last-window executed"

# Test 2.6: Select specific window
Write-Test "select-window by index"
& $PSMUX select-window -t battle_test:0 2>&1 | Out-Null
Start-Sleep -Milliseconds 200
& $PSMUX select-window -t battle_test:1 2>&1 | Out-Null
Start-Sleep -Milliseconds 200
& $PSMUX select-window -t battle_test:2 2>&1 | Out-Null
Write-Pass "select-window by index works"

# Test 2.7: Rename window
Write-Test "rename-window"
& $PSMUX rename-window -t battle_test "renamed_window" 2>&1 | Out-Null
Write-Pass "rename-window executed"

# ============================================================================
# TEST CATEGORY 3: PANE MANAGEMENT
# ============================================================================
Write-Section "PANE MANAGEMENT TESTS"

# Create fresh session for pane tests
Stop-Session -Name "pane_test"
Start-DetachedSession -Name "pane_test"

# Test 3.1: Vertical split
Write-Test "split-window -v (vertical)"
& $PSMUX split-window -v -t pane_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
$panes = & $PSMUX list-panes -t pane_test 2>&1
Write-Pass "Vertical split created"

# Test 3.2: Horizontal split
Write-Test "split-window -h (horizontal)"
& $PSMUX split-window -h -t pane_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
Write-Pass "Horizontal split created"

# Test 3.3: Multiple splits (stress test)
Write-Test "Multiple rapid splits"
$splitSuccess = 0
foreach ($i in 1..4) {
    & $PSMUX split-window -v -t pane_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 300
    & $PSMUX split-window -h -t pane_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 300
    $splitSuccess += 2
}
Write-Pass "Created $splitSuccess additional splits"

# Test 3.4: List panes
Write-Test "list-panes"
$panes = & $PSMUX list-panes -t pane_test 2>&1
if ($panes) {
    $paneCount = ($panes | Measure-Object -Line).Lines
    Write-Pass "list-panes shows panes"
    Write-Info "Pane count: $paneCount"
} else {
    Write-Fail "list-panes returned empty"
}

# Test 3.5: Select pane directions
Write-Test "select-pane in all directions"
foreach ($dir in @("-U", "-D", "-L", "-R")) {
    & $PSMUX select-pane $dir -t pane_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 100
}
Write-Pass "select-pane all directions executed"

# Test 3.6: Rapid pane navigation
Write-Test "Rapid pane navigation - 10 cycles"
foreach ($i in 1..10) {
    & $PSMUX select-pane -U -t pane_test 2>&1 | Out-Null
    & $PSMUX select-pane -R -t pane_test 2>&1 | Out-Null
    & $PSMUX select-pane -D -t pane_test 2>&1 | Out-Null
    & $PSMUX select-pane -L -t pane_test 2>&1 | Out-Null
}
Write-Pass "Rapid navigation completed"

# ============================================================================
# TEST CATEGORY 4: RESIZE PANES
# ============================================================================
Write-Section "RESIZE PANE TESTS"

# Create fresh session for resize tests
Stop-Session -Name "resize_test"
Start-DetachedSession -Name "resize_test"
& $PSMUX split-window -v -t resize_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
& $PSMUX split-window -h -t resize_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500

# Test 4.1: Resize up
Write-Test "resize-pane -U (up)"
foreach ($i in 1..5) {
    & $PSMUX resize-pane -U 2 -t resize_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 100
}
Write-Pass "Resize up executed 5 times"

# Test 4.2: Resize down
Write-Test "resize-pane -D (down)"
foreach ($i in 1..5) {
    & $PSMUX resize-pane -D 2 -t resize_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 100
}
Write-Pass "Resize down executed 5 times"

# Test 4.3: Resize left
Write-Test "resize-pane -L (left)"
foreach ($i in 1..5) {
    & $PSMUX resize-pane -L 2 -t resize_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 100
}
Write-Pass "Resize left executed 5 times"

# Test 4.4: Resize right
Write-Test "resize-pane -R (right)"
foreach ($i in 1..5) {
    & $PSMUX resize-pane -R 2 -t resize_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 100
}
Write-Pass "Resize right executed 5 times"

# Test 4.5: Larger resize operations
Write-Test "Large resize operations"
& $PSMUX resize-pane -U 10 -t resize_test 2>&1 | Out-Null
& $PSMUX resize-pane -D 10 -t resize_test 2>&1 | Out-Null
& $PSMUX resize-pane -L 15 -t resize_test 2>&1 | Out-Null
& $PSMUX resize-pane -R 15 -t resize_test 2>&1 | Out-Null
Write-Pass "Large resize operations completed"

# Test 4.6: Zoom pane
Write-Test "zoom-pane toggle"
& $PSMUX resize-pane -Z -t resize_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 300
& $PSMUX resize-pane -Z -t resize_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 300
Write-Pass "Zoom pane toggled twice"

# ============================================================================
# TEST CATEGORY 5: KILL OPERATIONS
# ============================================================================
Write-Section "KILL OPERATIONS TESTS"

# Create fresh session for kill tests
Stop-Session -Name "kill_test"
Start-DetachedSession -Name "kill_test"

# Test 5.1: Create panes then kill
Write-Test "Create and kill panes"
& $PSMUX split-window -v -t kill_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
& $PSMUX split-window -h -t kill_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
& $PSMUX split-window -v -t kill_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500

$panesBefore = & $PSMUX list-panes -t kill_test 2>&1
Write-Info "Panes before kill: $($panesBefore | Measure-Object -Line | Select-Object -ExpandProperty Lines)"

& $PSMUX kill-pane -t kill_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
Write-Pass "kill-pane executed"

# Test 5.2: Kill multiple panes
Write-Test "Kill multiple panes in succession"
& $PSMUX split-window -v -t kill_test 2>&1 | Out-Null
& $PSMUX split-window -v -t kill_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
& $PSMUX kill-pane -t kill_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 300
& $PSMUX kill-pane -t kill_test 2>&1 | Out-Null
Write-Pass "Multiple panes killed"

# Test 5.3: Create windows then kill window
Write-Test "Create and kill windows"
& $PSMUX new-window -t kill_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
& $PSMUX new-window -t kill_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500

$windowsBefore = & $PSMUX list-windows -t kill_test 2>&1
Write-Info "Windows before kill: $($windowsBefore | Measure-Object -Line | Select-Object -ExpandProperty Lines)"

& $PSMUX kill-window -t kill_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
Write-Pass "kill-window executed"

# Test 5.4: Kill session
Write-Test "Kill session"
& $PSMUX has-session -t kill_test 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
    & $PSMUX kill-session -t kill_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 500
    & $PSMUX has-session -t kill_test 2>&1 | Out-Null
    if ($LASTEXITCODE -ne 0) {
        Write-Pass "Session killed successfully"
    } else {
        Write-Fail "Session still exists after kill"
    }
} else {
    Write-Fail "Session didn't exist to kill"
}

# ============================================================================
# TEST CATEGORY 6: SEND-KEYS
# ============================================================================
Write-Section "SEND-KEYS TESTS"

# Create fresh session
Stop-Session -Name "keys_test"
Start-DetachedSession -Name "keys_test"

# Test 6.1: Basic send-keys
Write-Test "send-keys basic text"
& $PSMUX send-keys -t keys_test "echo hello world" Enter 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
Write-Pass "send-keys with Enter executed"

# Test 6.2: Literal send-keys
Write-Test "send-keys -l (literal)"
& $PSMUX send-keys -l -t keys_test "literal text test" 2>&1 | Out-Null
Write-Pass "send-keys literal executed"

# Test 6.3: Send special keys
Write-Test "send-keys special keys"
& $PSMUX send-keys -t keys_test Tab 2>&1 | Out-Null
& $PSMUX send-keys -t keys_test Escape 2>&1 | Out-Null
& $PSMUX send-keys -t keys_test Up 2>&1 | Out-Null
& $PSMUX send-keys -t keys_test Down 2>&1 | Out-Null
Write-Pass "Special keys sent"

# Test 6.4: Rapid key sending
Write-Test "Rapid send-keys (10 commands)"
foreach ($i in 1..10) {
    & $PSMUX send-keys -t keys_test "echo test $i" Enter 2>&1 | Out-Null
    Start-Sleep -Milliseconds 50
}
Write-Pass "Rapid send-keys completed"

# ============================================================================
# TEST CATEGORY 7: BUFFERS AND CAPTURE
# ============================================================================
Write-Section "BUFFER AND CAPTURE TESTS"

# Test 7.1: Set buffer
Write-Test "set-buffer"
& $PSMUX set-buffer -t keys_test "Test buffer content 12345" 2>&1 | Out-Null
Write-Pass "set-buffer executed"

# Test 7.2: List buffers
Write-Test "list-buffers"
$buffers = & $PSMUX list-buffers -t keys_test 2>&1
Write-Pass "list-buffers executed"

# Test 7.3: Show buffer
Write-Test "show-buffer"
$content = & $PSMUX show-buffer -t keys_test 2>&1
Write-Pass "show-buffer executed"

# Test 7.4: Capture pane
Write-Test "capture-pane"
$captured = & $PSMUX capture-pane -t keys_test -p 2>&1
if ($captured) {
    Write-Pass "capture-pane returned content"
} else {
    Write-Skip "capture-pane returned empty (may be expected)"
}

# ============================================================================
# TEST CATEGORY 8: SWAP AND ROTATE
# ============================================================================
Write-Section "SWAP AND ROTATE TESTS"

# Create fresh session
Stop-Session -Name "swap_test"
Start-DetachedSession -Name "swap_test"
& $PSMUX split-window -v -t swap_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500
& $PSMUX split-window -h -t swap_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500

# Test 8.1: Swap pane up
Write-Test "swap-pane -U"
& $PSMUX swap-pane -U -t swap_test 2>&1 | Out-Null
Write-Pass "swap-pane -U executed"

# Test 8.2: Swap pane down
Write-Test "swap-pane -D"
& $PSMUX swap-pane -D -t swap_test 2>&1 | Out-Null
Write-Pass "swap-pane -D executed"

# Test 8.3: Rotate window
Write-Test "rotate-window"
& $PSMUX rotate-window -t swap_test 2>&1 | Out-Null
Write-Pass "rotate-window executed"

# Test 8.4: Multiple rotations
Write-Test "Multiple rotations"
foreach ($i in 1..5) {
    & $PSMUX rotate-window -t swap_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 100
}
Write-Pass "5 rotations completed"

# ============================================================================
# TEST CATEGORY 9: LAYOUTS
# ============================================================================
Write-Section "LAYOUT TESTS"

# Create fresh session with multiple panes
Stop-Session -Name "layout_test"
Start-DetachedSession -Name "layout_test"
& $PSMUX split-window -v -t layout_test 2>&1 | Out-Null
& $PSMUX split-window -h -t layout_test 2>&1 | Out-Null
& $PSMUX split-window -v -t layout_test 2>&1 | Out-Null
Start-Sleep -Milliseconds 500

# Test 9.1: Even-horizontal layout
Write-Test "select-layout even-horizontal"
& $PSMUX select-layout -t layout_test even-horizontal 2>&1 | Out-Null
Start-Sleep -Milliseconds 300
Write-Pass "even-horizontal layout applied"

# Test 9.2: Even-vertical layout
Write-Test "select-layout even-vertical"
& $PSMUX select-layout -t layout_test even-vertical 2>&1 | Out-Null
Start-Sleep -Milliseconds 300
Write-Pass "even-vertical layout applied"

# Test 9.3: Main-horizontal layout
Write-Test "select-layout main-horizontal"
& $PSMUX select-layout -t layout_test main-horizontal 2>&1 | Out-Null
Start-Sleep -Milliseconds 300
Write-Pass "main-horizontal layout applied"

# Test 9.4: Main-vertical layout
Write-Test "select-layout main-vertical"
& $PSMUX select-layout -t layout_test main-vertical 2>&1 | Out-Null
Start-Sleep -Milliseconds 300
Write-Pass "main-vertical layout applied"

# Test 9.5: Tiled layout
Write-Test "select-layout tiled"
& $PSMUX select-layout -t layout_test tiled 2>&1 | Out-Null
Start-Sleep -Milliseconds 300
Write-Pass "tiled layout applied"

# ============================================================================
# TEST CATEGORY 10: STRESS TESTS
# ============================================================================
Write-Section "STRESS TESTS"

# Test 10.1: Rapid session create/destroy
Write-Test "Rapid session create/destroy (5 cycles)"
foreach ($i in 1..5) {
    Start-DetachedSession -Name "rapid_test" | Out-Null
    Start-Sleep -Milliseconds 200
    & $PSMUX kill-session -t rapid_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 200
}
Write-Pass "5 rapid session cycles completed"

# Test 10.2: Many windows in single session
Write-Test "Create 10 windows rapidly"
Stop-Session -Name "stress_test"
Start-DetachedSession -Name "stress_test"
foreach ($i in 1..10) {
    & $PSMUX new-window -t stress_test 2>&1 | Out-Null
    Start-Sleep -Milliseconds 100
}
$windows = & $PSMUX list-windows -t stress_test 2>&1
$windowCount = ($windows | Measure-Object -Line).Lines
Write-Pass "Created windows (count: $windowCount)"

# Test 10.3: Many operations on single session
Write-Test "Stress test: 50 mixed operations"
$operations = 0
foreach ($i in 1..10) {
    & $PSMUX split-window -v -t stress_test 2>&1 | Out-Null
    $operations++
    & $PSMUX select-pane -U -t stress_test 2>&1 | Out-Null
    $operations++
    & $PSMUX resize-pane -D 1 -t stress_test 2>&1 | Out-Null
    $operations++
    & $PSMUX next-window -t stress_test 2>&1 | Out-Null
    $operations++
    & $PSMUX select-pane -L -t stress_test 2>&1 | Out-Null
    $operations++
}
Write-Pass "$operations mixed operations completed"

# ============================================================================
# TEST CATEGORY 11: DISPLAY AND INFO COMMANDS
# ============================================================================
Write-Section "DISPLAY AND INFO COMMANDS"

# Test 11.1: Display message with format
Write-Test "display-message with format string"
$output = & $PSMUX display-message -t stress_test -p "#S:#I:#W" 2>&1
if ($output) {
    Write-Pass "display-message returned: $output"
} else {
    Write-Skip "display-message returned empty"
}

# Test 11.2: Display panes (q command simulation)
Write-Test "display-panes"
& $PSMUX display-panes -t stress_test 2>&1 | Out-Null
Write-Pass "display-panes executed"

# Test 11.3: List clients
Write-Test "list-clients"
$clients = & $PSMUX list-clients 2>&1
Write-Pass "list-clients executed"

# Test 11.4: List keys
Write-Test "list-keys"
$keys = & $PSMUX list-keys 2>&1
if ($keys) {
    Write-Pass "list-keys returned bindings"
} else {
    Write-Skip "list-keys returned empty"
}

# ============================================================================
# TEST CATEGORY 12: EDGE CASES
# ============================================================================
Write-Section "EDGE CASE TESTS"

# Test 12.1: Commands on non-existent session
Write-Test "Commands on non-existent session"
$result = & $PSMUX split-window -t nonexistent_xyz_123 2>&1
if ($LASTEXITCODE -ne 0 -or $result -match "error|not found|no session") {
    Write-Pass "Correctly handles non-existent session"
} else {
    Write-Skip "Non-existent session handling unclear"
}

# Test 12.2: Empty session name
Write-Test "Session operations with various names"
$specialNames = @("test-dash", "test_underscore", "Test123")
foreach ($name in $specialNames) {
    if (Start-DetachedSession -Name $name) {
        & $PSMUX kill-session -t $name 2>&1 | Out-Null
    }
}
Write-Pass "Various session names handled"

# Test 12.3: Very long session name
Write-Test "Long session name"
$longName = "test_" + ("a" * 50)
try {
    if (Start-DetachedSession -Name $longName) {
        & $PSMUX kill-session -t $longName 2>&1 | Out-Null
        Write-Pass "Long session name handled"
    } else {
        Write-Skip "Long session name creation unclear"
    }
} catch {
    Write-Skip "Long session name test: $_"
}

# ============================================================================
# CLEANUP
# ============================================================================
Write-Section "CLEANUP"

Write-Info "Cleaning up test sessions..."
$allTestSessions = @("battle_test", "test_session_1", "test_session_2", "test_session_3",
                     "pane_test", "resize_test", "kill_test", "keys_test", "swap_test",
                     "layout_test", "stress_test", "rapid_test", "test-dash", 
                     "test_underscore", "Test123")

foreach ($session in $allTestSessions) {
    try { & $PSMUX kill-session -t $session 2>&1 | Out-Null } catch {}
}

Start-Sleep -Seconds 1
Write-Info "Cleanup complete"

# ============================================================================
# FINAL SUMMARY
# ============================================================================
Write-Host ""
Write-Host "========================================================================" -ForegroundColor Cyan
Write-Host "                         FINAL RESULTS                                " -ForegroundColor Cyan
Write-Host "========================================================================" -ForegroundColor Cyan
Write-Host ""

$total = $script:TestsPassed + $script:TestsFailed + $script:TestsSkipped
Write-Host "  Total Tests: $total"
Write-Host "  [v] Passed:    $($script:TestsPassed)" -ForegroundColor Green
Write-Host "  [x] Failed:    $($script:TestsFailed)" -ForegroundColor Red
Write-Host "  [o] Skipped:   $($script:TestsSkipped)" -ForegroundColor Yellow
Write-Host ""

$passRate = if ($total -gt 0) { [math]::Round(($script:TestsPassed / $total) * 100, 1) } else { 0 }
Write-Host "  Pass Rate: $passRate%" -ForegroundColor $(if ($passRate -ge 80) { "Green" } elseif ($passRate -ge 60) { "Yellow" } else { "Red" })
Write-Host ""
Write-Info "Completed: $(Get-Date)"
Write-Host ""

if ($script:TestsFailed -eq 0) {
    Write-Host "ALL TESTS PASSED! psmux is battle-ready!" -ForegroundColor Green
    exit 0
} else {
    Write-Host "Some tests failed. Review the output above." -ForegroundColor Yellow
    exit 1
}