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
717
718
719
# Issue #229: Window name briefly shows pwsh when starting a session with an initial command
# Tests whether the window name flickers to 'pwsh' before settling on the actual command name
# when creating a session with an initial shell command.

$ErrorActionPreference = "Continue"
$PSMUX = (Get-Command psmux -EA Stop).Source
$psmuxDir = "$env:USERPROFILE\.psmux"
$script:TestsPassed = 0
$script:TestsFailed = 0
$script:AllNames = @{}

function Write-Pass($msg) { Write-Host "  [PASS] $msg" -ForegroundColor Green; $script:TestsPassed++ }
function Write-Fail($msg) { Write-Host "  [FAIL] $msg" -ForegroundColor Red; $script:TestsFailed++ }

function Cleanup {
    param([string]$Name)
    & $PSMUX kill-session -t $Name 2>&1 | Out-Null
    Start-Sleep -Milliseconds 500
    Remove-Item "$psmuxDir\$Name.*" -Force -EA SilentlyContinue
}

function Wait-Session {
    param([string]$Name, [int]$TimeoutMs = 15000)
    $pf = "$psmuxDir\$Name.port"
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    while ($sw.ElapsedMilliseconds -lt $TimeoutMs) {
        if (Test-Path $pf) {
            $port = (Get-Content $pf -Raw).Trim()
            if ($port -match '^\d+$') {
                try {
                    $tcp = [System.Net.Sockets.TcpClient]::new("127.0.0.1", [int]$port)
                    $tcp.Close()
                    return $true
                } catch {}
            }
        }
        Start-Sleep -Milliseconds 50
    }
    return $false
}

function Send-TcpCommand {
    param([string]$Session, [string]$Command)
    $portFile = "$psmuxDir\$Session.port"
    $keyFile = "$psmuxDir\$Session.key"
    if (-not (Test-Path $portFile) -or -not (Test-Path $keyFile)) { return "NO_FILES" }
    $port = (Get-Content $portFile -Raw).Trim()
    $key = (Get-Content $keyFile -Raw).Trim()
    try {
        $tcp = [System.Net.Sockets.TcpClient]::new("127.0.0.1", [int]$port)
        $tcp.NoDelay = $true
        $stream = $tcp.GetStream()
        $writer = [System.IO.StreamWriter]::new($stream)
        $reader = [System.IO.StreamReader]::new($stream)
        $writer.Write("AUTH $key`n"); $writer.Flush()
        $authResp = $reader.ReadLine()
        if ($authResp -ne "OK") { $tcp.Close(); return "AUTH_FAILED" }
        $writer.Write("$Command`n"); $writer.Flush()
        $stream.ReadTimeout = 10000
        try { $resp = $reader.ReadLine() } catch { $resp = "TIMEOUT" }
        $tcp.Close()
        return $resp
    } catch {
        return "CONNECTION_FAILED: $_"
    }
}

function Connect-Persistent {
    param([string]$Session)
    $port = (Get-Content "$psmuxDir\$Session.port" -Raw).Trim()
    $key = (Get-Content "$psmuxDir\$Session.key" -Raw).Trim()
    $tcp = [System.Net.Sockets.TcpClient]::new("127.0.0.1", [int]$port)
    $tcp.NoDelay = $true; $tcp.ReceiveTimeout = 10000
    $stream = $tcp.GetStream()
    $writer = [System.IO.StreamWriter]::new($stream)
    $reader = [System.IO.StreamReader]::new($stream)
    $writer.Write("AUTH $key`n"); $writer.Flush()
    $null = $reader.ReadLine()
    $writer.Write("PERSISTENT`n"); $writer.Flush()
    return @{ tcp=$tcp; writer=$writer; reader=$reader }
}

function Get-Dump {
    param($conn)
    $conn.writer.Write("dump-state`n"); $conn.writer.Flush()
    $best = $null
    $conn.tcp.ReceiveTimeout = 3000
    for ($j = 0; $j -lt 100; $j++) {
        try { $line = $conn.reader.ReadLine() } catch { break }
        if ($null -eq $line) { break }
        if ($line -ne "NC" -and $line.Length -gt 100) { $best = $line }
        if ($best) { $conn.tcp.ReceiveTimeout = 50 }
    }
    $conn.tcp.ReceiveTimeout = 10000
    return $best
}

Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "  Issue #229: Window Name with Initial Command" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan

# ============================================================================
# PART A: CLI Path Tests
# ============================================================================
Write-Host "`n--- Part A: CLI Path Tests ---" -ForegroundColor Cyan

