package social.grain import kotlinx.serialization.json.Json import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test import social.grain.data.api.SearchProfilesResponse /** * Profile search reuses ActorListItem, the same model the follower and * favourite lists decode into. A field typed wrong breaks all of them at once. * The fixture is a real response from the appview. */ class SearchDecodeTest { private val json = Json { ignoreUnknownKeys = true explicitNulls = false coerceInputValues = true } @Test fun `a real profile search response decodes`() { val fixture = """ { "items": [ { "did": "did:plc:bcgltzqazw5tb6k2g3ttenbj", "handle": "chadtmiller.com", "displayName": "chad", "description": "building @grain.social, suuuup", "avatar": "https://cdn.grain.social/x/avatar/plain/did:plc:bcgltzqazw5tb6k2g3ttenbj/y" } ], "cursor": "abc" } """.trimIndent() val response = json.decodeFromString(fixture) val profile = response.items!!.single() assertEquals("chadtmiller.com", profile.handle) assertEquals("chad", profile.displayName) assertTrue(profile.avatar!!.startsWith("https://")) assertEquals("abc", response.cursor) } @Test fun `no matches decodes to an empty list rather than failing`() { val response = json.decodeFromString("""{"items":[]}""") assertTrue(response.items!!.isEmpty()) assertNull(response.cursor) } @Test fun `a result missing optional fields decodes`() { val response = json.decodeFromString( """{"items":[{"did":"did:plc:sparse","handle":"sparse.test"}]}""", ) val profile = response.items!!.single() assertNull(profile.displayName) assertNull(profile.description) } }