|
| 1 | +"""Tests for control mode protocol parser.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import io |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from libtmux._internal.engines.control_mode.parser import ProtocolParser |
| 10 | + |
| 11 | + |
| 12 | +def test_parser_success_response() -> None: |
| 13 | + """Parse successful %begin/%end block.""" |
| 14 | + stdout = io.StringIO( |
| 15 | + "%begin 1234 1 0\noutput line 1\noutput line 2\n%end 1234 1 0\n" |
| 16 | + ) |
| 17 | + |
| 18 | + parser = ProtocolParser(stdout) |
| 19 | + result = parser.parse_response(["list-sessions"]) |
| 20 | + |
| 21 | + assert result.stdout == ["output line 1", "output line 2"] |
| 22 | + assert result.stderr == [] |
| 23 | + assert result.returncode == 0 |
| 24 | + |
| 25 | + |
| 26 | +def test_parser_error_response() -> None: |
| 27 | + """Parse error %begin/%error block.""" |
| 28 | + stdout = io.StringIO( |
| 29 | + "%begin 1234 2 0\nparse error: unknown command\n%error 1234 2 0\n" |
| 30 | + ) |
| 31 | + |
| 32 | + parser = ProtocolParser(stdout) |
| 33 | + result = parser.parse_response(["bad-command"]) |
| 34 | + |
| 35 | + assert result.stdout == ["parse error: unknown command"] |
| 36 | + assert result.returncode == 1 |
| 37 | + |
| 38 | + |
| 39 | +def test_parser_empty_output() -> None: |
| 40 | + """Handle response with no output lines.""" |
| 41 | + stdout = io.StringIO("%begin 1234 3 0\n%end 1234 3 0\n") |
| 42 | + |
| 43 | + parser = ProtocolParser(stdout) |
| 44 | + result = parser.parse_response(["some-command"]) |
| 45 | + |
| 46 | + assert result.stdout == [] |
| 47 | + assert result.returncode == 0 |
| 48 | + |
| 49 | + |
| 50 | +def test_parser_with_notifications() -> None: |
| 51 | + """Queue notifications between responses.""" |
| 52 | + stdout = io.StringIO( |
| 53 | + "%session-changed $0 mysession\n" |
| 54 | + "%window-add @1\n" |
| 55 | + "%begin 1234 4 0\n" |
| 56 | + "output\n" |
| 57 | + "%end 1234 4 0\n" |
| 58 | + ) |
| 59 | + |
| 60 | + parser = ProtocolParser(stdout) |
| 61 | + result = parser.parse_response(["list-windows"]) |
| 62 | + |
| 63 | + assert result.stdout == ["output"] |
| 64 | + assert result.returncode == 0 |
| 65 | + # Notifications should be queued |
| 66 | + assert len(parser.notifications) == 2 |
| 67 | + assert parser.notifications[0] == "%session-changed $0 mysession" |
| 68 | + assert parser.notifications[1] == "%window-add @1" |
| 69 | + |
| 70 | + |
| 71 | +def test_parser_notification_during_response() -> None: |
| 72 | + """Handle notification that arrives during response.""" |
| 73 | + stdout = io.StringIO( |
| 74 | + "%begin 1234 5 0\n" |
| 75 | + "line 1\n" |
| 76 | + "%sessions-changed\n" # Notification mid-response |
| 77 | + "line 2\n" |
| 78 | + "%end 1234 5 0\n" |
| 79 | + ) |
| 80 | + |
| 81 | + parser = ProtocolParser(stdout) |
| 82 | + result = parser.parse_response(["test"]) |
| 83 | + |
| 84 | + # Output lines should not include notification |
| 85 | + assert result.stdout == ["line 1", "line 2"] |
| 86 | + # Notification should be queued |
| 87 | + assert "%sessions-changed" in parser.notifications |
| 88 | + |
| 89 | + |
| 90 | +def test_parser_connection_closed() -> None: |
| 91 | + """Raise ConnectionError on EOF.""" |
| 92 | + stdout = io.StringIO("") # Empty stream = EOF |
| 93 | + |
| 94 | + parser = ProtocolParser(stdout) |
| 95 | + |
| 96 | + with pytest.raises(ConnectionError, match="connection closed"): |
| 97 | + parser.parse_response(["test"]) |
| 98 | + |
| 99 | + |
| 100 | +def test_parser_connection_closed_mid_response() -> None: |
| 101 | + """Raise ConnectionError if EOF during response.""" |
| 102 | + stdout = io.StringIO( |
| 103 | + "%begin 1234 6 0\npartial output\n" |
| 104 | + # No %end - connection closed |
| 105 | + ) |
| 106 | + |
| 107 | + parser = ProtocolParser(stdout) |
| 108 | + |
| 109 | + with pytest.raises(ConnectionError): |
| 110 | + parser.parse_response(["test"]) |
| 111 | + |
| 112 | + |
| 113 | +def test_parser_multiline_output() -> None: |
| 114 | + """Handle response with many output lines.""" |
| 115 | + lines = [f"line {i}" for i in range(50)] |
| 116 | + output = "\n".join(["%begin 1234 7 0", *lines, "%end 1234 7 0"]) + "\n" |
| 117 | + |
| 118 | + stdout = io.StringIO(output) |
| 119 | + parser = ProtocolParser(stdout) |
| 120 | + result = parser.parse_response(["test"]) |
| 121 | + |
| 122 | + assert len(result.stdout) == 50 |
| 123 | + assert result.stdout[0] == "line 0" |
| 124 | + assert result.stdout[49] == "line 49" |
| 125 | + |
| 126 | + |
| 127 | +def test_parser_multiple_responses_sequential() -> None: |
| 128 | + """Parse multiple responses sequentially.""" |
| 129 | + stdout = io.StringIO( |
| 130 | + "%begin 1234 1 0\n" |
| 131 | + "response 1\n" |
| 132 | + "%end 1234 1 0\n" |
| 133 | + "%begin 1234 2 0\n" |
| 134 | + "response 2\n" |
| 135 | + "%end 1234 2 0\n" |
| 136 | + ) |
| 137 | + |
| 138 | + parser = ProtocolParser(stdout) |
| 139 | + |
| 140 | + result1 = parser.parse_response(["cmd1"]) |
| 141 | + assert result1.stdout == ["response 1"] |
| 142 | + |
| 143 | + result2 = parser.parse_response(["cmd2"]) |
| 144 | + assert result2.stdout == ["response 2"] |
| 145 | + |
| 146 | + |
| 147 | +def test_parser_preserves_empty_lines() -> None: |
| 148 | + """Empty lines in output are preserved.""" |
| 149 | + stdout = io.StringIO( |
| 150 | + "%begin 1234 8 0\n" |
| 151 | + "line 1\n" |
| 152 | + "\n" # Empty line |
| 153 | + "line 3\n" |
| 154 | + "%end 1234 8 0\n" |
| 155 | + ) |
| 156 | + |
| 157 | + parser = ProtocolParser(stdout) |
| 158 | + result = parser.parse_response(["test"]) |
| 159 | + |
| 160 | + assert len(result.stdout) == 3 |
| 161 | + assert result.stdout[0] == "line 1" |
| 162 | + assert result.stdout[1] == "" |
| 163 | + assert result.stdout[2] == "line 3" |
| 164 | + |
| 165 | + |
| 166 | +def test_parser_complex_output() -> None: |
| 167 | + """Handle complex real-world output.""" |
| 168 | + # Simulates actual tmux list-sessions output |
| 169 | + stdout = io.StringIO( |
| 170 | + "%begin 1363006971 2 1\n" |
| 171 | + "0: ksh* (1 panes) [80x24] [layout b25f,80x24,0,0,2] @2 (active)\n" |
| 172 | + "1: bash (2 panes) [80x24] [layout b25f,80x24,0,0,3] @3\n" |
| 173 | + "%end 1363006971 2 1\n" |
| 174 | + ) |
| 175 | + |
| 176 | + parser = ProtocolParser(stdout) |
| 177 | + result = parser.parse_response(["list-sessions"]) |
| 178 | + |
| 179 | + assert len(result.stdout) == 2 |
| 180 | + assert "ksh*" in result.stdout[0] |
| 181 | + assert "bash" in result.stdout[1] |
| 182 | + assert result.returncode == 0 |
| 183 | + |
| 184 | + |
| 185 | +def test_parser_error_with_multiline_message() -> None: |
| 186 | + """Handle error with multi-line error message.""" |
| 187 | + stdout = io.StringIO( |
| 188 | + "%begin 1234 9 0\n" |
| 189 | + "error: command failed\n" |
| 190 | + "reason: invalid argument\n" |
| 191 | + "suggestion: try --help\n" |
| 192 | + "%error 1234 9 0\n" |
| 193 | + ) |
| 194 | + |
| 195 | + parser = ProtocolParser(stdout) |
| 196 | + result = parser.parse_response(["bad-cmd"]) |
| 197 | + |
| 198 | + assert result.returncode == 1 |
| 199 | + assert len(result.stdout) == 3 |
| 200 | + assert "error: command failed" in result.stdout[0] |
0 commit comments