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.

ChannelStateHandlerTest.kt 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.model.*
  6. import com.nhaarman.mockitokotlin2.doReturn
  7. import com.nhaarman.mockitokotlin2.mock
  8. import kotlinx.coroutines.runBlocking
  9. import org.junit.jupiter.api.Assertions.*
  10. import org.junit.jupiter.api.Test
  11. internal class ChannelStateHandlerTest {
  12. private val handler = ChannelStateHandler()
  13. private val channelStateMap = ChannelStateMap { CaseMapping.Rfc }
  14. private val serverState = ServerState("", "")
  15. private val ircClient = mock<IrcClient> {
  16. on { serverState } doReturn serverState
  17. on { channelState } doReturn channelStateMap
  18. on { isLocalUser(User("acidburn", "libby", "root.localhost")) } doReturn true
  19. }
  20. @Test
  21. fun `ChannelStateHandler creates new state object for local joins`() = runBlocking {
  22. handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("acidburn", "libby", "root.localhost"), "#thegibson"))
  23. assertTrue("#thegibson" in channelStateMap)
  24. }
  25. @Test
  26. fun `ChannelStateHandler does not create new state object for remote joins`() = runBlocking {
  27. handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson"))
  28. assertFalse("#thegibson" in channelStateMap)
  29. }
  30. @Test
  31. fun `ChannelStateHandler adds joiners to channel state`() = runBlocking {
  32. channelStateMap += ChannelState("#thegibson") { CaseMapping.Rfc }
  33. handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson"))
  34. assertTrue("zerocool" in channelStateMap["#thegibson"]?.users!!)
  35. }
  36. @Test
  37. fun `ChannelStateHandler clears existing users when getting a new list`() = runBlocking {
  38. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  39. channel.users += ChannelUser("acidBurn")
  40. channel.users += ChannelUser("thePlague")
  41. channelStateMap += channel
  42. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("zeroCool")))
  43. assertEquals(1, channel.users.count())
  44. assertNotNull(channel.users["zeroCool"])
  45. }
  46. @Test
  47. fun `ChannelStateHandler adds users from multiple name received events`() = runBlocking {
  48. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  49. channelStateMap += channel
  50. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("zeroCool")))
  51. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("acidBurn")))
  52. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("thePlague")))
  53. assertEquals(3, channel.users.count())
  54. assertNotNull(channel.users["zeroCool"])
  55. assertNotNull(channel.users["acidBurn"])
  56. assertNotNull(channel.users["thePlague"])
  57. }
  58. @Test
  59. fun `ChannelStateHandler clears and readds users on additional names received`() = runBlocking {
  60. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  61. channelStateMap += channel
  62. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("zeroCool")))
  63. handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
  64. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("acidBurn")))
  65. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("thePlague")))
  66. assertEquals(2, channel.users.count())
  67. assertNotNull(channel.users["acidBurn"])
  68. assertNotNull(channel.users["thePlague"])
  69. }
  70. @Test
  71. fun `ChannelStateHandler adds users with mode prefixes`() = runBlocking {
  72. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  73. channelStateMap += channel
  74. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  75. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@zeroCool", "@+acidBurn", "+thePlague", "cerealKiller")))
  76. handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
  77. assertEquals(4, channel.users.count())
  78. assertEquals("o", channel.users["zeroCool"]?.modes)
  79. assertEquals("ov", channel.users["acidBurn"]?.modes)
  80. assertEquals("v", channel.users["thePlague"]?.modes)
  81. assertEquals("", channel.users["cerealKiller"]?.modes)
  82. }
  83. @Test
  84. fun `ChannelStateHandler adds users with full hosts`() = runBlocking {
  85. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  86. channelStateMap += channel
  87. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  88. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@zeroCool!dade@root.localhost", "+acidBurn!libby@root.localhost")))
  89. handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
  90. assertEquals(2, channel.users.count())
  91. assertEquals("o", channel.users["zeroCool"]?.modes)
  92. assertEquals("v", channel.users["acidBurn"]?.modes)
  93. }
  94. @Test
  95. fun `ChannelStateHandler removes state object for local parts`() = runBlocking {
  96. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  97. channelStateMap += channel
  98. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("acidburn", "libby", "root.localhost"), "#thegibson"))
  99. assertFalse("#thegibson" in channelStateMap)
  100. }
  101. @Test
  102. fun `ChannelStateHandler removes user from channel member list for remote parts`() = runBlocking {
  103. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  104. channel.users += ChannelUser("ZeroCool")
  105. channelStateMap += channel
  106. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson"))
  107. assertFalse("zerocool" in channel.users)
  108. }
  109. @Test
  110. fun `ChannelStateHandler removes user from all channel member lists for quits`() = runBlocking {
  111. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  112. users += ChannelUser("ZeroCool")
  113. channelStateMap += this
  114. }
  115. with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
  116. users += ChannelUser("ZeroCool")
  117. channelStateMap += this
  118. }
  119. with (ChannelState("#chat") { CaseMapping.Rfc }) {
  120. users += ChannelUser("AcidBurn")
  121. channelStateMap += this
  122. }
  123. handler.processEvent(ircClient, UserQuit(TestConstants.time, User("zerocool", "dade", "root.localhost")))
  124. assertFalse("zerocool" in channelStateMap["#thegibson"]!!.users)
  125. assertFalse("zerocool" in channelStateMap["#dumpsterdiving"]!!.users)
  126. assertFalse("zerocool" in channelStateMap["#chat"]!!.users)
  127. assertTrue("acidburn" in channelStateMap["#chat"]!!.users)
  128. }
  129. @Test
  130. fun `ChannelStateHandler raises ChannelQuit event for each channel a user quits from`() = runBlocking {
  131. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  132. users += ChannelUser("ZeroCool")
  133. channelStateMap += this
  134. }
  135. with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
  136. users += ChannelUser("ZeroCool")
  137. channelStateMap += this
  138. }
  139. with (ChannelState("#chat") { CaseMapping.Rfc }) {
  140. users += ChannelUser("AcidBurn")
  141. channelStateMap += this
  142. }
  143. val events = handler.processEvent(ircClient, UserQuit(TestConstants.time, User("zerocool", "dade", "root.localhost"), "Hack the planet!"))
  144. val names = mutableListOf<String>()
  145. assertEquals(2, events.size)
  146. events.forEach { event ->
  147. (event as ChannelQuit).let {
  148. assertEquals(TestConstants.time, it.time)
  149. assertEquals("zerocool", it.user.nickname)
  150. assertEquals("Hack the planet!", it.reason)
  151. names.add(it.channel)
  152. }
  153. }
  154. assertTrue("#thegibson" in names)
  155. assertTrue("#dumpsterdiving" in names)
  156. }
  157. }