Add a test file
This commit is contained in:
parent
46e2f67f54
commit
4824c93707
5 changed files with 112 additions and 25 deletions
|
@ -42,6 +42,9 @@ export default abstract class Exporter {
|
||||||
protected exportOptions: IExportOptions,
|
protected exportOptions: IExportOptions,
|
||||||
protected exportProgressRef: MutableRefObject<HTMLParagraphElement>,
|
protected exportProgressRef: MutableRefObject<HTMLParagraphElement>,
|
||||||
) {
|
) {
|
||||||
|
if (exportOptions.maxSize < 1 || exportOptions.maxSize > 2000 || exportOptions.numberOfMessages > 10**8) {
|
||||||
|
throw new Error("Invalid export options");
|
||||||
|
}
|
||||||
this.cancelled = false;
|
this.cancelled = false;
|
||||||
this.files = [];
|
this.files = [];
|
||||||
this.client = MatrixClientPeg.get();
|
this.client = MatrixClientPeg.get();
|
||||||
|
@ -109,7 +112,7 @@ export default abstract class Exporter {
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getLimit(): number {
|
public getLimit(): number {
|
||||||
let limit: number;
|
let limit: number;
|
||||||
switch (this.exportType) {
|
switch (this.exportType) {
|
||||||
case ExportTypes.LAST_N_MESSAGES:
|
case ExportTypes.LAST_N_MESSAGES:
|
||||||
|
@ -152,11 +155,11 @@ export default abstract class Exporter {
|
||||||
const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper);
|
const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper);
|
||||||
|
|
||||||
for (const mxEv of matrixEvents) {
|
for (const mxEv of matrixEvents) {
|
||||||
if (this.exportOptions.startDate && mxEv.getTs() < this.exportOptions.startDate) {
|
// if (this.exportOptions.startDate && mxEv.getTs() < this.exportOptions.startDate) {
|
||||||
// Once the last message received is older than the start date, we break out of both the loops
|
// // Once the last message received is older than the start date, we break out of both the loops
|
||||||
limit = 0;
|
// limit = 0;
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
events.push(mxEv);
|
events.push(mxEv);
|
||||||
}
|
}
|
||||||
this.updateProgress(
|
this.updateProgress(
|
||||||
|
@ -208,7 +211,7 @@ export default abstract class Exporter {
|
||||||
return blob;
|
return blob;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected splitFileName(file: string): string[] {
|
public splitFileName(file: string): string[] {
|
||||||
const lastDot = file.lastIndexOf('.');
|
const lastDot = file.lastIndexOf('.');
|
||||||
if (lastDot === -1) return [file, ""];
|
if (lastDot === -1) return [file, ""];
|
||||||
const fileName = file.slice(0, lastDot);
|
const fileName = file.slice(0, lastDot);
|
||||||
|
@ -216,7 +219,7 @@ export default abstract class Exporter {
|
||||||
return [fileName, '.' + ext];
|
return [fileName, '.' + ext];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getFilePath(event: MatrixEvent): string {
|
public getFilePath(event: MatrixEvent): string {
|
||||||
const mediaType = event.getContent().msgtype;
|
const mediaType = event.getContent().msgtype;
|
||||||
let fileDirectory: string;
|
let fileDirectory: string;
|
||||||
switch (mediaType) {
|
switch (mediaType) {
|
||||||
|
|
|
@ -37,6 +37,8 @@ export const textForFormat = (format: string): string => {
|
||||||
return _t("JSON");
|
return _t("JSON");
|
||||||
case ExportFormats.PLAIN_TEXT:
|
case ExportFormats.PLAIN_TEXT:
|
||||||
return _t("Plain Text");
|
return _t("Plain Text");
|
||||||
|
default:
|
||||||
|
throw new Error("Unknown format");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -48,13 +50,15 @@ export const textForType = (type: string): string => {
|
||||||
return _t("Specify a number of messages");
|
return _t("Specify a number of messages");
|
||||||
case ExportTypes.TIMELINE:
|
case ExportTypes.TIMELINE:
|
||||||
return _t("Current Timeline");
|
return _t("Current Timeline");
|
||||||
|
default:
|
||||||
|
throw new Error("Unknown type: " + type);
|
||||||
// case exportTypes.START_DATE:
|
// case exportTypes.START_DATE:
|
||||||
// return _t("From a specific date");
|
// return _t("From a specific date");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface IExportOptions {
|
export interface IExportOptions {
|
||||||
startDate?: number;
|
// startDate?: number;
|
||||||
numberOfMessages?: number;
|
numberOfMessages?: number;
|
||||||
attachmentsIncluded: boolean;
|
attachmentsIncluded: boolean;
|
||||||
maxSize: number;
|
maxSize: number;
|
||||||
|
|
|
@ -202,8 +202,9 @@ export function mkMembership(opts) {
|
||||||
* @param {Object} opts Values for the message
|
* @param {Object} opts Values for the message
|
||||||
* @param {string} opts.room The room ID for the event.
|
* @param {string} opts.room The room ID for the event.
|
||||||
* @param {string} opts.user The user ID for the event.
|
* @param {string} opts.user The user ID for the event.
|
||||||
* @param {string} opts.msg Optional. The content.body for the event.
|
* @param {number} opts.ts The timestamp for the event.
|
||||||
* @param {boolean} opts.event True to make a MatrixEvent.
|
* @param {boolean} opts.event True to make a MatrixEvent.
|
||||||
|
* @param {string=} opts.msg Optional. The content.body for the event.
|
||||||
* @return {Object|MatrixEvent} The event
|
* @return {Object|MatrixEvent} The event
|
||||||
*/
|
*/
|
||||||
export function mkMessage(opts) {
|
export function mkMessage(opts) {
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
/*
|
|
||||||
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.
|
|
||||||
*/
|
|
94
test/utils/export-test.tsx
Normal file
94
test/utils/export-test.tsx
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { MatrixClient, Room } from "matrix-js-sdk";
|
||||||
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
||||||
|
import { textForFormat } from "../../src/utils/exportUtils/exportUtils";
|
||||||
|
// import HTMLExporter from "../../src/utils/exportUtils/HtmlExport";
|
||||||
|
// import PlainTextExporter from "../../src/utils/exportUtils/PlainTextExport";
|
||||||
|
import * as TestUtilsMatrix from '../test-utils';
|
||||||
|
import { stubClient } from '../test-utils';
|
||||||
|
|
||||||
|
let client: MatrixClient;
|
||||||
|
|
||||||
|
const MY_USER_ID = "@me:here";
|
||||||
|
|
||||||
|
function generateRoomId() {
|
||||||
|
return '!' + Math.random().toString().slice(2, 10) + ':domain';
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('export', function() {
|
||||||
|
stubClient();
|
||||||
|
client = MatrixClientPeg.get();
|
||||||
|
client.getUserId = () => {
|
||||||
|
return MY_USER_ID;
|
||||||
|
};
|
||||||
|
|
||||||
|
// const invalidExportOptions: IExportOptions[] = [
|
||||||
|
// {
|
||||||
|
// numberOfMessages: 10**9,
|
||||||
|
// maxSize: 1024,
|
||||||
|
// attachmentsIncluded: false,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// numberOfMessages: -1,
|
||||||
|
// maxSize: 4096,
|
||||||
|
// attachmentsIncluded: false,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// numberOfMessages: 0,
|
||||||
|
// maxSize: 1024,
|
||||||
|
// attachmentsIncluded: false,
|
||||||
|
// },
|
||||||
|
// ];
|
||||||
|
|
||||||
|
const events = mkEvents();
|
||||||
|
const room = createRoom();
|
||||||
|
console.log(events, room);
|
||||||
|
function createRoom() {
|
||||||
|
const room = new Room(generateRoomId(), null, client.getUserId());
|
||||||
|
return room;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mkEvents() {
|
||||||
|
const events = [];
|
||||||
|
const ts0 = Date.now();
|
||||||
|
for (let i = 0; i < 10; i++) {
|
||||||
|
events.push(TestUtilsMatrix.mkMessage({
|
||||||
|
event: true, room: "!room:id", user: "@user:id",
|
||||||
|
ts: ts0 + i * 1000,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
return events;
|
||||||
|
}
|
||||||
|
|
||||||
|
it('checks if the export format is valid', function() {
|
||||||
|
expect(textForFormat('HTML')).toBeTruthy();
|
||||||
|
expect(textForFormat('JSON')).toBeTruthy();
|
||||||
|
expect(textForFormat('PLAIN_TEXT')).toBeTruthy();
|
||||||
|
try {
|
||||||
|
textForFormat('PDF');
|
||||||
|
throw new Error("Expected to throw an error");
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.message).toBe("Unknown format");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('checks if the export options are valid', function() {
|
||||||
|
// const html = new PlainTextExporter(room, ExportTypes.BEGINNING, invalidExportOptions[0], null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue