2022-06-19 00:25:21 +00:00
|
|
|
import {expect} from "chai";
|
2016-03-15 05:40:48 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
import Chan from "../../server/models/chan";
|
|
|
|
import Msg from "../../server/models/msg";
|
|
|
|
import Network from "../../server/models/network";
|
|
|
|
import Prefix from "../../server/models/prefix";
|
|
|
|
import User from "../../server/models/user";
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("Chan", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const network = {
|
|
|
|
network: {
|
|
|
|
options: {
|
|
|
|
PREFIX: [
|
|
|
|
{symbol: "~", mode: "q"},
|
|
|
|
{symbol: "&", mode: "a"},
|
|
|
|
{symbol: "@", mode: "o"},
|
|
|
|
{symbol: "%", mode: "h"},
|
|
|
|
{symbol: "+", mode: "v"},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const prefixLookup = {modeToSymbol: {}} as Prefix;
|
2017-11-16 20:32:03 +00:00
|
|
|
|
|
|
|
network.network.options.PREFIX.forEach((mode) => {
|
2021-07-21 07:30:07 +00:00
|
|
|
prefixLookup.modeToSymbol[mode.mode] = mode.symbol;
|
2017-11-16 20:32:03 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("#findMessage(id)", function () {
|
2017-07-24 06:01:25 +00:00
|
|
|
const chan = new Chan({
|
|
|
|
messages: [
|
2018-04-27 10:16:23 +00:00
|
|
|
new Msg({id: 1}),
|
2017-07-24 06:01:25 +00:00
|
|
|
new Msg({
|
2018-04-27 10:16:23 +00:00
|
|
|
id: 2,
|
2017-11-15 06:35:15 +00:00
|
|
|
text: "Message to be found",
|
2017-07-24 06:01:25 +00:00
|
|
|
}),
|
2017-11-15 06:35:15 +00:00
|
|
|
new Msg(),
|
|
|
|
],
|
2017-07-24 06:01:25 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should find a message in the list of messages", function () {
|
2022-06-19 00:25:21 +00:00
|
|
|
expect(chan.findMessage(2)?.text).to.equal("Message to be found");
|
2017-07-24 06:01:25 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should not find a message that does not exist", function () {
|
2017-07-24 06:01:25 +00:00
|
|
|
expect(chan.findMessage(42)).to.be.undefined;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("#setUser(user)", function () {
|
|
|
|
it("should make key lowercase", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const chan = new Chan();
|
|
|
|
chan.setUser(new User({nick: "TestUser"}));
|
|
|
|
|
|
|
|
expect(chan.users.has("testuser")).to.be.true;
|
|
|
|
});
|
2016-09-24 16:34:35 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should update user object", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const chan = new Chan();
|
|
|
|
chan.setUser(new User({nick: "TestUser"}, prefixLookup));
|
|
|
|
chan.setUser(new User({nick: "TestUseR", modes: ["o"]}, prefixLookup));
|
|
|
|
const user = chan.getUser("TestUSER");
|
2016-09-24 16:34:35 +00:00
|
|
|
|
2017-11-16 20:32:03 +00:00
|
|
|
expect(user.mode).to.equal("@");
|
2016-09-24 16:34:35 +00:00
|
|
|
});
|
2017-11-16 20:32:03 +00:00
|
|
|
});
|
2016-09-24 16:34:35 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("#getUser(nick)", function () {
|
|
|
|
it("should returning existing object", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const chan = new Chan();
|
|
|
|
chan.setUser(new User({nick: "TestUseR", modes: ["o"]}, prefixLookup));
|
|
|
|
const user = chan.getUser("TestUSER");
|
|
|
|
|
|
|
|
expect(user.mode).to.equal("@");
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should make new User object if not found", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const chan = new Chan();
|
|
|
|
const user = chan.getUser("very-testy-user");
|
|
|
|
|
|
|
|
expect(user.nick).to.equal("very-testy-user");
|
|
|
|
});
|
|
|
|
});
|
2016-09-24 16:34:35 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("#getSortedUsers(irc)", function () {
|
2022-06-19 00:25:21 +00:00
|
|
|
const getUserNames = function (chan: Chan) {
|
|
|
|
return chan.getSortedUsers(network as Network["irc"]).map((u) => u.nick);
|
2016-09-24 16:34:35 +00:00
|
|
|
};
|
2016-03-15 05:40:48 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("returns unsorted list on null irc object", function () {
|
2018-02-07 09:56:49 +00:00
|
|
|
const chan = new Chan();
|
2019-07-17 09:33:59 +00:00
|
|
|
["JocelynD", "YaManicKill", "astorije", "xPaw", "Max-P"].forEach((nick) =>
|
|
|
|
chan.setUser(new User({nick}))
|
|
|
|
);
|
2018-02-07 09:56:49 +00:00
|
|
|
|
|
|
|
expect(chan.getSortedUsers().map((u) => u.nick)).to.deep.equal([
|
2019-07-17 09:33:59 +00:00
|
|
|
"JocelynD",
|
|
|
|
"YaManicKill",
|
|
|
|
"astorije",
|
|
|
|
"xPaw",
|
|
|
|
"Max-P",
|
2018-02-07 09:56:49 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should sort a simple user list", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const chan = new Chan();
|
2019-07-17 09:33:59 +00:00
|
|
|
["JocelynD", "YaManicKill", "astorije", "xPaw", "Max-P"].forEach((nick) =>
|
|
|
|
chan.setUser(new User({nick}, prefixLookup))
|
|
|
|
);
|
2016-03-15 05:40:48 +00:00
|
|
|
|
|
|
|
expect(getUserNames(chan)).to.deep.equal([
|
2019-07-17 09:33:59 +00:00
|
|
|
"astorije",
|
|
|
|
"JocelynD",
|
|
|
|
"Max-P",
|
|
|
|
"xPaw",
|
|
|
|
"YaManicKill",
|
2016-03-15 05:40:48 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should group users by modes", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const chan = new Chan();
|
|
|
|
chan.setUser(new User({nick: "JocelynD", modes: ["a", "o"]}, prefixLookup));
|
|
|
|
chan.setUser(new User({nick: "YaManicKill", modes: ["v"]}, prefixLookup));
|
|
|
|
chan.setUser(new User({nick: "astorije", modes: ["h"]}, prefixLookup));
|
|
|
|
chan.setUser(new User({nick: "xPaw", modes: ["q"]}, prefixLookup));
|
|
|
|
chan.setUser(new User({nick: "Max-P", modes: ["o"]}, prefixLookup));
|
2016-03-15 05:40:48 +00:00
|
|
|
|
|
|
|
expect(getUserNames(chan)).to.deep.equal([
|
2019-07-17 09:33:59 +00:00
|
|
|
"xPaw",
|
|
|
|
"JocelynD",
|
|
|
|
"Max-P",
|
|
|
|
"astorije",
|
|
|
|
"YaManicKill",
|
2016-03-15 05:40:48 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should sort a mix of users and modes", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const chan = new Chan();
|
|
|
|
chan.setUser(new User({nick: "JocelynD"}, prefixLookup));
|
|
|
|
chan.setUser(new User({nick: "YaManicKill", modes: ["o"]}, prefixLookup));
|
|
|
|
chan.setUser(new User({nick: "astorije"}, prefixLookup));
|
|
|
|
chan.setUser(new User({nick: "xPaw"}, prefixLookup));
|
|
|
|
chan.setUser(new User({nick: "Max-P", modes: ["o"]}, prefixLookup));
|
2016-03-15 05:40:48 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(getUserNames(chan)).to.deep.equal([
|
|
|
|
"Max-P",
|
|
|
|
"YaManicKill",
|
|
|
|
"astorije",
|
|
|
|
"JocelynD",
|
|
|
|
"xPaw",
|
|
|
|
]);
|
2016-03-15 05:40:48 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should be case-insensitive", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const chan = new Chan();
|
2019-07-17 09:33:59 +00:00
|
|
|
["aB", "Ad", "AA", "ac"].forEach((nick) =>
|
|
|
|
chan.setUser(new User({nick}, prefixLookup))
|
|
|
|
);
|
2016-03-15 05:40:48 +00:00
|
|
|
|
|
|
|
expect(getUserNames(chan)).to.deep.equal(["AA", "aB", "ac", "Ad"]);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should parse special characters successfully", function () {
|
2017-11-16 20:32:03 +00:00
|
|
|
const chan = new Chan();
|
|
|
|
[
|
2019-07-17 09:33:59 +00:00
|
|
|
"[foo",
|
|
|
|
"]foo",
|
|
|
|
"(foo)",
|
|
|
|
"{foo}",
|
|
|
|
"<foo>",
|
|
|
|
"_foo",
|
|
|
|
"@foo",
|
|
|
|
"^foo",
|
|
|
|
"&foo",
|
|
|
|
"!foo",
|
|
|
|
"+foo",
|
|
|
|
"Foo",
|
2018-03-05 00:59:16 +00:00
|
|
|
].forEach((nick) => chan.setUser(new User({nick}, prefixLookup)));
|
2016-03-15 05:40:48 +00:00
|
|
|
|
|
|
|
expect(getUserNames(chan)).to.deep.equal([
|
2019-07-17 09:33:59 +00:00
|
|
|
"!foo",
|
|
|
|
"&foo",
|
|
|
|
"(foo)",
|
|
|
|
"+foo",
|
|
|
|
"<foo>",
|
|
|
|
"@foo",
|
|
|
|
"[foo",
|
|
|
|
"]foo",
|
|
|
|
"^foo",
|
|
|
|
"_foo",
|
|
|
|
"Foo",
|
|
|
|
"{foo}",
|
2016-03-15 05:40:48 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2017-11-30 21:46:55 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("#getFilteredClone(lastActiveChannel, lastMessage)", function () {
|
|
|
|
it("should send empty user list", function () {
|
2017-11-30 21:46:55 +00:00
|
|
|
const chan = new Chan();
|
|
|
|
chan.setUser(new User({nick: "test"}));
|
|
|
|
|
|
|
|
expect(chan.getFilteredClone().users).to.be.empty;
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should keep necessary properties", function () {
|
2017-11-30 21:46:55 +00:00
|
|
|
const chan = new Chan();
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(chan.getFilteredClone())
|
|
|
|
.to.be.an("object")
|
|
|
|
.that.has.all.keys(
|
|
|
|
"firstUnread",
|
|
|
|
"highlight",
|
|
|
|
"id",
|
|
|
|
"key",
|
|
|
|
"messages",
|
2022-02-11 01:56:17 +00:00
|
|
|
"muted",
|
2019-10-17 10:27:15 +00:00
|
|
|
"totalMessages",
|
2019-07-17 09:33:59 +00:00
|
|
|
"name",
|
|
|
|
"state",
|
|
|
|
"topic",
|
|
|
|
"type",
|
|
|
|
"unread",
|
|
|
|
"users"
|
|
|
|
);
|
2017-11-30 21:46:55 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should send only last message for non active channel", function () {
|
2017-11-30 21:46:55 +00:00
|
|
|
const chan = new Chan({
|
|
|
|
id: 1337,
|
|
|
|
messages: [
|
|
|
|
new Msg({id: 10}),
|
|
|
|
new Msg({id: 11}),
|
|
|
|
new Msg({id: 12}),
|
|
|
|
new Msg({id: 13}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chan.id).to.equal(1337);
|
|
|
|
|
|
|
|
const messages = chan.getFilteredClone(999).messages;
|
|
|
|
|
|
|
|
expect(messages).to.have.lengthOf(1);
|
|
|
|
expect(messages[0].id).to.equal(13);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should send more messages for active channel", function () {
|
2017-11-30 21:46:55 +00:00
|
|
|
const chan = new Chan({
|
|
|
|
id: 1337,
|
|
|
|
messages: [
|
|
|
|
new Msg({id: 10}),
|
|
|
|
new Msg({id: 11}),
|
|
|
|
new Msg({id: 12}),
|
|
|
|
new Msg({id: 13}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chan.id).to.equal(1337);
|
|
|
|
|
|
|
|
const messages = chan.getFilteredClone(1337).messages;
|
|
|
|
|
|
|
|
expect(messages).to.have.lengthOf(4);
|
|
|
|
expect(messages[0].id).to.equal(10);
|
|
|
|
expect(messages[3].id).to.equal(13);
|
2017-12-03 14:29:50 +00:00
|
|
|
|
|
|
|
expect(chan.getFilteredClone(true).messages).to.have.lengthOf(4);
|
2017-11-30 21:46:55 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should only send new messages", function () {
|
2017-11-30 21:46:55 +00:00
|
|
|
const chan = new Chan({
|
|
|
|
id: 1337,
|
|
|
|
messages: [
|
|
|
|
new Msg({id: 10}),
|
|
|
|
new Msg({id: 11}),
|
|
|
|
new Msg({id: 12}),
|
|
|
|
new Msg({id: 13}),
|
|
|
|
new Msg({id: 14}),
|
|
|
|
new Msg({id: 15}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chan.id).to.equal(1337);
|
|
|
|
|
|
|
|
const messages = chan.getFilteredClone(1337, 12).messages;
|
|
|
|
|
|
|
|
expect(messages).to.have.lengthOf(3);
|
|
|
|
expect(messages[0].id).to.equal(13);
|
|
|
|
expect(messages[2].id).to.equal(15);
|
|
|
|
});
|
|
|
|
});
|
2016-03-15 05:40:48 +00:00
|
|
|
});
|