riot-web/test/utils/export-test.tsx

277 lines
9.4 KiB
TypeScript
Raw Normal View History

2021-08-03 11:06:21 +02:00
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-08-09 09:54:54 +02:00
import { IContent, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk";
2021-08-03 11:06:21 +02:00
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
2021-08-13 20:14:07 +02:00
import { IExportOptions, ExportType, ExportFormat } from "../../src/utils/exportUtils/exportUtils";
2021-08-03 11:23:23 +02:00
import '../skinned-sdk';
import PlainTextExporter from "../../src/utils/exportUtils/PlainTextExport";
2021-08-09 09:54:54 +02:00
import HTMLExporter from "../../src/utils/exportUtils/HtmlExport";
2021-08-03 11:06:21 +02:00
import * as TestUtilsMatrix from '../test-utils';
import { stubClient } from '../test-utils';
2021-08-09 09:54:54 +02:00
import { renderToString } from "react-dom/server";
2021-08-03 11:06:21 +02:00
let client: MatrixClient;
const MY_USER_ID = "@me:here";
function generateRoomId() {
return '!' + Math.random().toString().slice(2, 10) + ':domain';
}
2021-08-09 09:06:06 +02:00
interface ITestContent extends IContent {
expectedText: string;
}
2021-08-03 11:06:21 +02:00
describe('export', function() {
stubClient();
client = MatrixClientPeg.get();
client.getUserId = () => {
return MY_USER_ID;
};
2021-08-04 09:09:35 +02:00
const mockExportOptions: IExportOptions = {
numberOfMessages: 5,
maxSize: 100 * 1024 * 1024,
attachmentsIncluded: false,
};
2021-08-03 11:23:23 +02:00
const invalidExportOptions: IExportOptions[] = [
{
numberOfMessages: 10**9,
maxSize: 1024 * 1024 * 1024,
attachmentsIncluded: false,
},
{
numberOfMessages: -1,
maxSize: 4096 * 1024 * 1024,
attachmentsIncluded: false,
},
{
numberOfMessages: 0,
maxSize: 0,
attachmentsIncluded: false,
},
];
2021-08-03 11:06:21 +02:00
function createRoom() {
const room = new Room(generateRoomId(), null, client.getUserId());
return room;
}
2021-08-10 08:55:11 +02:00
const mockRoom = createRoom();
2021-08-03 11:06:21 +02:00
2021-08-13 20:14:07 +02:00
const ts0 = Date.now();
function mkRedactedEvent(i = 0) {
return new MatrixEvent({
type: "m.room.message",
sender: MY_USER_ID,
content: {},
unsigned: {
"age": 72,
"transaction_id": "m1212121212.23",
"redacted_because": {
"content": {},
"origin_server_ts": ts0 + i*1000,
"redacts": "$9999999999999999999999999999999999999999998",
"sender": "@me:here",
"type": "m.room.redaction",
"unsigned": {
"age": 94,
"transaction_id": "m1111111111.1",
},
"event_id": "$9999999999999999999999999999999999999999998",
"room_id": mockRoom.roomId,
},
},
event_id: "$9999999999999999999999999999999999999999999",
room_id: mockRoom.roomId,
});
}
function mkFileEvent() {
return new MatrixEvent({
"content": {
"body": "index.html",
"info": {
"mimetype": "text/html",
"size": 31613,
},
"msgtype": "m.file",
"url": "mxc://test.org",
},
"origin_server_ts": 1628872988364,
"sender": MY_USER_ID,
"type": "m.room.message",
"unsigned": {
"age": 266,
"transaction_id": "m99999999.2",
},
"event_id": "$99999999999999999999",
"room_id": mockRoom.roomId,
});
}
2021-08-03 11:06:21 +02:00
function mkEvents() {
2021-08-10 08:55:11 +02:00
const matrixEvents = [];
let i: number;
// plain text
for (i = 0; i < 10; i++) {
matrixEvents.push(TestUtilsMatrix.mkMessage({
2021-08-09 09:06:06 +02:00
event: true, room: "!room:id", user: "@user:id",
ts: ts0 + i * 1000,
2021-08-03 11:06:21 +02:00
}));
}
2021-08-10 08:55:11 +02:00
// reply events
for (i = 0; i < 10; i++) {
matrixEvents.push(TestUtilsMatrix.mkEvent({
"content": {
"body": "> <@me:here> Hi\n\nTest",
"format": "org.matrix.custom.html",
"m.relates_to": {
"m.in_reply_to": {
"event_id": "$" + Math.random() + "-" + Math.random(),
},
},
"msgtype": "m.text",
},
"user": "@me:here",
"type": "m.room.message",
"room": mockRoom.roomId,
"event": true,
}));
}
// membership events
for (i = 0; i < 10; i++) {
matrixEvents.push(TestUtilsMatrix.mkMembership({
event: true, room: "!room:id", user: "@user:id",
target: {
userId: "@user:id",
name: "Bob",
getAvatarUrl: () => {
return "avatar.jpeg";
},
getMxcAvatarUrl: () => 'mxc://avatar.url/image.png',
},
ts: ts0 + i*1000,
mship: 'join',
prevMship: 'join',
name: 'A user',
}));
}
// emote
matrixEvents.push(TestUtilsMatrix.mkEvent({
"content": {
"body": "waves",
"msgtype": "m.emote",
},
"user": "@me:here",
"type": "m.room.message",
"room": mockRoom.roomId,
"event": true,
}));
// redacted events
for (i = 0; i < 10; i++) {
2021-08-13 20:14:07 +02:00
matrixEvents.push(mkRedactedEvent(i));
2021-08-10 08:55:11 +02:00
}
return matrixEvents;
2021-08-03 11:06:21 +02:00
}
2021-08-10 08:55:11 +02:00
const events: MatrixEvent[] = mkEvents();
2021-08-03 11:06:21 +02:00
it('checks if the export format is valid', function() {
2021-08-13 20:14:07 +02:00
function isValidFormat(format: string): boolean {
const options: string[] = Object.values(ExportFormat);
return options.includes(format);
}
expect(isValidFormat("Html")).toBeTruthy();
expect(isValidFormat("Json")).toBeTruthy();
expect(isValidFormat("PlainText")).toBeTruthy();
expect(isValidFormat("Pdf")).toBeFalsy();
});
it("checks if the icons' html corresponds to export regex", function() {
const exporter = new HTMLExporter(mockRoom, ExportType.Beginning, mockExportOptions, null);
const fileRegex = /<span class="mx_MFileBody_info_icon">.*?<\/span>/;
expect(fileRegex.test(
renderToString(exporter.getEventTile(mkFileEvent(), true))),
).toBeTruthy();
2021-08-03 11:06:21 +02:00
});
it('checks if the export options are valid', function() {
2021-08-03 11:23:23 +02:00
for (const exportOption of invalidExportOptions) {
2021-08-13 05:04:54 +02:00
expect(
() =>
new PlainTextExporter(mockRoom, ExportType.Beginning, exportOption, null),
).toThrowError("Invalid export options");
2021-08-03 11:23:23 +02:00
}
2021-08-03 11:06:21 +02:00
});
2021-08-04 09:09:35 +02:00
it('tests the file extension splitter', function() {
2021-08-13 05:00:50 +02:00
const exporter = new PlainTextExporter(mockRoom, ExportType.Beginning, mockExportOptions, null);
2021-08-04 09:09:35 +02:00
const fileNameWithExtensions = {
"": ["", ""],
"name": ["name", ""],
"name.txt": ["name", ".txt"],
".htpasswd": ["", ".htpasswd"],
"name.with.many.dots.myext": ["name.with.many.dots", ".myext"],
};
for (const fileName in fileNameWithExtensions) {
expect(exporter.splitFileName(fileName)).toStrictEqual(fileNameWithExtensions[fileName]);
}
});
2021-08-09 09:06:06 +02:00
it('checks if the reply regex executes correctly', function() {
const eventContents: ITestContent[] = [
{
"msgtype": "m.text",
"body": "> <@me:here> Source\n\nReply",
"expectedText": "<@me:here \"Source\"> Reply",
},
{
"msgtype": "m.text",
2021-08-09 09:54:54 +02:00
// if the reply format is invalid, then return the body
2021-08-09 09:06:06 +02:00
"body": "Invalid reply format",
"expectedText": "Invalid reply format",
},
{
"msgtype": "m.text",
"body": "> <@me:here> The source is more than 32 characters\n\nReply",
"expectedText": "<@me:here \"The source is more than 32 chara...\"> Reply",
},
{
"msgtype": "m.text",
"body": "> <@me:here> This\nsource\nhas\nnew\nlines\n\nReply",
"expectedText": "<@me:here \"This\"> Reply",
},
];
2021-08-13 05:00:50 +02:00
const exporter = new PlainTextExporter(mockRoom, ExportType.Beginning, mockExportOptions, null);
2021-08-09 09:06:06 +02:00
for (const content of eventContents) {
expect(exporter.textForReplyEvent(content)).toBe(content.expectedText);
}
});
2021-08-09 09:54:54 +02:00
2021-08-10 09:52:40 +02:00
it("checks if the render to string doesn't throw any error for different types of events", function() {
2021-08-13 05:00:50 +02:00
const exporter = new HTMLExporter(mockRoom, ExportType.Beginning, mockExportOptions, null);
2021-08-09 09:54:54 +02:00
for (const event of events) {
expect(renderToString(exporter.getEventTile(event, false))).toBeTruthy();
}
});
2021-08-03 11:06:21 +02:00
});