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.

UserStateHandlerTest.kt 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.dmdirc.ktirc.handlers
  2. import com.dmdirc.ktirc.IrcClient
  3. import com.dmdirc.ktirc.TestConstants
  4. import com.dmdirc.ktirc.events.*
  5. import com.dmdirc.ktirc.io.CaseMapping
  6. import com.dmdirc.ktirc.model.*
  7. import com.nhaarman.mockitokotlin2.argForWhich
  8. import com.nhaarman.mockitokotlin2.doReturn
  9. import com.nhaarman.mockitokotlin2.mock
  10. import com.nhaarman.mockitokotlin2.whenever
  11. import org.junit.jupiter.api.Assertions.*
  12. import org.junit.jupiter.api.BeforeEach
  13. import org.junit.jupiter.api.Test
  14. internal class UserStateHandlerTest {
  15. private val serverState = ServerState("", "")
  16. private val userState = UserState { CaseMapping.Rfc }
  17. private val ircClient = mock<IrcClient> {
  18. on { serverState } doReturn serverState
  19. on { userState } doReturn userState
  20. on { isLocalUser(argForWhich<User> { nickname == "zeroCool" }) } doReturn true
  21. on { isLocalUser("zeroCool") } doReturn true
  22. }
  23. private val handler = UserStateHandler()
  24. @BeforeEach
  25. fun setUp() {
  26. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  27. }
  28. @Test
  29. fun `adds channel to user on join`() {
  30. userState += User("acidBurn")
  31. handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#thegibson"))
  32. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  33. }
  34. @Test
  35. fun `updates user info on join`() {
  36. userState += User("acidBurn")
  37. handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#thegibson"))
  38. val details = userState["acidBurn"]?.details!!
  39. assertEquals("libby", details.ident)
  40. assertEquals("root.localhost", details.hostname)
  41. }
  42. @Test
  43. fun `removes channel from user on part`() {
  44. userState += User("acidBurn")
  45. userState.addToChannel(User("acidBurn"), "#thegibson")
  46. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  47. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#dumpsterdiving"))
  48. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  49. }
  50. @Test
  51. fun `removes user on part from last channel`() {
  52. userState += User("acidBurn")
  53. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  54. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#dumpsterdiving"))
  55. assertNull(userState["acidBurn"])
  56. }
  57. @Test
  58. fun `removes channel from all users on local part`() {
  59. userState += User("acidBurn")
  60. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  61. userState.addToChannel(User("acidBurn"), "#thegibson")
  62. userState += User("zeroCool")
  63. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  64. userState.addToChannel(User("zeroCool"), "#thegibson")
  65. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
  66. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  67. assertEquals(listOf("#thegibson"), userState["zeroCool"]?.channels?.toList())
  68. }
  69. @Test
  70. fun `removes remote users with no remaining channels on local part`() {
  71. userState += User("acidBurn")
  72. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  73. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
  74. assertNull(userState["acidBurn"])
  75. }
  76. @Test
  77. fun `keeps local user with no remaining channels after local part`() {
  78. userState += User("zeroCool")
  79. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  80. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
  81. assertNotNull(userState["zeroCool"])
  82. }
  83. @Test
  84. fun `removes channel from user on kick`() {
  85. userState += User("acidBurn")
  86. userState.addToChannel(User("acidBurn"), "#thegibson")
  87. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  88. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "acidBurn"))
  89. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  90. }
  91. @Test
  92. fun `removes user on kick from last channel`() {
  93. userState += User("acidBurn")
  94. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  95. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "acidBurn"))
  96. assertNull(userState["acidBurn"])
  97. }
  98. @Test
  99. fun `removes channel from all users on local kick`() {
  100. userState += User("acidBurn")
  101. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  102. userState.addToChannel(User("acidBurn"), "#thegibson")
  103. userState += User("zeroCool")
  104. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  105. userState.addToChannel(User("zeroCool"), "#thegibson")
  106. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "zeroCool"))
  107. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  108. assertEquals(listOf("#thegibson"), userState["zeroCool"]?.channels?.toList())
  109. }
  110. @Test
  111. fun `removes remote users with no remaining channels on local kick`() {
  112. userState += User("acidBurn")
  113. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  114. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "zeroCool"))
  115. assertNull(userState["acidBurn"])
  116. }
  117. @Test
  118. fun `keeps local user with no remaining channels after local kick`() {
  119. userState += User("zeroCool")
  120. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  121. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "zeroCool"))
  122. assertNotNull(userState["zeroCool"])
  123. }
  124. @Test
  125. fun `removes user entirely on quit`() {
  126. userState += User("acidBurn")
  127. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  128. handler.processEvent(ircClient, UserQuit(TestConstants.time, User("acidBurn", "libby", "root.localhost")))
  129. assertNull(userState["acidBurn"])
  130. }
  131. @Test
  132. fun `adds users to channels on names received`() {
  133. userState += User("acidBurn")
  134. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  135. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@acidBurn")))
  136. assertEquals(listOf("#dumpsterdiving", "#thegibson"), userState["acidBurn"]?.channels?.toList())
  137. }
  138. @Test
  139. fun `updates user details on names received`() {
  140. userState += User("acidBurn")
  141. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  142. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@acidBurn!libby@root.localhost")))
  143. val details = userState["acidBurn"]?.details!!
  144. assertEquals("libby", details.ident)
  145. assertEquals("root.localhost", details.hostname)
  146. }
  147. @Test
  148. fun `updates user info on account change`() {
  149. userState += User("acidBurn")
  150. handler.processEvent(ircClient, UserAccountChanged(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "acidBurn"))
  151. val details = userState["acidBurn"]?.details!!
  152. assertEquals("acidBurn", details.account)
  153. }
  154. @Test
  155. fun `updates local nickname for local nick changes`() {
  156. val user = User("acidBurn", "libby", "root.localhost")
  157. whenever(ircClient.isLocalUser(user)).doReturn(true)
  158. handler.processEvent(ircClient, UserNickChanged(TestConstants.time, user, "acid~"))
  159. assertEquals("acid~", serverState.localNickname)
  160. }
  161. @Test
  162. fun `updates nickname for remote nick changes`() {
  163. val user = User("acidBurn", "libby", "root.localhost")
  164. userState += User("AcidBurn")
  165. handler.processEvent(ircClient, UserNickChanged(TestConstants.time, user, "acid~"))
  166. assertNotNull(userState["acid~"])
  167. assertNull(userState["AcidBurn"])
  168. assertEquals("acid~", userState["acid~"]?.details?.nickname)
  169. }
  170. }