2021-08-03 09:06:21 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-08-03 09:06:21 +00:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-08-03 09:06:21 +00:00
|
|
|
*/
|
|
|
|
|
2024-10-14 16:11:58 +00:00
|
|
|
import { render } from "jest-matrix-react";
|
2022-03-17 17:38:29 +00:00
|
|
|
import {
|
|
|
|
IContent,
|
|
|
|
MatrixClient,
|
|
|
|
MatrixEvent,
|
|
|
|
Room,
|
|
|
|
RoomMember,
|
|
|
|
RelationType,
|
|
|
|
EventType,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-03-18 14:40:52 +00:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
|
|
|
import { IExportOptions, ExportType, ExportFormat } from "../../../src/utils/exportUtils/exportUtils";
|
|
|
|
import PlainTextExporter from "../../../src/utils/exportUtils/PlainTextExport";
|
|
|
|
import HTMLExporter from "../../../src/utils/exportUtils/HtmlExport";
|
|
|
|
import * as TestUtilsMatrix from "../../test-utils";
|
|
|
|
import { stubClient } from "../../test-utils";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2021-08-03 09:06:21 +00:00
|
|
|
let client: MatrixClient;
|
|
|
|
|
|
|
|
const MY_USER_ID = "@me:here";
|
|
|
|
|
|
|
|
function generateRoomId() {
|
2022-12-12 11:24:14 +00:00
|
|
|
return "!" + Math.random().toString().slice(2, 10) + ":domain";
|
2021-08-03 09:06:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 07:06:06 +00:00
|
|
|
interface ITestContent extends IContent {
|
|
|
|
expectedText: string;
|
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("export", function () {
|
2023-02-16 17:21:44 +00:00
|
|
|
const setProgressText = jest.fn();
|
|
|
|
|
2022-12-13 15:09:15 +00:00
|
|
|
let mockExportOptions: IExportOptions;
|
|
|
|
let mockRoom: Room;
|
|
|
|
let ts0: number;
|
|
|
|
let events: MatrixEvent[];
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
2023-06-05 17:12:23 +00:00
|
|
|
client = MatrixClientPeg.safeGet();
|
2022-12-13 15:09:15 +00:00
|
|
|
client.getUserId = () => {
|
|
|
|
return MY_USER_ID;
|
|
|
|
};
|
|
|
|
|
|
|
|
mockExportOptions = {
|
|
|
|
numberOfMessages: 5,
|
|
|
|
maxSize: 100 * 1024 * 1024,
|
|
|
|
attachmentsIncluded: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
function createRoom() {
|
2023-02-16 17:21:44 +00:00
|
|
|
const room = new Room(generateRoomId(), client, client.getUserId()!);
|
2022-12-13 15:09:15 +00:00
|
|
|
return room;
|
|
|
|
}
|
|
|
|
mockRoom = createRoom();
|
|
|
|
ts0 = Date.now();
|
|
|
|
events = mkEvents();
|
|
|
|
jest.spyOn(client, "getRoom").mockReturnValue(mockRoom);
|
|
|
|
});
|
2021-08-13 18:14:07 +00:00
|
|
|
|
|
|
|
function mkRedactedEvent(i = 0) {
|
|
|
|
return new MatrixEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
sender: MY_USER_ID,
|
|
|
|
content: {},
|
|
|
|
unsigned: {
|
2022-12-12 11:24:14 +00:00
|
|
|
age: 72,
|
|
|
|
transaction_id: "m1212121212.23",
|
|
|
|
redacted_because: {
|
|
|
|
content: {},
|
|
|
|
origin_server_ts: ts0 + i * 1000,
|
|
|
|
redacts: "$9999999999999999999999999999999999999999998",
|
|
|
|
sender: "@me:here",
|
|
|
|
type: EventType.RoomRedaction,
|
|
|
|
unsigned: {
|
|
|
|
age: 94,
|
|
|
|
transaction_id: "m1111111111.1",
|
2021-08-13 18:14:07 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
event_id: "$9999999999999999999999999999999999999999998",
|
|
|
|
room_id: mockRoom.roomId,
|
2021-08-13 18:14:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
event_id: "$9999999999999999999999999999999999999999999",
|
|
|
|
room_id: mockRoom.roomId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mkFileEvent() {
|
|
|
|
return new MatrixEvent({
|
2022-12-12 11:24:14 +00:00
|
|
|
content: {
|
|
|
|
body: "index.html",
|
|
|
|
info: {
|
|
|
|
mimetype: "text/html",
|
|
|
|
size: 31613,
|
2021-08-13 18:14:07 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
msgtype: "m.file",
|
|
|
|
url: "mxc://test.org",
|
2021-08-13 18:14:07 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
origin_server_ts: 1628872988364,
|
|
|
|
sender: MY_USER_ID,
|
|
|
|
type: "m.room.message",
|
|
|
|
unsigned: {
|
|
|
|
age: 266,
|
|
|
|
transaction_id: "m99999999.2",
|
2021-08-13 18:14:07 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
event_id: "$99999999999999999999",
|
|
|
|
room_id: mockRoom.roomId,
|
2021-08-13 18:14:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-08 10:00:58 +00:00
|
|
|
function mkImageEvent() {
|
|
|
|
return new MatrixEvent({
|
2022-12-12 11:24:14 +00:00
|
|
|
content: {
|
|
|
|
body: "image.png",
|
|
|
|
info: {
|
|
|
|
mimetype: "image/png",
|
|
|
|
size: 31613,
|
2022-09-08 10:00:58 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
msgtype: "m.image",
|
|
|
|
url: "mxc://test.org",
|
2022-09-08 10:00:58 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
origin_server_ts: 1628872988364,
|
|
|
|
sender: MY_USER_ID,
|
|
|
|
type: "m.room.message",
|
|
|
|
unsigned: {
|
|
|
|
age: 266,
|
|
|
|
transaction_id: "m99999999.2",
|
2022-09-08 10:00:58 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
event_id: "$99999999999999999999",
|
|
|
|
room_id: mockRoom.roomId,
|
2022-09-08 10:00:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:06:21 +00:00
|
|
|
function mkEvents() {
|
2023-02-16 17:21:44 +00:00
|
|
|
const matrixEvents: MatrixEvent[] = [];
|
2021-08-10 06:55:11 +00:00
|
|
|
let i: number;
|
|
|
|
// plain text
|
|
|
|
for (i = 0; i < 10; i++) {
|
2022-12-12 11:24:14 +00:00
|
|
|
matrixEvents.push(
|
|
|
|
TestUtilsMatrix.mkMessage({
|
|
|
|
event: true,
|
|
|
|
room: "!room:id",
|
|
|
|
user: "@user:id",
|
|
|
|
ts: ts0 + i * 1000,
|
|
|
|
}),
|
|
|
|
);
|
2021-08-03 09:06:21 +00:00
|
|
|
}
|
2021-08-10 06:55:11 +00:00
|
|
|
// reply events
|
|
|
|
for (i = 0; i < 10; i++) {
|
2022-03-10 18:15:31 +00:00
|
|
|
const eventId = "$" + Math.random() + "-" + Math.random();
|
2022-12-12 11:24:14 +00:00
|
|
|
matrixEvents.push(
|
|
|
|
TestUtilsMatrix.mkEvent({
|
|
|
|
content: {
|
|
|
|
"body": "> <@me:here> Hi\n\nTest",
|
|
|
|
"format": "org.matrix.custom.html",
|
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": RelationType.Reference,
|
2022-03-10 18:15:31 +00:00
|
|
|
"event_id": eventId,
|
2022-12-12 11:24:14 +00:00
|
|
|
"m.in_reply_to": {
|
|
|
|
event_id: eventId,
|
|
|
|
},
|
2021-08-10 06:55:11 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
"msgtype": "m.text",
|
2021-08-10 06:55:11 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
user: "@me:here",
|
|
|
|
type: "m.room.message",
|
|
|
|
room: mockRoom.roomId,
|
|
|
|
event: true,
|
|
|
|
}),
|
|
|
|
);
|
2021-08-10 06:55:11 +00:00
|
|
|
}
|
|
|
|
// membership events
|
|
|
|
for (i = 0; i < 10; i++) {
|
2022-12-12 11:24:14 +00:00
|
|
|
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",
|
|
|
|
} as unknown as RoomMember,
|
|
|
|
ts: ts0 + i * 1000,
|
2024-03-12 14:52:54 +00:00
|
|
|
mship: KnownMembership.Join,
|
|
|
|
prevMship: KnownMembership.Join,
|
2022-12-12 11:24:14 +00:00
|
|
|
name: "A user",
|
|
|
|
}),
|
|
|
|
);
|
2021-08-10 06:55:11 +00:00
|
|
|
}
|
|
|
|
// emote
|
2022-12-12 11:24:14 +00:00
|
|
|
matrixEvents.push(
|
|
|
|
TestUtilsMatrix.mkEvent({
|
|
|
|
content: {
|
|
|
|
body: "waves",
|
|
|
|
msgtype: "m.emote",
|
|
|
|
},
|
|
|
|
user: "@me:here",
|
|
|
|
type: "m.room.message",
|
|
|
|
room: mockRoom.roomId,
|
|
|
|
event: true,
|
|
|
|
}),
|
|
|
|
);
|
2021-08-10 06:55:11 +00:00
|
|
|
// redacted events
|
|
|
|
for (i = 0; i < 10; i++) {
|
2021-08-13 18:14:07 +00:00
|
|
|
matrixEvents.push(mkRedactedEvent(i));
|
2021-08-10 06:55:11 +00:00
|
|
|
}
|
|
|
|
return matrixEvents;
|
2021-08-03 09:06:21 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 08:58:23 +00:00
|
|
|
function renderToString(elem: JSX.Element): string {
|
|
|
|
return render(elem).container.outerHTML;
|
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("checks if the export format is valid", function () {
|
2021-08-13 18:14:07 +00: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();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("checks if the icons' html corresponds to export regex", function () {
|
2023-02-16 17:21:44 +00:00
|
|
|
const exporter = new HTMLExporter(mockRoom, ExportType.Beginning, mockExportOptions, setProgressText);
|
2021-08-13 18:14:07 +00:00
|
|
|
const fileRegex = /<span class="mx_MFileBody_info_icon">.*?<\/span>/;
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(fileRegex.test(renderToString(exporter.getEventTile(mkFileEvent(), true)))).toBeTruthy();
|
2022-09-08 10:00:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should export images if attachments are enabled", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const exporter = new HTMLExporter(
|
|
|
|
mockRoom,
|
|
|
|
ExportType.Beginning,
|
|
|
|
{
|
|
|
|
numberOfMessages: 5,
|
|
|
|
maxSize: 100 * 1024 * 1024,
|
|
|
|
attachmentsIncluded: true,
|
|
|
|
},
|
2023-02-16 17:21:44 +00:00
|
|
|
setProgressText,
|
2022-12-12 11:24:14 +00:00
|
|
|
);
|
2023-02-28 08:58:23 +00:00
|
|
|
const imageRegex = /<img.+ src="mxc:\/\/test.org" alt="image\.png"\/?>/;
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(imageRegex.test(renderToString(exporter.getEventTile(mkImageEvent(), true)))).toBeTruthy();
|
2021-08-03 09:06:21 +00:00
|
|
|
});
|
|
|
|
|
2022-01-31 11:54:14 +00:00
|
|
|
const invalidExportOptions: [string, IExportOptions][] = [
|
2022-12-12 11:24:14 +00:00
|
|
|
[
|
|
|
|
"numberOfMessages exceeds max",
|
|
|
|
{
|
|
|
|
numberOfMessages: 10 ** 9,
|
|
|
|
maxSize: 1024 * 1024 * 1024,
|
|
|
|
attachmentsIncluded: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"maxSize exceeds 8GB",
|
|
|
|
{
|
|
|
|
numberOfMessages: -1,
|
|
|
|
maxSize: 8001 * 1024 * 1024,
|
|
|
|
attachmentsIncluded: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"maxSize is less than 1mb",
|
|
|
|
{
|
|
|
|
numberOfMessages: 0,
|
|
|
|
maxSize: 0,
|
|
|
|
attachmentsIncluded: false,
|
|
|
|
},
|
|
|
|
],
|
2022-01-31 11:54:14 +00:00
|
|
|
];
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(invalidExportOptions)("%s", (_d, options) => {
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(() => new PlainTextExporter(mockRoom, ExportType.Beginning, options, setProgressText)).toThrow(
|
2022-12-12 11:24:14 +00:00
|
|
|
"Invalid export options",
|
|
|
|
);
|
2021-08-03 09:06:21 +00:00
|
|
|
});
|
2021-08-04 07:09:35 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("tests the file extension splitter", function () {
|
2023-02-16 17:21:44 +00:00
|
|
|
const exporter = new PlainTextExporter(mockRoom, ExportType.Beginning, mockExportOptions, setProgressText);
|
2023-02-13 11:39:16 +00:00
|
|
|
const fileNameWithExtensions: Record<string, [string, string]> = {
|
2021-08-04 07:09:35 +00:00
|
|
|
"": ["", ""],
|
|
|
|
"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]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("checks if the reply regex executes correctly", function () {
|
2021-08-09 07:06:06 +00:00
|
|
|
const eventContents: ITestContent[] = [
|
|
|
|
{
|
2022-12-12 11:24:14 +00:00
|
|
|
msgtype: "m.text",
|
|
|
|
body: "> <@me:here> Source\n\nReply",
|
|
|
|
expectedText: '<@me:here "Source"> Reply',
|
2021-08-09 07:06:06 +00:00
|
|
|
},
|
|
|
|
{
|
2022-12-12 11:24:14 +00:00
|
|
|
msgtype: "m.text",
|
2021-08-09 07:54:54 +00:00
|
|
|
// if the reply format is invalid, then return the body
|
2022-12-12 11:24:14 +00:00
|
|
|
body: "Invalid reply format",
|
|
|
|
expectedText: "Invalid reply format",
|
2021-08-09 07:06:06 +00:00
|
|
|
},
|
|
|
|
{
|
2022-12-12 11:24:14 +00:00
|
|
|
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',
|
2021-08-09 07:06:06 +00:00
|
|
|
},
|
|
|
|
{
|
2022-12-12 11:24:14 +00:00
|
|
|
msgtype: "m.text",
|
|
|
|
body: "> <@me:here> This\nsource\nhas\nnew\nlines\n\nReply",
|
|
|
|
expectedText: '<@me:here "This"> Reply',
|
2021-08-09 07:06:06 +00:00
|
|
|
},
|
|
|
|
];
|
2023-02-16 17:21:44 +00:00
|
|
|
const exporter = new PlainTextExporter(mockRoom, ExportType.Beginning, mockExportOptions, setProgressText);
|
2021-08-09 07:06:06 +00:00
|
|
|
for (const content of eventContents) {
|
|
|
|
expect(exporter.textForReplyEvent(content)).toBe(content.expectedText);
|
|
|
|
}
|
|
|
|
});
|
2021-08-09 07:54:54 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("checks if the render to string doesn't throw any error for different types of events", function () {
|
2023-02-16 17:21:44 +00:00
|
|
|
const exporter = new HTMLExporter(mockRoom, ExportType.Beginning, mockExportOptions, setProgressText);
|
2021-08-09 07:54:54 +00:00
|
|
|
for (const event of events) {
|
|
|
|
expect(renderToString(exporter.getEventTile(event, false))).toBeTruthy();
|
|
|
|
}
|
|
|
|
});
|
2021-08-03 09:06:21 +00:00
|
|
|
});
|