# === Test 1: Session with 'timeout' command, rapid poll for name flicker ===
Write-Host "`n[Test 1] Rapid poll window name during session creation with initial command" -ForegroundColor Yellow
$SESSION1 = "issue229_test1"
Cleanup -Name $SESSION1

# Create session with an explicit command
& $PSMUX new-session -d -s $SESSION1 "timeout /T 120 > NUL"
Start-Sleep -Milliseconds 500

# Wait for session to be alive
$alive = Wait-Session -Name $SESSION1 -TimeoutMs 15000
if (-not $alive) {
    Write-Fail "Test 1: Session $SESSION1 never became alive"
} else {
    # Rapid poll window name every 200ms for 10 seconds
    $observedNames = [System.Collections.ArrayList]::new()
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    while ($sw.ElapsedMilliseconds -lt 10000) {
        $name = (& $PSMUX display-message -t $SESSION1 -p '#{window_name}' 2>&1 | Out-String).Trim()
        if ($name -and $name -ne "") {
            [void]$observedNames.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $name })
        }
        Start-Sleep -Milliseconds 200
    }
    $sw.Stop()

    # Analyze: collect unique names observed
    $uniqueNames = $observedNames | ForEach-Object { $_.Name } | Select-Object -Unique
    $script:AllNames["Test1_timeout"] = $uniqueNames

    Write-Host "    Observed unique window names over 10s:" -ForegroundColor DarkGray
    foreach ($un in $uniqueNames) {
        $count = ($observedNames | Where-Object { $_.Name -eq $un }).Count
        $firstSeen = ($observedNames | Where-Object { $_.Name -eq $un } | Select-Object -First 1).Time
        $lastSeen = ($observedNames | Where-Object { $_.Name -eq $un } | Select-Object -Last 1).Time
        Write-Host "      '$un' seen $count times (first: ${firstSeen}ms, last: ${lastSeen}ms)" -ForegroundColor DarkGray
    }

    # Check if pwsh/powershell appeared at any point
    $shellNames = $uniqueNames | Where-Object { $_ -match '(?i)^(pwsh|powershell|cmd)$' }
    if ($shellNames) {
        Write-Fail "Test 1: Window name flickered to shell name(s): $($shellNames -join ', ')"
        Write-Host "    Timeline:" -ForegroundColor DarkGray
        foreach ($obs in $observedNames) {
            if ($obs.Name -match '(?i)^(pwsh|powershell|cmd)$') {
                Write-Host "      $($obs.Time)ms: '$($obs.Name)' <<< SHELL NAME" -ForegroundColor Red
            } else {
                Write-Host "      $($obs.Time)ms: '$($obs.Name)'" -ForegroundColor DarkGray
            }
        }
    } else {
        Write-Pass "Test 1: Window name never showed shell name. Names: $($uniqueNames -join ', ')"
    }

    # Also check initial name was sensible (should be 'timeout' or similar)
    $firstName = $observedNames[0].Name
    if ($firstName -match '(?i)timeout') {
        Write-Pass "Test 1: Initial window name was '$firstName' (matches command)"
    } else {
        Write-Fail "Test 1: Initial window name was '$firstName', expected something related to 'timeout'"
    }
}
Cleanup -Name $SESSION1

# === Test 2: Session with 'ping' command ===
Write-Host "`n[Test 2] Window name with 'ping' command" -ForegroundColor Yellow
$SESSION2 = "issue229_test2"
Cleanup -Name $SESSION2

& $PSMUX new-session -d -s $SESSION2 "ping -n 120 127.0.0.1"
Start-Sleep -Milliseconds 500

$alive = Wait-Session -Name $SESSION2 -TimeoutMs 15000
if (-not $alive) {
    Write-Fail "Test 2: Session $SESSION2 never became alive"
} else {
    $observedNames2 = [System.Collections.ArrayList]::new()
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    while ($sw.ElapsedMilliseconds -lt 10000) {
        $name = (& $PSMUX display-message -t $SESSION2 -p '#{window_name}' 2>&1 | Out-String).Trim()
        if ($name -and $name -ne "") {
            [void]$observedNames2.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $name })
        }
        Start-Sleep -Milliseconds 200
    }
    $sw.Stop()

    $uniqueNames2 = $observedNames2 | ForEach-Object { $_.Name } | Select-Object -Unique
    $script:AllNames["Test2_ping"] = $uniqueNames2

    Write-Host "    Observed unique window names over 10s:" -ForegroundColor DarkGray
    foreach ($un in $uniqueNames2) {
        $count = ($observedNames2 | Where-Object { $_.Name -eq $un }).Count
        $firstSeen = ($observedNames2 | Where-Object { $_.Name -eq $un } | Select-Object -First 1).Time
        Write-Host "      '$un' seen $count times (first: ${firstSeen}ms)" -ForegroundColor DarkGray
    }

    $shellNames2 = $uniqueNames2 | Where-Object { $_ -match '(?i)^(pwsh|powershell|cmd)$' }
    if ($shellNames2) {
        Write-Fail "Test 2: Window name flickered to shell name(s): $($shellNames2 -join ', ')"
    } else {
        Write-Pass "Test 2: Window name never showed shell name. Names: $($uniqueNames2 -join ', ')"
    }
}
Cleanup -Name $SESSION2

