-
Notifications
You must be signed in to change notification settings - Fork 11
/
e2e-tests.js
303 lines (259 loc) · 10.5 KB
/
e2e-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
'use strict';
process.env.NODE_ENV = 'test';
const assert = require('assert');
const urlJoin = require('url-join');
const escapeStringRegexp = require('escape-string-regexp');
const { MatrixAuth } = require('matrix-bot-sdk');
const { parseHTML } = require('linkedom');
const { fetchEndpointAsText, fetchEndpointAsJson } = require('../server/lib/fetch-endpoint');
const MatrixPublicArchiveURLCreator = require('matrix-public-archive-shared/lib/url-creator');
const config = require('../server/lib/config');
const testMatrixServerUrl1 = config.get('testMatrixServerUrl1');
const testMatrixServerUrl2 = config.get('testMatrixServerUrl2');
assert(testMatrixServerUrl1);
assert(testMatrixServerUrl2);
const basePath = config.get('basePath');
assert(basePath);
const interactive = config.get('interactive');
const matrixPublicArchiveURLCreator = new MatrixPublicArchiveURLCreator(basePath);
const HOMESERVER_URL_TO_PRETTY_NAME_MAP = {
[testMatrixServerUrl1]: 'hs1',
[testMatrixServerUrl2]: 'hs2',
};
async function getTestClientForHs(testMatrixServerUrl) {
const auth = new MatrixAuth(testMatrixServerUrl);
const client = await auth.passwordRegister(
`user-t${new Date().getTime()}-r${Math.floor(Math.random() * 1000000000)}`,
'password'
);
return client;
}
async function createMessagesInRoom(client, roomId, numMessages, prefix) {
let eventIds = [];
for (let i = 0; i < numMessages; i++) {
const eventId = await client.sendMessage(roomId, {
msgtype: 'm.text',
body: `${prefix} - message${i}`,
});
eventIds.push(eventId);
}
return eventIds;
}
async function createTestRoom(client) {
const roomId = await client.createRoom({
preset: 'public_chat',
name: 'the hangout spot',
initial_state: [
{
type: 'm.room.history_visibility',
state_key: '',
content: {
history_visibility: 'world_readable',
},
},
],
});
return roomId;
}
describe('matrix-public-archive', () => {
let server;
before(() => {
// Start the archive server
server = require('../server/server');
});
after(() => {
if (!interactive) {
server.close();
}
});
// Sanity check that our test homeservers can actually federate with each
// other. The rest of the tests won't work properly if this isn't working.
it('Test federation between fixture homeservers', async () => {
try {
const hs1Client = await getTestClientForHs(testMatrixServerUrl1);
const hs2Client = await getTestClientForHs(testMatrixServerUrl2);
// Create a room on hs2
const hs2RoomId = await createTestRoom(hs2Client);
const room2EventIds = await createMessagesInRoom(
hs2Client,
hs2RoomId,
10,
HOMESERVER_URL_TO_PRETTY_NAME_MAP[hs2Client.homeserverUrl]
);
// Join hs1 to a room on hs2 (federation)
await hs1Client.joinRoom(hs2RoomId, 'hs2');
// From, hs1, make sure we can fetch messages from hs2
const messagesEndpoint = urlJoin(
hs1Client.homeserverUrl,
`_matrix/client/r0/rooms/${hs2RoomId}/messages?limit=5&dir=b&filter={"types":["m.room.message"]}`
);
const messageResData = await fetchEndpointAsJson(messagesEndpoint, {
accessToken: hs1Client.accessToken,
});
// Make sure it returned some messages
assert.strictEqual(messageResData.chunk.length, 5);
// Make sure all of the messages belong to the room
messageResData.chunk.map((event) => {
const isEventInRoomFromHs2 = room2EventIds.some((room2EventId) => {
return room2EventId === event.event_id;
});
// Make sure the message belongs to the room on hs2
assert.strictEqual(
isEventInRoomFromHs2,
true,
`Expected ${event.event_id} (${event.type}: "${
event.content.body
}") to be in room on hs2=${JSON.stringify(room2EventIds)}`
);
});
} catch (err) {
if (err.body) {
// FIXME: Remove this try/catch once the matrix-bot-sdk no longer throws
// huge response objects as errors, see
// https://github.com/turt2live/matrix-bot-sdk/pull/158
throw new Error(
`Error occured in matrix-bot-sdk (this new error is to stop it from logging the huge response) statusCode=${
err.statusCode
} body=${JSON.stringify(err.body)}`
);
}
throw err;
}
});
// eslint-disable-next-line max-statements
it('can render diverse messages', async () => {
try {
const client = await getTestClientForHs(testMatrixServerUrl1);
const roomId = await createTestRoom(client);
// TODO: Set avatar of user
// TODO: Set avatar of room
// Test image
const mxcUri = await client.uploadContentFromUrl(
'https://en.wikipedia.org/wiki/Friction#/media/File:Friction_between_surfaces.jpg'
);
const imageEventId = await client.sendMessage(roomId, {
body: 'Friction_between_surfaces.jpeg',
info: {
size: 396644,
mimetype: 'image/jpeg',
thumbnail_info: {
w: 800,
h: 390,
mimetype: 'image/jpeg',
size: 126496,
},
w: 1894,
h: 925,
'xyz.amorgan.blurhash': 'LkR3G|IU?w%NbwbIemae_NxuD$M{',
// TODO: How to get a proper thumnail URL that will load?
thumbnail_url: mxcUri,
},
msgtype: 'm.image',
url: mxcUri,
});
// A normal text message
const normalMessageText1 =
'^ Figure 1: Simulated blocks with fractal rough surfaces, exhibiting static frictional interactions';
const normalMessageEventId1 = await client.sendMessage(roomId, {
msgtype: 'm.text',
body: normalMessageText1,
});
// Another normal text message
const normalMessageText2 =
'The topography of the Moon has been measured with laser altimetry and stereo image analysis.';
const normalMessageEventId2 = await client.sendMessage(roomId, {
msgtype: 'm.text',
body: normalMessageText2,
});
// Test replies
const replyMessageText = `The concentration of maria on the near side likely reflects the substantially thicker crust of the highlands of the Far Side, which may have formed in a slow-velocity impact of a second moon of Earth a few tens of millions of years after the Moon's formation.`;
const replyMessageEventId = await client.sendMessage(roomId, {
'org.matrix.msc1767.message': [
{
body: '> <@ericgittertester:my.synapse.server> ${normalMessageText2}',
mimetype: 'text/plain',
},
{
body: `<mx-reply><blockquote><a href="https://matrix.to/#/${roomId}/${normalMessageEventId2}?via=my.synapse.server">In reply to</a> <a href="https://matrix.to/#/@ericgittertester:my.synapse.server">@ericgittertester:my.synapse.server</a><br>${normalMessageText2}</blockquote></mx-reply>${replyMessageText}`,
mimetype: 'text/html',
},
],
body: `> <@ericgittertester:my.synapse.server> ${normalMessageText2}\n\n${replyMessageText}`,
msgtype: 'm.text',
format: 'org.matrix.custom.html',
formatted_body: `<mx-reply><blockquote><a href="https://matrix.to/#/${roomId}/${normalMessageEventId2}?via=my.synapse.server">In reply to</a> <a href="https://matrix.to/#/@ericgittertester:my.synapse.server">@ericgittertester:my.synapse.server</a><br>${normalMessageText2}</blockquote></mx-reply>${replyMessageText}`,
'm.relates_to': {
'm.in_reply_to': {
event_id: normalMessageEventId2,
},
},
});
// Test reactions
const reactionText = '😅';
await client.sendEvent(roomId, 'm.reaction', {
'm.relates_to': {
rel_type: 'm.annotation',
event_id: replyMessageEventId,
key: reactionText,
},
});
const archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForDate(roomId, new Date());
if (interactive) {
console.log('Interactive URL for test', archiveUrl);
}
const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const dom = parseHTML(archivePageHtml);
// Make sure the image message is visible
const imageElement = dom.document.querySelector(`[data-event-id="${imageEventId}"] img`);
assert(imageElement);
assert.match(imageElement.getAttribute('src'), new RegExp(`^http://.*`));
assert.strictEqual(imageElement.getAttribute('alt'), 'Friction_between_surfaces.jpeg');
// Make sure the normal message is visible
assert.match(
dom.document.querySelector(`[data-event-id="${normalMessageEventId1}"]`).outerHTML,
new RegExp(`.*${escapeStringRegexp(normalMessageText1)}.*`)
);
// Make sure the other normal message is visible
assert.match(
dom.document.querySelector(`[data-event-id="${normalMessageEventId2}"]`).outerHTML,
new RegExp(`.*${escapeStringRegexp(normalMessageText2)}.*`)
);
const replyMessageElement = dom.document.querySelector(
`[data-event-id="${replyMessageEventId}"]`
);
// Make sure the reply text is there
assert.match(
replyMessageElement.outerHTML,
new RegExp(`.*${escapeStringRegexp(replyMessageText)}.*`)
);
// Make sure it also includes the message we're replying to
assert.match(
replyMessageElement.outerHTML,
new RegExp(`.*${escapeStringRegexp(normalMessageEventId2)}.*`)
);
// Make sure the reaction also exists
assert.match(
replyMessageElement.outerHTML,
new RegExp(`.*${escapeStringRegexp(reactionText)}.*`)
);
} catch (err) {
if (err.body) {
// FIXME: Remove this try/catch once the matrix-bot-sdk no longer throws
// huge response objects as errors, see
// https://github.com/turt2live/matrix-bot-sdk/pull/158
throw new Error(
`Error occured in matrix-bot-sdk (this new error is to stop it from logging the huge response) statusCode=${
err.statusCode
} body=${JSON.stringify(err.body)}`
);
}
throw err;
}
});
it(`can render day back in time from room on remote homeserver we haven't backfilled from`);
it(`will redirect to hour pagination when there are too many messages`);
it(`will render a room with only a day of messages`);
it(
`will render a room with a sparse amount of messages (a few per day) with no contamination between days`
);
});