123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- package com.dmdirc.ktirc.events.handlers
-
- import com.dmdirc.ktirc.BehaviourConfig
- import com.dmdirc.ktirc.IrcClient
- import com.dmdirc.ktirc.TestConstants
- import com.dmdirc.ktirc.events.*
- import com.dmdirc.ktirc.io.CaseMapping
- import com.dmdirc.ktirc.model.*
- import com.nhaarman.mockitokotlin2.doReturn
- import com.nhaarman.mockitokotlin2.mock
- import com.nhaarman.mockitokotlin2.never
- import com.nhaarman.mockitokotlin2.verify
- import org.junit.jupiter.api.Assertions.*
- import org.junit.jupiter.api.Test
-
- internal class ChannelStateHandlerTest {
-
- private val handler = ChannelStateHandler()
- private val channelStateMap = ChannelStateMap { CaseMapping.Rfc }
- private val serverState = ServerState("", "")
- private val behaviour = BehaviourConfig()
- private val ircClient = mock<IrcClient> {
- on { serverState } doReturn serverState
- on { channelState } doReturn channelStateMap
- on { behaviour } doReturn behaviour
- on { isLocalUser(User("acidburn", "libby", "root.localhost")) } doReturn true
- on { isLocalUser("acidburn") } doReturn true
- }
-
- @Test
- fun `creates new state object for local joins`() {
- handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("acidburn", "libby", "root.localhost"), "#thegibson"))
- assertTrue("#thegibson" in channelStateMap)
- }
-
- @Test
- fun `does not create new state object for remote joins`() {
- handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson"))
- assertFalse("#thegibson" in channelStateMap)
- }
-
- @Test
- fun `adds joiners to channel state`() {
- channelStateMap += ChannelState("#thegibson") { CaseMapping.Rfc }
-
- handler.processEvent(ircClient, ChannelJoined(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson"))
-
- assertTrue("zerocool" in channelStateMap["#thegibson"]?.users!!)
- }
-
- @Test
- fun `clears existing users when getting a new list`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channel.users += ChannelUser("acidBurn")
- channel.users += ChannelUser("thePlague")
- channelStateMap += channel
-
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("zeroCool")))
-
- assertEquals(1, channel.users.count())
- assertNotNull(channel.users["zeroCool"])
- }
-
- @Test
- fun `adds users from multiple name received events`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
-
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("zeroCool")))
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("acidBurn")))
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("thePlague")))
-
- assertEquals(3, channel.users.count())
- assertNotNull(channel.users["zeroCool"])
- assertNotNull(channel.users["acidBurn"])
- assertNotNull(channel.users["thePlague"])
- }
-
- @Test
- fun `clears and readds users on additional names received`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
-
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("zeroCool")))
- handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("acidBurn")))
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("thePlague")))
-
- assertEquals(2, channel.users.count())
- assertNotNull(channel.users["acidBurn"])
- assertNotNull(channel.users["thePlague"])
- }
-
- @Test
- fun `adds users with mode prefixes`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
- serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
-
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@zeroCool", "@+acidBurn", "+thePlague", "cerealKiller")))
- handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
-
- assertEquals(4, channel.users.count())
- assertEquals("o", channel.users["zeroCool"]?.modes)
- assertEquals("ov", channel.users["acidBurn"]?.modes)
- assertEquals("v", channel.users["thePlague"]?.modes)
- assertEquals("", channel.users["cerealKiller"]?.modes)
- }
-
- @Test
- fun `adds users with full hosts`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
- serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
-
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@zeroCool!dade@root.localhost", "+acidBurn!libby@root.localhost")))
- handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
-
- assertEquals(2, channel.users.count())
- assertEquals("o", channel.users["zeroCool"]?.modes)
- assertEquals("v", channel.users["acidBurn"]?.modes)
- }
-
- @Test
- fun `updates receiving user list state`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
- serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
-
- handler.processEvent(ircClient, ChannelNamesReceived(TestConstants.time, "#thegibson", listOf("@zeroCool!dade@root.localhost", "+acidBurn!libby@root.localhost")))
-
- assertTrue(channel.receivingUserList)
-
- handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
-
- assertFalse(channel.receivingUserList)
- }
-
- @Test
- fun `requests modes on end of names if configured and undiscovered`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
- serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
- behaviour.requestModesOnJoin = true
-
- handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
-
- verify(ircClient).send("MODE :#thegibson")
- }
-
- @Test
- fun `does not request modes on end of names if already discovered`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
- serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
- behaviour.requestModesOnJoin = true
- channel.modesDiscovered = true
-
- handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
-
- verify(ircClient, never()).send("MODE :#thegibson")
- }
-
- @Test
- fun `does not request modes on end of names if not configured`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
- serverState.features[ServerFeature.ModePrefixes] = ModePrefixMapping("ov", "@+")
- behaviour.requestModesOnJoin = false
-
- handler.processEvent(ircClient, ChannelNamesFinished(TestConstants.time, "#thegibson"))
-
- verify(ircClient, never()).send("MODE :#thegibson")
- }
-
- @Test
- fun `removes state object for local parts`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
-
- handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("acidburn", "libby", "root.localhost"), "#thegibson"))
-
- assertFalse("#thegibson" in channelStateMap)
- }
-
- @Test
- fun `removes user from channel member list for remote parts`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channel.users += ChannelUser("ZeroCool")
- channelStateMap += channel
-
- handler.processEvent(ircClient, ChannelParted(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson"))
-
- assertFalse("zerocool" in channel.users)
- }
-
- @Test
- fun `removes state object for local kicks`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
-
- handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson", "acidburn", "Bye!"))
-
- assertFalse("#thegibson" in channelStateMap)
- }
-
- @Test
- fun `removes user from channel member list for remote kicks`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channel.users += ChannelUser("ZeroCool")
- channelStateMap += channel
-
- handler.processEvent(ircClient, ChannelUserKicked(TestConstants.time, User("acidburn", "libby", "root.localhost"), "#thegibson", "zerocool", "Bye!"))
-
- assertFalse("zerocool" in channel.users)
- }
-
- @Test
- fun `removes user from channel member lists for quits`() {
- with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
- users += ChannelUser("ZeroCool")
- channelStateMap += this
- }
-
- with (ChannelState("#dumpsterdiving") { CaseMapping.Rfc }) {
- users += ChannelUser("ZeroCool")
- channelStateMap += this
- }
-
- with (ChannelState("#chat") { CaseMapping.Rfc }) {
- users += ChannelUser("AcidBurn")
- channelStateMap += this
- }
-
- handler.processEvent(ircClient, ChannelQuit(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#thegibson"))
- handler.processEvent(ircClient, ChannelQuit(TestConstants.time, User("zerocool", "dade", "root.localhost"), "#dumpsterdiving"))
-
- assertFalse("zerocool" in channelStateMap["#thegibson"]!!.users)
- assertFalse("zerocool" in channelStateMap["#dumpsterdiving"]!!.users)
- assertFalse("zerocool" in channelStateMap["#chat"]!!.users)
- assertTrue("acidburn" in channelStateMap["#chat"]!!.users)
- }
-
- @Test
- fun `renames user in channel member list for nick changes`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channel.users += ChannelUser("acidBurn")
- channelStateMap += channel
-
- handler.processEvent(ircClient, ChannelNickChanged(TestConstants.time, User("acidburn", "libby", "root.localhost"), "#thegibson", "acidB"))
- handler.processEvent(ircClient, ChannelNickChanged(TestConstants.time, User("acidburn", "libby", "root.localhost"), "#dumpsterdiving", "acidB"))
-
- assertFalse("acidBurn" in channel.users)
- assertTrue("acidB" in channel.users)
- assertEquals("acidB", channel.users["acidB"]?.nickname)
- }
-
- @Test
- fun `sets mode discovered flag when discovered mode event received`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
- serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
-
- handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+", emptyArray(), true))
-
- assertTrue(channel.modesDiscovered)
- }
-
- @Test
- fun `adds modes when discovered mode event received`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += channel
- serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
-
- handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+ceg", arrayOf("CCC", "EEE"), true))
-
- assertEquals("CCC", channel.modes['c'])
- assertEquals("EEE", channel.modes['e'])
- assertEquals("", channel.modes['g'])
- }
-
- @Test
- fun `adjusts complex modes when mode change event received`() {
- val channel = ChannelState("#thegibson") { CaseMapping.Rfc }
- channel.modes['c'] = "CCC"
- channel.modes['e'] = "EEE"
- channel.modes['h'] = ""
- channelStateMap += channel
- serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
-
- handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "-c+d-eh+fg", arrayOf("CCC", "DDD", "FFF"), true))
-
- assertNull(channel.modes['c'])
- assertEquals("DDD", channel.modes['d'])
- assertNull(channel.modes['e'])
- assertEquals("FFF", channel.modes['f'])
- assertEquals("", channel.modes['g'])
- assertNull(channel.modes['h'])
- }
-
- @Test
- fun `handles unprivileged user gaining new mode`() {
- with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
- users += ChannelUser("ZeroCool")
- channelStateMap += this
- }
-
- handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+o", arrayOf("zeroCool")))
-
- assertEquals("o", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
- }
-
- @Test
- fun `handles privileged user gaining lesser mode`() {
- with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
- users += ChannelUser("ZeroCool", "o")
- channelStateMap += this
- }
-
- handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+v", arrayOf("zeroCool")))
-
- assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
- }
-
- @Test
- fun `handles privileged user gaining greater mode`() {
- with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
- users += ChannelUser("ZeroCool", "v")
- channelStateMap += this
- }
-
- handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+o", arrayOf("zeroCool")))
-
- assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
- }
-
- @Test
- fun `handles user gaining multiple modes`() {
- with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
- users += ChannelUser("ZeroCool")
- channelStateMap += this
- }
-
- handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "+vo", arrayOf("zeroCool", "zeroCool")))
-
- assertEquals("ov", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
- }
-
- @Test
- fun `handles user losing multiple modes`() {
- with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
- users += ChannelUser("ZeroCool", "ov")
- channelStateMap += this
- }
-
- handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "-vo", arrayOf("zeroCool", "zeroCool")))
-
- assertEquals("", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
- }
-
- @Test
- fun `handles mixture of user modes and normal modes`() {
- with (ChannelState("#thegibson") { CaseMapping.Rfc }) {
- users += ChannelUser("ZeroCool", "v")
- channelStateMap += this
- }
- serverState.features[ServerFeature.ChannelModes] = arrayOf("ab", "cd", "ef", "gh")
-
- handler.processEvent(ircClient, ModeChanged(TestConstants.time, "#thegibson", "oa-v+b", arrayOf("zeroCool", "aaa", "zeroCool", "bbb")))
-
- assertEquals("o", channelStateMap["#thegibson"]?.users?.get("zeroCool")?.modes)
- assertEquals("aaa", channelStateMap["#thegibson"]?.modes?.get('a'))
- assertEquals("bbb", channelStateMap["#thegibson"]?.modes?.get('b'))
- }
-
- @Test
- fun `updates topic state when it's discovered for the first time`() {
- val state = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += state
-
- handler.processEvent(ircClient, ChannelTopicDiscovered(TestConstants.time, "#thegibson", "Hack the planet!"))
- handler.processEvent(ircClient, ChannelTopicMetadataDiscovered(TestConstants.time, "#thegibson", User("acidBurn"), TestConstants.otherTime))
-
- assertTrue(state.topicDiscovered)
- assertEquals(ChannelTopic("Hack the planet!", User("acidBurn"), TestConstants.otherTime), state.topic)
- }
-
- @Test
- fun `updates topic state when no topic is discovered for the first time`() {
- val state = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += state
-
- handler.processEvent(ircClient, ChannelTopicDiscovered(TestConstants.time, "#thegibson", null))
-
- assertTrue(state.topicDiscovered)
- assertEquals(ChannelTopic(), state.topic)
- }
-
- @Test
- fun `leaves topic state when it's discovered for a second time`() {
- val state = ChannelState("#thegibson") { CaseMapping.Rfc }
- state.topic = ChannelTopic("Hack the planet!", User("acidBurn"), TestConstants.otherTime)
- state.topicDiscovered = true
- channelStateMap += state
-
- handler.processEvent(ircClient, ChannelTopicDiscovered(TestConstants.time, "#thegibson", "Hack the planet"))
- handler.processEvent(ircClient, ChannelTopicMetadataDiscovered(TestConstants.time, "#thegibson", User("zeroCool"), TestConstants.time))
-
- assertTrue(state.topicDiscovered)
- assertEquals(ChannelTopic("Hack the planet!", User("acidBurn"), TestConstants.otherTime), state.topic)
- }
-
- @Test
- fun `updates topic state when the topic is changed`() {
- val state = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += state
-
- handler.processEvent(ircClient, ChannelTopicChanged(TestConstants.time, User("acidBurn"), "#thegibson", "Hack the planet!"))
-
- assertEquals(ChannelTopic("Hack the planet!", User("acidBurn"), TestConstants.time), state.topic)
- }
-
- @Test
- fun `updates topic state when the topic is unset`() {
- val state = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += state
-
- handler.processEvent(ircClient, ChannelTopicChanged(TestConstants.time, User("acidBurn"), "#thegibson", null))
-
- assertEquals(ChannelTopic(null, User("acidBurn"), TestConstants.time), state.topic)
- }
-
- @Test
- fun `ignores topic change when channel doesn't exist`() {
- val state = ChannelState("#thegibson") { CaseMapping.Rfc }
- channelStateMap += state
-
- handler.processEvent(ircClient, ChannelTopicChanged(TestConstants.time, User("acidBurn"), "#dumpsterdiving", "Hack the planet!"))
-
- assertEquals(ChannelTopic(), state.topic)
- }
-
- }
|