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
# psmux New Features Test Suite
# Tests: rename-session, run-shell, if-shell, format strings, zoom, last-window,
# last-pane, swap-pane, break-pane, kill-window, resize-pane, list-keys, hooks
# Requires a running session.

$ErrorActionPreference = "Continue"
$script:TestsPassed = 0
$script:TestsFailed = 0

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-Info { param($msg) Write-Host "[INFO] $msg" -ForegroundColor Cyan }
function Write-Test { param($msg) Write-Host "[TEST] $msg" -ForegroundColor White }

$PSMUX = "$PSScriptRoot\..\target\release\psmux.exe"
if (-not (Test-Path $PSMUX)) {
    $PSMUX = "$PSScriptRoot\..\target\debug\psmux.exe"
}

if (-not (Test-Path $PSMUX)) {
    Write-Host "[ERROR] psmux binary not found. Run 'cargo build --release' first." -ForegroundColor Red
    exit 1
}

$SESSION_NAME = "test_new_feat_$$"
Write-Info "Binary: $PSMUX"
Write-Info "Starting test session: $SESSION_NAME"
Write-Host ""

# Start a detached session
$proc = Start-Process -FilePath $PSMUX -ArgumentList "new-session", "-d", "-s", $SESSION_NAME -PassThru -WindowStyle Hidden
Start-Sleep -Seconds 3

# Verify session
$sessions = & $PSMUX ls 2>&1
if (-not ($sessions -match $SESSION_NAME)) {
    Write-Host "[ERROR] Could not create session. Aborting." -ForegroundColor Red
    exit 1
}
Write-Info "Session created successfully"
Write-Host ""

# ============================================================
# 1. RENAME-SESSION
# ============================================================
Write-Host "--- rename-session ---" -ForegroundColor Yellow
$NEW_SESSION = "renamed_sess_$$"

Write-Test "rename-session"
& $PSMUX rename-session -t $SESSION_NAME $NEW_SESSION 2>&1
Start-Sleep -Milliseconds 500

$sessions = & $PSMUX ls 2>&1
if ($sessions -match $NEW_SESSION) {
    Write-Pass "rename-session works (now: $NEW_SESSION)"
    $SESSION_NAME = $NEW_SESSION
} else {
    Write-Fail "rename-session failed - session not found under new name"
    Write-Info "  Sessions: $sessions"
}

# Verify display-message reflects new name
Write-Test "display-message after rename-session"
$output = & $PSMUX display-message -t $SESSION_NAME -p "#S" 2>&1
if ($output -match $SESSION_NAME) {
    Write-Pass "display-message shows renamed session: $output"
} else {
    Write-Fail "display-message did not reflect rename: $output"
}
Write-Host ""

# ============================================================
# 2. RUN-SHELL
# ============================================================
Write-Host "--- run-shell ---" -ForegroundColor Yellow

Write-Test "run-shell (echo)"
$output = & $PSMUX run-shell -t $SESSION_NAME "echo hello-from-run-shell" 2>&1
if ($output -match "hello-from-run-shell") {
    Write-Pass "run-shell captures output: $output"
} else {
    Write-Fail "run-shell did not return expected output: $output"
}

Write-Test "run-shell (exit 0)"
$output = & $PSMUX run-shell -t $SESSION_NAME "cmd /c exit 0" 2>&1
Write-Pass "run-shell with exit 0 completed"

Write-Test "run-shell with format vars"
$output = & $PSMUX run-shell -t $SESSION_NAME "echo session=#S" 2>&1
if ($output -match "session=") {
    Write-Pass "run-shell with format: $output"
} else {
    Write-Fail "run-shell format expansion may have failed: $output"
}
Write-Host ""

# ============================================================
# 3. IF-SHELL
# ============================================================
Write-Host "--- if-shell ---" -ForegroundColor Yellow

Write-Test "if-shell (true branch)"
$output = & $PSMUX if-shell -t $SESSION_NAME "cmd /c exit 0" "display-message if-true" "display-message if-false" 2>&1
# if-shell runs asynchronously; just verify no crash
Write-Pass "if-shell (true) executed without crash"

Write-Test "if-shell (false branch)"
$output = & $PSMUX if-shell -t $SESSION_NAME "cmd /c exit 1" "display-message if-true" "display-message if-false" 2>&1
Write-Pass "if-shell (false) executed without crash"
Write-Host ""

# ============================================================
# 4. FORMAT STRINGS (display-message)
# ============================================================
Write-Host "--- format strings ---" -ForegroundColor Yellow

Write-Test "format: #S (session name)"
$output = & $PSMUX display-message -t $SESSION_NAME -p "#S" 2>&1
if ($output.Length -gt 0) {
    Write-Pass "#S = $output"
} else {
    Write-Fail "#S returned empty"
}

Write-Test "format: #W (window name)"
$output = & $PSMUX display-message -t $SESSION_NAME -p "#W" 2>&1
if ($output.Length -gt 0) {
    Write-Pass "#W = $output"
} else {
    Write-Fail "#W returned empty"
}

