diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index 65ea4b77fd6961bcc617fb96e1ca2a803a090d25..8005d0041894d53c93bd1e5c758cd3b67cd57730 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -211,7 +211,7 @@ export class MatrixEventProcessor { return { attachment, name, - }; + } as Discord.FileOptions; } } return `[${name}](${url})`; diff --git a/test/test_clientfactory.ts b/test/test_clientfactory.ts index f507e16d5945b3112db68cda311becdb67596bbd..95ba37a6b63518b0435a08c818c6b42a41c851dc 100644 --- a/test/test_clientfactory.ts +++ b/test/test_clientfactory.ts @@ -35,71 +35,88 @@ const STORE = { describe("ClientFactory", () => { describe("init", () => { - it ("should start successfully", () => { - const config = new DiscordBridgeConfigAuth(); - config.botToken = "passme"; - const cf = new DiscordClientFactory(null, config); - return expect(cf.init()).to.eventually.be.fulfilled; - }); - it ("should fail if a config is not supplied", () => { - const cf = new DiscordClientFactory(null); - return expect(cf.init()).to.eventually.be.rejected; - }); - it ("should fail if the bot fails to connect", () => { - const config = new DiscordBridgeConfigAuth(); - config.botToken = "failme"; - const cf = new DiscordClientFactory(null, config); - return expect(cf.init()).to.eventually.be.rejected; - }); + it ("should start successfully", async () => { + const config = new DiscordBridgeConfigAuth(); + config.botToken = "passme"; + const cf = new DiscordClientFactory(null, config); + await cf.init(); + }); + it ("should fail if a config is not supplied", async () => { + const cf = new DiscordClientFactory(null); + try { + await cf.init(); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } + }); + it ("should fail if the bot fails to connect", async () => { + const config = new DiscordBridgeConfigAuth(); + config.botToken = "failme"; + const cf = new DiscordClientFactory(null, config); + try { + await cf.init(); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } + }); }); describe("getDiscordId", () => { - it("should fetch id successfully", () => { + it("should fetch id successfully", async () => { const config = new DiscordBridgeConfigAuth(); const cf = new DiscordClientFactory(null); - return expect(cf.getDiscordId("passme")).to.eventually.equal("12345"); + const discordId = await cf.getDiscordId("passme"); + expect(discordId).equals("12345"); }); - it("should fail if the token is not recognised", () => { + it("should fail if the token is not recognised", async () => { const config = new DiscordBridgeConfigAuth(); const cf = new DiscordClientFactory(null); - return expect(cf.getDiscordId("failme")).to.eventually.be.rejected; + try { + await cf.getDiscordId("failme"); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } }); }); describe("getClient", () => { - it("should fetch bot client successfully", () => { + it("should fetch bot client successfully", async () => { const config = new DiscordBridgeConfigAuth(); config.botToken = "passme"; const cf = new DiscordClientFactory(null, config); cf.botClient = 1; - return expect(cf.getClient()).to.eventually.equal(cf.botClient); + const client = await cf.getClient(); + expect(client).equals(cf.botClient); }); - it("should return cached client", () => { + it("should return cached client", async () => { const config = new DiscordBridgeConfigAuth(); const cf = new DiscordClientFactory(null); cf.clients.set("@user:localhost", "testclient"); - return expect(cf.getClient("@user:localhost")).to.eventually.equal("testclient"); + const client = await cf.getClient("@user:localhost"); + expect(client).equals("testclient"); }); - it("should fetch bot client if userid doesn't match", () => { + it("should fetch bot client if userid doesn't match", async () => { const config = new DiscordBridgeConfigAuth(); const cf = new DiscordClientFactory(STORE); cf.botClient = 1; - return expect(cf.getClient("@user:localhost")).to.eventually.equal(cf.botClient); + const client = await cf.getClient("@user:localhost"); + expect(client).equals(cf.botClient); }); - it("should fetch user client if userid matches", () => { + it("should fetch user client if userid matches", async () => { const config = new DiscordBridgeConfigAuth(); const cf = new DiscordClientFactory(STORE); - return cf.getClient("@valid:localhost").then((client) => { - expect(client).is.not.null; - expect(cf.clients.has("@valid:localhost")).to.be.true; - }); + const client = await cf.getClient("@valid:localhost"); + expect(client).is.not.null; + expect(cf.clients.has("@valid:localhost")).to.be.true; }); - it("should fail if the user client cannot log in", () => { + it("should fail if the user client cannot log in", async () => { const config = new DiscordBridgeConfigAuth(); const cf = new DiscordClientFactory(STORE); cf.botClient = 1; - return cf.getClient("@invalid:localhost").then((client) => { - expect(client).to.equal(cf.botClient); - expect(cf.clients.has("@invalid:localhost")).to.be.false; - }); + const client = await cf.getClient("@invalid:localhost"); + expect(client).to.equal(cf.botClient); + expect(cf.clients.has("@invalid:localhost")).to.be.false; }); }); }); diff --git a/test/test_discordbot.ts b/test/test_discordbot.ts index 9419fcda4ad9a52d59560530d6b2faffc7e4796f..f3c462ea5b8d279210e18423cb2cc98da76a7708 100644 --- a/test/test_discordbot.ts +++ b/test/test_discordbot.ts @@ -15,6 +15,7 @@ import { MockMessage } from "./mocks/message"; /* tslint:disable:no-unused-expression max-file-line-count no-any */ Chai.use(ChaiAsPromised); +const expect = Chai.expect; const assert = Chai.assert; // const should = Chai.should as any; @@ -63,39 +64,49 @@ describe("DiscordBot", () => { }, }; describe("run()", () => { - it("should resolve when ready.", () => { + it("should resolve when ready.", async () => { discordBot = new modDiscordBot.DiscordBot( config, null, ); discordBot.setBridge(mockBridge); - return discordBot.run(); + await discordBot.run(); }); }); describe("LookupRoom()", () => { - beforeEach(() => { + beforeEach( async () => { discordBot = new modDiscordBot.DiscordBot( config, null, ); discordBot.setBridge(mockBridge); - return discordBot.run(); + await discordBot.run(); }); - it("should reject a missing guild.", () => { - return assert.isRejected(discordBot.LookupRoom("541", "321")); + it("should reject a missing guild.", async () => { + try { + await discordBot.LookupRoom("541", "321"); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } }); - it("should reject a missing channel.", () => { - return assert.isRejected(discordBot.LookupRoom("123", "666")); + it("should reject a missing channel.", async () => { + try { + await discordBot.LookupRoom("123", "666"); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } }); - it("should resolve a guild and channel id.", () => { - return assert.isFulfilled(discordBot.LookupRoom("123", "321")); + it("should resolve a guild and channel id.", async () => { + await discordBot.LookupRoom("123", "321"); }); }); describe("OnMessageUpdate()", () => { - it("should return on an unchanged message", () => { + it("should return on an unchanged message", async () => { discordBot = new modDiscordBot.DiscordBot( config, mockBridge, @@ -117,12 +128,11 @@ describe("DiscordBot", () => { let checkMsgSent = false; discordBot.SendMatrixMessage = (...args) => checkMsgSent = true; - discordBot.OnMessageUpdate(oldMsg, newMsg).then(() => { - Chai.assert.equal(checkMsgSent, false); - }); + await discordBot.OnMessageUpdate(oldMsg, newMsg); + Chai.assert.equal(checkMsgSent, false); }); - it("should send a matrix message on an edited discord message", () => { + it("should send a matrix message on an edited discord message", async () => { discordBot = new modDiscordBot.DiscordBot( config, mockBridge, @@ -144,9 +154,8 @@ describe("DiscordBot", () => { let checkMsgSent = false; discordBot.SendMatrixMessage = (...args) => checkMsgSent = true; - discordBot.OnMessageUpdate(oldMsg, newMsg).then(() => { - Chai.assert.equal(checkMsgSent, true); - }); + await discordBot.OnMessageUpdate(oldMsg, newMsg); + Chai.assert.equal(checkMsgSent, true); }); }); describe("event:message", () => { diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index 5c94528acdb432875fd8458fbae8ad6dc41be795..e81d6c07fec0b8acdb82aac6d63e722e8382bc6c 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -600,43 +600,43 @@ describe("MatrixEventProcessor", () => { }); describe("HandleAttachment", () => { const SMALL_FILE = 200; - it("message without an attachment", () => { + it("message without an attachment", async () => { const processor = createMatrixEventProcessor(); - return expect(processor.HandleAttachment({ + const ret = await processor.HandleAttachment({ content: { msgtype: "m.text", }, - } as IMatrixEvent, mxClient)).to.eventually.eq(""); + } as IMatrixEvent, mxClient); + expect(ret).equals(""); }); - it("message without an info", () => { + it("message without an info", async () => { const processor = createMatrixEventProcessor(); - return expect(processor.HandleAttachment({ + const attachment = (await processor.HandleAttachment({ content: { body: "filename.webm", msgtype: "m.video", url: "mxc://localhost/200", }, - } as IMatrixEvent, mxClient)).to.eventually.satisfy((attachment) => { - expect(attachment.name).to.eq("filename.webm"); - expect(attachment.attachment.length).to.eq(SMALL_FILE); - return true; - }); + } as IMatrixEvent, mxClient)) as Discord.FileOptions; + expect(attachment.name).to.eq("filename.webm"); + expect(attachment.attachment.length).to.eq(SMALL_FILE); }); - it("message without a url", () => { + it("message without a url", async () => { const processor = createMatrixEventProcessor(); - return expect(processor.HandleAttachment({ + const ret = await processor.HandleAttachment({ content: { info: { size: 1, }, msgtype: "m.video", }, - } as IMatrixEvent, mxClient)).to.eventually.eq(""); + } as IMatrixEvent, mxClient); + expect(ret).equals(""); }); - it("message with a large info.size", () => { + it("message with a large info.size", async () => { const LARGE_FILE = 8000000; const processor = createMatrixEventProcessor(); - return expect(processor.HandleAttachment({ + const ret = await processor.HandleAttachment({ content: { body: "filename.webm", info: { @@ -645,12 +645,12 @@ describe("MatrixEventProcessor", () => { msgtype: "m.video", url: "mxc://localhost/8000000", }, - } as IMatrixEvent, mxClient)) - .to.eventually.eq("[filename.webm](https://localhost/8000000)"); + } as IMatrixEvent, mxClient); + expect(ret).equals("[filename.webm](https://localhost/8000000)"); }); - it("message with a small info.size", () => { + it("message with a small info.size", async () => { const processor = createMatrixEventProcessor(); - return expect(processor.HandleAttachment({ + const attachment = (await processor.HandleAttachment({ content: { body: "filename.webm", info: { @@ -659,15 +659,13 @@ describe("MatrixEventProcessor", () => { msgtype: "m.video", url: "mxc://localhost/200", }, - } as IMatrixEvent, mxClient)).to.eventually.satisfy((attachment) => { - expect(attachment.name).to.eq("filename.webm"); - expect(attachment.attachment.length).to.eq(SMALL_FILE); - return true; - }); + } as IMatrixEvent, mxClient)) as Discord.FileOptions; + expect(attachment.name).to.eq("filename.webm"); + expect(attachment.attachment.length).to.eq(SMALL_FILE); }); - it("message with a small info.size but a larger file", () => { + it("message with a small info.size but a larger file", async () => { const processor = createMatrixEventProcessor(); - return expect(processor.HandleAttachment({ + const ret = await processor.HandleAttachment({ content: { body: "filename.webm", info: { @@ -676,11 +674,12 @@ describe("MatrixEventProcessor", () => { msgtype: "m.video", url: "mxc://localhost/8000000", }, - } as IMatrixEvent, mxClient)).to.eventually.eq("[filename.webm](https://localhost/8000000)"); + } as IMatrixEvent, mxClient); + expect(ret).equals("[filename.webm](https://localhost/8000000)"); }); - it("Should handle stickers.", () => { + it("Should handle stickers.", async () => { const processor = createMatrixEventProcessor(); - return expect(processor.HandleAttachment({ + const attachment = (await processor.HandleAttachment({ content: { body: "Bunnies", info: { @@ -690,10 +689,8 @@ describe("MatrixEventProcessor", () => { }, sender: "@test:localhost", type: "m.sticker", - } as IMatrixEvent, mxClient)).to.eventually.satisfy((attachment) => { - expect(attachment.name).to.eq("Bunnies.png"); - return true; - }); + } as IMatrixEvent, mxClient)) as Discord.FileOptions; + expect(attachment.name).to.eq("Bunnies.png"); }); }); describe("GetEmbedForReply", () => { diff --git a/test/test_matrixroomhandler.ts b/test/test_matrixroomhandler.ts index 86d6bf001266ea3a38b3270ca72d82b221acefcb..4b926f9836ea1d3e1d30d2e555899c868e743f34 100644 --- a/test/test_matrixroomhandler.ts +++ b/test/test_matrixroomhandler.ts @@ -187,9 +187,9 @@ function createRH(opts: any = {}) { describe("MatrixRoomHandler", () => { describe("OnAliasQueried", () => { - it("should join successfully", () => { + it("should join successfully", async () => { const handler = createRH(); - return expect(handler.OnAliasQueried("#accept:localhost", "!accept:localhost")).to.be.fulfilled; + await handler.OnAliasQueried("#accept:localhost", "!accept:localhost"); }); it("should join successfully and create ghosts", async () => { const EXPECTEDUSERS = 2; @@ -197,26 +197,41 @@ describe("MatrixRoomHandler", () => { await handler.OnAliasQueried("#accept:localhost", "!accept:localhost"); expect(USERSJOINED).to.equal(EXPECTEDUSERS); }); - it("should not join successfully", () => { + it("should not join successfully", async () => { const handler = createRH(); - return expect(handler.OnAliasQueried("#reject:localhost", "!reject:localhost")).to.be.rejected; + try { + await handler.OnAliasQueried("#reject:localhost", "!reject:localhost"); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } }); }); describe("OnEvent", () => { - it("should reject old events", () => { + it("should reject old events", async () => { const AGE = 900001; // 15 * 60 * 1000 const handler = createRH(); - return expect(handler.OnEvent( - buildRequest({unsigned: {age: AGE}}), null)) - .to.be.rejectedWith("Event too old"); + try { + await handler.OnEvent(buildRequest({unsigned: {age: AGE}}), null); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + expect(e.message).equals("Event too old"); + } }); - it("should reject un-processable events", () => { + it("should reject un-processable events", async () => { const AGE = 900000; // 15 * 60 * 1000 const handler = createRH(); - return expect(handler.OnEvent(buildRequest({ - content: {}, - type: "m.potato", - unsigned: {age: AGE}}), null)).to.be.rejectedWith("Event not processed by bridge"); + try { + await handler.OnEvent(buildRequest({ + content: {}, + type: "m.potato", + unsigned: {age: AGE}}), null); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + expect(e.message).equals("Event not processed by bridge"); + } }); it("should handle invites", async () => { const handler = createRH(); @@ -261,15 +276,21 @@ describe("MatrixRoomHandler", () => { type: "m.room.redaction"}), context); expect(MESSAGE_PROCCESS).equals("redacted"); }); - it("should ignore redactions with no linked room", () => { + it("should ignore redactions with no linked room", async () => { const handler = createRH(); const context = { rooms: { remote: null, }, }; - return expect(handler.OnEvent(buildRequest({ - type: "m.room.redaction"}), context)).to.be.rejectedWith("Event not processed by bridge"); + try { + await handler.OnEvent(buildRequest({ + type: "m.room.redaction"}), context); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + expect(e.message).equals("Event not processed by bridge"); + } }); it("should process regular messages", async () => { const handler = createRH(); @@ -286,7 +307,7 @@ describe("MatrixRoomHandler", () => { }), context); expect(MESSAGE_PROCCESS).equals("processed"); }); - it("should alert if encryption is turned on", () => { + it("should alert if encryption is turned on", async () => { const handler = createRH(); const context = { rooms: { @@ -295,10 +316,10 @@ describe("MatrixRoomHandler", () => { }, }, }; - return expect(handler.OnEvent(buildRequest({ + await handler.OnEvent(buildRequest({ room_id: "!accept:localhost", type: "m.room.encryption", - }), context)).to.eventually.be.fulfilled; + }), context); }); it("should process !discord commands", async () => { const handler = createRH(); @@ -312,17 +333,23 @@ describe("MatrixRoomHandler", () => { }), null); expect(processedcmd).to.be.true; }); - it("should ignore regular messages with no linked room", () => { + it("should ignore regular messages with no linked room", async () => { const handler = createRH(); const context = { rooms: { remote: null, }, }; - return expect(handler.OnEvent(buildRequest({ - content: {body: "abc"}, - type: "m.room.message", - }), context)).to.be.rejectedWith("Event not processed by bridge"); + try { + await handler.OnEvent(buildRequest({ + content: {body: "abc"}, + type: "m.room.message", + }), context) + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + expect(e.message).equals("Event not processed by bridge"); + } }); it("should process stickers", async () => { const handler = createRH(); @@ -368,11 +395,12 @@ describe("MatrixRoomHandler", () => { }); }); describe("ProcessCommand", () => { - it("should not process command if not in room", () => { + it("should not process command if not in room", async () => { const handler: any = createRH({disableSS: true}); - return expect(handler.ProcessCommand({ + const ret = await handler.ProcessCommand({ room_id: "!666:localhost", - })).to.eventually.be.undefined; + }); + expect(ret).to.be.undefined; }); it("should warn if self service is disabled", async () => { const handler: any = createRH({disableSS: true}); @@ -570,26 +598,29 @@ describe("MatrixRoomHandler", () => { }); }); describe("OnAliasQuery", () => { - it("will create room", () => { + it("will create room", async () => { const handler: any = createRH({}); handler.createMatrixRoom = () => true; - return expect(handler.OnAliasQuery( + const ret = await handler.OnAliasQuery( "_discord_123_456:localhost", - "_discord_123_456")).to.eventually.be.true; + "_discord_123_456"); + expect(ret).to.be.true; }); - it("will not create room if guild cannot be found", () => { + it("will not create room if guild cannot be found", async () => { const handler: any = createRH({}); handler.createMatrixRoom = () => true; - return expect(handler.OnAliasQuery( + const ret = await handler.OnAliasQuery( "_discord_111_456:localhost", - "_discord_111_456")).to.eventually.be.undefined; + "_discord_111_456"); + expect(ret).to.be.undefined; }); - it("will not create room if channel cannot be found", () => { + it("will not create room if channel cannot be found", async () => { const handler: any = createRH({}); handler.createMatrixRoom = () => true; - return expect(handler.OnAliasQuery( + const ret = await handler.OnAliasQuery( "_discord_123_444:localhost", - "_discord_123_444")).to.eventually.be.undefined; + "_discord_123_444"); + expect(ret).to.be.undefined; }); it("will not create room if alias is wrong", async () => { const handler: any = createRH({}); @@ -601,48 +632,61 @@ describe("MatrixRoomHandler", () => { }); }); describe("tpGetProtocol", () => { - it("will return an object", () => { + it("will return an object", async () => { const handler: any = createRH({}); - return handler.tpGetProtocol("").then((protocol) => { - expect(protocol).to.not.be.null; - expect(protocol.instances[0].network_id).to.equal("123"); - expect(protocol.instances[0].bot_user_id).to.equal("@botuser:localhost"); - expect(protocol.instances[0].desc).to.equal("123"); - expect(protocol.instances[0].network_id).to.equal("123"); - }); + const protocol = await handler.tpGetProtocol(""); + expect(protocol).to.not.be.null; + expect(protocol.instances[0].network_id).to.equal("123"); + expect(protocol.instances[0].bot_user_id).to.equal("@botuser:localhost"); + expect(protocol.instances[0].desc).to.equal("123"); + expect(protocol.instances[0].network_id).to.equal("123"); }); }); describe("tpGetLocation", () => { - it("will return an array", () => { + it("will return an array", async () => { const handler: any = createRH({}); - return handler.tpGetLocation("", { + const channels = await handler.tpGetLocation("", { channel_name: "", guild_id: "", - }).then((channels) => { - expect(channels).to.be.a("array"); }); + expect(channels).to.be.a("array"); }); }); describe("tpParseLocation", () => { - it("will reject", () => { + it("will reject", async () => { const handler: any = createRH({}); - return expect(handler.tpParseLocation("alias")).to.eventually.be.rejected; + try { + await handler.tpParseLocation("alias"); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } }); }); describe("tpGetUser", () => { - it("will reject", () => { + it("will reject", async () => { const handler: any = createRH({}); - return expect(handler.tpGetUser("", {})).to.eventually.be.rejected; + try { + await handler.tpGetUser("", {}); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } }); }); describe("tpParseUser", () => { - it("will reject", () => { + it("will reject", async () => { const handler: any = createRH({}); - return expect(handler.tpParseUser("alias")).to.eventually.be.rejected; + try { + await handler.tpParseUser("alias"); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } }); }); describe("joinRoom", () => { - it("will join immediately", () => { + it("will join immediately", async () => { const handler: any = createRH({}); const intent = { getClient: () => { @@ -653,11 +697,12 @@ describe("MatrixRoomHandler", () => { }; const startTime = Date.now(); const MAXTIME = 1000; - return expect(handler.joinRoom(intent, "#test:localhost")).to.eventually.be.fulfilled.and.satisfy(() => { + await handler.joinRoom(intent, "#test:localhost"); + expect(1).to.satisfy(() => { return (Date.now() - startTime) < MAXTIME; }); }); - it("will fail first, join after", () => { + it("will fail first, join after", async () => { const handler: any = createRH({}); let shouldFail = true; const intent = { @@ -675,8 +720,9 @@ describe("MatrixRoomHandler", () => { }; const startTime = Date.now(); const MINTIME = 1000; - return expect(handler.joinRoom(intent, "#test:localhost")).to.eventually.be.fulfilled.and.satisfy(() => { - expect(shouldFail).to.be.false; + await handler.joinRoom(intent, "#test:localhost"); + expect(shouldFail).to.be.false; + expect(1).to.satisfy(() => { return (Date.now() - startTime) > MINTIME; }); }); @@ -691,7 +737,7 @@ describe("MatrixRoomHandler", () => { }); }); describe("HandleDiscordCommand", () => { - it("will kick a member", () => { + it("will kick a member", async () => { const handler: any = createRH({}); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); @@ -705,11 +751,10 @@ describe("MatrixRoomHandler", () => { content: "!matrix kick someuser", member, }; - return handler.HandleDiscordCommand(message).then(() => { - expect(USERSKICKED).equals(1); - }); + await handler.HandleDiscordCommand(message); + expect(USERSKICKED).equals(1); }); - it("will kick a member in all guild rooms", () => { + it("will kick a member in all guild rooms", async () => { const handler: any = createRH({}); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel, (new MockChannel("456"))]); @@ -723,12 +768,11 @@ describe("MatrixRoomHandler", () => { content: "!matrix kick someuser", member, }; - return handler.HandleDiscordCommand(message).then(() => { - // tslint:disable-next-line:no-magic-numbers - expect(USERSKICKED).equals(2); - }); + await handler.HandleDiscordCommand(message); + // tslint:disable-next-line:no-magic-numbers + expect(USERSKICKED).equals(2); }); - it("will deny permission", () => { + it("will deny permission", async () => { const handler: any = createRH({}); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); @@ -742,11 +786,10 @@ describe("MatrixRoomHandler", () => { content: "!matrix kick someuser", member, }; - return handler.HandleDiscordCommand(message).then(() => { - expect(USERSKICKED).equals(0); - }); + await handler.HandleDiscordCommand(message); + expect(USERSKICKED).equals(0); }); - it("will ban a member", () => { + it("will ban a member", async () => { const handler: any = createRH({}); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); @@ -760,11 +803,10 @@ describe("MatrixRoomHandler", () => { content: "!matrix ban someuser", member, }; - return handler.HandleDiscordCommand(message).then(() => { - expect(USERSBANNED).equals(1); - }); + await handler.HandleDiscordCommand(message); + expect(USERSBANNED).equals(1); }); - it("will unban a member", () => { + it("will unban a member", async () => { const handler: any = createRH({}); const channel = new MockChannel("123"); const guild = new MockGuild("456", [channel]); @@ -778,9 +820,8 @@ describe("MatrixRoomHandler", () => { content: "!matrix unban someuser", member, }; - return handler.HandleDiscordCommand(message).then(() => { - expect(USERSUNBANNED).equals(1); - }); + await handler.HandleDiscordCommand(message); + expect(USERSUNBANNED).equals(1); }); }); }); diff --git a/test/test_messageprocessor.ts b/test/test_messageprocessor.ts index e9df149c2769489b8def9ce5be01c6d93765169d..44ab55cae96259f7d6e830bbc31a41daf643ed4f 100644 --- a/test/test_messageprocessor.ts +++ b/test/test_messageprocessor.ts @@ -29,114 +29,114 @@ describe("MessageProcessor", () => { }); }); describe("FormatDiscordMessage", () => { - it("processes plain text messages correctly", async () => { - const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); - const msg = new MockMessage() as any; - msg.embeds = []; - msg.content = "Hello World!"; - const result = await processor.FormatDiscordMessage(msg); - Chai.assert(result.body, "Hello World!"); - Chai.assert(result.formattedBody, "Hello World!"); - }); - it("processes markdown messages correctly.", async () => { - const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); - const msg = new MockMessage() as any; - msg.embeds = []; - msg.content = "Hello *World*!"; - const result = await processor.FormatDiscordMessage(msg); - Chai.assert.equal(result.body, "Hello *World*!"); - Chai.assert.equal(result.formattedBody, "<p>Hello <em>World</em>!</p>"); - }); - it("processes non-discord markdown correctly.", async () => { - const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); - const msg = new MockMessage() as any; - msg.embeds = []; - msg.content = "> inb4 tests"; - let result = await processor.FormatDiscordMessage(msg); - Chai.assert.equal(result.body, "> inb4 tests"); - Chai.assert.equal(result.formattedBody, "<p>> inb4 tests</p>"); + it("processes plain text messages correctly", async () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); + const msg = new MockMessage() as any; + msg.embeds = []; + msg.content = "Hello World!"; + const result = await processor.FormatDiscordMessage(msg); + Chai.assert(result.body, "Hello World!"); + Chai.assert(result.formattedBody, "Hello World!"); + }); + it("processes markdown messages correctly.", async () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); + const msg = new MockMessage() as any; + msg.embeds = []; + msg.content = "Hello *World*!"; + const result = await processor.FormatDiscordMessage(msg); + Chai.assert.equal(result.body, "Hello *World*!"); + Chai.assert.equal(result.formattedBody, "<p>Hello <em>World</em>!</p>"); + }); + it("processes non-discord markdown correctly.", async () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); + const msg = new MockMessage() as any; + msg.embeds = []; + msg.content = "> inb4 tests"; + let result = await processor.FormatDiscordMessage(msg); + Chai.assert.equal(result.body, "> inb4 tests"); + Chai.assert.equal(result.formattedBody, "<p>> inb4 tests</p>"); - msg.embeds = []; - msg.content = "[test](http://example.com)"; - result = await processor.FormatDiscordMessage(msg); - Chai.assert.equal(result.body, "[test](http://example.com)"); - Chai.assert.equal(result.formattedBody, "<p>[test](<a href=\"http://example.com\">http://example.com</a>)</p>"); - }); - it("processes discord-specific markdown correctly.", async () => { - const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); - const msg = new MockMessage() as any; - msg.embeds = []; - msg.content = "_ italic _"; - const result = await processor.FormatDiscordMessage(msg); - Chai.assert.equal(result.body, "_ italic _"); - Chai.assert.equal(result.formattedBody, "<p><em> italic </em></p>"); - }); + msg.embeds = []; + msg.content = "[test](http://example.com)"; + result = await processor.FormatDiscordMessage(msg); + Chai.assert.equal(result.body, "[test](http://example.com)"); + Chai.assert.equal(result.formattedBody, "<p>[test](<a href=\"http://example.com\">http://example.com</a>)</p>"); + }); + it("processes discord-specific markdown correctly.", async () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); + const msg = new MockMessage() as any; + msg.embeds = []; + msg.content = "_ italic _"; + const result = await processor.FormatDiscordMessage(msg); + Chai.assert.equal(result.body, "_ italic _"); + Chai.assert.equal(result.formattedBody, "<p><em> italic </em></p>"); + }); }); describe("FormatEmbeds", () => { - it("should format embeds correctly", async () => { - const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); - const msg = new MockMessage() as any; - msg.embeds = [ - { - author: {} as any, - client: {} as any, - color: {} as any, - createdAt: {} as any, - createdTimestamp: {} as any, - description: "Description", - fields: {} as any, - footer: {} as any, - hexColor: {} as any, - image: {} as any, - message: {} as any, - provider: {} as any, - thumbnail: {} as any, - title: "Title", - type: {} as any, - url: "http://example.com", - video: {} as any, - }, - ]; - msg.content = "message"; - const result = await processor.FormatDiscordMessage(msg); - Chai.assert.equal(result.body, "message\n\n----\n##### [Title](http://example.com)\nDescription"); - Chai.assert.equal(result.formattedBody, "<p>message</p><hr><h5><a href=\"http://example.com\">Title</a>" + - "</h5><p>Description</p>"); - }); + it("should format embeds correctly", async () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); + const msg = new MockMessage() as any; + msg.embeds = [ + { + author: {} as any, + client: {} as any, + color: {} as any, + createdAt: {} as any, + createdTimestamp: {} as any, + description: "Description", + fields: {} as any, + footer: {} as any, + hexColor: {} as any, + image: {} as any, + message: {} as any, + provider: {} as any, + thumbnail: {} as any, + title: "Title", + type: {} as any, + url: "http://example.com", + video: {} as any, + }, + ]; + msg.content = "message"; + const result = await processor.FormatDiscordMessage(msg); + Chai.assert.equal(result.body, "message\n\n----\n##### [Title](http://example.com)\nDescription"); + Chai.assert.equal(result.formattedBody, "<p>message</p><hr><h5><a href=\"http://example.com\">Title</a>" + + "</h5><p>Description</p>"); + }); }); describe("FormatEdit", () => { - it("should format basic edits appropriately", async () => { - const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); - const oldMsg = new MockMessage() as any; - const newMsg = new MockMessage() as any; - oldMsg.embeds = []; - newMsg.embeds = []; + it("should format basic edits appropriately", async () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); + const oldMsg = new MockMessage() as any; + const newMsg = new MockMessage() as any; + oldMsg.embeds = []; + newMsg.embeds = []; - // Content updated but not changed - oldMsg.content = "a"; - newMsg.content = "b"; + // Content updated but not changed + oldMsg.content = "a"; + newMsg.content = "b"; - const result = await processor.FormatEdit(oldMsg, newMsg); - Chai.assert.equal(result.body, "*edit:* ~~a~~ -> b"); - Chai.assert.equal(result.formattedBody, "<p><em>edit:</em> <del>a</del> -> b</p>"); - }); + const result = await processor.FormatEdit(oldMsg, newMsg); + Chai.assert.equal(result.body, "*edit:* ~~a~~ -> b"); + Chai.assert.equal(result.formattedBody, "<p><em>edit:</em> <del>a</del> -> b</p>"); + }); - it("should format markdown heavy edits apropriately", async () => { - const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); - const oldMsg = new MockMessage() as any; - const newMsg = new MockMessage() as any; - oldMsg.embeds = []; - newMsg.embeds = []; + it("should format markdown heavy edits apropriately", async () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), bot as DiscordBot); + const oldMsg = new MockMessage() as any; + const newMsg = new MockMessage() as any; + oldMsg.embeds = []; + newMsg.embeds = []; - // Content updated but not changed - oldMsg.content = "a slice of **cake**"; - newMsg.content = "*a* slice of cake"; + // Content updated but not changed + oldMsg.content = "a slice of **cake**"; + newMsg.content = "*a* slice of cake"; - const result = await processor.FormatEdit(oldMsg, newMsg); - Chai.assert.equal(result.body, "*edit:* ~~a slice of **cake**~~ -> *a* slice of cake"); - Chai.assert.equal(result.formattedBody, "<p><em>edit:</em> <del>a slice of <strong>" + - "cake</strong></del> -> <em>a</em> slice of cake</p>"); - }); + const result = await processor.FormatEdit(oldMsg, newMsg); + Chai.assert.equal(result.body, "*edit:* ~~a slice of **cake**~~ -> *a* slice of cake"); + Chai.assert.equal(result.formattedBody, "<p><em>edit:</em> <del>a slice of <strong>" + + "cake</strong></del> -> <em>a</em> slice of cake</p>"); + }); }); diff --git a/test/test_usersyncroniser.ts b/test/test_usersyncroniser.ts index 6c37565c8640f53acd1fbbb9c8326aa7bce22449..51158597105b0448cb0a8c5fd71cb4e2526d5e48 100644 --- a/test/test_usersyncroniser.ts +++ b/test/test_usersyncroniser.ts @@ -149,14 +149,13 @@ describe("UserSyncroniser", () => { "test.jpg", "111", ); - return userSync.GetUserUpdateState(user as any).then((state) => { - expect(state.createUser).is.true; - expect(state.removeAvatar).is.false; - expect(state.displayName).equals("TestUsername#6969"); - expect(state.mxUserId).equals("@_discord_123456:localhost"); - expect(state.avatarId).equals("111"); - expect(state.avatarUrl).equals("test.jpg"); - }); + const state = await userSync.GetUserUpdateState(user as any); + expect(state.createUser).is.true; + expect(state.removeAvatar).is.false; + expect(state.displayName).equals("TestUsername#6969"); + expect(state.mxUserId).equals("@_discord_123456:localhost"); + expect(state.avatarId).equals("111"); + expect(state.avatarUrl).equals("test.jpg"); }); it("Will change display names", async () => { const remoteUser = new RemoteUser("123456", { @@ -172,14 +171,13 @@ describe("UserSyncroniser", () => { "test.jpg", "111", ); - return userSync.GetUserUpdateState(user as any).then((state) => { - expect(state.createUser, "CreateUser").is.false; - expect(state.removeAvatar, "RemoveAvatar").is.false; - expect(state.displayName, "DisplayName").equals("TestUsername#6969"); - expect(state.mxUserId , "UserId").equals("@_discord_123456:localhost"); - expect(state.avatarId, "AvatarID").is.empty; - expect(state.avatarUrl, "AvatarUrl").is.null; - }); + const state = await userSync.GetUserUpdateState(user as any); + expect(state.createUser, "CreateUser").is.false; + expect(state.removeAvatar, "RemoveAvatar").is.false; + expect(state.displayName, "DisplayName").equals("TestUsername#6969"); + expect(state.mxUserId , "UserId").equals("@_discord_123456:localhost"); + expect(state.avatarId, "AvatarID").is.empty; + expect(state.avatarUrl, "AvatarUrl").is.null; }); it("Will change avatars", async () => { const remoteUser = new RemoteUser("123456", { @@ -195,14 +193,13 @@ describe("UserSyncroniser", () => { "test2.jpg", "111", ); - return userSync.GetUserUpdateState(user as any).then((state) => { - expect(state.createUser, "CreateUser").is.false; - expect(state.removeAvatar, "RemoveAvatar").is.false; - expect(state.avatarUrl, "AvatarUrl").equals("test2.jpg"); - expect(state.mxUserId , "UserId").equals("@_discord_123456:localhost"); - expect(state.avatarId, "AvatarID").is.equals("111"); - expect(state.displayName, "DisplayName").is.null; - }); + const state = await userSync.GetUserUpdateState(user as any); + expect(state.createUser, "CreateUser").is.false; + expect(state.removeAvatar, "RemoveAvatar").is.false; + expect(state.avatarUrl, "AvatarUrl").equals("test2.jpg"); + expect(state.mxUserId , "UserId").equals("@_discord_123456:localhost"); + expect(state.avatarId, "AvatarID").is.equals("111"); + expect(state.displayName, "DisplayName").is.null; }); it("Will remove avatars", async () => { const remoteUser = new RemoteUser("123456", { @@ -218,14 +215,13 @@ describe("UserSyncroniser", () => { null, null, ); - return userSync.GetUserUpdateState(user as any).then((state) => { - expect(state.createUser, "CreateUser").is.false; - expect(state.removeAvatar, "RemoveAvatar").is.true; - expect(state.avatarUrl, "AvatarUrl").is.null; - expect(state.mxUserId , "UserId").equals("@_discord_123456:localhost"); - expect(state.avatarId, "AvatarID").is.empty; - expect(state.displayName, "DisplayName").is.null; - }); + const state = await userSync.GetUserUpdateState(user as any); + expect(state.createUser, "CreateUser").is.false; + expect(state.removeAvatar, "RemoveAvatar").is.true; + expect(state.avatarUrl, "AvatarUrl").is.null; + expect(state.mxUserId , "UserId").equals("@_discord_123456:localhost"); + expect(state.avatarId, "AvatarID").is.empty; + expect(state.displayName, "DisplayName").is.null; }); }); describe("ApplyStateToProfile", () => { @@ -240,11 +236,10 @@ describe("UserSyncroniser", () => { mxUserId: "@_discord_123456:localhost", removeAvatar: false, }; - return userSync.ApplyStateToProfile(state).then(() => { - expect(LINK_MX_USER).is.not.null; - expect(LINK_RM_USER).is.not.null; - expect(REMOTEUSER_SET).is.null; - }); + await userSync.ApplyStateToProfile(state); + expect(LINK_MX_USER).is.not.null; + expect(LINK_RM_USER).is.not.null; + expect(REMOTEUSER_SET).is.null; }); it("Will set a display name", async () => { const userSync = CreateUserSync(); @@ -257,15 +252,14 @@ describe("UserSyncroniser", () => { mxUserId: "@_discord_123456:localhost", removeAvatar: false, }; - return userSync.ApplyStateToProfile(state).then(() => { - expect(LINK_MX_USER).is.not.null; - expect(LINK_RM_USER).is.not.null; - expect(REMOTEUSER_SET).is.not.null; - expect(DISPLAYNAME_SET).equal("123456"); - expect(REMOTEUSER_SET.data.displayname).equal("123456"); - expect(AVATAR_SET).is.null; - expect(REMOTEUSER_SET.data.avatarurl).is.undefined; - }); + await userSync.ApplyStateToProfile(state); + expect(LINK_MX_USER).is.not.null; + expect(LINK_RM_USER).is.not.null; + expect(REMOTEUSER_SET).is.not.null; + expect(DISPLAYNAME_SET).equal("123456"); + expect(REMOTEUSER_SET.data.displayname).equal("123456"); + expect(AVATAR_SET).is.null; + expect(REMOTEUSER_SET.data.avatarurl).is.undefined; }); it("Will set an avatar", async () => { const userSync = CreateUserSync(); @@ -278,16 +272,15 @@ describe("UserSyncroniser", () => { mxUserId: "@_discord_123456:localhost", removeAvatar: false, }; - return userSync.ApplyStateToProfile(state).then(() => { - expect(LINK_MX_USER).is.not.null; - expect(LINK_RM_USER).is.not.null; - expect(AVATAR_SET).equal("avatarset"); - expect(UTIL_UPLOADED_AVATAR).to.be.true; - expect(REMOTEUSER_SET).is.not.null; - expect(REMOTEUSER_SET.data.avatarurl).equal("654321"); - expect(REMOTEUSER_SET.data.displayname).is.undefined; - expect(DISPLAYNAME_SET).is.null; - }); + await userSync.ApplyStateToProfile(state); + expect(LINK_MX_USER).is.not.null; + expect(LINK_RM_USER).is.not.null; + expect(AVATAR_SET).equal("avatarset"); + expect(UTIL_UPLOADED_AVATAR).to.be.true; + expect(REMOTEUSER_SET).is.not.null; + expect(REMOTEUSER_SET.data.avatarurl).equal("654321"); + expect(REMOTEUSER_SET.data.displayname).is.undefined; + expect(DISPLAYNAME_SET).is.null; }); it("Will remove an avatar", async () => { const userSync = CreateUserSync(); @@ -300,16 +293,15 @@ describe("UserSyncroniser", () => { mxUserId: "@_discord_123456:localhost", removeAvatar: true, }; - return userSync.ApplyStateToProfile(state).then(() => { - expect(LINK_MX_USER).is.not.null; - expect(LINK_RM_USER).is.not.null; - expect(AVATAR_SET).is.null; - expect(UTIL_UPLOADED_AVATAR).to.be.false; - expect(REMOTEUSER_SET).is.not.null; - expect(REMOTEUSER_SET.data.avatarurl).is.null; - expect(REMOTEUSER_SET.data.displayname).is.undefined; - expect(DISPLAYNAME_SET).is.null; - }); + await userSync.ApplyStateToProfile(state); + expect(LINK_MX_USER).is.not.null; + expect(LINK_RM_USER).is.not.null; + expect(AVATAR_SET).is.null; + expect(UTIL_UPLOADED_AVATAR).to.be.false; + expect(REMOTEUSER_SET).is.not.null; + expect(REMOTEUSER_SET.data.avatarurl).is.null; + expect(REMOTEUSER_SET.data.displayname).is.undefined; + expect(DISPLAYNAME_SET).is.null; }); it("will do nothing if nothing needs to be done", async () => { const userSync = CreateUserSync([new RemoteUser("123456")]); @@ -322,13 +314,12 @@ describe("UserSyncroniser", () => { mxUserId: "@_discord_123456:localhost", removeAvatar: false, }; - return userSync.ApplyStateToProfile(state).then(() => { - expect(LINK_MX_USER).is.null; - expect(LINK_RM_USER).is.null; - expect(AVATAR_SET).is.null; - expect(REMOTEUSER_SET).is.null; - expect(DISPLAYNAME_SET).is.null; - }); + await userSync.ApplyStateToProfile(state); + expect(LINK_MX_USER).is.null; + expect(LINK_RM_USER).is.null; + expect(AVATAR_SET).is.null; + expect(REMOTEUSER_SET).is.null; + expect(DISPLAYNAME_SET).is.null; }); }); describe("ApplyStateToRoom", () => { @@ -340,13 +331,12 @@ describe("UserSyncroniser", () => { mxUserId: "@_discord_123456:localhost", roles: [], }; - return userSync.ApplyStateToRoom(state, "!abc:localhost", "123456").then(() => { - expect(REMOTEUSER_SET).is.not.null; - expect(REMOTEUSER_SET.data.nick_123456).is.equal("Good Boy"); - expect(SEV_ROOM_ID).is.equal("!abc:localhost"); - expect(SEV_CONTENT.displayname).is.equal("Good Boy"); - expect(SEV_KEY).is.equal("@_discord_123456:localhost"); - }); + await userSync.ApplyStateToRoom(state, "!abc:localhost", "123456"); + expect(REMOTEUSER_SET).is.not.null; + expect(REMOTEUSER_SET.data.nick_123456).is.equal("Good Boy"); + expect(SEV_ROOM_ID).is.equal("!abc:localhost"); + expect(SEV_CONTENT.displayname).is.equal("Good Boy"); + expect(SEV_KEY).is.equal("@_discord_123456:localhost"); }); it("Will not apply unchanged nick", async () => { const userSync = CreateUserSync([new RemoteUser("123456")]); @@ -356,12 +346,11 @@ describe("UserSyncroniser", () => { mxUserId: "@_discord_123456:localhost", roles: [], }; - return userSync.ApplyStateToRoom(state, "!abc:localhost", "123456").then(() => { - expect(REMOTEUSER_SET).is.null; - expect(SEV_ROOM_ID).is.null; - expect(SEV_CONTENT).is.null; - expect(SEV_KEY).is.null; - }); + await userSync.ApplyStateToRoom(state, "!abc:localhost", "123456"); + expect(REMOTEUSER_SET).is.null; + expect(SEV_ROOM_ID).is.null; + expect(SEV_CONTENT).is.null; + expect(SEV_KEY).is.null; }); it("Will apply roles", async () => { const userSync = CreateUserSync([new RemoteUser("123456")]); @@ -380,15 +369,14 @@ describe("UserSyncroniser", () => { }, ], }; - return userSync.ApplyStateToRoom(state, "!abc:localhost", "12345678").then(() => { - const custKey = SEV_CONTENT["uk.half-shot.discord.member"]; - const roles = custKey.roles; - expect(custKey.id).is.equal("123456"); - expect(roles.length).is.equal(1); - expect(roles[0].name).is.equal(TESTROLE_NAME); - expect(roles[0].color).is.equal(TESTROLE_COLOR); - expect(roles[0].position).is.equal(TESTROLE_POSITION); - }); + await userSync.ApplyStateToRoom(state, "!abc:localhost", "12345678"); + const custKey = SEV_CONTENT["uk.half-shot.discord.member"]; + const roles = custKey.roles; + expect(custKey.id).is.equal("123456"); + expect(roles.length).is.equal(1); + expect(roles[0].name).is.equal(TESTROLE_NAME); + expect(roles[0].color).is.equal(TESTROLE_COLOR); + expect(roles[0].position).is.equal(TESTROLE_POSITION); }); }); describe("GetUserStateForGuildMember", () => { @@ -413,9 +401,8 @@ describe("UserSyncroniser", () => { "username", guild, "BestDog"); - return userSync.GetUserStateForGuildMember(member as any, "BestDog").then((state) => { - expect(state.displayName).is.empty; - }); + const state = await userSync.GetUserStateForGuildMember(member as any, "BestDog"); + expect(state.displayName).is.empty; }); it("Will correctly add roles", async () => { const userSync = CreateUserSync([new RemoteUser("123456")]); @@ -436,12 +423,11 @@ describe("UserSyncroniser", () => { position: TESTROLE_POSITION, }, ]; - return userSync.GetUserStateForGuildMember(member as any).then((state) => { - expect(state.roles.length).to.be.equal(1); - expect(state.roles[0].name).to.be.equal(TESTROLE_NAME); - expect(state.roles[0].color).to.be.equal(TESTROLE_COLOR); - expect(state.roles[0].position).to.be.equal(TESTROLE_POSITION); - }); + const state = await userSync.GetUserStateForGuildMember(member as any); + expect(state.roles.length).to.be.equal(1); + expect(state.roles[0].name).to.be.equal(TESTROLE_NAME); + expect(state.roles[0].color).to.be.equal(TESTROLE_COLOR); + expect(state.roles[0].position).to.be.equal(TESTROLE_POSITION); }); }); describe("OnAddGuildMember", () => { @@ -453,24 +439,22 @@ describe("UserSyncroniser", () => { "123456", "username", guild); - return userSync.OnAddGuildMember(member as any).then(() => { - expect(JOINS).to.equal(GUILD_ROOM_IDS.length); - }); + await userSync.OnAddGuildMember(member as any); + expect(JOINS).to.equal(GUILD_ROOM_IDS.length); }); }); describe("OnRemoveGuildMember", () => { - it("will leave users from rooms", async () => { - const userSync = CreateUserSync([new RemoteUser("123456")]); - const guild = new MockGuild( - "654321"); - const member = new MockMember( - "123456", - "username", - guild); - return userSync.OnRemoveGuildMember(member as any).then(() => { - expect(LEAVES).to.equal(GUILD_ROOM_IDS.length); - }); - }); + it("will leave users from rooms", async () => { + const userSync = CreateUserSync([new RemoteUser("123456")]); + const guild = new MockGuild( + "654321"); + const member = new MockMember( + "123456", + "username", + guild); + await userSync.OnRemoveGuildMember(member as any); + expect(LEAVES).to.equal(GUILD_ROOM_IDS.length); + }); }); describe("OnUpdateGuildMember", () => { it("will update state for rooms", async () => { @@ -486,9 +470,8 @@ describe("UserSyncroniser", () => { "username", guild, "FiddleDee"); - return userSync.OnUpdateGuildMember(oldMember as any, newMember as any).then(() => { - expect(SEV_COUNT).to.equal(GUILD_ROOM_IDS.length); - }); + await userSync.OnUpdateGuildMember(oldMember as any, newMember as any); + expect(SEV_COUNT).to.equal(GUILD_ROOM_IDS.length); }); it("will not update state for unchanged member", async () => { const userSync = CreateUserSync([new RemoteUser("123456")]); @@ -504,53 +487,54 @@ describe("UserSyncroniser", () => { "username", guild, "FiddleDee"); - return userSync.OnUpdateGuildMember(oldMember as any, newMember as any).then(() => { - expect(SEV_COUNT).to.equal(0); - }); + await userSync.OnUpdateGuildMember(oldMember as any, newMember as any); + expect(SEV_COUNT).to.equal(0); }); }); describe("OnMemberState", () => { it("will update state for rooms", async () => { const userSync = CreateUserSync([new RemoteUser("123456")]); - return userSync.OnMemberState({ + await userSync.OnMemberState({ content: { }, room_id: "!found:localhost", state_key: "123456", - } as IMatrixEvent, 0).then(() => { - expect(SEV_COUNT).to.equal(1); - }); + } as IMatrixEvent, 0); + expect(SEV_COUNT).to.equal(1); }); it("will not update state for a unknown user", async () => { const userSync = CreateUserSync([]); - return expect(userSync.OnMemberState({ + const ret = await userSync.OnMemberState({ content: { }, room_id: "!abcdef:localhost", state_key: "123456", - } as IMatrixEvent, 0)).to.eventually.equal(UserSyncroniser.ERR_USER_NOT_FOUND); + } as IMatrixEvent, 0); + expect(ret).equals(UserSyncroniser.ERR_USER_NOT_FOUND); }); it("will not update state for a unknown room", async () => { const userSync = CreateUserSync([new RemoteUser("123456")]); - return expect(userSync.OnMemberState({ + const ret = await userSync.OnMemberState({ content: { }, room_id: "!notfound:localhost", state_key: "123456", - } as IMatrixEvent, 0)).to.eventually.equal(UserSyncroniser.ERR_CHANNEL_MEMBER_NOT_FOUND); + } as IMatrixEvent, 0); + expect(ret).equals(UserSyncroniser.ERR_CHANNEL_MEMBER_NOT_FOUND); }); it("will not update state for a member not found in the channel", async () => { const userSync = CreateUserSync([new RemoteUser("111222")]); - return expect(userSync.OnMemberState({ + const ret = await userSync.OnMemberState({ content: { }, room_id: "!found:localhost", state_key: "111222", - } as IMatrixEvent, 0)).to.eventually.equal(UserSyncroniser.ERR_CHANNEL_MEMBER_NOT_FOUND); + } as IMatrixEvent, 0); + expect(ret).equals(UserSyncroniser.ERR_CHANNEL_MEMBER_NOT_FOUND); }); it("will not process old events", async () => { const DELAY_MS = 250; diff --git a/test/test_util.ts b/test/test_util.ts index 436643e113f10996f260c9c6ffe8f7263c248dc3..07a8151e089517bf7cba19c331adf4e468200f44 100644 --- a/test/test_util.ts +++ b/test/test_util.ts @@ -66,9 +66,8 @@ describe("Util", () => { }, }, }; - return Util.ParseCommand(action, parameters, ["hello", "world"]).then((retStr) => { - expect(retStr).equal("param1: param1_hello\nparam2: param2_world"); - }); + const retStr = await Util.ParseCommand(action, parameters, ["hello", "world"]); + expect(retStr).equal("param1: param1_hello\nparam2: param2_world"); }); }); describe("GetMxidFromName", () => { @@ -83,9 +82,8 @@ describe("Util", () => { ], }; const intent = CreateMockIntent(mockRooms); - return Util.GetMxidFromName(intent, "goodboy", ["abc"]).then((mxid) => { - expect(mxid).equal("@123:localhost"); - }); + const mxid = await Util.GetMxidFromName(intent, "goodboy", ["abc"]); + expect(mxid).equal("@123:localhost"); }); it("Errors on multiple members", async () => { const mockRooms = { @@ -103,9 +101,14 @@ describe("Util", () => { ], }; const intent = CreateMockIntent(mockRooms); - return expect(Util.GetMxidFromName(intent, "goodboy", ["abc"])).to.eventually.be.rejected; + try { + await Util.GetMxidFromName(intent, "goodboy", ["abc"]); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } }); - it("Errors on no member", () => { + it("Errors on no member", async () => { const mockRooms = { "/rooms/abc/members": [ { @@ -116,7 +119,12 @@ describe("Util", () => { ], }; const intent = CreateMockIntent(mockRooms); - return expect(Util.GetMxidFromName(intent, "badboy", ["abc"])).to.eventually.be.rejected; + try { + await Util.GetMxidFromName(intent, "badboy", ["abc"]); + throw new Error("didn't fail"); + } catch (e) { + expect(e.message).to.not.equal("didn't fail"); + } }); }); describe("GetReplyFromReplyBody", () => { @@ -124,23 +132,23 @@ describe("Util", () => { const reply = Util.GetReplyFromReplyBody(`> <@alice:example.org> This is the original body This is where the reply goes`); - return expect(reply).to.equal("This is where the reply goes"); + expect(reply).to.equal("This is where the reply goes"); }); it("Should get a multi-line reply from the body", () => { const reply = Util.GetReplyFromReplyBody(`> <@alice:example.org> This is the original body This is where the reply goes and there are even more lines here.`); - return expect(reply).to.equal("This is where the reply goes and\nthere are even more lines here."); + expect(reply).to.equal("This is where the reply goes and\nthere are even more lines here."); }); it("Should get empty string from an empty reply", () => { const reply = Util.GetReplyFromReplyBody(`> <@alice:example.org> This is the original body `); - return expect(reply).to.equal(""); + expect(reply).to.equal(""); }); it("Should return body if no reply found", () => { const reply = Util.GetReplyFromReplyBody("Test\nwith\nhalfy"); - return expect(reply).to.equal("Test\nwith\nhalfy"); + expect(reply).to.equal("Test\nwith\nhalfy"); }); }); });