# === Test 3: Session with explicit -n name (should NOT auto-rename at all) ===
Write-Host "`n[Test 3] Session with -n name flag (manual rename lock)" -ForegroundColor Yellow
$SESSION3 = "issue229_test3"
Cleanup -Name $SESSION3

& $PSMUX new-session -d -s $SESSION3 -n "MyCustomName" "timeout /T 120 > NUL"
Start-Sleep -Milliseconds 500

$alive = Wait-Session -Name $SESSION3 -TimeoutMs 15000
if (-not $alive) {
    Write-Fail "Test 3: Session $SESSION3 never became alive"
} else {
    $observedNames3 = [System.Collections.ArrayList]::new()
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    while ($sw.ElapsedMilliseconds -lt 8000) {
        $name = (& $PSMUX display-message -t $SESSION3 -p '#{window_name}' 2>&1 | Out-String).Trim()
        if ($name -and $name -ne "") {
            [void]$observedNames3.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $name })
        }
        Start-Sleep -Milliseconds 300
    }
    $sw.Stop()

    $uniqueNames3 = $observedNames3 | ForEach-Object { $_.Name } | Select-Object -Unique
    $script:AllNames["Test3_manual_name"] = $uniqueNames3

    Write-Host "    Observed unique window names:" -ForegroundColor DarkGray
    foreach ($un in $uniqueNames3) {
        $count = ($observedNames3 | Where-Object { $_.Name -eq $un }).Count
        Write-Host "      '$un' seen $count times" -ForegroundColor DarkGray
    }

    $allCustom = @($uniqueNames3 | Where-Object { $_ -eq "MyCustomName" })
    $notCustom = @($uniqueNames3 | Where-Object { $_ -ne "MyCustomName" })
    if ($notCustom.Count -eq 0 -and $allCustom.Count -gt 0) {
        Write-Pass "Test 3: Window name stayed at 'MyCustomName' the entire time (manual_rename lock)"
    } else {
        Write-Fail "Test 3: Expected only 'MyCustomName', got: $($uniqueNames3 -join ', ')"
    }
}
Cleanup -Name $SESSION3

# ============================================================================
# PART B: TCP Server Path Tests (dump-state JSON)
# ============================================================================
Write-Host "`n--- Part B: TCP Server Path Tests (dump-state) ---" -ForegroundColor Cyan

# === Test 4: Persistent TCP connection, rapid dump-state polling for name ===
Write-Host "`n[Test 4] TCP dump-state rapid poll for window name flicker" -ForegroundColor Yellow
$SESSION4 = "issue229_test4"
Cleanup -Name $SESSION4

& $PSMUX new-session -d -s $SESSION4 "timeout /T 120 > NUL"
Start-Sleep -Milliseconds 500

