11from __future__ import annotations
22
3+ import time
34from datetime import datetime , timezone
45from unittest .mock import MagicMock , patch
56
67import pytest
78
9+ from hiero_sdk_python .account .account_id import AccountId
810from hiero_sdk_python .client .client import Client
911from hiero_sdk_python .consensus .topic_id import TopicId
1012from hiero_sdk_python .hapi .mirror import consensus_service_pb2 as mirror_proto
1113from hiero_sdk_python .hapi .services import timestamp_pb2 as hapi_timestamp_pb2
14+ from hiero_sdk_python .hapi .services .consensus_submit_message_pb2 import ConsensusMessageChunkInfo
1215from hiero_sdk_python .query .topic_message_query import TopicMessageQuery
16+ from hiero_sdk_python .transaction .transaction_id import TransactionId
1317
1418
1519pytestmark = pytest .mark .unit
1923def mock_client ():
2024 """Fixture to provide a mock Client instance."""
2125 client = MagicMock (spec = Client )
22- client .operator_account_id = "0.0.12345"
26+ client .operator_account_id = AccountId (0 , 0 , 12345 )
27+
2328 return client
2429
2530
@@ -40,11 +45,20 @@ def mock_subscription_response():
4045 )
4146
4247
48+ def test_topic_message_query_initialization ():
49+ """Test initializing the query with various parameter types and setters."""
50+ start = datetime (2023 , 1 , 1 , tzinfo = timezone .utc )
51+ query = TopicMessageQuery ().set_topic_id ("0.0.123" ).set_start_time (start ).set_limit (5 ).set_chunking_enabled (True )
52+
53+ assert query ._topic_id .topicNum == 123
54+ assert query ._start_time .seconds == int (start .timestamp ())
55+ assert query ._limit == 5
56+ assert query ._chunking_enabled is True
57+
58+
4359# This test uses fixtures (mock_client, mock_topic_id, mock_subscription_response) as parameters
4460def test_topic_message_query_subscription (mock_client , mock_topic_id , mock_subscription_response ):
45- """
46- Test subscribing to topic messages using TopicMessageQuery.
47- """
61+ """Test subscribing to topic messages using TopicMessageQuery."""
4862 query = TopicMessageQuery ().set_topic_id (mock_topic_id ).set_start_time (datetime .now (tz = timezone .utc ))
4963
5064 with patch ("hiero_sdk_python.query.topic_message_query.TopicMessageQuery.subscribe" ) as mock_subscribe :
@@ -69,3 +83,29 @@ def side_effect(client, on_message, on_error): # noqa: ARG001
6983 on_error .assert_not_called ()
7084
7185 print ("Test passed: Subscription handled messages correctly." )
86+
87+
88+ def test_chunk_message_handling (mock_client ):
89+ """Test that multiple chunks are correctly buffered and released as a single message."""
90+ query = TopicMessageQuery (topic_id = "0.0.123" , chunking_enabled = True )
91+
92+ # Mocking two chunks for the same transaction
93+ tx_id = TransactionId .generate (mock_client .operator_account_id )._to_proto ()
94+ chunk1 = mirror_proto .ConsensusTopicResponse (
95+ message = b"part1" , chunkInfo = ConsensusMessageChunkInfo (initialTransactionID = tx_id , total = 2 , number = 1 )
96+ )
97+ chunk2 = mirror_proto .ConsensusTopicResponse (
98+ message = b"part2" , chunkInfo = ConsensusMessageChunkInfo (initialTransactionID = tx_id , total = 2 , number = 2 )
99+ )
100+
101+ mock_client .mirror_stub .subscribeTopic .return_value = [chunk1 , chunk2 ]
102+
103+ received_messages = []
104+ query .subscribe (mock_client , on_message = lambda m : received_messages .append (m ))
105+
106+ # Wait for thread execution
107+ time .sleep (0.1 )
108+
109+ assert len (received_messages ) == 1
110+ # Assuming TopicMessage.of_many joins messages
111+ assert b"part1" in received_messages [0 ].message
0 commit comments