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

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