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.

CapabilitiesHandlerTest.kt 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.dmdirc.ktirc.events
  2. import com.dmdirc.ktirc.IrcClient
  3. import com.dmdirc.ktirc.TestConstants
  4. import com.dmdirc.ktirc.model.CapabilitiesNegotiationState
  5. import com.dmdirc.ktirc.model.Capability
  6. import com.dmdirc.ktirc.model.ServerState
  7. import com.nhaarman.mockitokotlin2.argThat
  8. import com.nhaarman.mockitokotlin2.doReturn
  9. import com.nhaarman.mockitokotlin2.mock
  10. import com.nhaarman.mockitokotlin2.verify
  11. import kotlinx.coroutines.runBlocking
  12. import org.junit.jupiter.api.Assertions.assertEquals
  13. import org.junit.jupiter.api.Test
  14. internal class CapabilitiesHandlerTest {
  15. private val handler = CapabilitiesHandler()
  16. private val serverState = ServerState("")
  17. private val ircClient = mock<IrcClient> {
  18. on { serverState } doReturn serverState
  19. }
  20. @Test
  21. fun `CapabilitiesHandler adds new capabilities to the state`() {
  22. runBlocking {
  23. handler.processEvent(ircClient, ServerCapabilitiesReceived(TestConstants.time, hashMapOf(
  24. Capability.EchoMessages to "",
  25. Capability.HostsInNamesReply to "123"
  26. )))
  27. assertEquals(2, serverState.capabilities.advertisedCapabilities.size)
  28. assertEquals("", serverState.capabilities.advertisedCapabilities[Capability.EchoMessages])
  29. assertEquals("123", serverState.capabilities.advertisedCapabilities[Capability.HostsInNamesReply])
  30. }
  31. }
  32. @Test
  33. fun `CapabilitiesHandler updates negotiation state when capabilities finished`() {
  34. runBlocking {
  35. handler.processEvent(ircClient, ServerCapabilitiesFinished(TestConstants.time))
  36. assertEquals(CapabilitiesNegotiationState.AWAITING_ACK, serverState.capabilities.negotiationState)
  37. }
  38. }
  39. @Test
  40. fun `CapabilitiesHandler sends REQ when capabilities received`() {
  41. runBlocking {
  42. serverState.capabilities.advertisedCapabilities[Capability.EchoMessages] = ""
  43. serverState.capabilities.advertisedCapabilities[Capability.AccountChangeMessages] = ""
  44. handler.processEvent(ircClient, ServerCapabilitiesFinished(TestConstants.time))
  45. verify(ircClient).send(argThat { equals("CAP REQ :echo-message account-notify") || equals("CAP REQ :account-notify echo-message") })
  46. }
  47. }
  48. @Test
  49. fun `CapabilitiesHandler sends END when capabilities acknowledged`() {
  50. runBlocking {
  51. handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(TestConstants.time, hashMapOf(
  52. Capability.EchoMessages to "",
  53. Capability.HostsInNamesReply to "123"
  54. )))
  55. verify(ircClient).send("CAP END")
  56. }
  57. }
  58. @Test
  59. fun `CapabilitiesHandler updates negotiation state when capabilities acknowledged`() {
  60. runBlocking {
  61. handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(TestConstants.time, hashMapOf(
  62. Capability.EchoMessages to "",
  63. Capability.HostsInNamesReply to "123"
  64. )))
  65. assertEquals(CapabilitiesNegotiationState.FINISHED, serverState.capabilities.negotiationState)
  66. }
  67. }
  68. @Test
  69. fun `CapabilitiesHandler stores enabled caps when capabilities acknowledged`() {
  70. runBlocking {
  71. handler.processEvent(ircClient, ServerCapabilitiesAcknowledged(TestConstants.time, hashMapOf(
  72. Capability.EchoMessages to "",
  73. Capability.HostsInNamesReply to "123"
  74. )))
  75. assertEquals(2, serverState.capabilities.enabledCapabilities.size)
  76. assertEquals("", serverState.capabilities.enabledCapabilities[Capability.EchoMessages])
  77. assertEquals("123", serverState.capabilities.enabledCapabilities[Capability.HostsInNamesReply])
  78. }
  79. }
  80. }