|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import typing as t |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
5 | 9 | from libtmux._internal.engines.base import ( |
6 | 10 | CommandResult, |
7 | 11 | ExitStatus, |
|
11 | 15 | from libtmux._internal.engines.control_protocol import CommandContext, ControlProtocol |
12 | 16 |
|
13 | 17 |
|
| 18 | +class NotificationFixture(t.NamedTuple): |
| 19 | + """Fixture for notification parsing cases.""" |
| 20 | + |
| 21 | + test_id: str |
| 22 | + line: str |
| 23 | + expected_kind: NotificationKind |
| 24 | + expected_subset: dict[str, str] |
| 25 | + |
| 26 | + |
14 | 27 | def test_command_result_wraps_tmux_cmd() -> None: |
15 | 28 | """CommandResult should adapt cleanly into tmux_cmd wrapper.""" |
16 | 29 | result = CommandResult( |
@@ -59,3 +72,91 @@ def test_control_protocol_notifications() -> None: |
59 | 72 | proto.feed_line("%sessions-changed") |
60 | 73 | proto.feed_line("%sessions-changed") |
61 | 74 | assert proto.get_stats(restarts=0).dropped_notifications >= 1 |
| 75 | + |
| 76 | + |
| 77 | +NOTIFICATION_FIXTURES: list[NotificationFixture] = [ |
| 78 | + NotificationFixture( |
| 79 | + test_id="layout_change", |
| 80 | + line="%layout-change @1 abcd efgh 0", |
| 81 | + expected_kind=NotificationKind.WINDOW_LAYOUT_CHANGED, |
| 82 | + expected_subset={ |
| 83 | + "window_id": "@1", |
| 84 | + "window_layout": "abcd", |
| 85 | + "window_visible_layout": "efgh", |
| 86 | + "window_raw_flags": "0", |
| 87 | + }, |
| 88 | + ), |
| 89 | + NotificationFixture( |
| 90 | + test_id="unlinked_window_add", |
| 91 | + line="%unlinked-window-add @2", |
| 92 | + expected_kind=NotificationKind.UNLINKED_WINDOW_ADD, |
| 93 | + expected_subset={"window_id": "@2"}, |
| 94 | + ), |
| 95 | + NotificationFixture( |
| 96 | + test_id="unlinked_window_close", |
| 97 | + line="%unlinked-window-close @3", |
| 98 | + expected_kind=NotificationKind.UNLINKED_WINDOW_CLOSE, |
| 99 | + expected_subset={"window_id": "@3"}, |
| 100 | + ), |
| 101 | + NotificationFixture( |
| 102 | + test_id="unlinked_window_renamed", |
| 103 | + line="%unlinked-window-renamed @4 new-name", |
| 104 | + expected_kind=NotificationKind.UNLINKED_WINDOW_RENAMED, |
| 105 | + expected_subset={"window_id": "@4", "name": "new-name"}, |
| 106 | + ), |
| 107 | + NotificationFixture( |
| 108 | + test_id="client_session_changed", |
| 109 | + line="%client-session-changed c1 $5 sname", |
| 110 | + expected_kind=NotificationKind.CLIENT_SESSION_CHANGED, |
| 111 | + expected_subset={ |
| 112 | + "client_name": "c1", |
| 113 | + "session_id": "$5", |
| 114 | + "session_name": "sname", |
| 115 | + }, |
| 116 | + ), |
| 117 | + NotificationFixture( |
| 118 | + test_id="client_detached", |
| 119 | + line="%client-detached c1", |
| 120 | + expected_kind=NotificationKind.CLIENT_DETACHED, |
| 121 | + expected_subset={"client_name": "c1"}, |
| 122 | + ), |
| 123 | + NotificationFixture( |
| 124 | + test_id="session_renamed", |
| 125 | + line="%session-renamed $5 new-name", |
| 126 | + expected_kind=NotificationKind.SESSION_RENAMED, |
| 127 | + expected_subset={"session_id": "$5", "session_name": "new-name"}, |
| 128 | + ), |
| 129 | + NotificationFixture( |
| 130 | + test_id="paste_buffer_changed", |
| 131 | + line="%paste-buffer-changed buf1", |
| 132 | + expected_kind=NotificationKind.PASTE_BUFFER_CHANGED, |
| 133 | + expected_subset={"name": "buf1"}, |
| 134 | + ), |
| 135 | + NotificationFixture( |
| 136 | + test_id="paste_buffer_deleted", |
| 137 | + line="%paste-buffer-deleted buf1", |
| 138 | + expected_kind=NotificationKind.PASTE_BUFFER_DELETED, |
| 139 | + expected_subset={"name": "buf1"}, |
| 140 | + ), |
| 141 | +] |
| 142 | + |
| 143 | + |
| 144 | +@pytest.mark.parametrize( |
| 145 | + list(NotificationFixture._fields), |
| 146 | + NOTIFICATION_FIXTURES, |
| 147 | + ids=[fixture.test_id for fixture in NOTIFICATION_FIXTURES], |
| 148 | +) |
| 149 | +def test_control_protocol_notification_parsing( |
| 150 | + test_id: str, |
| 151 | + line: str, |
| 152 | + expected_kind: NotificationKind, |
| 153 | + expected_subset: dict[str, str], |
| 154 | +) -> None: |
| 155 | + """Ensure the parser recognizes mapped control-mode notifications.""" |
| 156 | + proto = ControlProtocol() |
| 157 | + proto.feed_line(line) |
| 158 | + notif = proto.get_notification(timeout=0.05) |
| 159 | + assert notif is not None |
| 160 | + assert notif.kind is expected_kind |
| 161 | + for key, value in expected_subset.items(): |
| 162 | + assert notif.data.get(key) == value |
0 commit comments