|
| 1 | +package gg.grounds.proxy.velocity.tab |
| 2 | + |
| 3 | +import java.util.UUID |
| 4 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 5 | +import org.junit.jupiter.api.Assertions.assertFalse |
| 6 | +import org.junit.jupiter.api.Assertions.assertNull |
| 7 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 8 | +import org.junit.jupiter.api.Test |
| 9 | + |
| 10 | +class VoicePresencesTest { |
| 11 | + private var now = 1_000L |
| 12 | + private val presences = VoicePresences { now } |
| 13 | + private val alice = UUID(0, 1) |
| 14 | + private val bob = UUID(0, 2) |
| 15 | + |
| 16 | + private fun snapshot(server: String, vararg players: Pair<UUID, String>) = |
| 17 | + """{"schemaVersion":1,"server":"$server","players":{${ |
| 18 | + players.joinToString(",") { "\"${it.first}\":\"${it.second}\"" } |
| 19 | + }}}""" |
| 20 | + |
| 21 | + @Test |
| 22 | + fun `a snapshot tells voice on from voice off and leaves others without an icon`() { |
| 23 | + assertTrue(presences.receive(snapshot("10.0.0.1:24460", alice to "on", bob to "off"))) |
| 24 | + assertEquals(true, presences.voiceOf(alice)) |
| 25 | + assertEquals(false, presences.voiceOf(bob)) |
| 26 | + assertNull(presences.voiceOf(UUID(0, 3))) |
| 27 | + assertFalse(presences.receive(snapshot("10.0.0.1:24460", alice to "on", bob to "off"))) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `the freshest server wins after a player switched servers`() { |
| 32 | + presences.receive(snapshot("10.0.0.1:24460", alice to "on")) |
| 33 | + now += 1_000 |
| 34 | + presences.receive(snapshot("10.0.0.2:24460", alice to "off")) |
| 35 | + assertEquals(false, presences.voiceOf(alice)) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `a server that stopped sending is forgotten`() { |
| 40 | + presences.receive(snapshot("10.0.0.1:24460", alice to "on")) |
| 41 | + now += VoicePresences.EXPIRY_MILLIS |
| 42 | + assertNull(presences.voiceOf(alice)) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `malformed snapshots are ignored`() { |
| 47 | + assertFalse(presences.receive("nope")) |
| 48 | + assertFalse(presences.receive("""{"schemaVersion":2,"server":"x","players":{}}""")) |
| 49 | + assertFalse(presences.receive(snapshot("x", alice to "loud"))) |
| 50 | + assertNull(presences.voiceOf(alice)) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `the subject covers every zone of the environment`() { |
| 55 | + assertEquals("voice.players.stage.>", VoicePresences.subject("stage")) |
| 56 | + assertNull(VoicePresences.subject("stage.*")) |
| 57 | + } |
| 58 | +} |
0 commit comments