@@ -2544,4 +2544,122 @@ void main() {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('PersistenceConnectionTests', () {
|
||||||
|
const apiKey = 'test-api-key';
|
||||||
|
late final api = FakeChatApi();
|
||||||
|
late final ws = FakeWebSocket();
|
||||||
|
|
||||||
|
final user = User(id: 'test-user-id');
|
||||||
|
final token = Token.development(user.id).rawValue;
|
||||||
|
|
||||||
|
late StreamChatClient client;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
client = StreamChatClient(apiKey, chatApi: api, ws: ws);
|
||||||
|
expect(client.persistenceEnabled, isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() {
|
||||||
|
client.chatPersistenceClient = null;
|
||||||
|
expect(client.persistenceEnabled, isFalse);
|
||||||
|
client.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('openPersistenceConnection connects the client to the user', () async {
|
||||||
|
client.chatPersistenceClient = MockPersistenceClient();
|
||||||
|
await client.openPersistenceConnection(user);
|
||||||
|
expect(client.persistenceEnabled, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'''multiple call to openPersistenceConnection does not throws an error if already connected to the same user''',
|
||||||
|
() async {
|
||||||
|
client.chatPersistenceClient = MockPersistenceClient();
|
||||||
|
await client.openPersistenceConnection(user);
|
||||||
|
expect(client.persistenceEnabled, isTrue);
|
||||||
|
|
||||||
|
expect(() => client.openPersistenceConnection(user), returnsNormally);
|
||||||
|
expect(() => client.openPersistenceConnection(user), returnsNormally);
|
||||||
|
expect(() => client.openPersistenceConnection(user), returnsNormally);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test(
|
||||||
|
'''openPersistenceConnection throws an error if client is already connected to a different user''',
|
||||||
|
() async {
|
||||||
|
client.chatPersistenceClient = MockPersistenceClient();
|
||||||
|
await client.openPersistenceConnection(user);
|
||||||
|
expect(client.persistenceEnabled, isTrue);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
() => client.openPersistenceConnection(user.copyWith(id: 'new-id')),
|
||||||
|
throwsA(const TypeMatcher<StreamChatError>()),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test(
|
||||||
|
'''openPersistenceConnection throws an error if chatPersistenceClient is not set''',
|
||||||
|
() async {
|
||||||
|
expect(
|
||||||
|
() => client.openPersistenceConnection(user),
|
||||||
|
throwsA(const TypeMatcher<StreamChatError>()),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test('closePersistenceConnection disconnects the client', () async {
|
||||||
|
client.chatPersistenceClient = MockPersistenceClient();
|
||||||
|
await client.openPersistenceConnection(user);
|
||||||
|
expect(client.persistenceEnabled, isTrue);
|
||||||
|
|
||||||
|
await client.closePersistenceConnection();
|
||||||
|
expect(client.persistenceEnabled, isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'''closePersistenceConnection does nothing if chatPersistenceClient is not connected''',
|
||||||
|
() async {
|
||||||
|
client.chatPersistenceClient = MockPersistenceClient();
|
||||||
|
expect(client.persistenceEnabled, isFalse);
|
||||||
|
|
||||||
|
expect(() => client.closePersistenceConnection(), returnsNormally);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test(
|
||||||
|
'''closePersistenceConnection does nothing if chatPersistenceClient is not set''',
|
||||||
|
() async {
|
||||||
|
expect(client.persistenceEnabled, isFalse);
|
||||||
|
expect(() => client.closePersistenceConnection(), returnsNormally);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test(
|
||||||
|
'''connectUser should re-use the persistence connection if already connected''',
|
||||||
|
() async {
|
||||||
|
client.chatPersistenceClient = MockPersistenceClient();
|
||||||
|
await client.openPersistenceConnection(user);
|
||||||
|
expect(client.persistenceEnabled, isTrue);
|
||||||
|
|
||||||
|
await client.connectUser(user, token, connectWebSocket: false);
|
||||||
|
expect(client.persistenceEnabled, isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test(
|
||||||
|
'''connectUser should throw if the persistence connection if already connected to a different user''',
|
||||||
|
() async {
|
||||||
|
client.chatPersistenceClient = MockPersistenceClient();
|
||||||
|
await client.openPersistenceConnection(user.copyWith(id: 'new-id'));
|
||||||
|
expect(client.persistenceEnabled, isTrue);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
() => client.connectUser(user, token, connectWebSocket: false),
|
||||||
|
throwsA(const TypeMatcher<StreamChatError>()),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,16 +64,26 @@ class MockAttachmentFileUploader extends Mock
|
|||||||
implements AttachmentFileUploader {}
|
implements AttachmentFileUploader {}
|
||||||
|
|
||||||
class MockPersistenceClient extends Mock implements ChatPersistenceClient {
|
class MockPersistenceClient extends Mock implements ChatPersistenceClient {
|
||||||
|
String? _userId;
|
||||||
bool _isConnected = false;
|
bool _isConnected = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool get isConnected => _isConnected;
|
bool get isConnected => _isConnected;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> connect(String userId) async => _isConnected = true;
|
String? get userId => _userId;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> disconnect({bool flush = false}) async => _isConnected = false;
|
Future<void> connect(String userId) async {
|
||||||
|
_userId = userId;
|
||||||
|
_isConnected = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> disconnect({bool flush = false}) async {
|
||||||
|
_userId = null;
|
||||||
|
_isConnected = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MockStreamChatClient extends Mock implements StreamChatClient {
|
class MockStreamChatClient extends Mock implements StreamChatClient {
|
||||||
|
|||||||
Reference in New Issue
Block a user