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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 org.junit.jupiter.api.Assertions.*
  9. import org.junit.jupiter.api.Test
  10. internal class ChannelStateHandlerTest {
  11. private val handler = ChannelStateHandler()
  12. private val channelStateMap = ChannelStateMap { CaseMapping.Rfc }
  13. private val serverState = ServerState("", "")
  14. private val ircClient = mock<IrcClient> {
  15. on { serverState } doReturn serverState
  16. on { channelState } doReturn channelStateMap
  17. on { isLocalUser(User("acidburn", "libby", "root.localhost")) } doReturn true
  18. on { isLocalUser("acidburn") } doReturn true
  19. }
  20. @Test
  21. fun `creates new state object for local joins`() {
  22. handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("acidburn", "libby", "root.localhost"), "#thegibson"))
  23. assertTrue("#thegibson" in channelStateMap)
  24. }
  25. @Test
  26. fun `does not create new state object for remote joins`() {
  27. handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson"))
  28. assertFalse("#thegibson" in channelStateMap)
  29. }
  30. @Test
  31. fun `adds joiners to channel state`() {
  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 `clears existing users when getting a new list`() {
  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 `adds users from multiple name received events`() {
  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 `clears and readds users on additional names received`() {
  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 `adds users with mode prefixes`() {
  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 `adds users with full hosts`() {
  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 `removes state object for local parts`() {
  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 `removes user from channel member list for remote parts`() {
  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 `removes state object for local kicks`() {
  111. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  112. channelStateMap += channel
  113. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson", "acidburn", "Bye!"))
  114. assertFalse("#thegibson" in channelStateMap)
  115. }
  116. @Test
  117. fun `removes user from channel member list for remote kicks`() {
  118. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  119. channel.users += ChannelUser("ZeroCool")
  120. channelStateMap += channel
  121. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("acidburn", "libby", "root.localhost"), "#thegibson", "zerocool", "Bye!"))
  122. assertFalse("zerocool" in channel.users)
  123. }
  124. @Test
  125. fun `removes user from all channel member lists for quits`() {
  126. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  127. users += ChannelUser("ZeroCool")
  128. channelStateMap += this
  129. }
  130. with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
  131. users += ChannelUser("ZeroCool")
  132. channelStateMap += this
  133. }
  134. with (ChannelState("#chat") { CaseMapping.Rfc }) {
  135. users += ChannelUser("AcidBurn")
  136. channelStateMap += this
  137. }
  138. handler.processEvent(ircClient, UserQuit(TestConstants.time, User("zerocool", "dade", "root.localhost")))
  139. assertFalse("zerocool" in channelStateMap["#thegibson"]!!.users)
  140. assertFalse("zerocool" in channelStateMap["#dumpsterdiving"]!!.users)
  141. assertFalse("zerocool" in channelStateMap["#chat"]!!.users)
  142. assertTrue("acidburn" in channelStateMap["#chat"]!!.users)
  143. }
  144. @Test
  145. fun `raises ChannelQuit event for each channel a user quits from`() {
  146. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  147. users += ChannelUser("ZeroCool")
  148. channelStateMap += this
  149. }
  150. with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
  151. users += ChannelUser("ZeroCool")
  152. channelStateMap += this
  153. }
  154. with (ChannelState("#chat") { CaseMapping.Rfc }) {
  155. users += ChannelUser("AcidBurn")
  156. channelStateMap += this
  157. }
  158. val events = handler.processEvent(ircClient, UserQuit(TestConstants.time, User("zerocool", "dade", "root.localhost"), "Hack the planet!"))
  159. val names = mutableListOf<String>()
  160. assertEquals(2, events.size)
  161. events.forEach { event ->
  162. (event as ChannelQuit).let {
  163. assertEquals(TestConstants.time, it.time)
  164. assertEquals("zerocool", it.user.nickname)
  165. assertEquals("Hack the planet!", it.reason)
  166. names.add(it.channel)
  167. }
  168. }
  169. assertTrue("#thegibson" in names)
  170. assertTrue("#dumpsterdiving" in names)
  171. }
  172. @Test
  173. fun `sets mode discovered flag when discovered mode event received`() {
  174. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  175. channelStateMap += channel
  176. serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
  177. handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+", emptyArray(), true))
  178. assertTrue(channel.modesDiscovered)
  179. }
  180. @Test
  181. fun `adds modes when discovered mode event received`() {
  182. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  183. channelStateMap += channel
  184. serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
  185. handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+ceg", arrayOf("CCC", "EEE"), true))
  186. assertEquals("CCC", channel.modes['c'])
  187. assertEquals("EEE", channel.modes['e'])
  188. assertEquals("", channel.modes['g'])
  189. }
  190. @Test
  191. fun `adjusts complex modes when mode change event received`() {
  192. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  193. channel.modes['c'] = "CCC"
  194. channel.modes['e'] = "EEE"
  195. channel.modes['h'] = ""
  196. channelStateMap += channel
  197. serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
  198. handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "-c+d-eh+fg", arrayOf("CCC", "DDD", "FFF"), true))
  199. assertNull(channel.modes['c'])
  200. assertEquals("DDD", channel.modes['d'])
  201. assertNull(channel.modes['e'])
  202. assertEquals("FFF", channel.modes['f'])
  203. assertEquals("", channel.modes['g'])
  204. assertNull(channel.modes['h'])
  205. }
  206. @Test
  207. fun `handles unprivileged user gaining new mode`() {
  208. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  209. users += ChannelUser("ZeroCool")
  210. channelStateMap += this
  211. }
  212. handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+o", arrayOf("zeroCool")))
  213. assertEquals("o", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  214. }
  215. @Test
  216. fun `handles privileged user gaining lesser mode`() {
  217. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  218. users += ChannelUser("ZeroCool", "o")
  219. channelStateMap += this
  220. }
  221. handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+v", arrayOf("zeroCool")))
  222. assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  223. }
  224. @Test
  225. fun `handles privileged user gaining greater mode`() {
  226. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  227. users += ChannelUser("ZeroCool", "v")
  228. channelStateMap += this
  229. }
  230. handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+o", arrayOf("zeroCool")))
  231. assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  232. }
  233. @Test
  234. fun `handles user gaining multiple modes`() {
  235. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  236. users += ChannelUser("ZeroCool")
  237. channelStateMap += this
  238. }
  239. handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+vo", arrayOf("zeroCool", "zeroCool")))
  240. assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  241. }
  242. @Test
  243. fun `handles user losing multiple modes`() {
  244. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  245. users += ChannelUser("ZeroCool", "ov")
  246. channelStateMap += this
  247. }
  248. handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "-vo", arrayOf("zeroCool", "zeroCool")))
  249. assertEquals("", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  250. }
  251. @Test
  252. fun `handles mixture of user modes and normal modes`() {
  253. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  254. users += ChannelUser("ZeroCool", "v")
  255. channelStateMap += this
  256. }
  257. serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
  258. handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "oa-v+b", arrayOf("zeroCool", "aaa", "zeroCool", "bbb")))
  259. assertEquals("o", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  260. assertEquals("aaa", channelStateMap["#thegibson"]?.modes?.get('a'))
  261. assertEquals("bbb", channelStateMap["#thegibson"]?.modes?.get('b'))
  262. }
  263. }