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
"""
PR #207 Workaround Elimination: Python Tests

Proves that psmux works with the EXACT subprocess patterns that
cli-agent-orchestrator (CAO) and libtmux use to communicate with tmux/psmux.

These tests exercise every workaround from PR #207 using Python subprocess
calls, matching the EXACT patterns from:
  - awslabs/cli-agent-orchestrator tmux.py
  - libtmux's internal Server/Session/Window API patterns

Run: .venv/Scripts/python.exe -m pytest tests/test_pr207_libtmux.py -v
"""

import subprocess
import time
import uuid
import os
import pytest

PSMUX_BIN = "psmux"


def find_psmux():
    """Find the psmux binary."""
    result = subprocess.run(
        ["where", "psmux"], capture_output=True, text=True
    )
    if result.returncode == 0:
        return result.stdout.strip().splitlines()[0]
    return PSMUX_BIN


def psmux(*args, psmux_path=None, input_data=None, check=False):
    """Run a psmux command and return the CompletedProcess."""
    bin_path = psmux_path or find_psmux()
    return subprocess.run(
        [bin_path, *args],
        capture_output=True, text=True, timeout=10,
        input=input_data,
    )


@pytest.fixture(scope="module")
def psmux_path():
    return find_psmux()


@pytest.fixture
def session(psmux_path):
    """Create a fresh psmux session. Cleanup after test."""
    name = f"pytest_{uuid.uuid4().hex[:8]}"
    psmux("new-session", "-d", "-s", name, psmux_path=psmux_path)
    time.sleep(1.5)
    yield name
    psmux("kill-session", "-t", name, psmux_path=psmux_path)


# ============================================================
# WA1: list-sessions -F was ignored
# CAO workaround: parse default 'NAME: N windows' text
# ============================================================
class TestWA1_ListSessionsFormat:
    """Proves -F format flag works on list-sessions."""

    def test_format_session_name(self, session, psmux_path):
        result = psmux("list-sessions", "-F", "#{session_name}", psmux_path=psmux_path)
        names = [l.strip() for l in result.stdout.strip().splitlines() if l.strip()]
        assert session in names

    def test_format_session_id(self, session, psmux_path):
        result = psmux("list-sessions", "-F", "#{session_id}", psmux_path=psmux_path)
        ids = [l.strip() for l in result.stdout.strip().splitlines() if l.strip()]
        assert any(sid.startswith("$") for sid in ids)

    def test_format_complex(self, session, psmux_path):
        result = psmux(
            "list-sessions", "-F",
            "#{session_name}:#{session_id}:#{session_windows}",
            psmux_path=psmux_path,
        )
        lines = [l.strip() for l in result.stdout.strip().splitlines() if l.strip()]
        matching = [l for l in lines if l.startswith(session + ":")]
        assert len(matching) == 1
        parts = matching[0].split(":")
        assert len(parts) == 3

    def test_not_default_format(self, session, psmux_path):
        """Verify -F produces formatted output, not the default 'NAME: N windows' text."""
        result = psmux("list-sessions", "-F", "#{session_name}", psmux_path=psmux_path)
        assert "windows" not in result.stdout
        assert "created" not in result.stdout


# ============================================================
# WA2: -F#{fmt} (concatenated, no space) was ignored
# CAO workaround: always use space-separated -F '#{fmt}'
# ============================================================
class TestWA2_ConcatenatedFormat:
    """Proves -F#{fmt} (no space) works identically to -F '#{fmt}'."""

    def test_concat_equals_space(self, session, psmux_path):
        r_space = psmux("list-sessions", "-F", "#{session_name}", psmux_path=psmux_path)
        r_concat = psmux("list-sessions", "-F#{session_name}", psmux_path=psmux_path)
        assert r_space.stdout.strip() == r_concat.stdout.strip()

    def test_concat_new_session(self, psmux_path):
        name = f"pytest_concat_{uuid.uuid4().hex[:8]}"
        result = psmux(
            "new-session", "-d", "-s", name, "-P", "-F#{session_id}",
            psmux_path=psmux_path,
        )
        time.sleep(1.0)
        assert result.stdout.strip().startswith("$")
        psmux("kill-session", "-t", name, psmux_path=psmux_path)

    def test_concat_list_windows(self, session, psmux_path):
        result = psmux(
            "list-windows", "-t", session, "-F#{window_name}",
            psmux_path=psmux_path,
        )
        assert result.stdout.strip() != ""