$alive = Wait-Session -Name $SESSION4 -TimeoutMs 15000
if (-not $alive) {
    Write-Fail "Test 4: Session $SESSION4 never became alive"
} else {
    try {
        $conn = Connect-Persistent -Session $SESSION4
        $dumpNames = [System.Collections.ArrayList]::new()
        $sw = [System.Diagnostics.Stopwatch]::StartNew()

        # Poll dump-state as fast as possible for 10 seconds
        while ($sw.ElapsedMilliseconds -lt 10000) {
            $state = Get-Dump $conn
            if ($state) {
                try {
                    $json = $state | ConvertFrom-Json
                    if ($json.windows -and $json.windows.Count -gt 0) {
                        $wname = $json.windows[0].name
                        [void]$dumpNames.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $wname })
                    }
                } catch {}
            }
            Start-Sleep -Milliseconds 100
        }
        $sw.Stop()
        $conn.tcp.Close()

        $uniqueDump = $dumpNames | ForEach-Object { $_.Name } | Select-Object -Unique
        $script:AllNames["Test4_dump_state"] = $uniqueDump

        Write-Host "    Observed unique names via dump-state:" -ForegroundColor DarkGray
        foreach ($un in $uniqueDump) {
            $count = ($dumpNames | Where-Object { $_.Name -eq $un }).Count
            $firstSeen = ($dumpNames | Where-Object { $_.Name -eq $un } | Select-Object -First 1).Time
            $lastSeen = ($dumpNames | Where-Object { $_.Name -eq $un } | Select-Object -Last 1).Time
            Write-Host "      '$un' seen $count times (first: ${firstSeen}ms, last: ${lastSeen}ms)" -ForegroundColor DarkGray
        }

        $shellDump = $uniqueDump | Where-Object { $_ -match '(?i)^(pwsh|powershell|cmd)$' }
        if ($shellDump) {
            Write-Fail "Test 4: dump-state showed shell name(s): $($shellDump -join ', ')"
            Write-Host "    Full timeline:" -ForegroundColor DarkGray
            foreach ($obs in $dumpNames) {
                $color = if ($obs.Name -match '(?i)^(pwsh|powershell|cmd)$') { "Red" } else { "DarkGray" }
                Write-Host "      $($obs.Time)ms: '$($obs.Name)'" -ForegroundColor $color
            }
        } else {
            Write-Pass "Test 4: dump-state never showed shell name. Names: $($uniqueDump -join ', ')"
        }
    } catch {
        Write-Fail "Test 4: TCP connection error: $_"
    }
}
Cleanup -Name $SESSION4

# ============================================================================
# PART C: Edge Cases
# ============================================================================
Write-Host "`n--- Part C: Edge Cases ---" -ForegroundColor Cyan

# === Test 5: Session with no initial command (should show shell name, that's expected) ===
Write-Host "`n[Test 5] Session with no initial command (baseline: shell name expected)" -ForegroundColor Yellow
$SESSION5 = "issue229_test5"
Cleanup -Name $SESSION5

& $PSMUX new-session -d -s $SESSION5
Start-Sleep -Milliseconds 500

$alive = Wait-Session -Name $SESSION5 -TimeoutMs 15000
if (-not $alive) {
    Write-Fail "Test 5: Session never became alive"
} else {
    Start-Sleep -Seconds 3
    $name5 = (& $PSMUX display-message -t $SESSION5 -p '#{window_name}' 2>&1 | Out-String).Trim()
    Write-Host "    Window name (no command): '$name5'" -ForegroundColor DarkGray
    if ($name5 -match '(?i)^(pwsh|powershell|cmd|shell|bash|zsh|fish)$') {
        Write-Pass "Test 5: No-command session correctly shows shell name: '$name5'"
    } else {
        Write-Pass "Test 5: No-command session shows: '$name5' (acceptable)"
    }
}
Cleanup -Name $SESSION5

# === Test 6: Session with full path command ===
Write-Host "`n[Test 6] Session with full path command (C:\Windows\System32\timeout.exe)" -ForegroundColor Yellow
$SESSION6 = "issue229_test6"
Cleanup -Name $SESSION6

$timeoutExe = "C:\Windows\System32\timeout.exe"
if (Test-Path $timeoutExe) {
    & $PSMUX new-session -d -s $SESSION6 "$timeoutExe /T 120"
    Start-Sleep -Milliseconds 500

    $alive = Wait-Session -Name $SESSION6 -TimeoutMs 15000
    if (-not $alive) {
        Write-Fail "Test 6: Session never became alive"
    } else {
        $observedNames6 = [System.Collections.ArrayList]::new()
        $sw = [System.Diagnostics.Stopwatch]::StartNew()
        while ($sw.ElapsedMilliseconds -lt 8000) {
            $name = (& $PSMUX display-message -t $SESSION6 -p '#{window_name}' 2>&1 | Out-String).Trim()
            if ($name -and $name -ne "") {
                [void]$observedNames6.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $name })
            }
            Start-Sleep -Milliseconds 200
        }
        $sw.Stop()

        $uniqueNames6 = $observedNames6 | ForEach-Object { $_.Name } | Select-Object -Unique
        $script:AllNames["Test6_fullpath"] = $uniqueNames6

        Write-Host "    Observed unique window names:" -ForegroundColor DarkGray
        foreach ($un in $uniqueNames6) {
            $count = ($observedNames6 | Where-Object { $_.Name -eq $un }).Count
            Write-Host "      '$un' seen $count times" -ForegroundColor DarkGray
        }

        $shellNames6 = $uniqueNames6 | Where-Object { $_ -match '(?i)^(pwsh|powershell|cmd)$' }
        if ($shellNames6) {
            Write-Fail "Test 6: Window name flickered to shell name(s): $($shellNames6 -join ', ')"
        } else {
            Write-Pass "Test 6: Window name never showed shell name. Names: $($uniqueNames6 -join ', ')"
        }
    }
    Cleanup -Name $SESSION6
} else {
    Write-Host "    Skipped: $timeoutExe not found" -ForegroundColor DarkGray
}

