refactor: port room test to nullsafety

This commit is contained in:
Nicolas Werner 2021-10-28 02:38:28 +02:00
parent 2fd4425099
commit 3f83e5481c
No known key found for this signature in database
GPG Key ID: C8D75E610773F2D9
10 changed files with 88 additions and 95 deletions

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2021 Famedly GmbH
@ -27,16 +26,16 @@ import 'fake_matrix_api.dart';
void main() {
group('Commands', () {
Client client;
Room room;
late Client client;
late Room room;
var olmEnabled = true;
final getLastMessagePayload =
([String type = 'm.room.message', String stateKey]) {
([String type = 'm.room.message', String? stateKey]) {
final state = stateKey != null;
return json.decode(FakeMatrixApi.calledEndpoints.entries
.firstWhere((e) => e.key.startsWith(
'/client/r0/rooms/${Uri.encodeComponent(room.id)}/${state ? 'state' : 'send'}/${Uri.encodeComponent(type)}${state && stateKey.isNotEmpty ? '/' + Uri.encodeComponent(stateKey) : ''}'))
'/client/r0/rooms/${Uri.encodeComponent(room.id)}/${state ? 'state' : 'send'}/${Uri.encodeComponent(type)}${state && stateKey?.isNotEmpty == true ? '/' + Uri.encodeComponent(stateKey!) : ''}'))
.value
.first);
};
@ -151,7 +150,7 @@ void main() {
'formatted_body': '<b>yay</b>',
},
originServerTs: DateTime.now(),
senderId: client.userID,
senderId: client.userID!,
));
final sent = getLastMessagePayload('m.reaction');
expect(sent, {
@ -169,7 +168,7 @@ void main() {
expect(
FakeMatrixApi
.calledEndpoints['/client/r0/join/!newroom%3Aexample.com']
.first !=
?.first !=
null,
true);
});
@ -181,7 +180,7 @@ void main() {
FakeMatrixApi
.calledEndpoints[
'/client/r0/rooms/!1234%3AfakeServer.notExisting/leave']
.first !=
?.first !=
null,
true);
});
@ -209,7 +208,7 @@ void main() {
json.decode(FakeMatrixApi
.calledEndpoints[
'/client/r0/rooms/!1234%3AfakeServer.notExisting/kick']
.first),
?.first),
{
'user_id': '@baduser:example.org',
});
@ -222,7 +221,7 @@ void main() {
json.decode(FakeMatrixApi
.calledEndpoints[
'/client/r0/rooms/!1234%3AfakeServer.notExisting/ban']
.first),
?.first),
{
'user_id': '@baduser:example.org',
});
@ -235,7 +234,7 @@ void main() {
json.decode(FakeMatrixApi
.calledEndpoints[
'/client/r0/rooms/!1234%3AfakeServer.notExisting/unban']
.first),
?.first),
{
'user_id': '@baduser:example.org',
});
@ -248,7 +247,7 @@ void main() {
json.decode(FakeMatrixApi
.calledEndpoints[
'/client/r0/rooms/!1234%3AfakeServer.notExisting/invite']
.first),
?.first),
{
'user_id': '@baduser:example.org',
});
@ -276,14 +275,14 @@ void main() {
test('discardsession', () async {
if (olmEnabled) {
await client.encryption.keyManager.createOutboundGroupSession(room.id);
await client.encryption?.keyManager.createOutboundGroupSession(room.id);
expect(
client.encryption.keyManager.getOutboundGroupSession(room.id) !=
client.encryption?.keyManager.getOutboundGroupSession(room.id) !=
null,
true);
await room.sendTextEvent('/discardsession');
expect(
client.encryption.keyManager.getOutboundGroupSession(room.id) !=
client.encryption?.keyManager.getOutboundGroupSession(room.id) !=
null,
false);
}

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
@ -49,8 +48,8 @@ Future<bool> olmEnabled() async {
void testDatabase(
Future<DatabaseApi> futureDatabase,
) {
DatabaseApi database;
int toDeviceQueueIndex;
late DatabaseApi database;
late int toDeviceQueueIndex;
test('Open', () async {
database = await futureDatabase;
});
@ -151,7 +150,7 @@ void testDatabase(
);
final client = await database.getClient('name');
expect(client['token'], 'token');
expect(client?['token'], 'token');
});
test('updateClient', () async {
await database.updateClient(
@ -164,21 +163,21 @@ void testDatabase(
'olmAccount',
);
final client = await database.getClient('name');
expect(client['token'], 'token_different');
expect(client?['token'], 'token_different');
});
test('updateClientKeys', () async {
await database.updateClientKeys(
'olmAccount2',
);
final client = await database.getClient('name');
expect(client['olm_account'], 'olmAccount2');
expect(client?['olm_account'], 'olmAccount2');
});
test('storeSyncFilterId', () async {
await database.storeSyncFilterId(
'1234',
);
final client = await database.getClient('name');
expect(client['sync_filter_id'], '1234');
expect(client?['sync_filter_id'], '1234');
});
test('getAccountData', () async {
await database.getAccountData();
@ -218,28 +217,29 @@ void testDatabase(
Client('testclient'));
});
test('getEventById', () async {
final event = await database.getEventById(
'\$event:example.com', Room(id: '!testroom:example.com'));
expect(event.type, EventTypes.Message);
final event = await database.getEventById('\$event:example.com',
Room(id: '!testroom:example.com', client: Client('testclient')));
expect(event?.type, EventTypes.Message);
});
test('getEventList', () async {
final events =
await database.getEventList(Room(id: '!testroom:example.com'));
final events = await database.getEventList(
Room(id: '!testroom:example.com', client: Client('testclient')));
expect(events.single.type, EventTypes.Message);
});
test('getUser', () async {
final user = await database.getUser(
'@bob:example.org', Room(id: '!testroom:example.com'));
final user = await database.getUser('@bob:example.org',
Room(id: '!testroom:example.com', client: Client('testclient')));
expect(user, null);
});
test('getUsers', () async {
final users = await database.getUsers(Room(id: '!testroom:example.com'));
final users = await database.getUsers(
Room(id: '!testroom:example.com', client: Client('testclient')));
expect(users.isEmpty, true);
});
test('removeEvent', () async {
await database.removeEvent('\$event:example.com', '!testroom:example.com');
final event = await database.getEventById(
'\$event:example.com', Room(id: '!testroom:example.com'));
final event = await database.getEventById('\$event:example.com',
Room(id: '!testroom:example.com', client: Client('testclient')));
expect(event, null);
});
test('getAllInboundGroupSessions', () async {
@ -267,7 +267,7 @@ void testDatabase(
'!testroom:example.com',
'sessionId',
);
expect(jsonDecode(session.content)['foo'], 'bar');
expect(jsonDecode(session!.content)['foo'], 'bar');
});
test('markInboundGroupSessionAsUploaded', () async {
await database.markInboundGroupSessionAsUploaded(
@ -296,7 +296,7 @@ void testDatabase(
});
test('storeSSSSCache', () async {
await database.storeSSSSCache('type', 'keyId', 'ciphertext', '{}');
final cache = await database.getSSSSCache('type');
final cache = (await database.getSSSSCache('type'))!;
expect(cache.type, 'type');
expect(cache.keyId, 'keyId');
expect(cache.ciphertext, 'ciphertext');
@ -349,7 +349,7 @@ void testDatabase(
'!testroom:example.com',
'@alice:example.com',
);
expect(session.devices.isEmpty, true);
expect(session?.devices.isEmpty, true);
});
test('getLastSentMessageUserDeviceKey', () async {
final list = await database.getLastSentMessageUserDeviceKey(
@ -361,7 +361,7 @@ void testDatabase(
test('getUnimportantRoomEventStatesForRoom', () async {
final events = await database.getUnimportantRoomEventStatesForRoom(
['events'],
Room(id: '!mep'),
Room(id: '!mep', client: Client('testclient')),
);
expect(events.isEmpty, true);
});

View File

@ -2124,35 +2124,35 @@ class FakeMatrixApi extends MockClient {
'/client/r0/pushrules/global/content/nocake/enabled': (var req) => {},
'/client/r0/pushrules/global/content/nocake/actions': (var req) => {},
'/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.history_visibility':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.join_rules':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/state/m.room.guest_access':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.invite/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.answer/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.select_answer/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.reject/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.negotiate/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.candidates/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.hangup/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.replaces/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.asserted_identity/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/m.call.sdp_stream_metadata_changed/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/org.matrix.call.sdp_stream_metadata_changed/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!localpart%3Aserver.abc/send/org.matrix.call.asserted_identity/1234':
(var req) => {},
(var req) => {'event_id': '1234'},
'/client/r0/rooms/!1234%3Aexample.com/redact/1143273582443PhrSn%3Aexample.org/1234':
(var req) => {'event_id': '1234'},
'/client/r0/pushrules/global/room/!localpart%3Aserver.abc': (var req) =>

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2021 Famedly GmbH

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2020 Famedly GmbH

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2020 Famedly GmbH

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
@ -34,11 +33,11 @@ void main() {
);
expect(matrixException.errcode, 'M_FORBIDDEN');
final flows = matrixException.authenticationFlows;
expect(flows.length, 1);
expect(flows.first.stages.length, 1);
expect(flows.first.stages.first, 'example.type.foo');
expect(flows?.length, 1);
expect(flows?.first.stages.length, 1);
expect(flows?.first.stages.first, 'example.type.foo');
expect(
matrixException.authenticationParams['example.type.baz'],
matrixException.authenticationParams?['example.type.baz'],
{'example_key': 'foobar'},
);
expect(matrixException.completedAuthenticationFlows.length, 1);

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
@ -33,8 +32,8 @@ import 'fake_client.dart';
import 'fake_matrix_api.dart';
void main() {
Client matrix;
Room room;
late Client matrix;
late Room room;
/// All Tests related to the Event
group('Room', () {
@ -96,8 +95,9 @@ void main() {
expect(room.summary.mInvitedMemberCount, notificationCount);
expect(room.summary.mHeroes, heroes);
expect(room.displayname, 'Alice, Bob, Charley');
expect(room.getState('m.room.join_rules').content['join_rule'], 'public');
expect(room.roomAccountData['com.test.foo'].content['foo'], 'bar');
expect(
room.getState('m.room.join_rules')?.content['join_rule'], 'public');
expect(room.roomAccountData['com.test.foo']?.content['foo'], 'bar');
room.setState(
Event(
@ -181,9 +181,9 @@ void main() {
stateKey: '',
),
);
expect(room.lastEvent.eventId, '12345');
expect(room.lastEvent.body, 'abc');
expect(room.timeCreated, room.lastEvent.originServerTs);
expect(room.lastEvent?.eventId, '12345');
expect(room.lastEvent?.body, 'abc');
expect(room.timeCreated, room.lastEvent?.originServerTs);
});
test('lastEvent is set properly', () {
@ -199,7 +199,7 @@ void main() {
stateKey: '',
),
);
expect(room.lastEvent.body, 'cd');
expect(room.lastEvent?.body, 'cd');
room.setState(
Event(
senderId: '@test:example.com',
@ -212,7 +212,7 @@ void main() {
stateKey: '',
),
);
expect(room.lastEvent.body, 'cdc');
expect(room.lastEvent?.body, 'cdc');
room.setState(
Event(
senderId: '@test:example.com',
@ -230,7 +230,7 @@ void main() {
stateKey: '',
),
);
expect(room.lastEvent.body, 'cdc'); // because we edited the "cd" message
expect(room.lastEvent?.body, 'cdc'); // because we edited the "cd" message
room.setState(
Event(
senderId: '@test:example.com',
@ -248,7 +248,7 @@ void main() {
stateKey: '',
),
);
expect(room.lastEvent.body, 'edited cdc');
expect(room.lastEvent?.body, 'edited cdc');
});
test('lastEvent when reply parent edited', () async {
room.setState(
@ -263,7 +263,7 @@ void main() {
stateKey: '',
),
);
expect(room.lastEvent.body, 'A');
expect(room.lastEvent?.body, 'A');
room.setState(
Event(
@ -281,7 +281,7 @@ void main() {
stateKey: '',
),
);
expect(room.lastEvent.body, 'B');
expect(room.lastEvent?.body, 'B');
room.setState(
Event(
senderId: '@test:example.com',
@ -299,7 +299,7 @@ void main() {
stateKey: '',
),
);
expect(room.lastEvent.body, 'B');
expect(room.lastEvent?.body, 'B');
});
test('sendReadMarker', () async {
await room.setReadMarker('§1234:fakeServer.notExisting');
@ -313,12 +313,12 @@ void main() {
expect(user.displayName, 'Alice Margatroid');
expect(user.membership, Membership.join);
expect(user.avatarUrl.toString(), 'mxc://example.org/SEsfnsuifSDFSSEF');
expect(user.room.id, '!localpart:server.abc');
expect(user.room?.id, '!localpart:server.abc');
});
test('getEventByID', () async {
final event = await room.getEventById('1234');
expect(event.eventId, '143273582443PhrSn:example.org');
expect(event?.eventId, '143273582443PhrSn:example.org');
});
test('setName', () async {
@ -367,7 +367,7 @@ void main() {
stateKey: ''),
);
expect(room.ownPowerLevel, 100);
expect(room.getPowerLevelByUserId(matrix.userID), room.ownPowerLevel);
expect(room.getPowerLevelByUserId(matrix.userID!), room.ownPowerLevel);
expect(room.getPowerLevelByUserId('@nouser:example.com'), 10);
expect(room.ownPowerLevel, 100);
expect(room.canBan, true);
@ -381,7 +381,7 @@ void main() {
expect(room.canSendEvent('m.room.power_levels'), true);
expect(room.canSendEvent('m.room.member'), true);
expect(room.powerLevels,
room.getState('m.room.power_levels').content['users']);
room.getState('m.room.power_levels')?.content['users']);
room.setState(
Event(
@ -454,12 +454,12 @@ void main() {
});
test('getUserByMXID', () async {
User user;
User? user;
try {
user = await room.requestUser('@getme:example.com');
} catch (_) {}
expect(user.stateKey, '@getme:example.com');
expect(user.calcDisplayname(), 'Getme');
expect(user?.stateKey, '@getme:example.com');
expect(user?.calcDisplayname(), 'Getme');
});
test('setAvatar', () async {
@ -472,14 +472,14 @@ void main() {
final dynamic resp = await room.sendEvent(
{'msgtype': 'm.text', 'body': 'hello world'},
txid: 'testtxid');
expect(resp.startsWith('\$event'), true);
expect(resp?.startsWith('\$event'), true);
});
test('sendEvent', () async {
FakeMatrixApi.calledEndpoints.clear();
final dynamic resp =
await room.sendTextEvent('Hello world', txid: 'testtxid');
expect(resp.startsWith('\$event'), true);
expect(resp?.startsWith('\$event'), true);
final entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
final content = json.decode(entry.value.first);
@ -493,7 +493,7 @@ void main() {
FakeMatrixApi.calledEndpoints.clear();
final dynamic resp = await room.sendTextEvent('Hello world',
txid: 'testtxid', editEventId: '\$otherEvent');
expect(resp.startsWith('\$event'), true);
expect(resp?.startsWith('\$event'), true);
final entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
final content = json.decode(entry.value.first);
@ -524,7 +524,7 @@ void main() {
FakeMatrixApi.calledEndpoints.clear();
var resp = await room.sendTextEvent('Hello world',
txid: 'testtxid', inReplyTo: event);
expect(resp.startsWith('\$event'), true);
expect(resp?.startsWith('\$event'), true);
var entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
var content = json.decode(entry.value.first);
@ -553,7 +553,7 @@ void main() {
FakeMatrixApi.calledEndpoints.clear();
resp = await room.sendTextEvent('Hello world\nfox',
txid: 'testtxid', inReplyTo: event);
expect(resp.startsWith('\$event'), true);
expect(resp?.startsWith('\$event'), true);
entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
content = json.decode(entry.value.first);
@ -585,7 +585,7 @@ void main() {
FakeMatrixApi.calledEndpoints.clear();
resp = await room.sendTextEvent('Hello world',
txid: 'testtxid', inReplyTo: event);
expect(resp.startsWith('\$event'), true);
expect(resp?.startsWith('\$event'), true);
entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
content = json.decode(entry.value.first);
@ -614,7 +614,7 @@ void main() {
FakeMatrixApi.calledEndpoints.clear();
resp = await room.sendTextEvent('Hello world',
txid: 'testtxid', inReplyTo: event);
expect(resp.startsWith('\$event'), true);
expect(resp?.startsWith('\$event'), true);
entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
content = json.decode(entry.value.first);
@ -636,7 +636,7 @@ void main() {
FakeMatrixApi.calledEndpoints.clear();
final dynamic resp =
await room.sendReaction('\$otherEvent', '🦊', txid: 'testtxid');
expect(resp.startsWith('\$event'), true);
expect(resp?.startsWith('\$event'), true);
final entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.reaction/'));
final content = json.decode(entry.value.first);
@ -656,7 +656,7 @@ void main() {
final geoUri = 'geo:0.0,0.0';
final dynamic resp =
await room.sendLocation(body, geoUri, txid: 'testtxid');
expect(resp.startsWith('\$event'), true);
expect(resp?.startsWith('\$event'), true);
final entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
@ -684,8 +684,8 @@ void main() {
test('pushRuleState', () async {
expect(room.pushRuleState, PushRuleState.mentionsOnly);
matrix.accountData['m.push_rules'].content['global']['override']
.add(matrix.accountData['m.push_rules'].content['global']['room'][0]);
matrix.accountData['m.push_rules']?.content['global']['override'].add(
matrix.accountData['m.push_rules']?.content['global']['room'][0]);
expect(room.pushRuleState, PushRuleState.dontNotify);
});
@ -756,7 +756,7 @@ void main() {
'type': 'm.tag'
});
expect(room.tags.length, 1);
expect(room.tags[TagType.favourite].order, 0.1);
expect(room.tags[TagType.favourite]?.order, 0.1);
expect(room.isFavourite, true);
await room.setFavourite(false);
});