|
| 1 | +import 'package:test/test.dart'; |
| 2 | +import 'package:flutter_deriv_api/services/connection/call_manager/call_history.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('CallHistory tests =>', () { |
| 6 | + late CallHistory callHistory; |
| 7 | + |
| 8 | + setUp(() { |
| 9 | + callHistory = CallHistory(); |
| 10 | + }); |
| 11 | + |
| 12 | + test('test pushIncoming method.', () { |
| 13 | + const int timestamp = 1621188000; |
| 14 | + const String method = 'test'; |
| 15 | + |
| 16 | + final Map<String, dynamic> message = <String, dynamic>{ |
| 17 | + 'content': 'Hello' |
| 18 | + }; |
| 19 | + |
| 20 | + callHistory.pushIncoming( |
| 21 | + timestamp: timestamp, |
| 22 | + method: method, |
| 23 | + message: message, |
| 24 | + ); |
| 25 | + |
| 26 | + expect(callHistory.incoming.length, equals(1)); |
| 27 | + expect(callHistory.incoming.first.timeStamp, equals(timestamp)); |
| 28 | + expect(callHistory.incoming.first.method, equals(method)); |
| 29 | + expect(callHistory.incoming.first.message, equals(message)); |
| 30 | + }); |
| 31 | + |
| 32 | + test('test pushOutgoing method.', () { |
| 33 | + const int timestamp = 1621188000; |
| 34 | + const String method = 'test'; |
| 35 | + |
| 36 | + final Map<String, dynamic> message = <String, dynamic>{ |
| 37 | + 'content': 'World' |
| 38 | + }; |
| 39 | + |
| 40 | + callHistory.pushOutgoing( |
| 41 | + timestamp: timestamp, |
| 42 | + method: method, |
| 43 | + message: message, |
| 44 | + ); |
| 45 | + |
| 46 | + expect(callHistory.outgoing.length, equals(1)); |
| 47 | + expect(callHistory.outgoing.first.timeStamp, equals(timestamp)); |
| 48 | + expect(callHistory.outgoing.first.method, equals(method)); |
| 49 | + expect(callHistory.outgoing.first.message, equals(message)); |
| 50 | + }); |
| 51 | + |
| 52 | + test('should trim history based on limit for incomming responses.', () { |
| 53 | + const int limit = 2; |
| 54 | + |
| 55 | + callHistory.limit = limit; |
| 56 | + |
| 57 | + const int timestamp1 = 1621188000; |
| 58 | + const String method1 = 'test 1'; |
| 59 | + |
| 60 | + final Map<String, dynamic> message1 = <String, dynamic>{ |
| 61 | + 'content': 'Hello' |
| 62 | + }; |
| 63 | + |
| 64 | + callHistory.pushIncoming( |
| 65 | + timestamp: timestamp1, |
| 66 | + method: method1, |
| 67 | + message: message1, |
| 68 | + ); |
| 69 | + |
| 70 | + const int timestamp2 = 1621189000; |
| 71 | + const String method2 = 'test 2'; |
| 72 | + |
| 73 | + final Map<String, dynamic> message2 = <String, dynamic>{ |
| 74 | + 'content': 'World' |
| 75 | + }; |
| 76 | + |
| 77 | + callHistory.pushIncoming( |
| 78 | + timestamp: timestamp2, |
| 79 | + method: method2, |
| 80 | + message: message2, |
| 81 | + ); |
| 82 | + |
| 83 | + const int timestamp3 = 1621190000; |
| 84 | + const String method3 = 'test 3'; |
| 85 | + |
| 86 | + final Map<String, dynamic> message3 = <String, dynamic>{ |
| 87 | + 'content': 'Test' |
| 88 | + }; |
| 89 | + |
| 90 | + callHistory.pushIncoming( |
| 91 | + timestamp: timestamp3, |
| 92 | + method: method3, |
| 93 | + message: message3, |
| 94 | + ); |
| 95 | + |
| 96 | + expect(callHistory.incoming.length, equals(limit)); |
| 97 | + expect(callHistory.incoming.first.timeStamp, equals(timestamp2)); |
| 98 | + expect(callHistory.incoming.first.method, equals(method2)); |
| 99 | + expect(callHistory.incoming.first.message, equals(message2)); |
| 100 | + expect(callHistory.incoming.last.timeStamp, equals(timestamp3)); |
| 101 | + expect(callHistory.incoming.last.method, equals(method3)); |
| 102 | + expect(callHistory.incoming.last.message, equals(message3)); |
| 103 | + }); |
| 104 | + |
| 105 | + test('should trim history based on limit for outgoing responses.', () { |
| 106 | + const int limit = 2; |
| 107 | + |
| 108 | + callHistory.limit = limit; |
| 109 | + |
| 110 | + const int timestamp1 = 1621188000; |
| 111 | + const String method1 = 'test 1'; |
| 112 | + |
| 113 | + final Map<String, dynamic> message1 = <String, dynamic>{ |
| 114 | + 'content': 'Hello' |
| 115 | + }; |
| 116 | + |
| 117 | + callHistory.pushOutgoing( |
| 118 | + timestamp: timestamp1, |
| 119 | + method: method1, |
| 120 | + message: message1, |
| 121 | + ); |
| 122 | + |
| 123 | + const int timestamp2 = 1621189000; |
| 124 | + const String method2 = 'test 2'; |
| 125 | + |
| 126 | + final Map<String, dynamic> message2 = <String, dynamic>{ |
| 127 | + 'content': 'World' |
| 128 | + }; |
| 129 | + |
| 130 | + callHistory.pushOutgoing( |
| 131 | + timestamp: timestamp2, |
| 132 | + method: method2, |
| 133 | + message: message2, |
| 134 | + ); |
| 135 | + |
| 136 | + const int timestamp3 = 1621190000; |
| 137 | + const String method3 = 'test 3'; |
| 138 | + |
| 139 | + final Map<String, dynamic> message3 = <String, dynamic>{ |
| 140 | + 'content': 'Test' |
| 141 | + }; |
| 142 | + |
| 143 | + callHistory.pushOutgoing( |
| 144 | + timestamp: timestamp3, |
| 145 | + method: method3, |
| 146 | + message: message3, |
| 147 | + ); |
| 148 | + |
| 149 | + expect(callHistory.outgoing.length, equals(limit)); |
| 150 | + expect(callHistory.outgoing.first.timeStamp, equals(timestamp2)); |
| 151 | + expect(callHistory.outgoing.first.method, equals(method2)); |
| 152 | + expect(callHistory.outgoing.first.message, equals(message2)); |
| 153 | + expect(callHistory.outgoing.last.timeStamp, equals(timestamp3)); |
| 154 | + expect(callHistory.outgoing.last.method, equals(method3)); |
| 155 | + expect(callHistory.outgoing.last.message, equals(message3)); |
| 156 | + }); |
| 157 | + }); |
| 158 | +} |
0 commit comments