Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
3.2 kB · 79 lines
Kotlin
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980package social.grain
import androidx.test.ext.junit.runners.AndroidJUnit4import kotlinx.coroutines.runBlockingimport kotlinx.serialization.json.Jsonimport okhttp3.OkHttpClientimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Testimport org.junit.runner.RunWithimport social.grain.data.api.XrpcClientimport social.grain.data.api.getCommentThreadimport social.grain.data.api.getFeedimport social.grain.data.api.getGalleryimport social.grain.data.auth.AuthManagerimport java.util.concurrent.TimeUnit
/** * Decodes live gallery-detail and comment responses into the ported models. * * Same class of risk the feed models carried: a field typed wrong here shows up * only as an empty comment sheet with a decode error buried in the log. */@RunWith(AndroidJUnit4::class)class CommentDecodeTest {
private val client = XrpcClient( baseUrl = AuthManager.SERVER_URL, http = OkHttpClient.Builder() .connectTimeout(20, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .build(), json = Json { ignoreUnknownKeys = true explicitNulls = false coerceInputValues = true }, )
@Test fun galleryDetailDecodesForARealGallery() = runBlocking { val uri = client.getFeed(feed = "recent", limit = 1).items.orEmpty().first().uri
val gallery = client.getGallery(uri).gallery assertEquals(uri, gallery.uri) assertTrue("Creator DID missing", gallery.creator.did.startsWith("did:")) assertTrue("Detail returned no photos", gallery.items.orEmpty().isNotEmpty()) }
@Test fun commentThreadDecodesAndRepliesNameTheirRoot() = runBlocking { // Walk the feed for a gallery that actually has comments rather than // asserting against whatever happens to be newest. val withComments = client.getFeed(feed = "recent", limit = 60).items.orEmpty() .firstOrNull { (it.commentCount ?: 0) > 0 }
if (withComments == null) { // Nothing to assert against; still exercise the decode path so a // broken response type fails rather than silently skipping. client.getCommentThread(client.getFeed(feed = "recent", limit = 1).items!!.first().uri) return@runBlocking }
val thread = client.getCommentThread(withComments.uri) assertTrue("Gallery reported comments but the thread was empty", thread.comments.isNotEmpty())
val uris = thread.comments.map { it.uri }.toSet() for (comment in thread.comments) { assertTrue("Comment URI should be an at:// URI", comment.uri.startsWith("at://")) assertTrue("Comment author DID missing", comment.author.did.startsWith("did:")) comment.replyTo?.let { assertTrue("replyTo should be an at:// URI, was '$it'", it.startsWith("at://")) } // Exercise the facet path against whatever real records contain. comment.facets?.forEach { it.parsedFeatures } } assertEquals("Comment URIs should be unique", thread.comments.size, uris.size) }}