# ============================================================
# WA3: has-session -t =NAME not supported
# CAO workaround: call without = prefix
# ============================================================
class TestWA3_HasSessionExactMatch:
    """Proves has-session -t =NAME exact-match works."""

    def test_exact_match_existing(self, session, psmux_path):
        result = psmux("has-session", "-t", f"={session}", psmux_path=psmux_path)
        assert result.returncode == 0

    def test_exact_match_nonexistent(self, psmux_path):
        result = psmux("has-session", "-t", "=nonexistent_xyz_99", psmux_path=psmux_path)
        assert result.returncode != 0

    def test_no_prefix_match(self, session, psmux_path):
        """=NAME should NOT prefix-match a longer session name."""
        long_name = session + "_extended"
        psmux("new-session", "-d", "-s", long_name, psmux_path=psmux_path)
        time.sleep(1.0)

        # =session should find session (exact), not long_name
        r1 = psmux("has-session", "-t", f"={session}", psmux_path=psmux_path)
        assert r1.returncode == 0

        # =session + "_ext" (partial of long_name) should NOT match
        partial = session + "_ext"
        r2 = psmux("has-session", "-t", f"={partial}", psmux_path=psmux_path)
        assert r2.returncode != 0

        psmux("kill-session", "-t", long_name, psmux_path=psmux_path)

    def test_backward_compat_without_equals(self, session, psmux_path):
        result = psmux("has-session", "-t", session, psmux_path=psmux_path)
        assert result.returncode == 0


# ============================================================
# WA4: -e KEY=VAL not propagated into shell
# CAO workaround: stamp env vars via powershell prefix command
# ============================================================
class TestWA4_EnvironmentVariables:
    """Proves -e KEY=VAL propagation works."""

    def test_env_var_propagated(self, psmux_path):
        name = f"pytest_env_{uuid.uuid4().hex[:8]}"
        env_val = f"python_test_{uuid.uuid4().hex[:6]}"

        psmux(
            "new-session", "-d", "-s", name,
            "-e", f"PYTEST_VAR={env_val}",
            psmux_path=psmux_path,
        )
        time.sleep(1.5)

        # Send command to echo the env var
        psmux(
            "send-keys", "-t", name,
            f'Write-Output "ENV_CHECK:$env:PYTEST_VAR"', "Enter",
            psmux_path=psmux_path,
        )
        time.sleep(1.5)

        result = psmux("capture-pane", "-t", name, "-p", psmux_path=psmux_path)
        assert f"ENV_CHECK:{env_val}" in result.stdout

        psmux("kill-session", "-t", name, psmux_path=psmux_path)


