209 lines
7.0 KiB
Python
209 lines
7.0 KiB
Python
"""
|
|
MicroPython Text Receiver Test
|
|
Tests the smartreceive function with text payloads
|
|
|
|
Note: This test is designed for both MicroPython and desktop Python
|
|
for compatibility testing.
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import json
|
|
|
|
# Add parent directory to path
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from natbridge_mpy import smartreceive, DEFAULT_BROKER_URL, DEFAULT_FILESERVER_URL
|
|
|
|
TEST_BROKER_URL = os.environ.get('NATS_URL', 'nats://localhost:4222')
|
|
TEST_FILESERVER_URL = os.environ.get('FILESERVER_URL', 'http://localhost:8080')
|
|
|
|
|
|
def run_test():
|
|
print('=== MicroPython Text Receiver Test ===\n')
|
|
|
|
from natbridge_mpy import _generate_uuid
|
|
|
|
# Create a mock NATS message with text payload
|
|
test_text = 'Hello, NATSBridge! This is a test message.'
|
|
import base64
|
|
|
|
test_data = {
|
|
'correlation_id': 'mpy-receiver-test-' + _generate_uuid(),
|
|
'msg_id': _generate_uuid(),
|
|
'timestamp': '2024-01-15T10:30:00Z',
|
|
'send_to': '/test/text',
|
|
'msg_purpose': 'test',
|
|
'sender_name': 'mpy-text-test',
|
|
'sender_id': _generate_uuid(),
|
|
'receiver_name': 'mpy-receiver',
|
|
'receiver_id': _generate_uuid(),
|
|
'reply_to': '',
|
|
'reply_to_msg_id': '',
|
|
'broker_url': TEST_BROKER_URL,
|
|
'metadata': {},
|
|
'payloads': [
|
|
{
|
|
'id': _generate_uuid(),
|
|
'dataname': 'message',
|
|
'payload_type': 'text',
|
|
'transport': 'direct',
|
|
'encoding': 'base64',
|
|
'size': len(test_text.encode('utf8')),
|
|
'data': base64.b64encode(test_text.encode('utf8')).decode('ascii'),
|
|
'metadata': {'payload_bytes': len(test_text.encode('utf8'))}
|
|
}
|
|
]
|
|
}
|
|
|
|
mock_msg = {
|
|
'payload': json.dumps(test_data)
|
|
}
|
|
|
|
print('Mock Message Created:')
|
|
print(f' Correlation ID: {test_data["correlation_id"]}')
|
|
print(f' Payloads: {len(test_data["payloads"])}')
|
|
print(f' Payload dataname: {test_data["payloads"][0]["dataname"]}')
|
|
print(f' Payload type: {test_data["payloads"][0]["payload_type"]}')
|
|
print(f' Transport: {test_data["payloads"][0]["transport"]}\n')
|
|
|
|
try:
|
|
# Receive and process the message
|
|
print('Receiving and processing message...')
|
|
env = smartreceive(
|
|
mock_msg,
|
|
max_retries=3,
|
|
base_delay=100,
|
|
max_delay=1000
|
|
)
|
|
|
|
print('\n=== Received Envelope ===')
|
|
print(f'Correlation ID: {env["correlation_id"]}')
|
|
print(f'Message ID: {env["msg_id"]}')
|
|
print(f'Timestamp: {env["timestamp"]}')
|
|
print(f'Subject: {env["send_to"]}')
|
|
print(f'Payloads: {len(env["payloads"])}\n')
|
|
|
|
# Validate received data
|
|
print('=== Validation ===')
|
|
passed = True
|
|
|
|
if not env.get('correlation_id'):
|
|
print('❌ correlation_id is missing')
|
|
passed = False
|
|
else:
|
|
print('✅ correlation_id present')
|
|
|
|
if len(env['payloads']) != 1:
|
|
print(f'❌ Expected 1 payload, got {len(env["payloads"])}')
|
|
passed = False
|
|
else:
|
|
print('✅ Correct number of payloads')
|
|
|
|
payload = env['payloads'][0]
|
|
if payload[0] != 'message':
|
|
print(f"❌ Expected dataname 'message', got '{payload[0]}'")
|
|
passed = False
|
|
else:
|
|
print('✅ Correct dataname')
|
|
|
|
if payload[2] != 'text':
|
|
print(f"❌ Expected type 'text', got '{payload[2]}'")
|
|
passed = False
|
|
else:
|
|
print('✅ Correct type')
|
|
|
|
if payload[1] != test_text:
|
|
print('❌ Data mismatch')
|
|
print(f' Expected: {test_text}')
|
|
print(f' Got: {payload[1]}')
|
|
passed = False
|
|
else:
|
|
print('✅ Data correctly deserialized')
|
|
|
|
# Test with multiple text payloads
|
|
print('\n=== Multiple Text Payloads Test ===')
|
|
multi_test_data = {
|
|
'correlation_id': 'multi-receiver-' + _generate_uuid(),
|
|
'msg_id': _generate_uuid(),
|
|
'timestamp': '2024-01-15T10:30:00Z',
|
|
'send_to': '/test/text',
|
|
'msg_purpose': 'test',
|
|
'sender_name': 'mpy-text-test',
|
|
'sender_id': _generate_uuid(),
|
|
'receiver_name': 'mpy-receiver',
|
|
'receiver_id': _generate_uuid(),
|
|
'reply_to': '',
|
|
'reply_to_msg_id': '',
|
|
'broker_url': TEST_BROKER_URL,
|
|
'metadata': {},
|
|
'payloads': [
|
|
{
|
|
'id': _generate_uuid(),
|
|
'dataname': 'msg1',
|
|
'payload_type': 'text',
|
|
'transport': 'direct',
|
|
'encoding': 'base64',
|
|
'size': 16,
|
|
'data': base64.b64encode(b'First message').decode('ascii'),
|
|
'metadata': {'payload_bytes': 16}
|
|
},
|
|
{
|
|
'id': _generate_uuid(),
|
|
'dataname': 'msg2',
|
|
'payload_type': 'text',
|
|
'transport': 'direct',
|
|
'encoding': 'base64',
|
|
'size': 16,
|
|
'data': base64.b64encode(b'Second message').decode('ascii'),
|
|
'metadata': {'payload_bytes': 16}
|
|
},
|
|
{
|
|
'id': _generate_uuid(),
|
|
'dataname': 'msg3',
|
|
'payload_type': 'text',
|
|
'transport': 'direct',
|
|
'encoding': 'base64',
|
|
'size': 16,
|
|
'data': base64.b64encode(b'Third message').decode('ascii'),
|
|
'metadata': {'payload_bytes': 16}
|
|
}
|
|
]
|
|
}
|
|
|
|
mock_multi_msg = {'payload': json.dumps(multi_test_data)}
|
|
multi_env = smartreceive(mock_multi_msg)
|
|
|
|
if len(multi_env['payloads']) == 3:
|
|
print('✅ Multiple payloads handled correctly')
|
|
|
|
# Verify each payload
|
|
expected_messages = ['First message', 'Second message', 'Third message']
|
|
for i in range(3):
|
|
if multi_env['payloads'][i][1] == expected_messages[i]:
|
|
print(f'✅ Payload {i + 1} correctly deserialized')
|
|
else:
|
|
print(f'❌ Payload {i + 1} mismatch')
|
|
passed = False
|
|
else:
|
|
print(f'❌ Expected 3 payloads, got {len(multi_env["payloads"])}')
|
|
passed = False
|
|
|
|
# Final result
|
|
print('\n=== Test Result ===')
|
|
if passed:
|
|
print('✅ ALL TESTS PASSED')
|
|
sys.exit(0)
|
|
else:
|
|
print('❌ SOME TESTS FAILED')
|
|
sys.exit(1)
|
|
|
|
except Exception as e:
|
|
print(f'❌ Test failed with error: {e}')
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run_test() |