Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider b88c1523 rédigé par Sorunome's avatar Sorunome
Parcourir les fichiers

accidentally add tests

parent 28eccc02
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
import * as Discord from "discord.js"; import * as Discord from "discord.js";
import { MockUser } from "./user"; import { MockUser } from "./user";
import { MockCollection } from "./collection";
// we are a test file and thus need those // we are a test file and thus need those
/* tslint:disable:no-unused-expression max-file-line-count no-any */ /* tslint:disable:no-unused-expression max-file-line-count no-any */
export class MockMessage { export class MockMessage {
public attachments = new MockCollection<string, any>();
public embeds: any[] = []; public embeds: any[] = [];
public content = ""; public content = "";
public channel: Discord.TextChannel | undefined; public channel: Discord.TextChannel | undefined;
......
...@@ -8,6 +8,7 @@ import { MockMember } from "./mocks/member"; ...@@ -8,6 +8,7 @@ import { MockMember } from "./mocks/member";
import { DiscordBot } from "../src/bot"; import { DiscordBot } from "../src/bot";
import { MockDiscordClient } from "./mocks/discordclient"; import { MockDiscordClient } from "./mocks/discordclient";
import { MockMessage } from "./mocks/message"; import { MockMessage } from "./mocks/message";
import { Util } from "../src/util";
// we are a test file and thus need those // we are a test file and thus need those
/* tslint:disable:no-unused-expression max-file-line-count no-any */ /* tslint:disable:no-unused-expression max-file-line-count no-any */
...@@ -46,6 +47,14 @@ const mockBridge = { ...@@ -46,6 +47,14 @@ const mockBridge = {
const modDiscordBot = Proxyquire("../src/bot", { const modDiscordBot = Proxyquire("../src/bot", {
"./clientfactory": require("./mocks/discordclientfactory"), "./clientfactory": require("./mocks/discordclientfactory"),
"./util": {
Util: {
AsyncForEach: Util.AsyncForEach,
UploadContentFromUrl: async () => {
return {mxcUrl: "uploaded"};
},
},
},
}); });
describe("DiscordBot", () => { describe("DiscordBot", () => {
let discordBot; let discordBot;
...@@ -103,6 +112,169 @@ describe("DiscordBot", () => { ...@@ -103,6 +112,169 @@ describe("DiscordBot", () => {
await discordBot.LookupRoom("123", "321"); await discordBot.LookupRoom("123", "321");
}); });
}); });
describe("OnMessage()", () => {
let SENT_MESSAGE = false;
let MARKED = -1;
let HANDLE_COMMAND = false;
let ATTACHMENT = {} as any;
let MSGTYPE = "";
function getDiscordBot() {
SENT_MESSAGE = false;
MARKED = -1;
HANDLE_COMMAND = false;
ATTACHMENT = {};
MSGTYPE = "";
const discord = new modDiscordBot.DiscordBot(
config,
mockBridge,
);
discord.bot = { user: { id: "654" } };
discord.provisioner = {
HasPendingRequest: (chan) => true,
MarkApproved: async (chan, member, approved) => {
MARKED = approved ? 1 : 0;
return approved;
},
};
discord.GetIntentFromDiscordMember = (_) => {return {
sendMessage: async (room, msg) => {
SENT_MESSAGE = true;
if (msg.info) {
ATTACHMENT = msg.info;
}
MSGTYPE = msg.msgtype;
return {
event_id: "$fox:localhost",
};
},
}; };
discord.userSync = {
OnUpdateUser: async (user) => { },
};
discord.channelSync = {
GetRoomIdsFromChannel: async (chan) => ["!asdf:localhost"],
};
discord.roomHandler = {
HandleDiscordCommand: async (msg) => { HANDLE_COMMAND = true; },
};
discord.store = {
Insert: async (_) => { },
};
return discord;
}
it("ignores own messages", async () => {
discordBot = getDiscordBot();
const guild: any = new MockGuild("123", []);
const author = new MockMember("654", "TestUsername");
guild._mockAddMember(author);
const channel = new Discord.TextChannel(guild, {} as any);
const msg = new MockMessage(channel) as any;
msg.author = author;
msg.content = "Hi!";
await discordBot.OnMessage(msg);
Chai.assert.equal(SENT_MESSAGE, false);
});
it("accepts !approve", async () => {
discordBot = getDiscordBot();
const channel = new Discord.TextChannel({} as any, {} as any);
const msg = new MockMessage(channel) as any;
msg.content = "!approve";
await discordBot.OnMessage(msg);
Chai.assert.equal(MARKED, 1);
});
it("denies !deny", async () => {
discordBot = getDiscordBot();
const channel = new Discord.TextChannel({} as any, {} as any);
const msg = new MockMessage(channel) as any;
msg.content = "!deny";
await discordBot.OnMessage(msg);
Chai.assert.equal(MARKED, 0);
});
it("Passes on !matrix commands", async () => {
discordBot = getDiscordBot();
const channel = new Discord.TextChannel({} as any, {} as any);
const msg = new MockMessage(channel) as any;
msg.content = "!matrix test";
await discordBot.OnMessage(msg);
Chai.assert.equal(HANDLE_COMMAND, true);
});
it("skips empty messages", async () => {
discordBot = getDiscordBot();
const channel = new Discord.TextChannel({} as any, {} as any);
const msg = new MockMessage(channel) as any;
msg.content = "";
await discordBot.OnMessage(msg);
Chai.assert.equal(SENT_MESSAGE, false);
});
it("sends normal messages", async () => {
discordBot = getDiscordBot();
const channel = new Discord.TextChannel({} as any, {} as any);
const msg = new MockMessage(channel) as any;
msg.content = "Foxies are amazing!";
await discordBot.OnMessage(msg);
Chai.assert.equal(SENT_MESSAGE, true);
});
it("uploads images", async () => {
discordBot = getDiscordBot();
const channel = new Discord.TextChannel({} as any, {} as any);
const msg = new MockMessage(channel) as any;
msg.attachments.set("1234", {
filename: "someimage.png",
filesize: 42,
height: 0,
url: "asdf",
width: 0,
});
await discordBot.OnMessage(msg);
Chai.assert.equal(MSGTYPE, "m.image");
Chai.assert.equal(ATTACHMENT.mimetype, "image/png");
});
it("uploads videos", async () => {
discordBot = getDiscordBot();
const channel = new Discord.TextChannel({} as any, {} as any);
const msg = new MockMessage(channel) as any;
msg.attachments.set("1234", {
filename: "foxes.mov",
filesize: 42,
height: 0,
url: "asdf",
width: 0,
});
await discordBot.OnMessage(msg);
Chai.assert.equal(MSGTYPE, "m.video");
Chai.assert.equal(ATTACHMENT.mimetype, "video/quicktime");
});
it("uploads audio", async () => {
discordBot = getDiscordBot();
const channel = new Discord.TextChannel({} as any, {} as any);
const msg = new MockMessage(channel) as any;
msg.attachments.set("1234", {
filename: "meow.mp3",
filesize: 42,
height: 0,
url: "asdf",
width: 0,
});
await discordBot.OnMessage(msg);
Chai.assert.equal(MSGTYPE, "m.audio");
Chai.assert.equal(ATTACHMENT.mimetype, "audio/mpeg");
});
it("uploads other files", async () => {
discordBot = getDiscordBot();
const channel = new Discord.TextChannel({} as any, {} as any);
const msg = new MockMessage(channel) as any;
msg.attachments.set("1234", {
filename: "meow.zip",
filesize: 42,
height: 0,
url: "asdf",
width: 0,
});
await discordBot.OnMessage(msg);
Chai.assert.equal(MSGTYPE, "m.file");
Chai.assert.equal(ATTACHMENT.mimetype, "application/zip");
});
});
describe("OnMessageUpdate()", () => { describe("OnMessageUpdate()", () => {
it("should return on an unchanged message", async () => { it("should return on an unchanged message", async () => {
discordBot = new modDiscordBot.DiscordBot( discordBot = new modDiscordBot.DiscordBot(
...@@ -232,7 +404,4 @@ describe("DiscordBot", () => { ...@@ -232,7 +404,4 @@ describe("DiscordBot", () => {
// return assert.isFulfilled(discordBot.OnTyping( {id: "321"}, {id: "12345"}, true)); // return assert.isFulfilled(discordBot.OnTyping( {id: "321"}, {id: "12345"}, true));
// }); // });
// }); // });
// describe("OnMessage()", () => {
//
// });
}); });
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter