|
|
const assert = require('assert'); const EventEmitter = require('events'); const IRCServer = require("../src/server.js");
describe("PRIVMSG OK", function () { it("should handle a PRIVMSG command -> PRIVMSG some_nick :I'm a message", 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("PRIVMSG") >= 0) { assert.equal(answer, ":some_nick PRIVMSG some_nick :I'm a message\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("PRIVMSG some_nick :I'm a message\r\n", "ascii")) })
it("should handle a PRIVMSG command for multiple users -> PRIVMSG other_nick :I'm a message", function (done) { const server = IRCServer.create() let mockedSock1 = new EventEmitter() mockedSock1.address = function () { return {port: 12346, family: 'IPv4', address: '127.0.0.1'} } mockedSock1.write = function (data) { return } 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) { let answer = data.toString("ascii") if (answer.indexOf("PRIVMSG") >= 0) { assert.equal(answer, ":some_nick PRIVMSG other_nick :I'm a message\r\n") done() } } 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")) 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"))
mockedSock1.emit('data', Buffer.from("PRIVMSG other_nick :I'm a message\r\n", "ascii")) }) it("dont send message to unregistered user", function (done) { const server = IRCServer.create() let mockedSock1 = new EventEmitter() mockedSock1.address = function () { return {port: 12346, family: 'IPv4', address: '127.0.0.1'} } mockedSock1.write = function (data) { answer = data.toString('ascii') if (answer.indexOf("401")>=0) { assert.equal(answer, ":irc.example.com 401 some_nick no such nick/channel") } done() } 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) { let answer = data.toString("ascii") if (answer.indexOf("PRIVMSG") >= 0) { assert.equal(answer, ":some_nick PRIVMSG other_nick :I'm a message\r\n") done("I got a message that I shouldn't have seen") } } 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")) mockedSock2.emit('data', Buffer.from("NICK other_nick\r\n", "ascii"))
mockedSock1.emit('data', Buffer.from("PRIVMSG other_nick :I'm a message\r\n", "ascii")) })
it("should allow to send message to channel -> PRIVMSG #testchan :I'm a message", 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("PRIVMSG") >= 0) { assert.equal(answer, ":some_nick PRIVMSG #testchan :I'm a message\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")) mockedSock.emit('data', Buffer.from("PRIVMSG #testchan :I'm a message\r\n", "ascii")) }) })
|