Files
Playtube-Android/app/src/test/java/de/fojadrachi/playtube/SocialLogicTest.kt
T

71 lines
3.5 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package de.fojadrachi.playtube
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
class SocialLogicTest {
@Test
fun readsFriendList() {
val json = JSONObject(
"""{"t":"friends","friends":[
{"id":"u1","name":"Anna","status":"listening","client":"pc","track":{"title":"Song","artist":"Band","thumb":null,"videoId":"dQw4w9WgXcQ"}},
{"id":"u2","name":"Max","status":"online","track":null,"client":"android"},
{"id":"u3","name":"Zoe","status":"offline","track":null,"client":null}],
"incoming":[{"id":"u4","name":"Tom"}],"outgoing":[]}""",
)
val list = SocialLogic.parseFriends(json)
assertEquals(3, list.friends.size)
val anna = list.friends[0]
assertTrue(anna.listening)
assertNull(anna.track!!.thumb)
assertEquals("dQw4w9WgXcQ", anna.track!!.videoId)
assertEquals("♪ Song – Band", SocialLogic.friendLine(anna, "Offline", "Online"))
assertEquals("Online", SocialLogic.friendLine(list.friends[1], "Offline", "Online"))
assertFalse(list.friends[2].online)
assertEquals("Offline", SocialLogic.friendLine(list.friends[2], "Offline", "Online"))
assertEquals("Tom", list.incoming.single().name)
assertTrue(list.outgoing.isEmpty())
}
@Test
fun readsInvitesAndUsers() {
val invite = SocialLogic.parseInvite(
JSONObject("""{"t":"invite","id":"i1","from":{"id":"u1","name":"Anna"},"code":"abc234","host":"Anna","current":{"title":"Song","artist":"Band"}}"""),
)!!
assertEquals("ABC234", invite.code)
assertEquals("Anna", invite.fromName)
assertEquals("Song", invite.songTitle)
assertNull(SocialLogic.parseInvite(JSONObject("""{"t":"invite","id":"i1","from":{"id":"u1"},"code":"ABC10O"}""")))
val user = SocialLogic.parseUser(JSONObject("""{"id":"u0","name":"Chris","email":"[email protected]","share":false}"""))!!
assertFalse(user.share)
assertNull(SocialLogic.parseUser(JSONObject("{}")))
assertEquals("u9", SocialLogic.parseResults(JSONObject("""{"results":[{"id":"u9","name":"X","relation":"incoming"}]}""")).single().id)
}
@Test
fun buildsPresence() {
val playing = NowPlaying("Song", "Band", "https://lh3.googleusercontent.com/x", playing = true, positionMs = 0, durationMs = 1000)
val msg = SocialLogic.presence(playing, "dQw4w9WgXcQ")
assertEquals("presence", msg.getString("t"))
assertTrue(msg.getBoolean("playing"))
assertEquals("Song", msg.getString("title"))
assertEquals("https://lh3.googleusercontent.com/x", msg.getString("thumb"))
assertEquals("dQw4w9WgXcQ", msg.getString("videoId"))
assertFalse(SocialLogic.presence(playing.copy(playing = false), null).getBoolean("playing"))
assertFalse(SocialLogic.presence(null, null).getBoolean("playing"))
assertFalse(SocialLogic.presence(playing.copy(artworkUrl = "http://x"), null).has("thumb"))
}
@Test
fun mapsErrors() {
assertEquals("wrong_login", SocialLogic.errorOf(401, JSONObject("""{"error":"wrong_login"}""")))
assertEquals("unauthorized", SocialLogic.errorOf(401, null))
assertEquals("rate_limited", SocialLogic.errorOf(429, JSONObject("{}")))
assertEquals("failed", SocialLogic.errorOf(500, null))
}
}