famedlysdk/test/timeline_test.dart

567 lines
20 KiB
Dart
Raw Normal View History

2019-06-21 10:18:54 +00:00
/*
2020-06-03 10:16:01 +00:00
* Ansible inventory script used at Famedly GmbH for managing many hosts
* Copyright (C) 2019, 2020 Famedly GmbH
2019-06-21 10:18:54 +00:00
*
2020-06-03 10:16:01 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-06-21 10:18:54 +00:00
*
2020-06-03 10:16:01 +00:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-06-21 10:18:54 +00:00
*
2020-06-03 10:16:01 +00:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-06-21 10:18:54 +00:00
*/
2020-06-03 10:16:01 +00:00
import 'package:famedlysdk/matrix_api.dart';
2019-10-04 09:44:32 +00:00
import 'package:test/test.dart';
import 'package:famedlysdk/src/client.dart';
import 'package:famedlysdk/src/room.dart';
import 'package:famedlysdk/src/timeline.dart';
2020-06-03 10:16:01 +00:00
import 'package:famedlysdk/src/utils/event_update.dart';
import 'package:famedlysdk/src/utils/room_update.dart';
import 'fake_matrix_api.dart';
2019-06-21 10:18:54 +00:00
void main() {
/// All Tests related to the MxContent
group('Timeline', () {
final roomID = '!1234:example.com';
2020-01-02 14:09:49 +00:00
final testTimeStamp = DateTime.now().millisecondsSinceEpoch;
var updateCount = 0;
var insertList = <int>[];
2019-06-21 10:18:54 +00:00
2020-09-21 10:28:13 +00:00
var client = Client('testclient',
httpClient: FakeMatrixApi(), sendMessageTimeoutSeconds: 5);
2019-06-21 10:18:54 +00:00
var room = Room(
id: roomID, client: client, prev_batch: '1234', roomAccountData: {});
var timeline = Timeline(
2019-06-26 14:36:34 +00:00
room: room,
events: [],
onUpdate: () {
updateCount++;
},
onInsert: (int insertID) {
insertList.add(insertID);
});
2019-06-21 10:18:54 +00:00
test('Create', () async {
2020-10-23 09:34:08 +00:00
await client.checkHomeserver('https://fakeServer.notExisting');
2020-07-21 07:34:30 +00:00
2020-01-02 14:09:49 +00:00
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2019-06-21 10:18:54 +00:00
roomID: roomID,
eventType: 'm.room.message',
2019-06-21 10:18:54 +00:00
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 2,
2020-07-21 07:34:30 +00:00
'event_id': '2',
'origin_server_ts': testTimeStamp - 1000
2020-05-15 18:40:17 +00:00
},
sortOrder: room.newSortOrder));
2020-01-02 14:09:49 +00:00
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2019-06-21 11:30:39 +00:00
roomID: roomID,
eventType: 'm.room.message',
2019-06-21 11:30:39 +00:00
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 2,
2020-07-21 07:34:30 +00:00
'event_id': '1',
'origin_server_ts': testTimeStamp
2020-05-15 18:40:17 +00:00
},
2020-07-21 07:34:30 +00:00
sortOrder: room.newSortOrder));
2019-06-21 11:30:39 +00:00
2019-06-21 10:18:54 +00:00
expect(timeline.sub != null, true);
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-06-21 10:18:54 +00:00
2019-06-21 11:30:39 +00:00
expect(updateCount, 2);
expect(insertList, [0, 0]);
expect(insertList.length, timeline.events.length);
2019-06-21 11:30:39 +00:00
expect(timeline.events.length, 2);
expect(timeline.events[0].eventId, '1');
expect(timeline.events[0].sender.id, '@alice:example.com');
2020-06-03 10:16:01 +00:00
expect(timeline.events[0].originServerTs.millisecondsSinceEpoch,
testTimeStamp);
expect(timeline.events[0].body, 'Testcase');
2020-01-02 14:09:49 +00:00
expect(
2020-06-03 10:16:01 +00:00
timeline.events[0].originServerTs.millisecondsSinceEpoch >
timeline.events[1].originServerTs.millisecondsSinceEpoch,
2020-01-02 14:09:49 +00:00
true);
2019-10-20 09:44:14 +00:00
expect(timeline.events[0].receipts, []);
2020-06-03 10:16:01 +00:00
room.roomAccountData['m.receipt'] = BasicRoomEvent.fromJson({
'type': 'm.receipt',
'content': {
'@alice:example.com': {
'event_id': '1',
'ts': 1436451550453,
2019-10-20 09:44:14 +00:00
}
},
'room_id': roomID,
2020-06-03 10:16:01 +00:00
});
2019-10-20 09:44:14 +00:00
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-10-20 09:44:14 +00:00
expect(timeline.events[0].receipts.length, 1);
expect(timeline.events[0].receipts[0].user.id, '@alice:example.com');
2019-12-12 12:19:18 +00:00
2020-01-02 14:09:49 +00:00
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2019-12-12 12:19:18 +00:00
roomID: roomID,
eventType: 'm.room.redaction',
2019-12-12 12:19:18 +00:00
content: {
'type': 'm.room.redaction',
'content': {'reason': 'spamming'},
'sender': '@alice:example.com',
'redacts': '2',
'event_id': '3',
'origin_server_ts': testTimeStamp + 1000
2020-05-15 18:40:17 +00:00
},
sortOrder: room.newSortOrder));
2019-12-12 12:19:18 +00:00
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-12-12 12:19:18 +00:00
expect(updateCount, 3);
expect(insertList, [0, 0]);
expect(insertList.length, timeline.events.length);
expect(timeline.events.length, 2);
expect(timeline.events[1].redacted, true);
2019-06-21 10:18:54 +00:00
});
2019-06-26 14:36:34 +00:00
test('Send message', () async {
await room.sendTextEvent('test', txid: '1234');
2019-06-26 14:36:34 +00:00
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-06-26 14:36:34 +00:00
2019-12-12 12:19:18 +00:00
expect(updateCount, 5);
2019-06-26 14:36:34 +00:00
expect(insertList, [0, 0, 0]);
expect(insertList.length, timeline.events.length);
2020-06-05 09:32:02 +00:00
final eventId = timeline.events[0].eventId;
expect(eventId.startsWith('\$event'), true);
2019-06-26 14:36:34 +00:00
expect(timeline.events[0].status, 1);
2020-01-02 14:09:49 +00:00
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2019-06-26 14:36:34 +00:00
roomID: roomID,
eventType: 'm.room.message',
2019-06-26 14:36:34 +00:00
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'test'},
'sender': '@alice:example.com',
'status': 2,
2020-06-05 09:32:02 +00:00
'event_id': eventId,
'unsigned': {'transaction_id': '1234'},
'origin_server_ts': DateTime.now().millisecondsSinceEpoch
2020-05-15 18:40:17 +00:00
},
sortOrder: room.newSortOrder));
2019-06-26 14:36:34 +00:00
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-06-26 14:36:34 +00:00
2019-12-12 12:19:18 +00:00
expect(updateCount, 6);
2019-06-26 14:36:34 +00:00
expect(insertList, [0, 0, 0]);
expect(insertList.length, timeline.events.length);
2020-06-05 09:32:02 +00:00
expect(timeline.events[0].eventId, eventId);
2019-06-26 14:36:34 +00:00
expect(timeline.events[0].status, 2);
});
2019-06-26 14:37:49 +00:00
test('Send message with error', () async {
2020-01-02 14:09:49 +00:00
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 0,
'event_id': 'abc',
'origin_server_ts': testTimeStamp
2020-05-15 18:40:17 +00:00
},
sortOrder: room.newSortOrder));
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2020-07-30 08:48:47 +00:00
expect(updateCount, 7);
await room.sendTextEvent('test', txid: 'errortxid');
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2020-07-30 08:48:47 +00:00
expect(updateCount, 9);
await room.sendTextEvent('test', txid: 'errortxid2');
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
await room.sendTextEvent('test', txid: 'errortxid3');
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-06-26 14:37:49 +00:00
2019-12-12 12:19:18 +00:00
expect(updateCount, 13);
expect(insertList, [0, 0, 0, 0, 0, 0, 0]);
expect(insertList.length, timeline.events.length);
2019-06-26 14:37:49 +00:00
expect(timeline.events[0].status, -1);
expect(timeline.events[1].status, -1);
expect(timeline.events[2].status, -1);
2019-06-26 14:37:49 +00:00
});
test('Remove message', () async {
2020-01-02 14:33:26 +00:00
await timeline.events[0].remove();
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-12-12 12:19:18 +00:00
expect(updateCount, 14);
expect(insertList, [0, 0, 0, 0, 0, 0, 0]);
expect(timeline.events.length, 6);
expect(timeline.events[0].status, -1);
});
test('Resend message', () async {
timeline.events.clear();
2020-07-23 08:09:00 +00:00
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': -1,
'event_id': 'new-test-event',
'origin_server_ts': testTimeStamp,
'unsigned': {'transaction_id': 'newresend'},
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, -1);
2020-07-23 08:09:00 +00:00
await timeline.events[0].sendAgain();
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-12-12 12:19:18 +00:00
expect(updateCount, 17);
expect(insertList, [0, 0, 0, 0, 0, 0, 0, 0]);
expect(timeline.events.length, 1);
expect(timeline.events[0].status, 1);
});
test('Request history', () async {
timeline.events.clear();
await room.requestHistory();
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-12-12 12:19:18 +00:00
expect(updateCount, 20);
expect(timeline.events.length, 3);
expect(timeline.events[0].eventId, '3143273582443PhrSn:example.org');
expect(timeline.events[1].eventId, '2143273582443PhrSn:example.org');
expect(timeline.events[2].eventId, '1143273582443PhrSn:example.org');
expect(room.prev_batch, 't47409-4357353_219380_26003_2265');
await timeline.events[2].redact(reason: 'test', txid: '1234');
});
test('Clear cache on limited timeline', () async {
client.onRoomUpdate.add(RoomUpdate(
id: roomID,
membership: Membership.join,
notification_count: 0,
highlight_count: 0,
limitedTimeline: true,
prev_batch: 'blah',
));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events.isEmpty, true);
});
2020-07-23 08:09:00 +00:00
test('sort errors on top', () async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': -1,
'event_id': 'abc',
'origin_server_ts': testTimeStamp
},
sortOrder: room.newSortOrder));
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 2,
'event_id': 'def',
'origin_server_ts': testTimeStamp + 5
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, -1);
expect(timeline.events[1].status, 2);
});
2020-07-23 08:09:00 +00:00
test('sending event to failed update', () async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 0,
'event_id': 'will-fail',
'origin_server_ts': testTimeStamp
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': -1,
'event_id': 'will-fail',
'origin_server_ts': testTimeStamp
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, -1);
expect(timeline.events.length, 1);
});
test('sending an event and the http request finishes first, 0 -> 1 -> 2',
() async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 0,
'event_id': 'transaction',
'origin_server_ts': testTimeStamp
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 1,
'event_id': '\$event',
'origin_server_ts': testTimeStamp,
'unsigned': {'transaction_id': 'transaction'}
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 1);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 2,
'event_id': '\$event',
'origin_server_ts': testTimeStamp,
'unsigned': {'transaction_id': 'transaction'}
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 2);
expect(timeline.events.length, 1);
});
test('sending an event where the sync reply arrives first, 0 -> 2 -> 1',
() async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'event_id': 'transaction',
'origin_server_ts': testTimeStamp,
'unsigned': {
MessageSendingStatusKey: 0,
'transaction_id': 'transaction',
},
2020-07-23 08:09:00 +00:00
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'event_id': '\$event',
'origin_server_ts': testTimeStamp,
'unsigned': {
'transaction_id': 'transaction',
MessageSendingStatusKey: 2,
},
2020-07-23 08:09:00 +00:00
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 2);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'event_id': '\$event',
'origin_server_ts': testTimeStamp,
'unsigned': {
'transaction_id': 'transaction',
MessageSendingStatusKey: 1,
},
2020-07-23 08:09:00 +00:00
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 2);
expect(timeline.events.length, 1);
});
test('sending an event 0 -> -1 -> 2', () async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 0,
'event_id': 'transaction',
'origin_server_ts': testTimeStamp
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': -1,
'origin_server_ts': testTimeStamp,
'unsigned': {'transaction_id': 'transaction'},
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, -1);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 2,
'event_id': '\$event',
'origin_server_ts': testTimeStamp,
'unsigned': {'transaction_id': 'transaction'},
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 2);
expect(timeline.events.length, 1);
});
test('sending an event 0 -> 2 -> -1', () async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 0,
'event_id': 'transaction',
'origin_server_ts': testTimeStamp
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 2,
'event_id': '\$event',
'origin_server_ts': testTimeStamp,
'unsigned': {'transaction_id': 'transaction'},
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 2);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
2020-10-22 10:21:20 +00:00
type: EventUpdateType.timeline,
2020-07-23 08:09:00 +00:00
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': -1,
'origin_server_ts': testTimeStamp,
'unsigned': {'transaction_id': 'transaction'},
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events[0].status, 2);
expect(timeline.events.length, 1);
});
2019-06-21 10:18:54 +00:00
});
}