Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

UserStateHandlerTest.kt 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 { nickname == "zeroCool" }) } doReturn true
  20. }
  21. private val handler = UserStateHandler()
  22. @BeforeEach
  23. fun setUp() {
  24. serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
  25. }
  26. @Test
  27. fun `adds channel to user on join`() {
  28. runBlocking {
  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. }
  34. @Test
  35. fun `updates user info on join`() {
  36. runBlocking {
  37. userState += User("acidBurn")
  38. handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#thegibson"))
  39. val details = userState["acidBurn"]?.details!!
  40. assertEquals("libby", details.ident)
  41. assertEquals("root.localhost", details.hostname)
  42. }
  43. }
  44. @Test
  45. fun `removes channel from user on part`() {
  46. runBlocking {
  47. userState += User("acidBurn")
  48. userState.addToChannel(User("acidBurn"), "#thegibson")
  49. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  50. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#dumpsterdiving"))
  51. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  52. }
  53. }
  54. @Test
  55. fun `removes user on part from last channel`() {
  56. runBlocking {
  57. userState += User("acidBurn")
  58. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  59. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("acidBurn", "libby", "root.localhost"), "#dumpsterdiving"))
  60. assertNull(userState["acidBurn"])
  61. }
  62. }
  63. @Test
  64. fun `removes channel from all users on local part`() {
  65. runBlocking {
  66. userState += User("acidBurn")
  67. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  68. userState.addToChannel(User("acidBurn"), "#thegibson")
  69. userState += User("zeroCool")
  70. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  71. userState.addToChannel(User("zeroCool"), "#thegibson")
  72. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
  73. assertEquals(listOf("#thegibson"), userState["acidBurn"]?.channels?.toList())
  74. assertEquals(listOf("#thegibson"), userState["zeroCool"]?.channels?.toList())
  75. }
  76. }
  77. @Test
  78. fun `removes remote users with no remaining channels on local part`() {
  79. runBlocking {
  80. userState += User("acidBurn")
  81. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  82. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
  83. assertNull(userState["acidBurn"])
  84. }
  85. }
  86. @Test
  87. fun `keeps local user with no remaining channels after local part`() {
  88. runBlocking {
  89. userState += User("zeroCool")
  90. userState.addToChannel(User("zeroCool"), "#dumpsterdiving")
  91. handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zeroCool", "dade", "root.localhost"), "#dumpsterdiving"))
  92. assertNotNull(userState["zeroCool"])
  93. }
  94. }
  95. @Test
  96. fun `removes user entirely on quit`() {
  97. runBlocking {
  98. userState += User("acidBurn")
  99. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  100. handler.processEvent(ircClient, UserQuit(TestConstants.time, User("acidBurn", "libby", "root.localhost")))
  101. assertNull(userState["acidBurn"])
  102. }
  103. }
  104. @Test
  105. fun `adds users to channels on names received`() {
  106. runBlocking {
  107. userState += User("acidBurn")
  108. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  109. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@acidBurn")))
  110. assertEquals(listOf("#dumpsterdiving", "#thegibson"), userState["acidBurn"]?.channels?.toList())
  111. }
  112. }
  113. @Test
  114. fun `updates user details on names received`() {
  115. runBlocking {
  116. userState += User("acidBurn")
  117. userState.addToChannel(User("acidBurn"), "#dumpsterdiving")
  118. handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@acidBurn!libby@root.localhost")))
  119. val details = userState["acidBurn"]?.details!!
  120. assertEquals("libby", details.ident)
  121. assertEquals("root.localhost", details.hostname)
  122. }
  123. }
  124. }