Apply strictNullChecks to src/utils/exportUtils (#10379)

* Apply `strictNullChecks` to `src/utils/exportUtils`

* strict fix

* test coverage

* lint

* test coverage

* one more test
This commit is contained in:
Kerry 2023-03-30 10:47:07 +13:00 committed by GitHub
parent 1447829543
commit 9a733a6444
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 178 additions and 9 deletions

View file

@ -14,7 +14,16 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { EventType, IRoomEvent, MatrixClient, MatrixEvent, MsgType, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import {
EventType,
IRoomEvent,
MatrixClient,
MatrixEvent,
MsgType,
Room,
RoomMember,
RoomState,
} from "matrix-js-sdk/src/matrix";
import fetchMock from "fetch-mock-jest";
import { filterConsole, mkStubRoom, REPEATABLE_DATE, stubClient } from "../../test-utils";
@ -51,6 +60,20 @@ const EVENT_ATTACHMENT: IRoomEvent = {
},
};
const EVENT_ATTACHMENT_MALFORMED: IRoomEvent = {
event_id: "$2",
type: EventType.RoomMessage,
sender: "@alice:example.com",
origin_server_ts: 1,
content: {
msgtype: MsgType.File,
body: "hello.txt",
file: {
url: undefined,
},
},
};
describe("HTMLExport", () => {
let client: jest.Mocked<MatrixClient>;
let room: Room;
@ -107,6 +130,22 @@ describe("HTMLExport", () => {
fetchMock.get(media.srcHttp!, body);
}
it("should throw when created with invalid config for LastNMessages", async () => {
expect(
() =>
new HTMLExporter(
room,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: undefined,
},
() => {},
),
).toThrow("Invalid export options");
});
it("should have an SDK-branded destination file name", () => {
const roomName = "My / Test / Room: Welcome";
const stubOptions: IExportOptions = {
@ -266,6 +305,56 @@ describe("HTMLExport", () => {
expect(await file.text()).toBe(avatarContent);
});
it("should handle when an event has no sender", async () => {
const EVENT_MESSAGE_NO_SENDER: IRoomEvent = {
event_id: "$1",
type: EventType.RoomMessage,
sender: "",
origin_server_ts: 0,
content: {
msgtype: "m.text",
body: "Message with no sender",
},
};
mockMessages(EVENT_MESSAGE_NO_SENDER);
const exporter = new HTMLExporter(
room,
ExportType.Timeline,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
},
() => {},
);
await exporter.export();
const file = getMessageFile(exporter);
expect(await file.text()).toContain(EVENT_MESSAGE_NO_SENDER.content.body);
});
it("should handle when events sender cannot be found in room state", async () => {
mockMessages(EVENT_MESSAGE);
jest.spyOn(RoomState.prototype, "getSentinelMember").mockReturnValue(null);
const exporter = new HTMLExporter(
room,
ExportType.Timeline,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
},
() => {},
);
await exporter.export();
const file = getMessageFile(exporter);
expect(await file.text()).toContain(EVENT_MESSAGE.content.body);
});
it("should include attachments", async () => {
mockMessages(EVENT_MESSAGE, EVENT_ATTACHMENT);
const attachmentBody = "Lorem ipsum dolor sit amet";
@ -294,6 +383,68 @@ describe("HTMLExport", () => {
expect(text).toBe(attachmentBody);
});
it("should handle when attachment cannot be fetched", async () => {
mockMessages(EVENT_MESSAGE, EVENT_ATTACHMENT_MALFORMED, EVENT_ATTACHMENT);
const attachmentBody = "Lorem ipsum dolor sit amet";
mockMxc("mxc://example.org/test-id", attachmentBody);
const exporter = new HTMLExporter(
room,
ExportType.Timeline,
{
attachmentsIncluded: true,
maxSize: 1_024 * 1_024,
},
() => {},
);
await exporter.export();
// good attachment present
const files = getFiles(exporter);
const file = files[Object.keys(files).find((k) => k.endsWith(".txt"))!];
expect(file).not.toBeUndefined();
// Ensure that the attachment has the expected content
const text = await file.text();
expect(text).toBe(attachmentBody);
// messages export still successful
const messagesFile = getMessageFile(exporter);
expect(await messagesFile.text()).toBeTruthy();
});
it("should handle when attachment srcHttp is falsy", async () => {
mockMessages(EVENT_MESSAGE, EVENT_ATTACHMENT);
const attachmentBody = "Lorem ipsum dolor sit amet";
mockMxc("mxc://example.org/test-id", attachmentBody);
jest.spyOn(client, "mxcUrlToHttp").mockReturnValue(null);
const exporter = new HTMLExporter(
room,
ExportType.Timeline,
{
attachmentsIncluded: true,
maxSize: 1_024 * 1_024,
},
() => {},
);
await exporter.export();
// attachment not present
const files = getFiles(exporter);
const file = files[Object.keys(files).find((k) => k.endsWith(".txt"))!];
expect(file).toBeUndefined();
// messages export still successful
const messagesFile = getMessageFile(exporter);
expect(await messagesFile.text()).toBeTruthy();
});
it("should omit attachments", async () => {
mockMessages(EVENT_MESSAGE, EVENT_ATTACHMENT);
@ -323,6 +474,7 @@ describe("HTMLExport", () => {
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 5000,
},
() => {},
);