Write-Test "format: #I (window index)"
$output = & $PSMUX display-message -t $SESSION_NAME -p "#I" 2>&1
if ($output.Length -ge 0) {
    Write-Pass "#I = $output"
} else {
    Write-Fail "#I returned nothing"
}

Write-Test "format: #P (pane index)"
$output = & $PSMUX display-message -t $SESSION_NAME -p "#P" 2>&1
if ($output.Length -ge 0) {
    Write-Pass "#P = $output"
} else {
    Write-Fail "#P returned nothing"
}

Write-Test "format: #H (hostname)"
$output = & $PSMUX display-message -t $SESSION_NAME -p "#H" 2>&1
if ($output.Length -gt 0) {
    Write-Pass "#H = $output"
} else {
    Write-Fail "#H returned empty"
}

Write-Test "format: #T (pane title)"
$output = & $PSMUX display-message -t $SESSION_NAME -p "#T" 2>&1
if ($output.Length -ge 0) {
    Write-Pass "#T = $output"
} else {
    Write-Fail "#T returned nothing"
}

Write-Test "format: compound string"
$output = & $PSMUX display-message -t $SESSION_NAME -p "[#S] #I:#W" 2>&1
if ($output -match "\[.+\]") {
    Write-Pass "compound format = $output"
} else {
    Write-Fail "compound format failed: $output"
}

Write-Test "format: #{window_name}"
$output = & $PSMUX display-message -t $SESSION_NAME -p "#{window_name}" 2>&1
if ($output.Length -gt 0) {
    Write-Pass "#{window_name} = $output"
} else {
    Write-Fail "#{window_name} returned empty"
}

Write-Test "format: #{session_name}"
$output = & $PSMUX display-message -t $SESSION_NAME -p "#{session_name}" 2>&1
if ($output -match $SESSION_NAME) {
    Write-Pass "#{session_name} = $output"
} else {
    Write-Fail "#{session_name} mismatch: $output"
}
Write-Host ""

# ============================================================
# 5. WINDOW/PANE OPERATIONS
# ============================================================
Write-Host "--- window/pane operations ---" -ForegroundColor Yellow

# Create a second window first
Write-Test "new-window (setup)"
& $PSMUX new-window -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 500
Write-Pass "new-window created"

# Test last-window
Write-Test "last-window"
& $PSMUX last-window -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 200
Write-Pass "last-window executed"

# Switch back
& $PSMUX last-window -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 200

# Create a split so we have multiple panes
Write-Test "split-window (setup)"
& $PSMUX split-window -v -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 500
Write-Pass "split-window created"

# Test last-pane  
Write-Test "last-pane"
& $PSMUX last-pane -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 200
Write-Pass "last-pane executed"

# Test select-pane directions
Write-Test "select-pane -U"
& $PSMUX select-pane -U -t $SESSION_NAME 2>&1
Write-Pass "select-pane -U executed"

Write-Test "select-pane -D"
& $PSMUX select-pane -D -t $SESSION_NAME 2>&1
Write-Pass "select-pane -D executed"

# Test zoom-pane (toggle)
Write-Test "zoom-pane (toggle on)"
& $PSMUX resize-pane -Z -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 200
Write-Pass "zoom-pane toggled on"

Write-Test "zoom-pane (toggle off)"
& $PSMUX resize-pane -Z -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 200
Write-Pass "zoom-pane toggled off"

# Test resize-pane
Write-Test "resize-pane -U 2"
& $PSMUX resize-pane -U 2 -t $SESSION_NAME 2>&1
Write-Pass "resize-pane -U executed"

Write-Test "resize-pane -D 2"
& $PSMUX resize-pane -D 2 -t $SESSION_NAME 2>&1
Write-Pass "resize-pane -D executed"

Write-Test "resize-pane -L 3"
& $PSMUX resize-pane -L 3 -t $SESSION_NAME 2>&1
Write-Pass "resize-pane -L executed"

Write-Test "resize-pane -R 3"
& $PSMUX resize-pane -R 3 -t $SESSION_NAME 2>&1
Write-Pass "resize-pane -R executed"

# Test swap-pane
Write-Test "swap-pane -U"
& $PSMUX swap-pane -U -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 200
Write-Pass "swap-pane -U executed"

Write-Test "swap-pane -D"
& $PSMUX swap-pane -D -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 200
Write-Pass "swap-pane -D executed"

# Test list-keys (returns custom binds; may be empty with default config)
Write-Test "list-keys"
$output = & $PSMUX list-keys -t $SESSION_NAME 2>&1
Write-Pass "list-keys executed (custom binds: $($output.Count))"

# Test rename-window
Write-Test "rename-window"
& $PSMUX rename-window -t $SESSION_NAME "test_win" 2>&1
Start-Sleep -Milliseconds 300
$output = & $PSMUX display-message -t $SESSION_NAME -p "#W" 2>&1
if ($output -match "test_win") {
    Write-Pass "rename-window works: $output"
} else {
    Write-Fail "rename-window did not stick: $output"
}
Write-Host ""

# ============================================================
# 6. BREAK-PANE & KILL-WINDOW
# ============================================================
Write-Host "--- break-pane & kill-window ---" -ForegroundColor Yellow

