Skip to content

Commit

Permalink
fix: commands test
Browse files Browse the repository at this point in the history
Signed-off-by: The one with the braid <[email protected]>
  • Loading branch information
TheOneWithTheBraid committed Jan 6, 2025
1 parent 3fc0164 commit 06ba0c4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 59 deletions.
29 changes: 23 additions & 6 deletions lib/fake_matrix_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class FakeMatrixApi extends BaseClient {

static Map<String, List<dynamic>> get calledEndpoints =>
currentApi!._calledEndpoints;

static int get eventCounter => currentApi!._eventCounter;

static set eventCounter(int c) {
currentApi!._eventCounter = c;
}
Expand All @@ -67,6 +69,8 @@ class FakeMatrixApi extends BaseClient {
bool _trace = false;
final _apiCallStream = StreamController<String>.broadcast();

static RoomsUpdate? _pendingRoomsUpdate;

static FakeMatrixApi? currentApi;

static Future<String> firstWhereValue(String value) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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) => {},
Expand Down
21 changes: 16 additions & 5 deletions lib/src/utils/commands_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -521,11 +522,21 @@ class DefaultCommandOutput {
}
if (json['format'] != format) return null;
return DefaultCommandOutput(
rooms: json['rooms'] as List<String>?,
events: json['events'] as List<String>?,
users: json['users'] as List<String>?,
messages: json['messages'] as List<String>?,
custom: json['custom'] as Map<String, Object?>?,
rooms: json['rooms'] == null
? null
: List<String>.from(json['rooms'] as Iterable),
events: json['events'] == null
? null
: List<String>.from(json['events'] as Iterable),
users: json['users'] == null
? null
: List<String>.from(json['users'] as Iterable),
messages: json['messages'] == null
? null
: List<String>.from(json['messages'] as Iterable),
custom: json['custom'] == null
? null
: Map<String, Object?>.from(json['custom'] as Map),
);
}

Expand Down
50 changes: 2 additions & 48 deletions test/commands_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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',
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 06ba0c4

Please sign in to comment.