|
1 | | -from fishjam import receive_binary |
| 1 | +import pytest |
| 2 | + |
| 3 | +from fishjam import decode_server_notifications, receive_binary |
2 | 4 | from fishjam.events import ( |
3 | 5 | ServerMessagePeerConnected, |
4 | 6 | ServerMessageRoomCreated, |
@@ -87,3 +89,85 @@ def test_empty_batch_returns_empty_list(): |
87 | 89 | result = receive_binary(binary) |
88 | 90 |
|
89 | 91 | assert result == [] |
| 92 | + |
| 93 | + |
| 94 | +def test_receive_binary_is_deprecated(): |
| 95 | + binary = bytes(ServerMessage(room_created=ServerMessageRoomCreated(room_id="r1"))) |
| 96 | + |
| 97 | + with pytest.warns(DeprecationWarning): |
| 98 | + receive_binary(binary) |
| 99 | + |
| 100 | + |
| 101 | +def test_decode_single_notification_returns_one_element_list(): |
| 102 | + binary = bytes(ServerMessage(room_created=ServerMessageRoomCreated(room_id="r1"))) |
| 103 | + |
| 104 | + result = decode_server_notifications(binary) |
| 105 | + |
| 106 | + assert isinstance(result, list) |
| 107 | + assert [type(n) for n in result] == [ServerMessageRoomCreated] |
| 108 | + assert result[0].room_id == "r1" |
| 109 | + |
| 110 | + |
| 111 | +def test_decode_unsupported_single_message_returns_empty_list(): |
| 112 | + binary = bytes(ServerMessage(auth_request=ServerMessageAuthRequest(token="t"))) |
| 113 | + |
| 114 | + assert decode_server_notifications(binary) == [] |
| 115 | + |
| 116 | + |
| 117 | +def test_decode_batch_is_unpacked_into_ordered_list(): |
| 118 | + binary = bytes( |
| 119 | + ServerMessage( |
| 120 | + notification_batch=ServerMessageNotificationBatch( |
| 121 | + notifications=[ |
| 122 | + ServerMessage(room_created=ServerMessageRoomCreated(room_id="r1")), |
| 123 | + ServerMessage( |
| 124 | + peer_connected=ServerMessagePeerConnected( |
| 125 | + room_id="r1", peer_id="p1" |
| 126 | + ) |
| 127 | + ), |
| 128 | + ServerMessage(room_deleted=ServerMessageRoomDeleted(room_id="r1")), |
| 129 | + ] |
| 130 | + ) |
| 131 | + ) |
| 132 | + ) |
| 133 | + |
| 134 | + result = decode_server_notifications(binary) |
| 135 | + |
| 136 | + assert [type(n) for n in result] == [ |
| 137 | + ServerMessageRoomCreated, |
| 138 | + ServerMessagePeerConnected, |
| 139 | + ServerMessageRoomDeleted, |
| 140 | + ] |
| 141 | + assert result[0].room_id == "r1" |
| 142 | + assert result[1].peer_id == "p1" |
| 143 | + |
| 144 | + |
| 145 | +def test_decode_batch_filters_out_unsupported_members(): |
| 146 | + binary = bytes( |
| 147 | + ServerMessage( |
| 148 | + notification_batch=ServerMessageNotificationBatch( |
| 149 | + notifications=[ |
| 150 | + ServerMessage(room_created=ServerMessageRoomCreated(room_id="r1")), |
| 151 | + ServerMessage(auth_request=ServerMessageAuthRequest(token="t")), |
| 152 | + ServerMessage(room_deleted=ServerMessageRoomDeleted(room_id="r1")), |
| 153 | + ] |
| 154 | + ) |
| 155 | + ) |
| 156 | + ) |
| 157 | + |
| 158 | + result = decode_server_notifications(binary) |
| 159 | + |
| 160 | + assert [type(n) for n in result] == [ |
| 161 | + ServerMessageRoomCreated, |
| 162 | + ServerMessageRoomDeleted, |
| 163 | + ] |
| 164 | + |
| 165 | + |
| 166 | +def test_decode_empty_batch_returns_empty_list(): |
| 167 | + binary = bytes( |
| 168 | + ServerMessage( |
| 169 | + notification_batch=ServerMessageNotificationBatch(notifications=[]) |
| 170 | + ) |
| 171 | + ) |
| 172 | + |
| 173 | + assert decode_server_notifications(binary) == [] |
0 commit comments