123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package com.dmdirc.ktirc.events
-
- import com.dmdirc.ktirc.IrcClient
- import com.dmdirc.ktirc.TestConstants
- import com.dmdirc.ktirc.model.*
- import com.nhaarman.mockitokotlin2.doReturn
- import com.nhaarman.mockitokotlin2.mock
- import kotlinx.coroutines.runBlocking
- import org.junit.jupiter.api.Assertions.*
- import org.junit.jupiter.api.Test
-
- internal class ServerStateHandlerTest {
-
- private val serverState = ServerState("", "", emptyList())
- private val ircClient = mock<IrcClient> {
- on { serverState } doReturn serverState
- }
-
- private val handler = ServerStateHandler()
-
- @Test
- fun `ServerStateHandler sets local nickname on welcome event`() = runBlocking {
- handler.processEvent(ircClient, ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"))
- assertEquals("acidBurn", serverState.localNickname)
- }
-
- @Test
- fun `ServerStateHandler sets server name on welcome event`() = runBlocking {
- handler.processEvent(ircClient, ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"))
- assertEquals("the.gibson", serverState.serverName)
- }
-
- @Test
- fun `ServerStateHandler sets receivedWelcome on welcome event`() = runBlocking {
- handler.processEvent(ircClient, ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"))
- assertTrue(serverState.receivedWelcome)
- }
-
- @Test
- fun `ServerStateHandler sets state to connecting on event`() = runBlocking {
- handler.processEvent(ircClient, ServerConnecting(TestConstants.time))
- assertEquals(ServerStatus.Connecting, serverState.status)
- }
-
- @Test
- fun `ServerStateHandler sets state to disconnected on event`() = runBlocking {
- serverState.status = ServerStatus.Ready
- handler.processEvent(ircClient, ServerDisconnected(TestConstants.time))
- assertEquals(ServerStatus.Disconnected, serverState.status)
- }
-
- @Test
- fun `ServerStateHandler sets state to negotiating on connected`() = runBlocking {
- handler.processEvent(ircClient, ServerConnected(TestConstants.time))
- assertEquals(ServerStatus.Negotiating, serverState.status)
- }
-
- @Test
- fun `ServerStateHandler sets server state to ready on receiving post-005 line`() = runBlocking {
- ircClient.serverState.status = ServerStatus.Negotiating
-
- listOf(
- ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"),
- PingReceived(TestConstants.time, "1234".toByteArray()),
- ServerCapabilitiesReceived(TestConstants.time, emptyMap()),
- ServerCapabilitiesAcknowledged(TestConstants.time, emptyMap()),
- ServerCapabilitiesFinished(TestConstants.time),
- MessageReceived(TestConstants.time, User("zeroCool"), "acidBurn", "Welcome!")
- ).forEach {
- assertEquals(ServerStatus.Negotiating, serverState.status)
- handler.processEvent(ircClient, it)
- }
-
- assertEquals(ServerStatus.Ready, serverState.status)
- }
-
- @Test
- fun `ServerStateHandler emits event on receiving post-005 line`() = runBlocking {
- ircClient.serverState.status = ServerStatus.Negotiating
-
- listOf(
- ServerWelcome(TestConstants.time, "the.gibson", "acidBurn"),
- PingReceived(TestConstants.time, "1234".toByteArray()),
- ServerCapabilitiesReceived(TestConstants.time, emptyMap()),
- ServerCapabilitiesAcknowledged(TestConstants.time, emptyMap()),
- ServerCapabilitiesFinished(TestConstants.time)
- ).forEach {
- assertTrue(handler.processEvent(ircClient, it).isEmpty())
- }
-
- val events = handler.processEvent(ircClient, MessageReceived(TestConstants.time, User("zeroCool"), "acidBurn", "Welcome!"))
- assertEquals(1, events.size)
- assertTrue(events[0] is ServerReady)
- }
-
- @Test
- fun `ServerStateHandler updates features on features event`() = runBlocking {
- val features = ServerFeatureMap()
- features[ServerFeature.ChannelModes] = arrayOf("abc", "def")
- features[ServerFeature.WhoxSupport] = true
-
- handler.processEvent(ircClient, ServerFeaturesUpdated(TestConstants.time, features))
-
- assertArrayEquals(arrayOf("abc", "def"), serverState.features[ServerFeature.ChannelModes])
- assertEquals(true, serverState.features[ServerFeature.WhoxSupport])
- }
-
- }
|