import 'package:flutter_test/flutter_test.dart'; import 'package:votianlt_app/models/message_envelope.dart'; void main() { group('MessageEnvelope', () { group('fromJson', () { test('parses all required fields correctly', () { final json = { 'messageId': 'test-uuid-123', 'timestamp': '2024-01-15T10:30:00.000Z', 'topic': '/server/user123/message', 'payload': {'key': 'value'}, }; final envelope = MessageEnvelope.fromJson(json); expect(envelope.messageId, 'test-uuid-123'); expect(envelope.timestamp, DateTime.utc(2024, 1, 15, 10, 30, 0)); expect(envelope.topic, '/server/user123/message'); expect(envelope.payload, {'key': 'value'}); expect(envelope.requiresAck, true); // default expect(envelope.retryCount, 0); // default expect(envelope.expiresAt, isNull); }); test('parses optional fields when present', () { final json = { 'messageId': 'test-uuid-456', 'timestamp': '2024-01-15T10:30:00.000Z', 'topic': '/server/user123/message', 'payload': {'data': 123}, 'requiresAck': false, 'retryCount': 3, 'expiresAt': '2024-01-15T11:30:00.000Z', }; final envelope = MessageEnvelope.fromJson(json); expect(envelope.requiresAck, false); expect(envelope.retryCount, 3); expect(envelope.expiresAt, DateTime.utc(2024, 1, 15, 11, 30, 0)); }); test('handles list payload', () { final json = { 'messageId': 'test-uuid-789', 'timestamp': '2024-01-15T10:30:00.000Z', 'topic': '/server/user123/jobs', 'payload': [ {'id': '1'}, {'id': '2'} ], }; final envelope = MessageEnvelope.fromJson(json); expect(envelope.payload, isList); expect(envelope.payload.length, 2); }); test('handles null payload', () { final json = { 'messageId': 'test-uuid-null', 'timestamp': '2024-01-15T10:30:00.000Z', 'topic': '/server/user123/ping', 'payload': null, }; final envelope = MessageEnvelope.fromJson(json); expect(envelope.payload, isNull); }); }); group('toJson', () { test('serializes all fields correctly', () { final envelope = MessageEnvelope( messageId: 'test-uuid-123', timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0), topic: '/server/user123/message', payload: {'key': 'value'}, requiresAck: true, retryCount: 2, expiresAt: DateTime.utc(2024, 1, 15, 11, 30, 0), ); final json = envelope.toJson(); expect(json['messageId'], 'test-uuid-123'); expect(json['timestamp'], '2024-01-15T10:30:00.000Z'); expect(json['topic'], '/server/user123/message'); expect(json['payload'], {'key': 'value'}); expect(json['requiresAck'], true); expect(json['retryCount'], 2); expect(json['expiresAt'], '2024-01-15T11:30:00.000Z'); }); test('omits expiresAt when null', () { final envelope = MessageEnvelope( messageId: 'test-uuid-123', timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0), topic: '/server/user123/message', payload: {'key': 'value'}, ); final json = envelope.toJson(); expect(json.containsKey('expiresAt'), false); }); }); group('fromJson/toJson roundtrip', () { test('preserves all data through serialization', () { final original = MessageEnvelope( messageId: 'roundtrip-uuid', timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0), topic: '/server/user123/message', payload: {'nested': {'key': 'value'}, 'list': [1, 2, 3]}, requiresAck: false, retryCount: 5, expiresAt: DateTime.utc(2024, 1, 16, 10, 30, 0), ); final json = original.toJson(); final restored = MessageEnvelope.fromJson(json); expect(restored.messageId, original.messageId); expect(restored.timestamp, original.timestamp); expect(restored.topic, original.topic); expect(restored.payload, original.payload); expect(restored.requiresAck, original.requiresAck); expect(restored.retryCount, original.retryCount); expect(restored.expiresAt, original.expiresAt); }); }); group('copyWith', () { test('creates copy with updated messageId', () { final original = MessageEnvelope( messageId: 'original-id', timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0), topic: '/server/user123/message', payload: {'key': 'value'}, ); final copy = original.copyWith(messageId: 'new-id'); expect(copy.messageId, 'new-id'); expect(copy.timestamp, original.timestamp); expect(copy.topic, original.topic); expect(copy.payload, original.payload); }); test('creates copy with updated retryCount', () { final original = MessageEnvelope( messageId: 'test-id', timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0), topic: '/server/user123/message', payload: {'key': 'value'}, retryCount: 0, ); final copy = original.copyWith(retryCount: 3); expect(copy.retryCount, 3); expect(copy.messageId, original.messageId); }); }); group('toString', () { test('returns readable representation', () { final envelope = MessageEnvelope( messageId: 'test-id', timestamp: DateTime.utc(2024, 1, 15, 10, 30, 0), topic: '/server/user123/message', payload: {'key': 'value'}, requiresAck: true, retryCount: 2, ); final str = envelope.toString(); expect(str, contains('test-id')); expect(str, contains('/server/user123/message')); expect(str, contains('requiresAck: true')); expect(str, contains('retryCount: 2')); }); }); }); }