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.

ServerStateHandlerTest.kt 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.dmdirc.ktirc.events
  2. import com.dmdirc.ktirc.IrcClient
  3. import com.dmdirc.ktirc.TestConstants
  4. import com.dmdirc.ktirc.model.*
  5. import com.nhaarman.mockitokotlin2.doReturn
  6. import com.nhaarman.mockitokotlin2.mock
  7. import kotlinx.coroutines.runBlocking
  8. import org.junit.jupiter.api.Assertions.*
  9. import org.junit.jupiter.api.Test
  10. internal class ServerStateHandlerTest {
  11. private val serverState = ServerState("", "")
  12. private val ircClient = mock<IrcClient> {
  13. on { serverState } doReturn serverState
  14. }
  15. private val handler = ServerStateHandler()
  16. @Test
  17. fun `ServerStateHandler sets local nickname on welcome event`() = runBlocking {
  18. handler.processEvent(ircClient, ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"))
  19. assertEquals("acidBurn", serverState.localNickname)
  20. }
  21. @Test
  22. fun `ServerStateHandler sets server name on welcome event`() = runBlocking {
  23. handler.processEvent(ircClient, ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"))
  24. assertEquals("the.gibson", serverState.serverName)
  25. }
  26. @Test
  27. fun `ServerStateHandler sets receivedWelcome on welcome event`() = runBlocking {
  28. handler.processEvent(ircClient, ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"))
  29. assertTrue(serverState.receivedWelcome)
  30. }
  31. @Test
  32. fun `ServerStateHandler sets state to connecting on event`() = runBlocking {
  33. handler.processEvent(ircClient, ServerConnecting(TestConstants.time))
  34. assertEquals(ServerStatus.Connecting, serverState.status)
  35. }
  36. @Test
  37. fun `ServerStateHandler sets state to disconnected on event`() = runBlocking {
  38. serverState.status = ServerStatus.Ready
  39. handler.processEvent(ircClient, ServerDisconnected(TestConstants.time))
  40. assertEquals(ServerStatus.Disconnected, serverState.status)
  41. }
  42. @Test
  43. fun `ServerStateHandler sets state to negotiating on connected`() = runBlocking {
  44. handler.processEvent(ircClient, ServerConnected(TestConstants.time))
  45. assertEquals(ServerStatus.Negotiating, serverState.status)
  46. }
  47. @Test
  48. fun `ServerStateHandler sets server state to ready on receiving post-005 line`() = runBlocking {
  49. ircClient.serverState.status = ServerStatus.Negotiating
  50. listOf(
  51. ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"),
  52. PingReceived(TestConstants.time, "1234".toByteArray()),
  53. ServerCapabilitiesReceived(TestConstants.time, emptyMap()),
  54. ServerCapabilitiesAcknowledged(TestConstants.time, emptyMap()),
  55. ServerCapabilitiesFinished(TestConstants.time),
  56. MessageReceived(TestConstants.time, User("zeroCool"), "acidBurn", "Welcome!")
  57. ).forEach {
  58. assertEquals(ServerStatus.Negotiating, serverState.status)
  59. handler.processEvent(ircClient, it)
  60. }
  61. assertEquals(ServerStatus.Ready, serverState.status)
  62. }
  63. @Test
  64. fun `ServerStateHandler emits event on receiving post-005 line`() = runBlocking {
  65. ircClient.serverState.status = ServerStatus.Negotiating
  66. listOf(
  67. ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"),
  68. PingReceived(TestConstants.time, "1234".toByteArray()),
  69. ServerCapabilitiesReceived(TestConstants.time, emptyMap()),
  70. ServerCapabilitiesAcknowledged(TestConstants.time, emptyMap()),
  71. ServerCapabilitiesFinished(TestConstants.time)
  72. ).forEach {
  73. assertTrue(handler.processEvent(ircClient, it).isEmpty())
  74. }
  75. val events = handler.processEvent(ircClient, MessageReceived(TestConstants.time, User("zeroCool"), "acidBurn", "Welcome!"))
  76. assertEquals(1, events.size)
  77. assertTrue(events[0] is ServerReady)
  78. }
  79. @Test
  80. fun `ServerStateHandler updates features on features event`() = runBlocking {
  81. val features = ServerFeatureMap()
  82. features[ServerFeature.ChannelModes] = arrayOf("abc", "def")
  83. features[ServerFeature.WhoxSupport] = true
  84. handler.processEvent(ircClient, ServerFeaturesUpdated(TestConstants.time, features))
  85. assertArrayEquals(arrayOf("abc", "def"), serverState.features[ServerFeature.ChannelModes])
  86. assertEquals(true, serverState.features[ServerFeature.WhoxSupport])
  87. }
  88. }