diff --git a/test/mocks/collection.ts b/test/mocks/collection.ts new file mode 100644 index 0000000000000000000000000000000000000000..b72484108081c3e017e18fa5abee680d707e8214 --- /dev/null +++ b/test/mocks/collection.ts @@ -0,0 +1,9 @@ +export class MockCollection<T1, T2> extends Map { + public array(): T2[] { + return [...this.values()]; + } + + public keyArray(): T1[] { + return [...this.keys()]; + } +} diff --git a/test/mocks/discordclient.ts b/test/mocks/discordclient.ts index a1131628ee13283f5b3f13f2b4a7131feb3c3bb4..c65eb7d3cb8397cca5d01bcb68c43b2f9f1e6c41 100644 --- a/test/mocks/discordclient.ts +++ b/test/mocks/discordclient.ts @@ -1,3 +1,8 @@ +import {MockCollection} from "./collection"; +import {MockGuild} from "./guild"; +import {MockUser} from "./user"; + + export class MockDiscordClient { public guilds = new MockCollection<string, MockGuild>(); public user: MockUser; @@ -37,39 +42,3 @@ export class MockDiscordClient { this.testCallbacks[0](); } } - -class MockMember { - public id = ""; - constructor(id: string) { - this.id = id; - } -} - -class MockUser { - public id = ""; - constructor(id: string) { - this.id = id; - } -} - -class MockGuild { - public channels = new MockCollection<string, any>(); - public members = new MockCollection<string, MockMember>(); - public id: string; - constructor(id: string, channels: any[]) { - this.id = id; - channels.forEach((item) => { - this.channels.set(item.id, item); - }); - } -} - -class MockCollection<T1, T2> extends Map { - public array(): T2[] { - return [...this.values()]; - } - - public keyArray(): T1[] { - return [...this.keys()]; - } -} diff --git a/test/mocks/guild.ts b/test/mocks/guild.ts new file mode 100644 index 0000000000000000000000000000000000000000..d79fd8823165274d9679c7c227b6ecf400095a55 --- /dev/null +++ b/test/mocks/guild.ts @@ -0,0 +1,18 @@ +import {MockCollection} from "./collection"; +import {MockMember} from "./member"; + +export class MockGuild { + public channels = new MockCollection<string, any>(); + public members = new MockCollection<string, MockMember>(); + public id: string; + constructor(id: string, channels: any[]) { + this.id = id; + channels.forEach((item) => { + this.channels.set(item.id, item); + }); + } + + public _mockAddMember(member: MockMember) { + this.members.set(member.id, member); + } +} diff --git a/test/mocks/member.ts b/test/mocks/member.ts new file mode 100644 index 0000000000000000000000000000000000000000..c4f4b442edd0c02d73e3089ba0c741cf4471c2e1 --- /dev/null +++ b/test/mocks/member.ts @@ -0,0 +1,11 @@ +import {MockUser} from "./user"; + +export class MockMember { + public id = ""; + public presence = {status: "offline"}; // TODO: Mock this + public user: MockUser; + constructor(id: string, username: string) { + this.id = id; + this.user = new MockUser(this.id, username); + } +} diff --git a/test/mocks/user.ts b/test/mocks/user.ts new file mode 100644 index 0000000000000000000000000000000000000000..d66db47989c99d347fab338e7eb36208a15ea7b4 --- /dev/null +++ b/test/mocks/user.ts @@ -0,0 +1,8 @@ +export class MockUser { + public id = ""; + public username: string; + constructor(id: string, username: string = "") { + this.id = id; + this.username = username; + } +} diff --git a/test/test_discordbot.ts b/test/test_discordbot.ts index 6040d7c7748ddab9a1e43332f09c02e06ca56dfe..8cb5061c15edad1ffc1131333f58e88a1b05c2b7 100644 --- a/test/test_discordbot.ts +++ b/test/test_discordbot.ts @@ -40,8 +40,11 @@ describe("DiscordBot", () => { let discordBot; const config = { auth: { - botToken: "blah", + botToken: "blah", }, + bridge: { + domain: "localhost", + } }; describe("run()", () => { it("should resolve when ready.", () => { diff --git a/test/test_messageprocessor.ts b/test/test_messageprocessor.ts new file mode 100644 index 0000000000000000000000000000000000000000..fa3120486d12276f51d3a736581e009f2f58d43a --- /dev/null +++ b/test/test_messageprocessor.ts @@ -0,0 +1,82 @@ +import * as Chai from "chai"; +import * as ChaiAsPromised from "chai-as-promised"; +import * as log from "npmlog"; +import * as Discord from "discord.js"; +// import * as Proxyquire from "proxyquire"; +import { MessageProcessor, MessageProcessorOpts } from "../src/messageprocessor"; +import { MockGuild } from "./mocks/guild"; +import { MockMember } from "./mocks/member"; + +Chai.use(ChaiAsPromised); +const expect = Chai.expect; +log.level = "silly"; + +// const assert = Chai.assert; + +describe("MessageProcessor", () => { + describe("init", () => { + it("constructor", () => { + new MessageProcessor(new MessageProcessorOpts("localhost")); + }); + }); + describe("FormatDiscordMessage", () => { + it("processes plain text messages correctly", () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); + const msg = new Discord.Message(null,null,null); + msg.content = "Hello World!"; + const result = processor.FormatDiscordMessage(msg); + Chai.assert(result.body, "Hello World!"); + Chai.assert(result.formatted_body, "Hello World!"); + }); + it("processes markdown messages correctly.", () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); + const msg = new Discord.Message(null,null,null); + msg.content = "Hello *World*!"; + const result = processor.FormatDiscordMessage(msg); + Chai.assert.equal(result.body, "Hello *World*!"); + Chai.assert.equal(result.formatted_body, "<p>Hello <em>World</em>!</p>\n"); + }); + }); + describe("ReplaceMembers", () => { + it("processes members missing from the guild correctly", () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); + const guild :any = new MockGuild("123", []); + const channel = new Discord.TextChannel(guild,null); + const msg = new Discord.Message(channel,null,null); + let content = "Hello <@!12345>"; + content = processor.ReplaceMembers(content, msg); + Chai.assert.equal(content, "Hello @_discord_12345:localhost"); + }); + it("processes members with usernames correctly", () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); + const guild :any = new MockGuild("123", []); + guild._mockAddMember(new MockMember("12345", "TestUsername")); + const channel = new Discord.TextChannel(guild,null); + const msg = new Discord.Message(channel,null,null); + let content = "Hello <@!12345>"; + content = processor.ReplaceMembers(content, msg); + Chai.assert.equal(content, "Hello TestUsername"); + }); + }); + describe("ReplaceChannels", () => { + it("processes unknown channel correctly", () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); + const guild :any = new MockGuild("123", []); + const channel = new Discord.TextChannel(guild,{id:"456", name:"TestChannel"}); + const msg = new Discord.Message(channel,null,null); + let content = "Hello <#123456789>"; + content = processor.ReplaceChannels(content, msg); + Chai.assert.equal(content, "Hello [#123456789](https://matrix.to/#/#_discord_123_123456789:localhost)"); + }); + it("processes channels correctly", () => { + const processor = new MessageProcessor(new MessageProcessorOpts("localhost")); + const guild :any = new MockGuild("123", []); + const channel = new Discord.TextChannel(guild,{id:"456", name:"TestChannel"}); + guild.channels.set("456", channel); + const msg = new Discord.Message(channel,null,null); + let content = "Hello <#456>"; + content = processor.ReplaceChannels(content, msg); + Chai.assert.equal(content, "Hello [#TestChannel](https://matrix.to/#/#_discord_123_456:localhost)"); + }); + }); +});