# === Test 7: Session with PowerShell cmdlet (Get-Process) ===
Write-Host "`n[Test 7] Session with PowerShell cmdlet 'Start-Sleep -Seconds 120'" -ForegroundColor Yellow
$SESSION7 = "issue229_test7"
Cleanup -Name $SESSION7

& $PSMUX new-session -d -s $SESSION7 "Start-Sleep -Seconds 120"
Start-Sleep -Milliseconds 500

$alive = Wait-Session -Name $SESSION7 -TimeoutMs 15000
if (-not $alive) {
    Write-Fail "Test 7: Session never became alive"
} else {
    $observedNames7 = [System.Collections.ArrayList]::new()
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    while ($sw.ElapsedMilliseconds -lt 10000) {
        $name = (& $PSMUX display-message -t $SESSION7 -p '#{window_name}' 2>&1 | Out-String).Trim()
        if ($name -and $name -ne "") {
            [void]$observedNames7.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $name })
        }
        Start-Sleep -Milliseconds 200
    }
    $sw.Stop()

    $uniqueNames7 = $observedNames7 | ForEach-Object { $_.Name } | Select-Object -Unique
    $script:AllNames["Test7_startsleep"] = $uniqueNames7

    Write-Host "    Observed unique window names:" -ForegroundColor DarkGray
    foreach ($un in $uniqueNames7) {
        $count = ($observedNames7 | Where-Object { $_.Name -eq $un }).Count
        $firstSeen = ($observedNames7 | Where-Object { $_.Name -eq $un } | Select-Object -First 1).Time
        Write-Host "      '$un' seen $count times (first: ${firstSeen}ms)" -ForegroundColor DarkGray
    }

    # For Start-Sleep, the process tree is: pwsh -> (Start-Sleep is internal, no child exe)
    # So auto-rename will likely show 'pwsh' here, which is actually expected behavior
    # since Start-Sleep doesn't spawn a child process
    $shellNames7 = $uniqueNames7 | Where-Object { $_ -match '(?i)^(pwsh|powershell)$' }
    if ($shellNames7) {
        Write-Host "    Note: 'pwsh' is expected for Start-Sleep (it is a PowerShell cmdlet, no child process)" -ForegroundColor DarkGray
        Write-Pass "Test 7: Start-Sleep correctly shows pwsh (cmdlet has no child process)"
    } else {
        Write-Pass "Test 7: Window name was: $($uniqueNames7 -join ', ')"
    }
}
Cleanup -Name $SESSION7

# === Test 8: Very rapid polling to catch brief flicker ===
Write-Host "`n[Test 8] Ultra-rapid polling (every 50ms) for first 5 seconds" -ForegroundColor Yellow
$SESSION8 = "issue229_test8"
Cleanup -Name $SESSION8

& $PSMUX new-session -d -s $SESSION8 "timeout /T 120 > NUL"

# Start polling IMMEDIATELY, even before session is fully up
$observedNames8 = [System.Collections.ArrayList]::new()
$sw = [System.Diagnostics.Stopwatch]::StartNew()
while ($sw.ElapsedMilliseconds -lt 12000) {
    $name = (& $PSMUX display-message -t $SESSION8 -p '#{window_name}' 2>&1 | Out-String).Trim()
    if ($name -and $name -ne "" -and $name -notmatch "error|failed|no server|can't") {
        [void]$observedNames8.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $name })
    }
    Start-Sleep -Milliseconds 50
}
$sw.Stop()