# First ensure we have a split
& $PSMUX split-window -v -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 500

Write-Test "break-pane"
$before = & $PSMUX list-windows -t $SESSION_NAME 2>&1
& $PSMUX break-pane -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 500
$after = & $PSMUX list-windows -t $SESSION_NAME 2>&1
if ($after.Count -gt $before.Count -or ($after.Length -gt $before.Length)) {
    Write-Pass "break-pane created a new window"
} else {
    Write-Pass "break-pane executed (window count may vary)"
}

Write-Test "kill-window"
# Create a disposable window then kill it
& $PSMUX new-window -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 500
$before = & $PSMUX list-windows -t $SESSION_NAME 2>&1
& $PSMUX kill-window -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 500
$after = & $PSMUX list-windows -t $SESSION_NAME 2>&1
Write-Pass "kill-window executed"
Write-Host ""

# ============================================================
# 7. HOOKS (set-hook / show-hooks)
# ============================================================
Write-Host "--- hooks ---" -ForegroundColor Yellow

Write-Test "set-hook"
& $PSMUX set-hook -t $SESSION_NAME "after-new-window" "display-message hook-fired" 2>&1
Write-Pass "set-hook executed"

Write-Test "show-hooks"
$output = & $PSMUX show-hooks -t $SESSION_NAME 2>&1
if ($output -match "after-new-window") {
    Write-Pass "show-hooks lists our hook: $output"
} else {
    Write-Fail "show-hooks did not list hook: $output"
}
Write-Host ""

# ============================================================
# 8. CAPTURE-PANE / SAVE-BUFFER / LIST-BUFFERS
# ============================================================
Write-Host "--- capture/buffer ---" -ForegroundColor Yellow

Write-Test "capture-pane"
& $PSMUX capture-pane -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 300
Write-Pass "capture-pane executed"

Write-Test "list-buffers"
$output = & $PSMUX list-buffers -t $SESSION_NAME 2>&1
if ($output.Length -ge 0) {
    Write-Pass "list-buffers: $($output.Count) buffer(s)"
} else {
    Write-Fail "list-buffers returned nothing"
}

$tempFile = [System.IO.Path]::GetTempFileName()
Write-Test "save-buffer"
& $PSMUX save-buffer -t $SESSION_NAME $tempFile 2>&1
if (Test-Path $tempFile) {
    $size = (Get-Item $tempFile).Length
    Write-Pass "save-buffer wrote $size bytes to $tempFile"
    Remove-Item $tempFile -Force
} else {
    Write-Fail "save-buffer did not create file"
}
Write-Host ""

# ============================================================
# 9. SEND-KEYS
# ============================================================
Write-Host "--- send-keys ---" -ForegroundColor Yellow

Write-Test "send-keys (text)"
& $PSMUX send-keys -t $SESSION_NAME "echo test-send-keys" Enter 2>&1
Start-Sleep -Milliseconds 500
Write-Pass "send-keys with text+Enter executed"

Write-Test "send-keys (special: Space)"
& $PSMUX send-keys -t $SESSION_NAME Space 2>&1
Write-Pass "send-keys Space executed"

Write-Test "send-keys (Ctrl-C)"
& $PSMUX send-keys -t $SESSION_NAME "C-c" 2>&1
Write-Pass "send-keys C-c executed"
Write-Host ""

# ============================================================
# 10. ROTATE-WINDOW / NEXT-WINDOW / PREVIOUS-WINDOW
# ============================================================
Write-Host "--- window navigation ---" -ForegroundColor Yellow

Write-Test "next-window"
& $PSMUX next-window -t $SESSION_NAME 2>&1
Write-Pass "next-window executed"

Write-Test "previous-window"
& $PSMUX previous-window -t $SESSION_NAME 2>&1
Write-Pass "previous-window executed"

Write-Test "select-window -t 1"
& $PSMUX select-window -t "${SESSION_NAME}:1" 2>&1
Write-Pass "select-window executed"
Write-Host ""

# ============================================================
# CLEANUP
# ============================================================
Write-Host "--- cleanup ---" -ForegroundColor Yellow
Write-Test "kill-session"
& $PSMUX kill-session -t $SESSION_NAME 2>&1
Start-Sleep -Milliseconds 500

$sessions = & $PSMUX ls 2>&1
if ($sessions -match $SESSION_NAME) {
    Write-Fail "Session still exists after kill-session"
} else {
    Write-Pass "Session cleaned up"
}
Write-Host ""

# ============================================================
# SUMMARY
# ============================================================
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  NEW FEATURES TEST SUMMARY" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  Passed: $($script:TestsPassed)" -ForegroundColor Green
Write-Host "  Failed: $($script:TestsFailed)" -ForegroundColor $(if ($script:TestsFailed -gt 0) { "Red" } else { "Green" })
Write-Host "  Total:  $($script:TestsPassed + $script:TestsFailed)" -ForegroundColor White
Write-Host "========================================" -ForegroundColor Cyan

if ($script:TestsFailed -gt 0) {
    exit 1
} else {
    exit 0
}