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.

72 lines
3.0 KiB

  1. const assert = require('assert');
  2. const EventEmitter = require('events');
  3. const IRCServer = require("../src/server.js");
  4. describe("JOIN OK", function () {
  5. it("should handle a JOIN command -> JOIN #testchan", function (done) {
  6. const server = IRCServer.create()
  7. let mockedSock = new EventEmitter()
  8. mockedSock.address = function () {
  9. return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
  10. }
  11. mockedSock.write = function (data) {
  12. let answer = data.toString("ascii")
  13. if (answer.indexOf("JOIN") >= 0) {
  14. assert.equal(answer, ":some_nick JOIN #testchan\r\n")
  15. done()
  16. }
  17. }
  18. mockedSock.destroy = function () {
  19. done("Destroyed socket without answering")
  20. }
  21. server.emit("connection", mockedSock)
  22. mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
  23. mockedSock.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
  24. mockedSock.emit('data', Buffer.from("JOIN #testchan\r\n", "ascii"))
  25. })
  26. it("should handle a JOIN command for more than one user -> JOIN #testchan and send join events to all channel members", function (done) {
  27. const server = IRCServer.create()
  28. // helper to count join messages for channel
  29. // we should see 2 of them
  30. let counter = 0
  31. let mockedSock1 = new EventEmitter()
  32. mockedSock1.address = function () {
  33. return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
  34. }
  35. mockedSock1.write = function (data) {
  36. let answer = data.toString("ascii")
  37. if (answer.indexOf("JOIN") >= 0 && counter === 1) {
  38. assert.equal(answer, ":other_nick JOIN #testchan\r\n")
  39. done()
  40. }
  41. if (answer === ":some_nick JOIN #testchan\r\n") {
  42. counter++
  43. }
  44. }
  45. mockedSock1.destroy = function () {
  46. done("Destroyed socket without answering")
  47. }
  48. let mockedSock2 = new EventEmitter()
  49. mockedSock2.address = function () {
  50. return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
  51. }
  52. mockedSock2.write = function (data) {
  53. return
  54. }
  55. mockedSock2.destroy = function () {
  56. done("Destroyed socket without answering")
  57. }
  58. server.emit("connection", mockedSock1)
  59. server.emit("connection", mockedSock2)
  60. mockedSock1.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
  61. mockedSock1.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
  62. mockedSock1.emit('data', Buffer.from("JOIN #testchan\r\n", "ascii"))
  63. mockedSock2.emit('data', Buffer.from("NICK other_nick\r\n", "ascii"))
  64. mockedSock2.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
  65. mockedSock2.emit('data', Buffer.from("JOIN #testchan\r\n", "ascii"))
  66. })
  67. })