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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 kotlinx.coroutines.runBlocking
  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. runBlocking {
  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. }
  35. @Test
  36. fun `updates user info on join`() {
  37. runBlocking {
  38. userState += User("acidBurn")
  39. handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#thegibson"))
  40. val details = userState["acidBurn"]?.details!!
  41. assertEquals("libby", details.ident)
  42. assertEquals("root.localhost", details.hostname)
  43. }
  44. }
  45. @Test
  46. fun `removes channel from user on part`() {
  47. runBlocking {
  48. userState += User("acidBurn")
  49. userState.addToChannel(User("acidBurn"), "#thegibson")
  50. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  51. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#dumpsterdiving"))
  52. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  53. }
  54. }
  55. @Test
  56. fun `removes user on part from last channel`() {
  57. runBlocking {
  58. userState += User("acidBurn")
  59. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  60. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#dumpsterdiving"))
  61. assertNull(userState["acidBurn"])
  62. }
  63. }
  64. @Test
  65. fun `removes channel from all users on local part`() {
  66. runBlocking {
  67. userState += User("acidBurn")
  68. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  69. userState.addToChannel(User("acidBurn"), "#thegibson")
  70. userState += User("zeroCool")
  71. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  72. userState.addToChannel(User("zeroCool"), "#thegibson")
  73. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
  74. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  75. assertEquals(listOf("#thegibson"), userState["zeroCool"]?.channels?.toList())
  76. }
  77. }
  78. @Test
  79. fun `removes remote users with no remaining channels on local part`() {
  80. runBlocking {
  81. userState += User("acidBurn")
  82. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  83. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
  84. assertNull(userState["acidBurn"])
  85. }
  86. }
  87. @Test
  88. fun `keeps local user with no remaining channels after local part`() {
  89. runBlocking {
  90. userState += User("zeroCool")
  91. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  92. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
  93. assertNotNull(userState["zeroCool"])
  94. }
  95. }
  96. @Test
  97. fun `removes channel from user on kick`() {
  98. runBlocking {
  99. userState += User("acidBurn")
  100. userState.addToChannel(User("acidBurn"), "#thegibson")
  101. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  102. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "acidBurn"))
  103. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  104. }
  105. }
  106. @Test
  107. fun `removes user on kick from last channel`() {
  108. runBlocking {
  109. userState += User("acidBurn")
  110. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  111. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "acidBurn"))
  112. assertNull(userState["acidBurn"])
  113. }
  114. }
  115. @Test
  116. fun `removes channel from all users on local kick`() {
  117. runBlocking {
  118. userState += User("acidBurn")
  119. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  120. userState.addToChannel(User("acidBurn"), "#thegibson")
  121. userState += User("zeroCool")
  122. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  123. userState.addToChannel(User("zeroCool"), "#thegibson")
  124. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "zeroCool"))
  125. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  126. assertEquals(listOf("#thegibson"), userState["zeroCool"]?.channels?.toList())
  127. }
  128. }
  129. @Test
  130. fun `removes remote users with no remaining channels on local kick`() {
  131. runBlocking {
  132. userState += User("acidBurn")
  133. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  134. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "zeroCool"))
  135. assertNull(userState["acidBurn"])
  136. }
  137. }
  138. @Test
  139. fun `keeps local user with no remaining channels after local kick`() {
  140. runBlocking {
  141. userState += User("zeroCool")
  142. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  143. handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("thePlague"), "#dumpsterdiving", "zeroCool"))
  144. assertNotNull(userState["zeroCool"])
  145. }
  146. }
  147. @Test
  148. fun `removes user entirely on quit`() {
  149. runBlocking {
  150. userState += User("acidBurn")
  151. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  152. handler.processEvent(ircClient, UserQuit(TestConstants.time, User("acidBurn", "libby", "root.localhost")))
  153. assertNull(userState["acidBurn"])
  154. }
  155. }
  156. @Test
  157. fun `adds users to channels on names received`() {
  158. runBlocking {
  159. userState += User("acidBurn")
  160. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  161. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@acidBurn")))
  162. assertEquals(listOf("#dumpsterdiving", "#thegibson"), userState["acidBurn"]?.channels?.toList())
  163. }
  164. }
  165. @Test
  166. fun `updates user details on names received`() {
  167. runBlocking {
  168. userState += User("acidBurn")
  169. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  170. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@acidBurn!libby@root.localhost")))
  171. val details = userState["acidBurn"]?.details!!
  172. assertEquals("libby", details.ident)
  173. assertEquals("root.localhost", details.hostname)
  174. }
  175. }
  176. @Test
  177. fun `updates user info on account change`() {
  178. runBlocking {
  179. userState += User("acidBurn")
  180. handler.processEvent(ircClient, UserAccountChanged(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "acidBurn"))
  181. val details = userState["acidBurn"]?.details!!
  182. assertEquals("acidBurn", details.account)
  183. }
  184. }
  185. }