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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.assertEquals
  9. import org.junit.jupiter.api.Assertions.assertTrue
  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, "acidBurn"))
  20. assertEquals("acidBurn", serverState.localNickname)
  21. }
  22. @Test
  23. fun `ServerStateHandler sets receivedWelcome on welcome event`() = runBlocking {
  24. handler.processEvent(ircClient, ServerWelcome(TestConstants.time, "acidBurn"))
  25. assertTrue(serverState.receivedWelcome)
  26. }
  27. @Test
  28. fun `ServerStateHandler sets state to negotiating on connected`() = runBlocking {
  29. handler.processEvent(ircClient, ServerConnected(TestConstants.time))
  30. assertEquals(ServerStatus.Negotiating, serverState.status)
  31. }
  32. @Test
  33. fun `ServerStateHandler sets server state to ready on receiving post-005 line`() = runBlocking {
  34. ircClient.serverState.status = ServerStatus.Negotiating
  35. listOf(
  36. ServerWelcome(TestConstants.time, "acidBurn"),
  37. PingReceived(TestConstants.time, "1234".toByteArray()),
  38. ServerCapabilitiesReceived(TestConstants.time, emptyMap()),
  39. ServerCapabilitiesAcknowledged(TestConstants.time, emptyMap()),
  40. ServerCapabilitiesFinished(TestConstants.time),
  41. MessageReceived(TestConstants.time, User("zeroCool"), "acidBurn", "Welcome!")
  42. ).forEach {
  43. assertEquals(ServerStatus.Negotiating, serverState.status)
  44. handler.processEvent(ircClient, it)
  45. }
  46. assertEquals(ServerStatus.Ready, serverState.status)
  47. }
  48. @Test
  49. fun `ServerStateHandler emits event on receiving post-005 line`() = runBlocking {
  50. ircClient.serverState.status = ServerStatus.Negotiating
  51. listOf(
  52. ServerWelcome(TestConstants.time, "acidBurn"),
  53. PingReceived(TestConstants.time, "1234".toByteArray()),
  54. ServerCapabilitiesReceived(TestConstants.time, emptyMap()),
  55. ServerCapabilitiesAcknowledged(TestConstants.time, emptyMap()),
  56. ServerCapabilitiesFinished(TestConstants.time)
  57. ).forEach {
  58. assertTrue(handler.processEvent(ircClient, it).isEmpty())
  59. }
  60. val events = handler.processEvent(ircClient, MessageReceived(TestConstants.time, User("zeroCool"), "acidBurn", "Welcome!"))
  61. assertEquals(1, events.size)
  62. assertTrue(events[0] is ServerReady)
  63. }
  64. @Test
  65. fun `ServerStateHandler updates features on features event`() = runBlocking {
  66. val features = ServerFeatureMap()
  67. features[ServerFeature.ChannelModes] = "abc"
  68. features[ServerFeature.WhoxSupport] = true
  69. handler.processEvent(ircClient, ServerFeaturesUpdated(TestConstants.time, features))
  70. assertEquals("abc", serverState.features[ServerFeature.ChannelModes])
  71. assertEquals(true, serverState.features[ServerFeature.WhoxSupport])
  72. }
  73. }