|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:iris_chat/core/utils/reaction_updates.dart'; |
| 3 | +import 'package:iris_chat/features/chat/domain/models/message.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + ChatMessage message({ |
| 7 | + required String id, |
| 8 | + String? eventId, |
| 9 | + String? rumorId, |
| 10 | + Map<String, List<String>> reactions = const {}, |
| 11 | + }) { |
| 12 | + return ChatMessage( |
| 13 | + id: id, |
| 14 | + sessionId: 'session-1', |
| 15 | + text: 'hello', |
| 16 | + timestamp: DateTime.fromMillisecondsSinceEpoch(0), |
| 17 | + direction: MessageDirection.incoming, |
| 18 | + status: MessageStatus.delivered, |
| 19 | + eventId: eventId, |
| 20 | + rumorId: rumorId, |
| 21 | + reactions: reactions, |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + test('applies reaction by internal id and moves actor between emojis', () { |
| 26 | + final current = [ |
| 27 | + message( |
| 28 | + id: 'm1', |
| 29 | + reactions: { |
| 30 | + '❤️': ['alice', 'bob'], |
| 31 | + '👍': ['carol'], |
| 32 | + }, |
| 33 | + ), |
| 34 | + ]; |
| 35 | + |
| 36 | + final applied = applyReactionToMessages( |
| 37 | + current, |
| 38 | + messageId: 'm1', |
| 39 | + emoji: '👍', |
| 40 | + actorPubkeyHex: 'alice', |
| 41 | + ); |
| 42 | + |
| 43 | + expect(applied, isNotNull); |
| 44 | + expect(applied!.updatedMessage.reactions['❤️'], ['bob']); |
| 45 | + expect(applied.updatedMessage.reactions['👍'], ['carol', 'alice']); |
| 46 | + }); |
| 47 | + |
| 48 | + test('matches event id only when enabled', () { |
| 49 | + final current = [message(id: 'm1', eventId: 'evt-1', rumorId: 'rumor-1')]; |
| 50 | + |
| 51 | + final noEventMatch = applyReactionToMessages( |
| 52 | + current, |
| 53 | + messageId: 'evt-1', |
| 54 | + emoji: '🔥', |
| 55 | + actorPubkeyHex: 'alice', |
| 56 | + matchEventId: false, |
| 57 | + ); |
| 58 | + expect(noEventMatch, isNull); |
| 59 | + |
| 60 | + final withEventMatch = applyReactionToMessages( |
| 61 | + current, |
| 62 | + messageId: 'evt-1', |
| 63 | + emoji: '🔥', |
| 64 | + actorPubkeyHex: 'alice', |
| 65 | + matchEventId: true, |
| 66 | + ); |
| 67 | + expect(withEventMatch, isNotNull); |
| 68 | + expect(withEventMatch!.updatedMessage.reactions['🔥'], ['alice']); |
| 69 | + }); |
| 70 | + |
| 71 | + test('falls back to rumor id match', () { |
| 72 | + final current = [message(id: 'm1', rumorId: 'rumor-42')]; |
| 73 | + |
| 74 | + final applied = applyReactionToMessages( |
| 75 | + current, |
| 76 | + messageId: 'rumor-42', |
| 77 | + emoji: '✅', |
| 78 | + actorPubkeyHex: 'alice', |
| 79 | + ); |
| 80 | + |
| 81 | + expect(applied, isNotNull); |
| 82 | + expect(applied!.updatedMessage.reactions['✅'], ['alice']); |
| 83 | + }); |
| 84 | +} |
0 commit comments