From 2d84e6c5200d844694bc229e54328af1178780fa Mon Sep 17 00:00:00 2001 From: The one with the braid Date: Fri, 13 Dec 2024 16:00:17 +0100 Subject: [PATCH] fix: commands test Signed-off-by: The one with the braid --- lib/fake_matrix_api.dart | 29 ++++++++++++---- lib/src/utils/commands_extension.dart | 21 ++++++++--- test/commands_test.dart | 50 ++------------------------- 3 files changed, 41 insertions(+), 59 deletions(-) diff --git a/lib/fake_matrix_api.dart b/lib/fake_matrix_api.dart index 37f3e91e..5991bd78 100644 --- a/lib/fake_matrix_api.dart +++ b/lib/fake_matrix_api.dart @@ -43,7 +43,9 @@ class FakeMatrixApi extends BaseClient { static Map> get calledEndpoints => currentApi!._calledEndpoints; + static int get eventCounter => currentApi!._eventCounter; + static set eventCounter(int c) { currentApi!._eventCounter = c; } @@ -67,6 +69,8 @@ class FakeMatrixApi extends BaseClient { bool _trace = false; final _apiCallStream = StreamController.broadcast(); + static RoomsUpdate? _pendingRoomsUpdate; + static FakeMatrixApi? currentApi; static Future firstWhereValue(String value) { @@ -105,12 +109,13 @@ class FakeMatrixApi extends BaseClient { '${request.url.path.split('/_matrix').last}?${request.url.query}'; } - // ignore: avoid_print - if (_trace) print('called $action'); - if (action.endsWith('?')) { action = action.substring(0, action.length - 1); } + + // ignore: avoid_print + if (_trace) print('called $action'); + if (action.endsWith('?server_name')) { // This can be removed after matrix_api_lite is released with: // https://gitlab.com/famedly/libraries/matrix_api_lite/-/merge_requests/16 @@ -214,6 +219,8 @@ class FakeMatrixApi extends BaseClient { 'curve25519': 10, 'signed_curve25519': 100, }, + if (_pendingRoomsUpdate != null) + 'rooms': _pendingRoomsUpdate?.toJson(), }; } else if (method == 'PUT' && _client != null && @@ -2537,9 +2544,19 @@ class FakeMatrixApi extends BaseClient { '/client/v3/pushers/set': (var reqI) => {}, '/client/v3/join/1234': (var reqI) => {'room_id': '1234'}, '/client/v3/logout/all': (var reqI) => {}, - '/client/v3/createRoom': (var reqI) => { - 'room_id': '!1234:fakeServer.notExisting', - }, + '/client/v3/createRoom': (var reqI) { + final roomId = '!1234:fakeServer.notExisting'; + unawaited( + Future.delayed(Duration(milliseconds: 100)).then((_) { + _pendingRoomsUpdate = + RoomsUpdate(join: {roomId: JoinedRoomUpdate()}); + }), + ); + + return { + 'room_id': roomId, + }; + }, '/client/v3/rooms/!localpart%3Aserver.abc/read_markers': (var reqI) => {}, '/client/v3/rooms/!localpart:server.abc/kick': (var reqI) => {}, '/client/v3/rooms/!localpart%3Aserver.abc/ban': (var reqI) => {}, diff --git a/lib/src/utils/commands_extension.dart b/lib/src/utils/commands_extension.dart index 48f59df7..2962b156 100644 --- a/lib/src/utils/commands_extension.dart +++ b/lib/src/utils/commands_extension.dart @@ -158,6 +158,7 @@ extension CommandsClientExtension on Client { final roomId = await args.client.createGroupChat( groupName: groupName.isNotEmpty ? groupName : null, enableEncryption: !parts.any((part) => part == '--no-encryption'), + waitForSync: false, ); stdout?.write(DefaultCommandOutput(rooms: [roomId]).toString()); return null; @@ -521,11 +522,21 @@ class DefaultCommandOutput { } if (json['format'] != format) return null; return DefaultCommandOutput( - rooms: json['rooms'] as List?, - events: json['events'] as List?, - users: json['users'] as List?, - messages: json['messages'] as List?, - custom: json['custom'] as Map?, + rooms: json['rooms'] == null + ? null + : List.from(json['rooms'] as Iterable), + events: json['events'] == null + ? null + : List.from(json['events'] as Iterable), + users: json['users'] == null + ? null + : List.from(json['users'] as Iterable), + messages: json['messages'] == null + ? null + : List.from(json['messages'] as Iterable), + custom: json['custom'] == null + ? null + : Map.from(json['custom'] as Map), ); } diff --git a/test/commands_test.dart b/test/commands_test.dart index 6ebd4329..92d0fd6a 100644 --- a/test/commands_test.dart +++ b/test/commands_test.dart @@ -395,7 +395,7 @@ void main() { await room.sendTextEvent('/dm @alice:example.com --no-encryption'); expect( json.decode( - FakeMatrixApi.calledEndpoints['/client/v3/createRoom']?.first, + FakeMatrixApi.calledEndpoints['/client/v3/createRoom']?.last, ), { 'invite': ['@alice:example.com'], @@ -409,7 +409,7 @@ void main() { await room.sendTextEvent('/create New room --no-encryption'); expect( json.decode( - FakeMatrixApi.calledEndpoints['/client/v3/createRoom']?.first, + FakeMatrixApi.calledEndpoints['/client/v3/createRoom']?.last, ), { 'name': 'New room', @@ -535,52 +535,6 @@ void main() { expect(client.prevBatch, null); }); - test('client - dm', () async { - FakeMatrixApi.calledEndpoints.clear(); - final stdout = StringBuffer(); - await client.parseAndRunCommand( - null, - '/dm @alice:example.com --no-encryption', - stdout: stdout, - ); - expect( - json.decode( - FakeMatrixApi.calledEndpoints['/client/v3/createRoom']?.first, - ), - { - 'invite': ['@alice:example.com'], - 'is_direct': true, - 'preset': 'trusted_private_chat', - }); - expect( - (jsonDecode(stdout.toString()) as DefaultCommandOutput).rooms?.first, - '!1234:fakeServer.notExisting', - ); - }); - - test('client - create', () async { - FakeMatrixApi.calledEndpoints.clear(); - final stdout = StringBuffer(); - await client.parseAndRunCommand( - null, - '/create New room --no-encryption', - stdout: stdout, - ); - expect( - json.decode( - FakeMatrixApi.calledEndpoints['/client/v3/createRoom']?.first, - ), - { - 'name': 'New room', - 'preset': 'private_chat', - }, - ); - expect( - (jsonDecode(stdout.toString()) as DefaultCommandOutput).rooms?.first, - '!1234:fakeServer.notExisting', - ); - }); - test('client - missing room - discardsession', () async { Object? error; try {