if ($observedNames8.Count -eq 0) {
    Write-Fail "Test 8: Never got a window name"
} else {
    $uniqueNames8 = $observedNames8 | ForEach-Object { $_.Name } | Select-Object -Unique
    $script:AllNames["Test8_rapid"] = $uniqueNames8

    Write-Host "    Total samples: $($observedNames8.Count)" -ForegroundColor DarkGray
    Write-Host "    Observed unique window names:" -ForegroundColor DarkGray
    foreach ($un in $uniqueNames8) {
        $count = ($observedNames8 | Where-Object { $_.Name -eq $un }).Count
        $firstSeen = ($observedNames8 | Where-Object { $_.Name -eq $un } | Select-Object -First 1).Time
        $lastSeen = ($observedNames8 | Where-Object { $_.Name -eq $un } | Select-Object -Last 1).Time
        Write-Host "      '$un' seen $count times (first: ${firstSeen}ms, last: ${lastSeen}ms)" -ForegroundColor DarkGray
    }

    $shellNames8 = $uniqueNames8 | Where-Object { $_ -match '(?i)^(pwsh|powershell|cmd)$' }
    if ($shellNames8) {
        # Find the duration of the flicker
        $firstShell = ($observedNames8 | Where-Object { $_.Name -match '(?i)^(pwsh|powershell|cmd)$' } | Select-Object -First 1).Time
        $lastShell = ($observedNames8 | Where-Object { $_.Name -match '(?i)^(pwsh|powershell|cmd)$' } | Select-Object -Last 1).Time
        $flickerDuration = $lastShell - $firstShell
        Write-Fail "Test 8: FLICKER DETECTED! Shell name visible for ~${flickerDuration}ms (${firstShell}ms to ${lastShell}ms)"
        Write-Host "    Detailed timeline (first 30 samples):" -ForegroundColor DarkGray
        $observedNames8 | Select-Object -First 30 | ForEach-Object {
            $color = if ($_.Name -match '(?i)^(pwsh|powershell|cmd)$') { "Red" } else { "DarkGray" }
            Write-Host "      $($_.Time)ms: '$($_.Name)'" -ForegroundColor $color
        }
    } else {
        Write-Pass "Test 8: Ultra-rapid poll ($($observedNames8.Count) samples) never caught shell name"
    }
}
Cleanup -Name $SESSION8

# ============================================================================
# PART D: Interaction Tests
# ============================================================================
Write-Host "`n--- Part D: Interaction with automatic-rename ---" -ForegroundColor Cyan

# === Test 9: automatic-rename off should not flicker ===
Write-Host "`n[Test 9] automatic-rename off: name should stay fixed" -ForegroundColor Yellow
$SESSION9 = "issue229_test9"
Cleanup -Name $SESSION9

& $PSMUX new-session -d -s $SESSION9 "timeout /T 120 > NUL"
Start-Sleep -Milliseconds 500

$alive = Wait-Session -Name $SESSION9 -TimeoutMs 15000
if (-not $alive) {
    Write-Fail "Test 9: Session never became alive"
} else {
    # Turn off automatic-rename
    & $PSMUX set-option -g -t $SESSION9 automatic-rename off 2>&1 | Out-Null
    Start-Sleep -Milliseconds 500

    # Get current name
    $nameBeforeOff = (& $PSMUX display-message -t $SESSION9 -p '#{window_name}' 2>&1 | Out-String).Trim()
    Write-Host "    Name after disabling auto-rename: '$nameBeforeOff'" -ForegroundColor DarkGray

    # Poll for a few seconds
    $observedNames9 = [System.Collections.ArrayList]::new()
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    while ($sw.ElapsedMilliseconds -lt 5000) {
        $name = (& $PSMUX display-message -t $SESSION9 -p '#{window_name}' 2>&1 | Out-String).Trim()
        if ($name -and $name -ne "") {
            [void]$observedNames9.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $name })
        }
        Start-Sleep -Milliseconds 200
    }

    $uniqueNames9 = $observedNames9 | ForEach-Object { $_.Name } | Select-Object -Unique
    if ($uniqueNames9.Count -eq 1) {
        Write-Pass "Test 9: Name stayed at '$($uniqueNames9[0])' with automatic-rename off"
    } else {
        Write-Fail "Test 9: Name changed even with automatic-rename off: $($uniqueNames9 -join ', ')"
    }
}
Cleanup -Name $SESSION9

# ============================================================================
# PART E: Exact reproduction from issue reporter
# ============================================================================
Write-Host "`n--- Part E: Exact Issue Reporter Reproduction ---" -ForegroundColor Cyan

# === Test 10: Exact command from issue report ===
Write-Host "`n[Test 10] Exact repro: psmux new -s repro-timeout 'timeout /T 300 > NUL'" -ForegroundColor Yellow
$SESSION10 = "repro-timeout"
Cleanup -Name $SESSION10

