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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package com.dmdirc.ktirc.events.handlers
  2. import com.dmdirc.ktirc.BehaviourConfig
  3. import com.dmdirc.ktirc.IrcClient
  4. import com.dmdirc.ktirc.TestConstants
  5. import com.dmdirc.ktirc.events.*
  6. import com.dmdirc.ktirc.io.CaseMapping
  7. import com.dmdirc.ktirc.model.*
  8. import com.nhaarman.mockitokotlin2.doReturn
  9. import com.nhaarman.mockitokotlin2.mock
  10. import com.nhaarman.mockitokotlin2.never
  11. import com.nhaarman.mockitokotlin2.verify
  12. import org.junit.jupiter.api.Assertions.*
  13. import org.junit.jupiter.api.Test
  14. internal class ChannelStateHandlerTest {
  15. private val handler = ChannelStateHandler()
  16. private val channelStateMap = ChannelStateMap { CaseMapping.Rfc }
  17. private val serverState = ServerState("", "")
  18. private val behaviour = BehaviourConfig()
  19. private val ircClient = mock<IrcClient> {
  20. on { serverState } doReturn serverState
  21. on { channelState } doReturn channelStateMap
  22. on { behaviour } doReturn behaviour
  23. on { isLocalUser(User("acidburn", "libby", "root.localhost")) } doReturn true
  24. on { isLocalUser("acidburn") } doReturn true
  25. }
  26. @Test
  27. fun `creates new state object for local joins`() {
  28. handler.processEvent(ircClient, ChannelJoined(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#thegibson"))
  29. assertTrue("#thegibson" in channelStateMap)
  30. }
  31. @Test
  32. fun `does not create new state object for remote joins`() {
  33. handler.processEvent(ircClient, ChannelJoined(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson"))
  34. assertFalse("#thegibson" in channelStateMap)
  35. }
  36. @Test
  37. fun `adds joiners to channel state`() {
  38. channelStateMap += ChannelState("#thegibson") { CaseMapping.Rfc }
  39. handler.processEvent(ircClient, ChannelJoined(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson"))
  40. assertTrue("zerocool" in channelStateMap["#thegibson"]?.users!!)
  41. }
  42. @Test
  43. fun `clears existing users when getting a new list`() {
  44. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  45. channel.users += ChannelUser("acidBurn")
  46. channel.users += ChannelUser("thePlague")
  47. channelStateMap += channel
  48. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("zeroCool")))
  49. assertEquals(1, channel.users.count())
  50. assertNotNull(channel.users["zeroCool"])
  51. }
  52. @Test
  53. fun `adds users from multiple name received events`() {
  54. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  55. channelStateMap += channel
  56. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("zeroCool")))
  57. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("acidBurn")))
  58. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("thePlague")))
  59. assertEquals(3, channel.users.count())
  60. assertNotNull(channel.users["zeroCool"])
  61. assertNotNull(channel.users["acidBurn"])
  62. assertNotNull(channel.users["thePlague"])
  63. }
  64. @Test
  65. fun `clears and readds users on additional names received`() {
  66. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  67. channelStateMap += channel
  68. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("zeroCool")))
  69. handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
  70. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("acidBurn")))
  71. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("thePlague")))
  72. assertEquals(2, channel.users.count())
  73. assertNotNull(channel.users["acidBurn"])
  74. assertNotNull(channel.users["thePlague"])
  75. }
  76. @Test
  77. fun `adds users with mode prefixes`() {
  78. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  79. channelStateMap += channel
  80. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  81. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("@zeroCool", "@+acidBurn", "+thePlague", "cerealKiller")))
  82. handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
  83. assertEquals(4, channel.users.count())
  84. assertEquals("o", channel.users["zeroCool"]?.modes)
  85. assertEquals("ov", channel.users["acidBurn"]?.modes)
  86. assertEquals("v", channel.users["thePlague"]?.modes)
  87. assertEquals("", channel.users["cerealKiller"]?.modes)
  88. }
  89. @Test
  90. fun `adds users with full hosts`() {
  91. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  92. channelStateMap += channel
  93. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  94. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("@zeroCool!dade@root.localhost", "+acidBurn!libby@root.localhost")))
  95. handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
  96. assertEquals(2, channel.users.count())
  97. assertEquals("o", channel.users["zeroCool"]?.modes)
  98. assertEquals("v", channel.users["acidBurn"]?.modes)
  99. }
  100. @Test
  101. fun `updates receiving user list state`() {
  102. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  103. channelStateMap += channel
  104. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  105. handler.processEvent(ircClient, ChannelNamesReceived(EventMetadata(TestConstants.time), "#thegibson", listOf("@zeroCool!dade@root.localhost", "+acidBurn!libby@root.localhost")))
  106. assertTrue(channel.receivingUserList)
  107. handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
  108. assertFalse(channel.receivingUserList)
  109. }
  110. @Test
  111. fun `requests modes on end of names if configured and undiscovered`() {
  112. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  113. channelStateMap += channel
  114. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  115. behaviour.requestModesOnJoin = true
  116. handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
  117. verify(ircClient).send("MODE", "#thegibson")
  118. }
  119. @Test
  120. fun `does not request modes on end of names if already discovered`() {
  121. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  122. channelStateMap += channel
  123. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  124. behaviour.requestModesOnJoin = true
  125. channel.modesDiscovered = true
  126. handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
  127. verify(ircClient, never()).send("MODE", "#thegibson")
  128. }
  129. @Test
  130. fun `does not request modes on end of names if not configured`() {
  131. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  132. channelStateMap += channel
  133. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  134. behaviour.requestModesOnJoin = false
  135. handler.processEvent(ircClient, ChannelNamesFinished(EventMetadata(TestConstants.time), "#thegibson"))
  136. verify(ircClient, never()).send("MODE", "#thegibson")
  137. }
  138. @Test
  139. fun `removes state object for local parts`() {
  140. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  141. channelStateMap += channel
  142. handler.processEvent(ircClient, ChannelParted(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#thegibson"))
  143. assertFalse("#thegibson" in channelStateMap)
  144. }
  145. @Test
  146. fun `removes user from channel member list for remote parts`() {
  147. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  148. channel.users += ChannelUser("ZeroCool")
  149. channelStateMap += channel
  150. handler.processEvent(ircClient, ChannelParted(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson"))
  151. assertFalse("zerocool" in channel.users)
  152. }
  153. @Test
  154. fun `removes state object for local kicks`() {
  155. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  156. channelStateMap += channel
  157. handler.processEvent(ircClient, ChannelUserKicked(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson", "acidburn", "Bye!"))
  158. assertFalse("#thegibson" in channelStateMap)
  159. }
  160. @Test
  161. fun `removes user from channel member list for remote kicks`() {
  162. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  163. channel.users += ChannelUser("ZeroCool")
  164. channelStateMap += channel
  165. handler.processEvent(ircClient, ChannelUserKicked(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#thegibson", "zerocool", "Bye!"))
  166. assertFalse("zerocool" in channel.users)
  167. }
  168. @Test
  169. fun `removes user from channel member lists for quits`() {
  170. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  171. users += ChannelUser("ZeroCool")
  172. channelStateMap += this
  173. }
  174. with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
  175. users += ChannelUser("ZeroCool")
  176. channelStateMap += this
  177. }
  178. with (ChannelState("#chat") { CaseMapping.Rfc }) {
  179. users += ChannelUser("AcidBurn")
  180. channelStateMap += this
  181. }
  182. handler.processEvent(ircClient, ChannelQuit(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#thegibson"))
  183. handler.processEvent(ircClient, ChannelQuit(EventMetadata(TestConstants.time), User("zerocool", "dade", "root.localhost"), "#dumpsterdiving"))
  184. assertFalse("zerocool" in channelStateMap["#thegibson"]!!.users)
  185. assertFalse("zerocool" in channelStateMap["#dumpsterdiving"]!!.users)
  186. assertFalse("zerocool" in channelStateMap["#chat"]!!.users)
  187. assertTrue("acidburn" in channelStateMap["#chat"]!!.users)
  188. }
  189. @Test
  190. fun `renames user in channel member list for nick changes`() {
  191. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  192. channel.users += ChannelUser("acidBurn")
  193. channelStateMap += channel
  194. handler.processEvent(ircClient, ChannelNickChanged(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#thegibson", "acidB"))
  195. handler.processEvent(ircClient, ChannelNickChanged(EventMetadata(TestConstants.time), User("acidburn", "libby", "root.localhost"), "#dumpsterdiving", "acidB"))
  196. assertFalse("acidBurn" in channel.users)
  197. assertTrue("acidB" in channel.users)
  198. assertEquals("acidB", channel.users["acidB"]?.nickname)
  199. }
  200. @Test
  201. fun `sets mode discovered flag when discovered mode event received`() {
  202. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  203. channelStateMap += channel
  204. serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
  205. handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+", emptyArray(), true))
  206. assertTrue(channel.modesDiscovered)
  207. }
  208. @Test
  209. fun `adds modes when discovered mode event received`() {
  210. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  211. channelStateMap += channel
  212. serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
  213. handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+ceg", arrayOf("CCC", "EEE"), true))
  214. assertEquals("CCC", channel.modes['c'])
  215. assertEquals("EEE", channel.modes['e'])
  216. assertEquals("", channel.modes['g'])
  217. }
  218. @Test
  219. fun `adjusts complex modes when mode change event received`() {
  220. val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
  221. channel.modes['c'] = "CCC"
  222. channel.modes['e'] = "EEE"
  223. channel.modes['h'] = ""
  224. channelStateMap += channel
  225. serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
  226. handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "-c+d-eh+fg", arrayOf("CCC", "DDD", "FFF"), true))
  227. assertNull(channel.modes['c'])
  228. assertEquals("DDD", channel.modes['d'])
  229. assertNull(channel.modes['e'])
  230. assertEquals("FFF", channel.modes['f'])
  231. assertEquals("", channel.modes['g'])
  232. assertNull(channel.modes['h'])
  233. }
  234. @Test
  235. fun `handles unprivileged user gaining new mode`() {
  236. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  237. users += ChannelUser("ZeroCool")
  238. channelStateMap += this
  239. }
  240. handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+o", arrayOf("zeroCool")))
  241. assertEquals("o", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  242. }
  243. @Test
  244. fun `handles privileged user gaining lesser mode`() {
  245. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  246. users += ChannelUser("ZeroCool", "o")
  247. channelStateMap += this
  248. }
  249. handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+v", arrayOf("zeroCool")))
  250. assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  251. }
  252. @Test
  253. fun `handles privileged user gaining greater mode`() {
  254. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  255. users += ChannelUser("ZeroCool", "v")
  256. channelStateMap += this
  257. }
  258. handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+o", arrayOf("zeroCool")))
  259. assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  260. }
  261. @Test
  262. fun `handles user gaining multiple modes`() {
  263. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  264. users += ChannelUser("ZeroCool")
  265. channelStateMap += this
  266. }
  267. handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "+vo", arrayOf("zeroCool", "zeroCool")))
  268. assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  269. }
  270. @Test
  271. fun `handles user losing multiple modes`() {
  272. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  273. users += ChannelUser("ZeroCool", "ov")
  274. channelStateMap += this
  275. }
  276. handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "-vo", arrayOf("zeroCool", "zeroCool")))
  277. assertEquals("", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  278. }
  279. @Test
  280. fun `handles mixture of user modes and normal modes`() {
  281. with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
  282. users += ChannelUser("ZeroCool", "v")
  283. channelStateMap += this
  284. }
  285. serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
  286. handler.processEvent(ircClient, ModeChanged(EventMetadata(TestConstants.time), "#thegibson", "oa-v+b", arrayOf("zeroCool", "aaa", "zeroCool", "bbb")))
  287. assertEquals("o", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
  288. assertEquals("aaa", channelStateMap["#thegibson"]?.modes?.get('a'))
  289. assertEquals("bbb", channelStateMap["#thegibson"]?.modes?.get('b'))
  290. }
  291. @Test
  292. fun `updates topic state when it's discovered for the first time`() {
  293. val state = ChannelState("#thegibson") { CaseMapping.Rfc }
  294. channelStateMap += state
  295. handler.processEvent(ircClient, ChannelTopicDiscovered(EventMetadata(TestConstants.time), "#thegibson", "Hack the planet!"))
  296. handler.processEvent(ircClient, ChannelTopicMetadataDiscovered(EventMetadata(TestConstants.time), "#thegibson", User("acidBurn"), TestConstants.otherTime))
  297. assertTrue(state.topicDiscovered)
  298. assertEquals(ChannelTopic("Hack the planet!", User("acidBurn"), TestConstants.otherTime), state.topic)
  299. }
  300. @Test
  301. fun `updates topic state when no topic is discovered for the first time`() {
  302. val state = ChannelState("#thegibson") { CaseMapping.Rfc }
  303. channelStateMap += state
  304. handler.processEvent(ircClient, ChannelTopicDiscovered(EventMetadata(TestConstants.time), "#thegibson", null))
  305. assertTrue(state.topicDiscovered)
  306. assertEquals(ChannelTopic(), state.topic)
  307. }
  308. @Test
  309. fun `leaves topic state when it's discovered for a second time`() {
  310. val state = ChannelState("#thegibson") { CaseMapping.Rfc }
  311. state.topic = ChannelTopic("Hack the planet!", User("acidBurn"), TestConstants.otherTime)
  312. state.topicDiscovered = true
  313. channelStateMap += state
  314. handler.processEvent(ircClient, ChannelTopicDiscovered(EventMetadata(TestConstants.time), "#thegibson", "Hack the planet"))
  315. handler.processEvent(ircClient, ChannelTopicMetadataDiscovered(EventMetadata(TestConstants.time), "#thegibson", User("zeroCool"), TestConstants.time))
  316. assertTrue(state.topicDiscovered)
  317. assertEquals(ChannelTopic("Hack the planet!", User("acidBurn"), TestConstants.otherTime), state.topic)
  318. }
  319. @Test
  320. fun `updates topic state when the topic is changed`() {
  321. val state = ChannelState("#thegibson") { CaseMapping.Rfc }
  322. channelStateMap += state
  323. handler.processEvent(ircClient, ChannelTopicChanged(EventMetadata(TestConstants.time), User("acidBurn"), "#thegibson", "Hack the planet!"))
  324. assertEquals(ChannelTopic("Hack the planet!", User("acidBurn"), TestConstants.time), state.topic)
  325. }
  326. @Test
  327. fun `updates topic state when the topic is unset`() {
  328. val state = ChannelState("#thegibson") { CaseMapping.Rfc }
  329. channelStateMap += state
  330. handler.processEvent(ircClient, ChannelTopicChanged(EventMetadata(TestConstants.time), User("acidBurn"), "#thegibson", null))
  331. assertEquals(ChannelTopic(null, User("acidBurn"), TestConstants.time), state.topic)
  332. }
  333. @Test
  334. fun `ignores topic change when channel doesn't exist`() {
  335. val state = ChannelState("#thegibson") { CaseMapping.Rfc }
  336. channelStateMap += state
  337. handler.processEvent(ircClient, ChannelTopicChanged(EventMetadata(TestConstants.time), User("acidBurn"), "#dumpsterdiving", "Hack the planet!"))
  338. assertEquals(ChannelTopic(), state.topic)
  339. }
  340. }