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.

65 lines
2.7 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")
  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")
  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. })