thelounge/test/commands/mode.ts

150 lines
5 KiB
TypeScript
Raw Normal View History

import {expect} from "chai";
2024-04-17 18:17:14 +00:00
import Chan from "../../server/models/chan";
import {ChanType} from "../../shared/types/chan";
import ModeCommand from "../../server/plugins/inputs/mode";
2016-10-08 15:56:41 +00:00
describe("Commands", function () {
describe("/mode", function () {
2016-10-08 15:56:41 +00:00
const channel = new Chan({
name: "#thelounge",
2016-10-08 15:56:41 +00:00
});
const lobby = new Chan({
name: "Network Lobby",
type: ChanType.LOBBY,
2016-10-08 15:56:41 +00:00
});
const testableNetwork = {
firstCommand: null,
2016-10-08 15:56:41 +00:00
lastCommand: null,
nick: "xPaw",
irc: {
2021-07-03 21:20:28 +00:00
network: {
supports(type: string) {
2021-07-03 21:20:28 +00:00
if (type.toUpperCase() === "MODES") {
return "4";
}
},
},
raw(...args: string[]) {
testableNetwork.firstCommand = testableNetwork.lastCommand;
testableNetwork.lastCommand = args.join(" ");
},
},
} as {
firstCommand: string | null;
lastCommand: string | null;
nick: string;
irc: {
network: {
supports(type: string): string;
};
raw(...args: string[]): void;
};
2016-10-08 15:56:41 +00:00
};
const testableNetworkNoSupports = Object.assign({}, testableNetwork, {
irc: {
network: {
supports() {
return null;
},
},
raw(...args: string[]) {
testableNetworkNoSupports.firstCommand = testableNetworkNoSupports.lastCommand;
testableNetworkNoSupports.lastCommand = args.join(" ");
},
},
});
2024-04-17 18:17:14 +00:00
function modeCommandInputCall(net, chan, cmd, args) {
ModeCommand.input.call({} as any, net as any, chan, cmd, Array.from(args));
}
it("should not mess with the given target", function () {
const test = function (expected: string, args: string[]) {
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "mode", Array.from(args));
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal(expected);
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, lobby, "mode", args);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal(expected);
};
test("MODE xPaw +i", ["xPaw", "+i"]);
test("MODE xPaw -w", ["xPaw", "-w"]);
test("MODE #thelounge +o xPaw", ["#thelounge", "+o", "xPaw"]);
test("MODE #thelounge -v xPaw", ["#thelounge", "-v", "xPaw"]);
test("MODE #thelounge +o-o xPaw Max-P", ["#thelounge", "+o-o", "xPaw", "Max-P"]);
test("MODE #thelounge", ["#thelounge"]);
});
it("should assume target if none given", function () {
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "mode", []);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, lobby, "mode", []);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE xPaw");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "mode", ["+b"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +b");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, lobby, "mode", ["+b"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE xPaw +b");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "mode", ["-o", "hey"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -o hey");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, lobby, "mode", ["-i", "idk"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE xPaw -i idk");
});
it("should support shorthand commands", function () {
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "op", ["xPaw"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +o xPaw");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "deop", ["xPaw"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -o xPaw");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "hop", ["xPaw"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +h xPaw");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "dehop", ["xPaw"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -h xPaw");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "voice", ["xPaw"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +v xPaw");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "devoice", ["xPaw"]);
2016-10-08 15:56:41 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v xPaw");
});
2016-10-08 15:56:41 +00:00
it("should use ISUPPORT MODES on shorthand commands", function () {
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "voice", ["xPaw", "Max-P"]);
2021-07-03 21:20:28 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +vv xPaw Max-P");
// since the limit for modes on tests is 4, it should send two commands
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetwork, channel, "devoice", [
2021-07-03 21:20:28 +00:00
"xPaw",
"Max-P",
"hey",
"idk",
"thelounge",
]);
expect(testableNetwork.firstCommand).to.equal(
"MODE #thelounge -vvvv xPaw Max-P hey idk"
);
2021-07-03 21:20:28 +00:00
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v thelounge");
2016-10-08 15:56:41 +00:00
});
2021-07-06 15:48:01 +00:00
it("should fallback to all modes at once for shorthand commands", function () {
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetworkNoSupports, channel, "voice", ["xPaw"]);
expect(testableNetworkNoSupports.lastCommand).to.equal("MODE #thelounge +v xPaw");
2024-04-17 18:17:14 +00:00
modeCommandInputCall(testableNetworkNoSupports, channel, "devoice", ["xPaw", "Max-P"]);
2021-07-06 15:48:01 +00:00
expect(testableNetworkNoSupports.lastCommand).to.equal(
"MODE #thelounge -vv xPaw Max-P"
);
});
2016-10-08 15:56:41 +00:00
});
});