Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566package social.grain
import kotlinx.serialization.json.Jsonimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport 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<SearchProfilesResponse>(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<SearchProfilesResponse>("""{"items":[]}""") assertTrue(response.items!!.isEmpty()) assertNull(response.cursor) }
@Test fun `a result missing optional fields decodes`() { val response = json.decodeFromString<SearchProfilesResponse>( """{"items":[{"did":"did:plc:sparse","handle":"sparse.test"}]}""", ) val profile = response.items!!.single() assertNull(profile.displayName) assertNull(profile.description) }}