Skip to content

Commit

Permalink
feat: add non-room related command_runner tests
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 Dec 13, 2024
1 parent 8b15077 commit 5b2f809
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 10 deletions.
23 changes: 15 additions & 8 deletions lib/src/utils/commands_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extension CommandsClientExtension on Client {

/// Parse and execute a string, `msg` is the input. Optionally `inReplyTo` is the event being
/// replied to and `editEventId` is the eventId of the event being replied to
/// The [room] parameter can be null unless you execute a command strictly
/// requiring a [Room] to run hon.
Future<String?> parseAndRunCommand(
Room? room,
String msg, {
Expand Down Expand Up @@ -140,10 +142,12 @@ extension CommandsClientExtension on Client {
mxid,
enableEncryption: !parts.any((part) => part == '--no-encryption'),
);
stdout?.write(DefaultCommandOutput(
rooms: [roomId],
users: [mxid],
).toString());
stdout?.write(
DefaultCommandOutput(
rooms: [roomId],
users: [mxid],
).toString(),
);
return null;
});
addCommand('create', (args, stdout) async {
Expand All @@ -152,7 +156,7 @@ extension CommandsClientExtension on Client {
final parts = args.msg.split(' ');

final roomId = await args.client.createGroupChat(
groupName: groupName,
groupName: groupName.isNotEmpty ? groupName : null,
enableEncryption: !parts.any((part) => part == '--no-encryption'),
);
stdout?.write(DefaultCommandOutput(rooms: [roomId]).toString());
Expand Down Expand Up @@ -235,7 +239,8 @@ extension CommandsClientExtension on Client {
pl = int.tryParse(parts[1]);
if (pl == null) {
throw CommandException(
'Invalid power level ${parts[1]} when using /op');
'Invalid power level ${parts[1]} when using /op',
);
}
}
final mxid = parts.first;
Expand Down Expand Up @@ -293,7 +298,8 @@ extension CommandsClientExtension on Client {
final mxid = parts.first;
if (!mxid.isValidMatrixId) {
throw CommandException(
'You must enter a valid mxid when using /invite');
'You must enter a valid mxid when using /invite',
);
}
await room.invite(mxid);
stdout?.write(DefaultCommandOutput(users: [mxid]).toString());
Expand Down Expand Up @@ -361,7 +367,8 @@ extension CommandsClientExtension on Client {
final mxid = args.msg.split(' ').first;
if (!mxid.isValidMatrixId) {
throw CommandException(
'You must enter a valid mxid when using /maskasdm');
'You must enter a valid mxid when using /maskasdm',
);
}
if (await room.requestUser(mxid, requestProfile: false) == null) {
throw CommandException('User $mxid is not in this room');
Expand Down
69 changes: 67 additions & 2 deletions test/commands_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,15 @@ void main() {

test('create', () async {
FakeMatrixApi.calledEndpoints.clear();
await room.sendTextEvent('/create @alice:example.com --no-encryption');
await room.sendTextEvent('/create New room --no-encryption');
expect(
json.decode(
FakeMatrixApi.calledEndpoints['/client/v3/createRoom']?.first,
),
{'preset': 'private_chat'},
{
'name': 'New room',
'preset': 'private_chat',
},
);
});

Expand Down Expand Up @@ -527,6 +530,68 @@ void main() {
expect(sent, CuteEventContent.cuddle);
});

test('client - clearcache', () async {
await client.parseAndRunCommand(null, '/clearcache');
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 {
await client.parseAndRunCommand(null, '/discardsession');
} catch (e) {
error = e;
}

expect(error is RoomCommandException, isTrue);
});

test('dispose client', () async {
await client.dispose(closeDatabase: true);
});
Expand Down

0 comments on commit 5b2f809

Please sign in to comment.