"""Tests for building and reading the TCP handshake.""" from neotalk.core.entities.constants import FrameType from neotalk.core.entities.responses import ResponseTypes from neotalk.core.use_cases.conversation.handshake import ( build_handshake_use_case, read_handshake_use_case, ) def test_handshake_round_trips(identity): # Given a handshake built from our identity built = build_handshake_use_case(identity) frame = built["data"]["frame"] # When the other side reads it read = read_handshake_use_case(frame) # Then the identity fields survive the trip assert read["type"] == ResponseTypes.SUCCESS assert read["data"]["peer_id"] == identity.peer_id assert read["data"]["alias"] == identity.alias assert read["data"]["public_key"] == identity.public_key def test_read_handshake_rejects_wrong_frame_type(): # Given a non-handshake frame frame = {"type": FrameType.SECURE, "body": "..."} # When reading it as a handshake response = read_handshake_use_case(frame) # Then it is rejected assert response["type"] == ResponseTypes.PARAMETERS_ERROR def test_read_handshake_rejects_missing_alias(): # Given a handshake missing its alias frame = {"type": FrameType.HANDSHAKE, "peer_id": "id", "pubkey": "AA=="} # When reading it response = read_handshake_use_case(frame) # Then it is a parameters error assert response["type"] == ResponseTypes.PARAMETERS_ERROR assert response["errors"][0]["field"] == "alias"