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.

128 lines
5.5 KiB

  1. const assert = require('assert');
  2. const EventEmitter = require('events');
  3. const IRCServer = require("../src/server.js");
  4. describe("PRIVMSG OK", function () {
  5. it("should handle a PRIVMSG command -> PRIVMSG some_nick :I'm a message", 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("PRIVMSG") >= 0) {
  14. assert.equal(answer, ":some_nick PRIVMSG some_nick :I'm a message\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("PRIVMSG some_nick :I'm a message\r\n", "ascii"))
  25. })
  26. it("should handle a PRIVMSG command for multiple users -> PRIVMSG other_nick :I'm a message", function (done) {
  27. const server = IRCServer.create()
  28. let mockedSock1 = new EventEmitter()
  29. mockedSock1.address = function () {
  30. return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
  31. }
  32. mockedSock1.write = function (data) {
  33. return
  34. }
  35. mockedSock1.destroy = function () {
  36. done("Destroyed socket without answering")
  37. }
  38. let mockedSock2 = new EventEmitter()
  39. mockedSock2.address = function () {
  40. return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
  41. }
  42. mockedSock2.write = function (data) {
  43. let answer = data.toString("ascii")
  44. if (answer.indexOf("PRIVMSG") >= 0) {
  45. assert.equal(answer, ":some_nick PRIVMSG other_nick :I'm a message\r\n")
  46. done()
  47. }
  48. }
  49. mockedSock2.destroy = function () {
  50. done("Destroyed socket without answering")
  51. }
  52. server.emit("connection", mockedSock1)
  53. server.emit("connection", mockedSock2)
  54. mockedSock1.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
  55. mockedSock1.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
  56. mockedSock2.emit('data', Buffer.from("NICK other_nick\r\n", "ascii"))
  57. mockedSock2.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
  58. mockedSock1.emit('data', Buffer.from("PRIVMSG other_nick :I'm a message\r\n", "ascii"))
  59. })
  60. it("dont send message to unregistered user", function (done) {
  61. const server = IRCServer.create()
  62. let mockedSock1 = new EventEmitter()
  63. mockedSock1.address = function () {
  64. return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
  65. }
  66. mockedSock1.write = function (data) {
  67. answer = data.toString('ascii')
  68. if (answer.indexOf("401")>=0) {
  69. assert.equal(answer, ":irc.example.com 401 some_nick no such nick/channel")
  70. }
  71. done()
  72. }
  73. mockedSock1.destroy = function () {
  74. done("Destroyed socket without answering")
  75. }
  76. let mockedSock2 = new EventEmitter()
  77. mockedSock2.address = function () {
  78. return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
  79. }
  80. mockedSock2.write = function (data) {
  81. let answer = data.toString("ascii")
  82. if (answer.indexOf("PRIVMSG") >= 0) {
  83. assert.equal(answer, ":some_nick PRIVMSG other_nick :I'm a message\r\n")
  84. done("I got a message that I shouldn't have seen")
  85. }
  86. }
  87. mockedSock2.destroy = function () {
  88. done("Destroyed socket without answering")
  89. }
  90. server.emit("connection", mockedSock1)
  91. server.emit("connection", mockedSock2)
  92. mockedSock1.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
  93. mockedSock1.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
  94. mockedSock2.emit('data', Buffer.from("NICK other_nick\r\n", "ascii"))
  95. mockedSock1.emit('data', Buffer.from("PRIVMSG other_nick :I'm a message\r\n", "ascii"))
  96. })
  97. it("should allow to send message to channel -> PRIVMSG #testchan :I'm a message", function (done) {
  98. const server = IRCServer.create()
  99. let mockedSock = new EventEmitter()
  100. mockedSock.address = function () {
  101. return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
  102. }
  103. mockedSock.write = function (data) {
  104. let answer = data.toString("ascii")
  105. if (answer.indexOf("PRIVMSG") >= 0) {
  106. assert.equal(answer, ":some_nick PRIVMSG #testchan :I'm a message\r\n")
  107. done()
  108. }
  109. }
  110. mockedSock.destroy = function () {
  111. done("Destroyed socket without answering")
  112. }
  113. server.emit("connection", mockedSock)
  114. mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
  115. mockedSock.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
  116. mockedSock.emit('data', Buffer.from("JOIN #testchan\r\n", "ascii"))
  117. mockedSock.emit('data', Buffer.from("PRIVMSG #testchan :I'm a message\r\n", "ascii"))
  118. })
  119. })