Sheogorath
5 years ago
No known key found for this signature in database
GPG Key ID: 1F05CC3635CDDFFD
12 changed files with 492 additions and 98 deletions
-
19README.md
-
2package.json
-
32src/channel.js
-
167src/server.js
-
29src/user.js
-
58test/channel.js
-
72test/command_join.js
-
4test/command_nick.js
-
39test/command_pass.js
-
81test/command_privmsg.js
-
1test/command_quit.js
-
84test/user.js
@ -0,0 +1,32 @@ |
|||||
|
function Channel(name) { |
||||
|
this.name = name |
||||
|
this.userlist = [] |
||||
|
|
||||
|
this.join = function(user) { |
||||
|
let channel = this |
||||
|
this.userlist.push(user) |
||||
|
this.userlist.forEach(function(item) { |
||||
|
item.sendRaw(`:${user.nickname} JOIN ${channel.name}`) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
this.part = function(user) { |
||||
|
let channel = this |
||||
|
let userIndex = this.userlist.indexOf(user) |
||||
|
if (userIndex >= 0) { |
||||
|
this.userlist.forEach(function(item) { |
||||
|
item.sendRaw(`:${user.nickname} PART ${channel.name}`) |
||||
|
}) |
||||
|
this.userlist.splice(index, 1) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
this.sendMsg = function(from, message) { |
||||
|
const channel = this |
||||
|
this.userlist.forEach(function(item) { |
||||
|
item.sendMsg(from, message, channel.name) |
||||
|
}) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = Channel |
@ -0,0 +1,58 @@ |
|||||
|
const assert = require('assert'); |
||||
|
const Channel = require("../src/channel.js"); |
||||
|
|
||||
|
describe('Channel', function () { |
||||
|
describe('#contructor()', function () { |
||||
|
it("should create a channel", function () { |
||||
|
const channel = new Channel("#testchan") |
||||
|
assert.equal(typeof channel, 'object') |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
describe('#join(user)', function () { |
||||
|
it("should add the user to a channel and send a JOIN command to the user", function (done) { |
||||
|
const channel = new Channel("#testchan") |
||||
|
let mockedUser = {nickname: "some_nick"} |
||||
|
mockedUser.sendRaw = function(data) { |
||||
|
assert.equal(data, ":some_nick JOIN #testchan") |
||||
|
done() |
||||
|
} |
||||
|
channel.join(mockedUser) |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
describe('#part(user)', function () { |
||||
|
it("should remove a user from a channel and send a PART command to the user", function (done) { |
||||
|
const channel = new Channel("#testchan") |
||||
|
let mockedUser = {nickname: "some_nick"} |
||||
|
mockedUser.sendRaw = function(data) { |
||||
|
// we expect a PART message from our user to the channel (including us)
|
||||
|
if (data.indexOf("PART") >= 0) { |
||||
|
assert.equal(data, ":some_nick PART #testchan") |
||||
|
done() |
||||
|
} |
||||
|
} |
||||
|
// we can't part a channel without joining it first
|
||||
|
channel.join(mockedUser) |
||||
|
|
||||
|
channel.part(mockedUser) |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
describe('#sendMsg(from, message)', function () { |
||||
|
it("should remove a user from a channel and send a PART command to the user", function (done) { |
||||
|
const channel = new Channel("#testchan") |
||||
|
let mockedUser = {nickname: "some_nick", sendRaw: function() {}} |
||||
|
mockedUser.sendMsg = function(from, message, to) { |
||||
|
assert.equal(from, mockedUser) |
||||
|
assert.equal(message, "test message") |
||||
|
assert.equal(to, "#testchan") |
||||
|
done() |
||||
|
} |
||||
|
// we can't part a channel without joining it first
|
||||
|
channel.join(mockedUser) |
||||
|
|
||||
|
channel.sendMsg(mockedUser, "test message") |
||||
|
}) |
||||
|
}) |
||||
|
}) |
@ -0,0 +1,72 @@ |
|||||
|
const assert = require('assert'); |
||||
|
const EventEmitter = require('events'); |
||||
|
const IRCServer = require("../src/server.js"); |
||||
|
|
||||
|
describe("JOIN OK", function () { |
||||
|
it("should handle a JOIN command -> JOIN #testchan", function (done) { |
||||
|
const server = IRCServer.create() |
||||
|
let mockedSock = new EventEmitter() |
||||
|
mockedSock.address = function () { |
||||
|
return {port: 12346, family: 'IPv4', address: '127.0.0.1'} |
||||
|
} |
||||
|
mockedSock.write = function (data) { |
||||
|
let answer = data.toString("ascii") |
||||
|
if (answer.indexOf("JOIN") >= 0) { |
||||
|
assert.equal(answer, ":some_nick JOIN #testchan\r\n") |
||||
|
done() |
||||
|
} |
||||
|
} |
||||
|
mockedSock.destroy = function () { |
||||
|
done("Destroyed socket without answering") |
||||
|
} |
||||
|
|
||||
|
server.emit("connection", mockedSock) |
||||
|
mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii")) |
||||
|
mockedSock.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii")) |
||||
|
mockedSock.emit('data', Buffer.from("JOIN #testchan\r\n", "ascii")) |
||||
|
}) |
||||
|
|
||||
|
it("should handle a JOIN command for more than one user -> JOIN #testchan and send join events to all channel members", function (done) { |
||||
|
const server = IRCServer.create() |
||||
|
// helper to count join messages for channel
|
||||
|
// we should see 2 of them
|
||||
|
let counter = 0 |
||||
|
let mockedSock1 = new EventEmitter() |
||||
|
mockedSock1.address = function () { |
||||
|
return {port: 12346, family: 'IPv4', address: '127.0.0.1'} |
||||
|
} |
||||
|
mockedSock1.write = function (data) { |
||||
|
let answer = data.toString("ascii") |
||||
|
if (answer.indexOf("JOIN") >= 0 && counter === 1) { |
||||
|
assert.equal(answer, ":other_nick JOIN #testchan\r\n") |
||||
|
done() |
||||
|
} |
||||
|
if (answer === ":some_nick JOIN #testchan\r\n") { |
||||
|
counter++ |
||||
|
} |
||||
|
} |
||||
|
mockedSock1.destroy = function () { |
||||
|
done("Destroyed socket without answering") |
||||
|
} |
||||
|
|
||||
|
let mockedSock2 = new EventEmitter() |
||||
|
mockedSock2.address = function () { |
||||
|
return {port: 12346, family: 'IPv4', address: '127.0.0.1'} |
||||
|
} |
||||
|
mockedSock2.write = function (data) { |
||||
|
return |
||||
|
} |
||||
|
mockedSock2.destroy = function () { |
||||
|
done("Destroyed socket without answering") |
||||
|
} |
||||
|
|
||||
|
server.emit("connection", mockedSock1) |
||||
|
server.emit("connection", mockedSock2) |
||||
|
mockedSock1.emit('data', Buffer.from("NICK some_nick\r\n", "ascii")) |
||||
|
mockedSock1.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii")) |
||||
|
mockedSock1.emit('data', Buffer.from("JOIN #testchan\r\n", "ascii")) |
||||
|
mockedSock2.emit('data', Buffer.from("NICK other_nick\r\n", "ascii")) |
||||
|
mockedSock2.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii")) |
||||
|
mockedSock2.emit('data', Buffer.from("JOIN #testchan\r\n", "ascii")) |
||||
|
}) |
||||
|
}) |
@ -0,0 +1,84 @@ |
|||||
|
const assert = require('assert'); |
||||
|
const User = require("../src/user.js"); |
||||
|
|
||||
|
describe('User', function () { |
||||
|
describe('#contructor()', function () { |
||||
|
it("should create a user while no password is required", function () { |
||||
|
let mockedSock = {} |
||||
|
const user = new User(mockedSock, true) |
||||
|
assert.equal(typeof user, 'object') |
||||
|
}) |
||||
|
|
||||
|
it("should create a user while password is required", function () { |
||||
|
let mockedSock = {} |
||||
|
const user = new User(mockedSock, false) |
||||
|
assert.equal(typeof user, 'object') |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
describe('#sendMsg(from, message, to)', function() { |
||||
|
it("should send a message to the user's socket with the user himself as target", function (done) { |
||||
|
let mockedSock = {write: function (data) { |
||||
|
assert.equal(data, ":some_nick PRIVMSG some_nick :test message\r\n") |
||||
|
done() |
||||
|
}} |
||||
|
const user = new User(mockedSock, true) |
||||
|
user.setNickname("some_nick") |
||||
|
user.register("some_nick") |
||||
|
user.sendMsg(user, "test message") |
||||
|
}) |
||||
|
|
||||
|
it("should send a message to the user's socket with a different target", function (done) { |
||||
|
let mockedSock = {write: function (data) { |
||||
|
assert.equal(data, ":some_nick PRIVMSG #testchan :test message\r\n") |
||||
|
done() |
||||
|
}} |
||||
|
const user = new User(mockedSock, true) |
||||
|
user.setNickname("some_nick") |
||||
|
user.register("some_nick") |
||||
|
user.sendMsg(user, "test message", "#testchan") |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
describe('#sendRaw(message)', function() { |
||||
|
it("should send a raw command to the user's socket", function(done) { |
||||
|
let mockedSock = {write: function (data) { |
||||
|
assert.equal(data, ":irc.example.com TEST this :command with parameters\r\n") |
||||
|
done() |
||||
|
}} |
||||
|
const user = new User(mockedSock, true) |
||||
|
user.setNickname("some_nick") |
||||
|
user.register("some_nick") |
||||
|
user.sendRaw(":irc.example.com TEST this :command with parameters") |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
describe('#closeConnection()', function() { |
||||
|
it("should call destroy on user's socket", function(done) { |
||||
|
let mockedSock = {destroy: function () { |
||||
|
done() |
||||
|
}} |
||||
|
const user = new User(mockedSock, true) |
||||
|
user.closeConnection() |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
describe('#setNickname(nickname)', function() { |
||||
|
it('should not answer on inital setting of nickname', function () { |
||||
|
let mockedSock = {} |
||||
|
const user = new User(mockedSock, true) |
||||
|
user.setNickname("some_nick") |
||||
|
assert.equal(user.getNickname(), "some_nick") |
||||
|
}) |
||||
|
|
||||
|
it('should answer with NICK message from original nick on rename', function (done) { |
||||
|
let mockedSock = {write: function (data) { |
||||
|
assert.equal(data, ":some_nick NICK changed_nick\r\n") |
||||
|
done() |
||||
|
}} |
||||
|
const user = new User(mockedSock, true) |
||||
|
user.setNickname("some_nick") |
||||
|
user.setNickname("changed_nick") |
||||
|
}) |
||||
|
}) |
||||
|
}) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue