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

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