corde
Version:
A simple library for Discord bot tests
249 lines (213 loc) • 7.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true,
});
exports.RoleMatchesImpl = exports.ToHaveResultMatcher = exports.MessageMatches = void 0;
const testCollector_1 = require("../common/testCollector");
const matches_1 = require("./matches");
const utils_1 = require("../utils");
const getStackTrace_1 = require("../utils/getStackTrace");
const runtime_1 = require("../common/runtime");
const toHaveResult_1 = require("./matches/toHaveResult");
class BaseMatcher {
constructor({ commandName, isNot, isCascade, channelId, guildId, channelIdToSendCommand }) {
this._commandName = commandName;
this._isNot = isNot ?? false;
this._isCascade = isCascade ?? false;
this._guildId = guildId;
this._channelId = channelId;
this._channelIdToSendCommand = channelIdToSendCommand;
}
async operationFactory(trace, type, cordeBot, ...params) {
const commandName = await (0, utils_1.resolveName)(this._commandName);
const op = new type({
cordeBot,
command: commandName,
isNot: this._isNot,
timeout: runtime_1.runtime.timeout,
isCascade: this._isCascade,
guildId: this._guildId ?? runtime_1.runtime.guildId,
channelId: this._channelId ?? runtime_1.runtime.channelId,
channelIdToSendCommand: this._channelIdToSendCommand,
});
if (
!this._isCascade &&
(commandName == undefined ||
(typeof commandName === "string" && (0, utils_1.stringIsNullOrEmpty)(commandName)))
) {
return {
pass: false,
message: (0, utils_1.buildReportMessage)("command can not be null or an empty string"),
testName: op.toString(),
};
}
const report = await op.action(...params);
if (!report) {
return {
pass: false,
message: (0, utils_1.buildReportMessage)("no report was provided by the test"),
testName: op.toString(),
trace,
};
}
if (report.pass) {
return report;
}
report.trace = trace;
return report;
}
returnOrAddToCollector(testFunction) {
if (this._isCascade) {
return testFunction;
}
return testCollector_1.testCollector.addTestFunction(testFunction);
}
}
class MessageMatches extends BaseMatcher {
toEmbedMatch(embed) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toEmbedMatch");
return this.returnOrAddToCollector((cordeBot) =>
this.operationFactory(trace, matches_1.ToEmbedMatch, cordeBot, embed),
);
}
toMessageContentContains(expectedContent) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toMessageContentContains");
return this.returnOrAddToCollector((cordeBot) =>
this.operationFactory(trace, matches_1.ToMessageContentContains, cordeBot, expectedContent),
);
}
toReturn(expect) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toReturn");
return this.returnOrAddToCollector((cordeBot) =>
this.operationFactory(trace, matches_1.ToReturn, cordeBot, expect),
);
}
toEditMessage(newValue, messageIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toEditMessage");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(
trace,
matches_1.ToEditMessage,
cordeBot,
newValue,
messageIdentifier,
);
});
}
toPin(messageIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toPin");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(trace, matches_1.ToPinMessage, cordeBot, messageIdentifier);
});
}
toUnPin(messageIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toUnPin");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(trace, matches_1.ToUnPinMessage, cordeBot, messageIdentifier);
});
}
toAddReaction(emojis, messageIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toAddReaction");
return this.returnOrAddToCollector((cordeBot) =>
this.operationFactory(trace, matches_1.ToAddReaction, cordeBot, emojis, messageIdentifier),
);
}
toRemoveReaction(emojis, messageIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toRemoveReaction");
return this.returnOrAddToCollector((cordeBot) =>
this.operationFactory(trace, matches_1.ToRemoveReaction, cordeBot, emojis, messageIdentifier),
);
}
}
exports.MessageMatches = MessageMatches;
class ToHaveResultMatcher extends BaseMatcher {
toHaveResult(...tests) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "todoInCascade");
testCollector_1.testCollector.addTestFunction((cordeBot) => {
const testEnhanced = tests.map((test) => () => test(cordeBot));
return this.operationFactory(trace, toHaveResult_1.IToHaveResult, cordeBot, ...testEnhanced);
});
}
}
exports.ToHaveResultMatcher = ToHaveResultMatcher;
class RoleMatchesImpl extends BaseMatcher {
toRenameRole(newName, roleIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toRenameRole");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(
trace,
matches_1.ToRenameRole,
cordeBot,
newName,
roleIdentifier,
);
});
}
toSetRoleColor(color, roleIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toSetRoleColor");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(
trace,
matches_1.ToSetRoleColor,
cordeBot,
color,
roleIdentifier,
);
});
}
toDeleteRole(roleIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toDeleteRole");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(trace, matches_1.ToDeleteRole, cordeBot, roleIdentifier);
});
}
toSetRoleMentionable(mentionable, roleIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toSetRoleMentionable");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(
trace,
matches_1.ToSetRoleMentionable,
cordeBot,
mentionable,
roleIdentifier,
);
});
}
toSetRoleHoist(hoist, roleIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toSetRoleHoist");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(
trace,
matches_1.ToSetRoleHoist,
cordeBot,
hoist,
roleIdentifier,
);
});
}
toSetRolePosition(newPosition, roleIdentifier) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toSetRolePosition");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(
trace,
matches_1.ToSetRolePosition,
cordeBot,
newPosition,
roleIdentifier,
);
});
}
toSetRolePermission(roleIdentifier, ...permissions) {
const trace = (0, getStackTrace_1.getStackTrace)(undefined, true, "toSetRolePermission");
return this.returnOrAddToCollector((cordeBot) => {
return this.operationFactory(
trace,
matches_1.ToSetRolePermission,
cordeBot,
roleIdentifier,
permissions,
);
});
}
}
exports.RoleMatchesImpl = RoleMatchesImpl;