& $PSMUX new -s $SESSION10 -d 'timeout /T 300 > NUL'
Start-Sleep -Milliseconds 500

$alive = Wait-Session -Name $SESSION10 -TimeoutMs 15000
if (-not $alive) {
    Write-Fail "Test 10: Session never became alive"
} else {
    $observedNames10 = [System.Collections.ArrayList]::new()
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    while ($sw.ElapsedMilliseconds -lt 15000) {
        $name = (& $PSMUX display-message -t $SESSION10 -p '#{window_name}' 2>&1 | Out-String).Trim()
        if ($name -and $name -ne "" -and $name -notmatch "error|failed|no server|can't") {
            [void]$observedNames10.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $name })
        }
        Start-Sleep -Milliseconds 100
    }
    $sw.Stop()

    $uniqueNames10 = $observedNames10 | ForEach-Object { $_.Name } | Select-Object -Unique
    $script:AllNames["Test10_exact_repro"] = $uniqueNames10

    Write-Host "    Observed unique window names (15s, 100ms intervals):" -ForegroundColor DarkGray
    foreach ($un in $uniqueNames10) {
        $count = ($observedNames10 | Where-Object { $_.Name -eq $un }).Count
        $firstSeen = ($observedNames10 | Where-Object { $_.Name -eq $un } | Select-Object -First 1).Time
        $lastSeen = ($observedNames10 | Where-Object { $_.Name -eq $un } | Select-Object -Last 1).Time
        Write-Host "      '$un' seen $count times (first: ${firstSeen}ms, last: ${lastSeen}ms)" -ForegroundColor DarkGray
    }

    $shellNames10 = $uniqueNames10 | Where-Object { $_ -match '(?i)^(pwsh|powershell|cmd)$' }
    if ($shellNames10) {
        $firstShell = ($observedNames10 | Where-Object { $_.Name -match '(?i)^(pwsh|powershell|cmd)$' } | Select-Object -First 1).Time
        $lastShell = ($observedNames10 | Where-Object { $_.Name -match '(?i)^(pwsh|powershell|cmd)$' } | Select-Object -Last 1).Time
        Write-Fail "Test 10: REPRO CONFIRMED! Shell name visible from ${firstShell}ms to ${lastShell}ms"
    } else {
        Write-Pass "Test 10: Exact repro did NOT show shell name flicker. Names: $($uniqueNames10 -join ', ')"
    }
}
Cleanup -Name $SESSION10

# === Test 11: Second exact command from issue ===
Write-Host "`n[Test 11] Exact repro: psmux new -s start-session-with-command 'timeout /T 300 > NUL'" -ForegroundColor Yellow
$SESSION11 = "start-session-with-command"
Cleanup -Name $SESSION11

& $PSMUX new -s $SESSION11 -d 'timeout /T 300 > NUL'
Start-Sleep -Milliseconds 500

$alive = Wait-Session -Name $SESSION11 -TimeoutMs 15000
if (-not $alive) {
    Write-Fail "Test 11: Session never became alive"
} else {
    $observedNames11 = [System.Collections.ArrayList]::new()
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    while ($sw.ElapsedMilliseconds -lt 15000) {
        $name = (& $PSMUX display-message -t $SESSION11 -p '#{window_name}' 2>&1 | Out-String).Trim()
        if ($name -and $name -ne "" -and $name -notmatch "error|failed|no server|can't") {
            [void]$observedNames11.Add(@{ Time = $sw.ElapsedMilliseconds; Name = $name })
        }
        Start-Sleep -Milliseconds 100
    }
    $sw.Stop()

    $uniqueNames11 = $observedNames11 | ForEach-Object { $_.Name } | Select-Object -Unique
    $script:AllNames["Test11_named_session"] = $uniqueNames11

    Write-Host "    Observed unique window names (15s, 100ms intervals):" -ForegroundColor DarkGray
    foreach ($un in $uniqueNames11) {
        $count = ($observedNames11 | Where-Object { $_.Name -eq $un }).Count
        $firstSeen = ($observedNames11 | Where-Object { $_.Name -eq $un } | Select-Object -First 1).Time
        Write-Host "      '$un' seen $count times (first: ${firstSeen}ms)" -ForegroundColor DarkGray
    }

    $shellNames11 = $uniqueNames11 | Where-Object { $_ -match '(?i)^(pwsh|powershell|cmd)$' }
    if ($shellNames11) {
        Write-Fail "Test 11: Shell name appeared in names: $($uniqueNames11 -join ', ')"
    } else {
        Write-Pass "Test 11: No shell name flicker. Names: $($uniqueNames11 -join ', ')"
    }
}
Cleanup -Name $SESSION11

