You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.3 KiB

  1. const assert = require('assert');
  2. const EventEmitter = require('events');
  3. const IRCServer = require("../src/server.js");
  4. describe("NICK OK", function () {
  5. it("should handle a NICK command -> NICK some_nickname", function () {
  6. const server = IRCServer.create()
  7. let mockedSock = new EventEmitter()
  8. mockedSock.write = function (data) {
  9. assert.equal(data.toString("ascii"), "001")
  10. done()
  11. }
  12. mockedSock.destroy = function () {
  13. done("Destroyed socket without answering")
  14. }
  15. server.emit("connection", mockedSock)
  16. mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
  17. assert.deepEqual(Object.keys(server.getUserlist()), ["some_nick"]);
  18. })
  19. })
  20. describe("NICK already registered", function () {
  21. it("should handle a NICK command -> NICK some_nickname", function (done) {
  22. const server = IRCServer.create()
  23. let mockedSock1 = new EventEmitter()
  24. mockedSock1.write = function (data) {
  25. done("send data to wrong socket")
  26. }
  27. mockedSock1.destroy = function () {
  28. done("Destroyed socket without answering")
  29. }
  30. let mockedSock2 = new EventEmitter()
  31. mockedSock2.write = function (data) {
  32. assert.equal(data.toString("ascii"), ":irc.example.com 433 some_nick :Nickname is already in use")
  33. done()
  34. }
  35. mockedSock2.destroy = function () {
  36. done("Destroyed socket without answering")
  37. }
  38. server.emit("connection", mockedSock1)
  39. server.emit("connection", mockedSock2)
  40. mockedSock1.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
  41. mockedSock2.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
  42. })
  43. })
  44. describe("NICK no NICK given", function () {
  45. it("should handle a NICK command -> NICK ", function (done) {
  46. const server = IRCServer.create()
  47. let mockedSock = new EventEmitter()
  48. mockedSock.write = function (data) {
  49. assert.equal(data.toString("ascii"), ":irc.example.com 431 :No nickname given")
  50. done()
  51. }
  52. mockedSock.destroy = function () {
  53. done("Destroyed socket without answering")
  54. }
  55. server.emit("connection", mockedSock)
  56. mockedSock.emit('data', Buffer.from("NICK \r\n", "ascii"))
  57. })
  58. })