const assert = require('assert'); const EventEmitter = require('events'); const IRCServer = require("../src/server.js"); describe("NICK OK", function () { it("should handle a NICK command -> NICK some_nickname", function () { const server = IRCServer.create() let mockedSock = new EventEmitter() mockedSock.write = function (data) { assert.equal(data.toString("ascii"), "001") done() } mockedSock.destroy = function () { done("Destroyed socket without answering") } server.emit("connection", mockedSock) mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii")) assert.deepEqual(Object.keys(server.getUserlist()), ["some_nick"]); }) }) describe("NICK already registered", function () { it("should handle a NICK command -> NICK some_nickname", function (done) { const server = IRCServer.create() let mockedSock1 = new EventEmitter() mockedSock1.write = function (data) { done("send data to wrong socket") } mockedSock1.destroy = function () { done("Destroyed socket without answering") } let mockedSock2 = new EventEmitter() mockedSock2.write = function (data) { assert.equal(data.toString("ascii"), ":irc.example.com 433 some_nick :Nickname is already in use") 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")) mockedSock2.emit('data', Buffer.from("NICK some_nick\r\n", "ascii")) }) }) describe("NICK no NICK given", function () { it("should handle a NICK command -> NICK ", function (done) { const server = IRCServer.create() let mockedSock = new EventEmitter() mockedSock.write = function (data) { assert.equal(data.toString("ascii"), ":irc.example.com 431 :No nickname given") done() } mockedSock.destroy = function () { done("Destroyed socket without answering") } server.emit("connection", mockedSock) mockedSock.emit('data', Buffer.from("NICK \r\n", "ascii")) }) })