# ============================================================
# WA5: Named paste buffers did not exist
# CAO workaround: fixed buffer name, serialize calls per window
# ============================================================
class TestWA5_NamedBuffers:
    """Proves UUID-named buffers work (set/show/delete/paste/load)."""

    def test_set_and_show(self, session, psmux_path):
        buf = f"buf_{uuid.uuid4().hex[:8]}"
        psmux("set-buffer", "-b", buf, "PYTHON_BUF_TEST", psmux_path=psmux_path)
        result = psmux("show-buffer", "-b", buf, psmux_path=psmux_path)
        assert result.stdout.strip() == "PYTHON_BUF_TEST"
        psmux("delete-buffer", "-b", buf, psmux_path=psmux_path)

    def test_independent_buffers(self, session, psmux_path):
        """Multiple UUID-named buffers should be independent (no collision)."""
        buffers = {}
        for i in range(5):
            name = f"ind_{uuid.uuid4().hex[:8]}"
            content = f"CONTENT_{i}_{uuid.uuid4().hex[:4]}"
            buffers[name] = content
            psmux("set-buffer", "-b", name, content, psmux_path=psmux_path)

        for name, expected in buffers.items():
            result = psmux("show-buffer", "-b", name, psmux_path=psmux_path)
            assert result.stdout.strip() == expected, (
                f"Buffer {name}: expected '{expected}', got '{result.stdout.strip()}'"
            )

        for name in buffers:
            psmux("delete-buffer", "-b", name, psmux_path=psmux_path)

    def test_delete_buffer(self, session, psmux_path):
        buf = f"del_{uuid.uuid4().hex[:8]}"
        psmux("set-buffer", "-b", buf, "TO_DELETE", psmux_path=psmux_path)
        psmux("delete-buffer", "-b", buf, psmux_path=psmux_path)
        result = psmux("show-buffer", "-b", buf, psmux_path=psmux_path)
        assert result.stdout.strip() == ""

    def test_paste_buffer_into_pane(self, session, psmux_path):
        """The EXACT CAO pattern: set-buffer -> paste-buffer -> send Enter."""
        buf = f"paste_{uuid.uuid4().hex[:8]}"
        content = "echo PASTE_OK_PYTHON"

        psmux("set-buffer", "-b", buf, content, psmux_path=psmux_path)
        psmux("send-keys", "-t", session, "clear", "Enter", psmux_path=psmux_path)
        time.sleep(1.0)
        psmux("paste-buffer", "-b", buf, "-t", session, psmux_path=psmux_path)
        time.sleep(0.5)
        psmux("send-keys", "-t", session, "Enter", psmux_path=psmux_path)
        time.sleep(1.5)

        result = psmux("capture-pane", "-t", session, "-p", psmux_path=psmux_path)
        assert "PASTE_OK_PYTHON" in result.stdout

        psmux("delete-buffer", "-b", buf, psmux_path=psmux_path)

    def test_load_buffer_from_stdin(self, session, psmux_path):
        """CAO uses load-buffer -b <uuid> - with stdin pipe."""
        buf = f"load_{uuid.uuid4().hex[:8]}"
        psmux(
            "load-buffer", "-b", buf, "-",
            psmux_path=psmux_path,
            input_data="LOADED_VIA_STDIN",
        )
        show = psmux("show-buffer", "-b", buf, psmux_path=psmux_path)
        assert "LOADED_VIA_STDIN" in show.stdout
        psmux("delete-buffer", "-b", buf, psmux_path=psmux_path)


# ============================================================
# WA6: paste-buffer -p (bracketed paste mode)
# CAO note: "Not yet a blocker"
# ============================================================
class TestWA6_BracketedPaste:
    """Proves paste-buffer -p at least pastes content."""

    def test_paste_p_pastes_content(self, session, psmux_path):
        buf = f"bp_{uuid.uuid4().hex[:8]}"
        psmux("set-buffer", "-b", buf, "BRACKETED_PYTHON_TEST", psmux_path=psmux_path)
        psmux("send-keys", "-t", session, "clear", "Enter", psmux_path=psmux_path)
        time.sleep(1.0)
        psmux("paste-buffer", "-p", "-b", buf, "-t", session, psmux_path=psmux_path)
        time.sleep(1.5)
        result = psmux("capture-pane", "-t", session, "-p", psmux_path=psmux_path)
        assert "BRACKETED_PYTHON_TEST" in result.stdout
        psmux("delete-buffer", "-b", buf, psmux_path=psmux_path)


# ============================================================
# CAO WORKFLOW: Exact send_keys() simulation
# ============================================================
class TestCAOWorkflow:
    """End-to-end simulation of CAO's send_keys() method."""

    def test_full_send_keys_workflow(self, session, psmux_path):
        """
        Replicate the EXACT sequence from awslabs/cli-agent-orchestrator tmux.py:
        1. load-buffer -b <uuid> - (from stdin)
        2. paste-buffer -p -b <uuid> -t <session>
        3. time.sleep(0.3)
        4. send-keys -t <session> Enter
        5. delete-buffer -b <uuid>  (in finally block)
        """
        cao_buf = f"cao_{uuid.uuid4().hex[:8]}"
        command = 'Write-Output "CAO_PYTHON_E2E_OK"'

        # Clear pane
        psmux("send-keys", "-t", session, "clear", "Enter", psmux_path=psmux_path)
        time.sleep(1.0)

        # Step 1: load-buffer from stdin
        psmux("load-buffer", "-b", cao_buf, "-",
              psmux_path=psmux_path, input_data=command)

        # Check if load worked, fallback to set-buffer
        check = psmux("show-buffer", "-b", cao_buf, psmux_path=psmux_path)
        if command not in check.stdout:
            psmux("set-buffer", "-b", cao_buf, command, psmux_path=psmux_path)

        # Step 2: paste-buffer -p
        psmux("paste-buffer", "-p", "-b", cao_buf, "-t", session,
              psmux_path=psmux_path)

        # Step 3: sleep (CAO sleeps 300ms)
        time.sleep(0.5)

        # Step 4: send Enter
        psmux("send-keys", "-t", session, "Enter", psmux_path=psmux_path)
        time.sleep(1.5)

        # Step 5: delete-buffer (finally block)
        psmux("delete-buffer", "-b", cao_buf, psmux_path=psmux_path)

        # Verify command was executed
        result = psmux("capture-pane", "-t", session, "-p", psmux_path=psmux_path)
        assert "CAO_PYTHON_E2E_OK" in result.stdout

        # Verify buffer was deleted
        buf_check = psmux("show-buffer", "-b", cao_buf, psmux_path=psmux_path)
        assert buf_check.stdout.strip() == ""