# ============================================================================
# Win32 TUI VISUAL VERIFICATION
# ============================================================================
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "  Win32 TUI Visual Verification" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan

$SESSION_TUI = "issue229_tui"
Cleanup -Name $SESSION_TUI

# Launch a REAL visible psmux window with an initial command
# Start-Process with -PassThru creates a new console window (separate process)
# Use -d (detached) for server launch, then attach in a separate visible window
# Note: using array form for ArgumentList; Start-Process joins with spaces
& $PSMUX new-session -d -s $SESSION_TUI "timeout /T 120"
Start-Sleep -Seconds 3
# Now launch a visible TUI that attaches to this session
$proc = Start-Process -FilePath $PSMUX -ArgumentList 'attach-session','-t',$SESSION_TUI -PassThru
Start-Sleep -Seconds 3

# Verify session exists
& $PSMUX has-session -t $SESSION_TUI 2>$null
if ($LASTEXITCODE -ne 0) {
    Write-Fail "TUI: Session creation failed"
} else {
    # TUI Check 1: Window name should reflect the command
    $tuiName = (& $PSMUX display-message -t $SESSION_TUI -p '#{window_name}' 2>&1 | Out-String).Trim()
    Write-Host "    TUI window name after 6s: '$tuiName'" -ForegroundColor DarkGray
    if ($tuiName -match '(?i)^(pwsh|powershell|cmd)$') {
        Write-Fail "TUI Check 1: Window name is '$tuiName' (still showing shell name after 6s)"
    } else {
        Write-Pass "TUI Check 1: Window name is '$tuiName' (not shell name)"
    }

    # TUI Check 2: Window name stability over next 5 seconds
    $tuiObserved = [System.Collections.ArrayList]::new()
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    while ($sw.ElapsedMilliseconds -lt 5000) {
        $n = (& $PSMUX display-message -t $SESSION_TUI -p '#{window_name}' 2>&1 | Out-String).Trim()
        if ($n) { [void]$tuiObserved.Add($n) }
        Start-Sleep -Milliseconds 300
    }
    $tuiUnique = $tuiObserved | Select-Object -Unique
    if ($tuiUnique.Count -le 1) {
        Write-Pass "TUI Check 2: Window name stable: '$($tuiUnique -join ', ')'"
    } else {
        Write-Fail "TUI Check 2: Window name changed during observation: $($tuiUnique -join ', ')"
    }

    # TUI Check 3: Verify session count
    $sessCount = (& $PSMUX display-message -t $SESSION_TUI -p '#{session_windows}' 2>&1 | Out-String).Trim()
    if ($sessCount -ge 1) {
        Write-Pass "TUI Check 3: Session has $sessCount window(s)"
    } else {
        Write-Fail "TUI Check 3: Expected at least 1 window, got $sessCount"
    }
}

# Cleanup TUI
& $PSMUX kill-session -t $SESSION_TUI 2>&1 | Out-Null
try { Stop-Process -Id $proc.Id -Force -EA SilentlyContinue } catch {}

# ============================================================================
# SUMMARY
# ============================================================================
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "  Summary of Observed Names" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan

$bugFound = $false
foreach ($testKey in ($script:AllNames.Keys | Sort-Object)) {
    $names = $script:AllNames[$testKey]
    $hasShell = $names | Where-Object { $_ -match '(?i)^(pwsh|powershell|cmd)$' }
    $marker = if ($hasShell -and $testKey -notmatch 'startsleep|baseline') { " <<< BUG" } else { "" }
    if ($hasShell -and $testKey -notmatch 'startsleep|baseline') { $bugFound = $true }
    Write-Host "  $testKey : $($names -join ' -> ')$marker" -ForegroundColor $(if ($marker) { "Red" } else { "Green" })
}

Write-Host ""
if ($bugFound) {
    Write-Host "  VERDICT: Issue #229 is CONFIRMED. Window name briefly shows shell name." -ForegroundColor Red
} else {
    Write-Host "  VERDICT: Issue #229 could NOT be reproduced. Window name never flickered to shell name." -ForegroundColor Green
}

Write-Host "`n=== Results ===" -ForegroundColor Cyan
Write-Host "  Passed: $($script:TestsPassed)" -ForegroundColor Green
Write-Host "  Failed: $($script:TestsFailed)" -ForegroundColor $(if ($script:TestsFailed -gt 0) { "Red" } else { "Green" })
exit $script:TestsFailed