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.

60 lines
2.4 KiB

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