# ============================================================
# LIBTMUX FORMAT PATTERNS: Exact patterns libtmux uses
# ============================================================
class TestLibtmuxPatterns:
    """Test the EXACT format patterns libtmux uses internally."""

    def test_new_session_print_format(self, psmux_path):
        """libtmux: new-session -P -F '#{session_id}:#{session_name}'"""
        name = f"pytest_pf_{uuid.uuid4().hex[:8]}"
        result = psmux(
            "new-session", "-d", "-s", name, "-P",
            "-F", "#{session_id}:#{session_name}",
            psmux_path=psmux_path,
        )
        time.sleep(1.0)
        output = result.stdout.strip()
        assert ":" in output
        assert name in output
        psmux("kill-session", "-t", name, psmux_path=psmux_path)

    def test_list_sessions_multi_field(self, session, psmux_path):
        """libtmux: list-sessions -F '#{session_id} #{session_name} #{session_windows}'"""
        result = psmux(
            "list-sessions", "-F",
            "#{session_id} #{session_name} #{session_windows}",
            psmux_path=psmux_path,
        )
        lines = [l.strip() for l in result.stdout.strip().splitlines() if l.strip()]
        matching = [l for l in lines if session in l]
        assert len(matching) >= 1
        parts = matching[0].split()
        assert len(parts) == 3
        assert parts[0].startswith("$")
        assert parts[1] == session
        assert parts[2].isdigit()

    def test_list_windows_multi_field(self, session, psmux_path):
        """libtmux: list-windows -F '#{window_id} #{window_name} #{window_index}'"""
        result = psmux(
            "list-windows", "-t", session, "-F",
            "#{window_id} #{window_name} #{window_index}",
            psmux_path=psmux_path,
        )
        output = result.stdout.strip()
        assert output != ""
        parts = output.split()
        assert len(parts) == 3
        assert parts[0].startswith("@")

    def test_list_panes_format(self, session, psmux_path):
        """libtmux: list-panes -F '#{pane_id} #{pane_index} #{pane_width} #{pane_height}'"""
        result = psmux(
            "list-panes", "-t", session, "-F",
            "#{pane_id} #{pane_index} #{pane_width} #{pane_height}",
            psmux_path=psmux_path,
        )
        output = result.stdout.strip()
        assert output != ""
        parts = output.split()
        assert len(parts) == 4
        assert parts[0].startswith("%")

    def test_session_format_keys(self, session, psmux_path):
        """Test key format variables libtmux needs for Session objects."""
        keys = ["session_name", "session_id", "session_windows",
                "session_created", "session_attached"]
        for key in keys:
            result = psmux(
                "list-sessions", "-F", f"#{{{key}}}",
                psmux_path=psmux_path,
            )
            lines = result.stdout.strip().splitlines()
            non_empty = [l.strip() for l in lines if l.strip()]
            assert len(non_empty) > 0, f"Format key {key} returned no values"


