data_classes_test.dart
1 import 'package:dead_drop/providers/notification_settings_provider.dart'; 2 import 'package:dead_drop/providers/user_profile_provider.dart'; 3 import 'package:dead_drop/services/contact_metadata_service.dart'; 4 import 'package:dead_drop/services/dead_drop_service.dart'; 5 import 'package:flutter_test/flutter_test.dart'; 6 7 import '../fixtures/test_fixtures.dart'; 8 9 void main() { 10 group('ContactMetadata', () { 11 test('toJson() serializes correctly', () { 12 const meta = ContactMetadata( 13 picturePath: 'contact_pictures/abc.jpg', 14 description: 'A friend', 15 ); 16 final json = meta.toJson(); 17 expect(json['picturePath'], equals('contact_pictures/abc.jpg')); 18 expect(json['description'], equals('A friend')); 19 }); 20 21 test('fromJson() deserializes correctly', () { 22 final json = { 23 'picturePath': 'contact_pictures/abc.jpg', 24 'description': 'A friend', 25 }; 26 final meta = ContactMetadata.fromJson(json); 27 expect(meta.picturePath, equals('contact_pictures/abc.jpg')); 28 expect(meta.description, equals('A friend')); 29 }); 30 31 test('toJson/fromJson roundtrip', () { 32 const original = ContactMetadata( 33 picturePath: 'pics/test.png', 34 description: 'Test desc', 35 ); 36 final restored = ContactMetadata.fromJson(original.toJson()); 37 expect(restored.picturePath, equals(original.picturePath)); 38 expect(restored.description, equals(original.description)); 39 }); 40 41 test('toJson/fromJson roundtrip with null values', () { 42 const original = ContactMetadata(); 43 final restored = ContactMetadata.fromJson(original.toJson()); 44 expect(restored.picturePath, isNull); 45 expect(restored.description, isNull); 46 }); 47 48 test('copyWith() updates fields', () { 49 const meta = ContactMetadata( 50 picturePath: 'old.jpg', 51 description: 'old desc', 52 ); 53 final updated = meta.copyWith( 54 picturePath: 'new.jpg', 55 description: 'new desc', 56 ); 57 expect(updated.picturePath, equals('new.jpg')); 58 expect(updated.description, equals('new desc')); 59 }); 60 61 test('copyWith(clearPicture: true) clears picture', () { 62 const meta = ContactMetadata(picturePath: 'pic.jpg', description: 'hi'); 63 final cleared = meta.copyWith(clearPicture: true); 64 expect(cleared.picturePath, isNull); 65 expect(cleared.description, equals('hi')); 66 }); 67 68 test('copyWith(clearDescription: true) clears description', () { 69 const meta = ContactMetadata(picturePath: 'pic.jpg', description: 'hi'); 70 final cleared = meta.copyWith(clearDescription: true); 71 expect(cleared.picturePath, equals('pic.jpg')); 72 expect(cleared.description, isNull); 73 }); 74 }); 75 76 group('NotificationSettings', () { 77 test('default values are true', () { 78 const settings = NotificationSettings(); 79 expect(settings.enabled, isTrue); 80 expect(settings.soundEnabled, isTrue); 81 }); 82 83 test('copyWith() updates fields', () { 84 const settings = NotificationSettings(); 85 final updated = settings.copyWith(enabled: false, soundEnabled: false); 86 expect(updated.enabled, isFalse); 87 expect(updated.soundEnabled, isFalse); 88 }); 89 90 test('copyWith() preserves unspecified fields', () { 91 const settings = NotificationSettings(enabled: false); 92 final updated = settings.copyWith(soundEnabled: false); 93 expect(updated.enabled, isFalse); 94 expect(updated.soundEnabled, isFalse); 95 }); 96 }); 97 98 group('UserProfileState', () { 99 test('default values are null/empty/false', () { 100 const state = UserProfileState(); 101 expect(state.picturePath, isNull); 102 expect(state.displayName, isNull); 103 expect(state.shareProfileContactIds, isEmpty); 104 expect(state.shareWithAll, isFalse); 105 }); 106 107 test('copyWith() updates fields', () { 108 const state = UserProfileState(); 109 final updated = state.copyWith( 110 picturePath: '/path/to/pic.jpg', 111 displayName: 'Chris', 112 shareWithAll: true, 113 shareProfileContactIds: {'abc', 'def'}, 114 ); 115 expect(updated.picturePath, equals('/path/to/pic.jpg')); 116 expect(updated.displayName, equals('Chris')); 117 expect(updated.shareWithAll, isTrue); 118 expect(updated.shareProfileContactIds, equals({'abc', 'def'})); 119 }); 120 121 test('copyWith(clearPicture: true) clears picture', () { 122 final state = const UserProfileState().copyWith( 123 picturePath: '/pic.jpg', 124 displayName: 'Chris', 125 ); 126 final cleared = state.copyWith(clearPicture: true); 127 expect(cleared.picturePath, isNull); 128 expect(cleared.displayName, equals('Chris')); 129 }); 130 131 test('copyWith(clearDisplayName: true) clears name', () { 132 final state = const UserProfileState().copyWith( 133 picturePath: '/pic.jpg', 134 displayName: 'Chris', 135 ); 136 final cleared = state.copyWith(clearDisplayName: true); 137 expect(cleared.picturePath, equals('/pic.jpg')); 138 expect(cleared.displayName, isNull); 139 }); 140 }); 141 142 group('ContactInfoDisplay extension', () { 143 test('displayName returns nickname when present', () { 144 final contact = TestFixtures.contactInfo(nickname: 'Alice'); 145 expect(contact.displayName, equals('Alice')); 146 }); 147 148 test('displayName returns fingerprint when no nickname', () { 149 final contact = TestFixtures.contactInfo( 150 nickname: null, 151 fingerprint: 'ABCD-EFGH-1234', 152 ); 153 expect(contact.displayName, equals('ABCD-EFGH-1234')); 154 }); 155 }); 156 }