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.

EventUtilsTest.kt 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.dmdirc.ktirc.events
  2. import com.dmdirc.ktirc.IrcClient
  3. import com.dmdirc.ktirc.TestConstants
  4. import com.dmdirc.ktirc.io.CaseMapping
  5. import com.dmdirc.ktirc.messages.tagMap
  6. import com.dmdirc.ktirc.model.*
  7. import com.nhaarman.mockitokotlin2.doReturn
  8. import com.nhaarman.mockitokotlin2.mock
  9. import com.nhaarman.mockitokotlin2.verify
  10. import org.junit.jupiter.api.Assertions.assertEquals
  11. import org.junit.jupiter.api.BeforeEach
  12. import org.junit.jupiter.api.Test
  13. internal class EventUtilsTest {
  14. private val serverState = ServerState("", "")
  15. private val ircClient = mock<IrcClient> {
  16. on { serverState } doReturn serverState
  17. on { caseMapping } doReturn CaseMapping.Ascii
  18. }
  19. @BeforeEach
  20. fun setUp() {
  21. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  22. }
  23. @Test
  24. fun `ChannelNamesReceived#toModesAndUsers parses nicknames and mode prefixes`() {
  25. val event = ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf(
  26. "@+acidBurn", "@zeroCool", "thePlague"
  27. ))
  28. val result = event.toModesAndUsers(ircClient)
  29. assertEquals(3, result.size)
  30. assertEquals("ov", result[0].first)
  31. assertEquals("acidBurn", result[0].second.nickname)
  32. assertEquals("o", result[1].first)
  33. assertEquals("zeroCool", result[1].second.nickname)
  34. assertEquals("", result[2].first)
  35. assertEquals("thePlague", result[2].second.nickname)
  36. }
  37. @Test
  38. fun `ChannelNamesReceived#toModesAndUsers parses extended joins with prefixes`() {
  39. val event = ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf(
  40. "@+acidBurn!libby@root.localhost", "zeroCool!dade@root.localhost"
  41. ))
  42. val result = event.toModesAndUsers(ircClient)
  43. assertEquals(2, result.size)
  44. assertEquals("ov", result[0].first)
  45. assertEquals("acidBurn", result[0].second.nickname)
  46. assertEquals("libby", result[0].second.ident)
  47. assertEquals("root.localhost", result[0].second.hostname)
  48. assertEquals("", result[1].first)
  49. assertEquals("zeroCool", result[1].second.nickname)
  50. assertEquals("dade", result[1].second.ident)
  51. assertEquals("root.localhost", result[1].second.hostname)
  52. }
  53. @Test
  54. fun `reply sends response to user when message is private`() {
  55. serverState.localNickname = "zeroCool"
  56. val message = MessageReceived(EventMetadata(TestConstants.time), User("acidBurn"), "Zerocool", "Hack the planet!")
  57. ircClient.reply(message, "OK")
  58. verify(ircClient).send(tagMap(), "PRIVMSG", "acidBurn", "OK")
  59. }
  60. @Test
  61. fun `reply sends unprefixed response to user when message is in a channel`() {
  62. val message = MessageReceived(EventMetadata(TestConstants.time), User("acidBurn"), "#TheGibson", "Hack the planet!")
  63. ircClient.reply(message, "OK")
  64. verify(ircClient).send(tagMap(), "PRIVMSG", "#TheGibson", "OK")
  65. }
  66. @Test
  67. fun `reply sends prefixed response to user when message is in a channel`() {
  68. val message = MessageReceived(EventMetadata(TestConstants.time), User("acidBurn"), "#TheGibson", "Hack the planet!")
  69. ircClient.reply(message, "OK", prefixWithNickname = true)
  70. verify(ircClient).send(tagMap(), "PRIVMSG", "#TheGibson", "acidBurn: OK")
  71. }
  72. @Test
  73. fun `reply sends response with message ID to user when message is private`() {
  74. serverState.localNickname = "zeroCool"
  75. val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "abc123"), User("acidBurn"), "Zerocool", "Hack the planet!")
  76. ircClient.reply(message, "OK")
  77. verify(ircClient).send(tagMap(MessageTag.Reply to "abc123"), "PRIVMSG", "acidBurn", "OK")
  78. }
  79. @Test
  80. fun `reply sends unprefixed response with message ID to user when message is in a channel`() {
  81. val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "abc123"), User("acidBurn"), "#TheGibson", "Hack the planet!")
  82. ircClient.reply(message, "OK")
  83. verify(ircClient).send(tagMap(MessageTag.Reply to "abc123"), "PRIVMSG", "#TheGibson", "OK")
  84. }
  85. @Test
  86. fun `reply sends prefixed response with message ID to user when message is in a channel`() {
  87. val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "abc123"), User("acidBurn"), "#TheGibson", "Hack the planet!")
  88. ircClient.reply(message, "OK", prefixWithNickname = true)
  89. verify(ircClient).send(tagMap(MessageTag.Reply to "abc123"), "PRIVMSG", "#TheGibson", "acidBurn: OK")
  90. }
  91. @Test
  92. fun `react sends response to user when message is private`() {
  93. serverState.localNickname = "zeroCool"
  94. val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "msgId"), User("acidBurn"), "Zerocool", "Hack the planet!")
  95. ircClient.react(message, ":P")
  96. verify(ircClient).send(tagMap(MessageTag.React to ":P", MessageTag.Reply to "msgId"), "TAGMSG", "acidBurn")
  97. }
  98. @Test
  99. fun `react sends unprefixed response to user when message is in a channel`() {
  100. val message = MessageReceived(EventMetadata(TestConstants.time, messageId = "msgId"), User("acidBurn"), "#TheGibson", "Hack the planet!")
  101. ircClient.react(message, ":P")
  102. verify(ircClient).send(tagMap(MessageTag.React to ":P", MessageTag.Reply to "msgId"), "TAGMSG", "#TheGibson")
  103. }
  104. }