# ============================================================
# CONCURRENT OPERATIONS: Prove no serialization needed
# ============================================================
class TestConcurrency:
    """Prove named buffers eliminate the need for CAO's per-window serialization."""

    def test_concurrent_buffer_ops(self, session, psmux_path):
        """Multiple named buffers can be set/shown/deleted without collisions."""
        bufs = {}
        for i in range(10):
            name = f"conc_{uuid.uuid4().hex[:8]}"
            content = f"CONCURRENT_{i}"
            bufs[name] = content
            psmux("set-buffer", "-b", name, content, psmux_path=psmux_path)

        for name, expected in bufs.items():
            result = psmux("show-buffer", "-b", name, psmux_path=psmux_path)
            assert result.stdout.strip() == expected

        for name in bufs:
            psmux("delete-buffer", "-b", name, psmux_path=psmux_path)

        for name in bufs:
            result = psmux("show-buffer", "-b", name, psmux_path=psmux_path)
            assert result.stdout.strip() == ""


# ============================================================
# LIBTMUX NATIVE API: Prove Server.sessions works out of the box
# ============================================================
class TestLibtmuxNativeAPI:
    """Prove libtmux's native Python API (Server.sessions, .windows, .panes)
    works with psmux out of the box — no workarounds, no PYTHONUTF8, no
    encoding patches.

    This is the KEY proof that libtmux works natively with psmux.
    Requires: pip install libtmux AND the libtmux encoding patch
    (encoding='utf-8' in common.py Popen call).
    """

    @pytest.fixture(autouse=True)
    def _check_libtmux(self):
        """Skip tests if libtmux is not installed."""
        pytest.importorskip("libtmux")

    @pytest.fixture
    def libtmux_session(self, psmux_path):
        """Create session via CLI, return libtmux Session object."""
        import libtmux
        name = f"lt_{uuid.uuid4().hex[:8]}"
        psmux("new-session", "-d", "-s", name, psmux_path=psmux_path)
        time.sleep(2)
        server = libtmux.Server(socket_name="default")
        sessions = server.sessions
        sess = sessions.get(session_name=name, default=None)
        assert sess is not None, f"Session {name} not found via libtmux API"
        yield sess
        psmux("kill-session", "-t", name, psmux_path=psmux_path)

    def test_server_sessions_returns_sessions(self, libtmux_session):
        """Server.sessions returns non-empty list with valid session objects."""
        import libtmux
        server = libtmux.Server(socket_name="default")
        sessions = server.sessions
        assert len(sessions) >= 1
        assert libtmux_session.name in [s.name for s in sessions]

    def test_session_has_valid_id(self, libtmux_session):
        """Session ID is in $N format."""
        assert libtmux_session.id.startswith("$")

    def test_session_windows(self, libtmux_session):
        """session.windows returns list of Window objects."""
        windows = libtmux_session.windows
        assert len(windows) >= 1
        w = windows[0]
        assert w.id.startswith("@")
        assert w.name  # has a name

    def test_window_panes(self, libtmux_session):
        """window.panes returns list of Pane objects."""
        w = libtmux_session.windows[0]
        panes = w.panes
        assert len(panes) >= 1
        p = panes[0]
        assert p.pane_id.startswith("%")

    def test_new_window(self, libtmux_session):
        """session.new_window creates a window accessible via libtmux API."""
        w = libtmux_session.new_window(window_name="lt_testwin")
        assert w.name == "lt_testwin"
        assert w.id.startswith("@")
        # Verify panes are accessible
        panes = w.panes
        assert len(panes) >= 1
        # Cleanup
        w.kill()

    def test_send_keys(self, libtmux_session):
        """pane.send_keys works through libtmux API."""
        w = libtmux_session.windows[0]
        p = w.panes[0]
        # Should not raise
        p.send_keys("echo libtmux_native_test", enter=True)
        time.sleep(0.5)

    def test_new_window_panes_accessible(self, libtmux_session):
        """Panes of a newly created window are accessible via @N targeting."""
        w = libtmux_session.new_window(window_name="lt_panetest")
        panes = w.panes
        assert len(panes) == 1
        assert panes[0].pane_id.startswith("%")
        w.kill()

    def test_window_kill(self, libtmux_session):
        """window.kill() removes the window."""
        initial_count = len(libtmux_session.windows)
        w = libtmux_session.new_window(window_name="lt_killme")
        assert len(libtmux_session.windows) == initial_count + 1
        w.kill()
        assert len(libtmux_session.windows) == initial_count


if __name__ == "__main__":
    pytest.main([__file__, "-v"])