From 8c096ef6caaa6dbd34088f13121cf31870f8fe05 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 21 Dec 2022 19:48:51 +0100 Subject: [PATCH] test: Add tests for account data store and retrieve fixes https://gitlab.com/famedly/company/frontend/famedlysdk/-/issues/326 --- test/client_test.dart | 32 ++++++++++++++++++++++++++++++++ test/fake_matrix_api.dart | 28 +++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/test/client_test.dart b/test/client_test.dart index fc8233d3..86c33917 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -287,6 +287,38 @@ void main() { // To check if the emoji is properly added, we need to wait for a sync roundtrip }); + test('accountData', () async { + final content = { + 'bla': 'blub', + }; + + final key = 'abc def!/_-'; + await matrix.setAccountData(matrix.userID!, key, content); + final dbContent = await matrix.database?.getAccountData(); + + expect(matrix.accountData[key]?.content, content); + expect(dbContent?[key]?.content, content); + }); + + test('roomAccountData', () async { + final content = { + 'bla': 'blub', + }; + + final key = 'abc def!/_-'; + final roomId = '!726s6s6q:example.com'; + await matrix.setAccountDataPerRoom(matrix.userID!, roomId, key, content); + final roomFromList = (await matrix.database?.getRoomList(matrix)) + ?.firstWhere((room) => room.id == roomId); + final roomFromDb = await matrix.database?.getSingleRoom(matrix, roomId); + + expect( + matrix.getRoomById(roomId)?.roomAccountData[key]?.content, content); + expect(roomFromList?.roomAccountData[key]?.content, content); + expect(roomFromDb?.roomAccountData[key]?.content, content, + skip: 'The single room function does not load account data'); + }); + test('Logout', () async { final loginStateFuture = matrix.onLoginStateChanged.stream.first; await matrix.logout(); diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index a4ddf78a..c00f87b5 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -164,7 +164,7 @@ class FakeMatrixApi extends BaseClient { } else if (method == 'PUT' && _client != null && action.contains('/account_data/') && - !action.contains('/room/')) { + !action.contains('/rooms/')) { final type = Uri.decodeComponent(action.split('/').last); final syncUpdate = sdk.SyncUpdate( nextBatch: '', @@ -178,6 +178,32 @@ class FakeMatrixApi extends BaseClient { await _client?.handleSync(syncUpdate); } res = {}; + } else if (method == 'PUT' && + _client != null && + action.contains('/account_data/') && + action.contains('/rooms/')) { + final segments = action.split('/'); + final type = Uri.decodeComponent(segments.last); + final roomId = Uri.decodeComponent(segments[segments.length - 3]); + final syncUpdate = sdk.SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + join: { + roomId: JoinedRoomUpdate(accountData: [ + sdk.BasicRoomEvent( + content: decodeJson(data), type: type, roomId: roomId) + ]) + }, + ), + ); + if (_client?.database != null) { + await _client?.database?.transaction(() async { + await _client?.handleSync(syncUpdate); + }); + } else { + await _client?.handleSync(syncUpdate); + } + res = {}; } else { res = {'errcode': 'M_UNRECOGNIZED', 'error': 'Unrecognized request'}; statusCode = 405;