2022-11-09 19:30:50 +01:00
|
|
|
package com.keylesspalace.tusky.usecase
|
|
|
|
|
|
|
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
2023-03-18 10:11:47 +01:00
|
|
|
import app.cash.turbine.test
|
|
|
|
import at.connyduck.calladapter.networkresult.NetworkResult
|
2022-11-09 19:30:50 +01:00
|
|
|
import com.keylesspalace.tusky.appstore.EventHub
|
|
|
|
import com.keylesspalace.tusky.appstore.PinEvent
|
|
|
|
import com.keylesspalace.tusky.entity.Status
|
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
2023-03-18 10:11:47 +01:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2022-11-09 19:30:50 +01:00
|
|
|
import okhttp3.ResponseBody.Companion.toResponseBody
|
2023-03-18 10:11:47 +01:00
|
|
|
import org.junit.Assert.assertEquals
|
2022-11-09 19:30:50 +01:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.runner.RunWith
|
|
|
|
import org.mockito.kotlin.doReturn
|
|
|
|
import org.mockito.kotlin.mock
|
|
|
|
import org.mockito.kotlin.stub
|
|
|
|
import org.robolectric.annotation.Config
|
|
|
|
import retrofit2.HttpException
|
|
|
|
import retrofit2.Response
|
2023-07-30 15:50:04 +02:00
|
|
|
import java.util.Date
|
2022-11-09 19:30:50 +01:00
|
|
|
|
|
|
|
@Config(sdk = [28])
|
|
|
|
@RunWith(AndroidJUnit4::class)
|
|
|
|
class TimelineCasesTest {
|
|
|
|
|
|
|
|
private lateinit var api: MastodonApi
|
|
|
|
private lateinit var eventHub: EventHub
|
|
|
|
private lateinit var timelineCases: TimelineCases
|
|
|
|
|
|
|
|
private val statusId = "1234"
|
|
|
|
|
|
|
|
@Before
|
|
|
|
fun setup() {
|
|
|
|
api = mock()
|
|
|
|
eventHub = EventHub()
|
|
|
|
timelineCases = TimelineCases(api, eventHub)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `pin success emits PinEvent`() {
|
|
|
|
api.stub {
|
2023-03-18 10:11:47 +01:00
|
|
|
onBlocking { pinStatus(statusId) } doReturn NetworkResult.success(mockStatus(pinned = true))
|
2022-11-09 19:30:50 +01:00
|
|
|
}
|
|
|
|
|
2023-03-18 10:11:47 +01:00
|
|
|
runBlocking {
|
|
|
|
eventHub.events.test {
|
|
|
|
timelineCases.pin(statusId, true)
|
|
|
|
assertEquals(PinEvent(statusId, true), awaitItem())
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 19:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `pin failure with server error throws TimelineError with server message`() {
|
|
|
|
api.stub {
|
2023-03-18 10:11:47 +01:00
|
|
|
onBlocking { pinStatus(statusId) } doReturn NetworkResult.failure(
|
2022-11-09 19:30:50 +01:00
|
|
|
HttpException(
|
|
|
|
Response.error<Status>(
|
|
|
|
422,
|
|
|
|
"{\"error\":\"Validation Failed: You have already pinned the maximum number of toots\"}".toResponseBody()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2023-03-18 10:11:47 +01:00
|
|
|
runBlocking {
|
|
|
|
assertEquals(
|
|
|
|
"Validation Failed: You have already pinned the maximum number of toots",
|
|
|
|
timelineCases.pin(statusId, true).exceptionOrNull()?.message
|
|
|
|
)
|
|
|
|
}
|
2022-11-09 19:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun mockStatus(pinned: Boolean = false): Status {
|
|
|
|
return Status(
|
|
|
|
id = "123",
|
|
|
|
url = "https://mastodon.social/@Tusky/100571663297225812",
|
|
|
|
account = mock(),
|
|
|
|
inReplyToId = null,
|
|
|
|
inReplyToAccountId = null,
|
|
|
|
reblog = null,
|
|
|
|
content = "",
|
|
|
|
createdAt = Date(),
|
2022-12-03 12:15:54 +01:00
|
|
|
editedAt = null,
|
2022-11-09 19:30:50 +01:00
|
|
|
emojis = emptyList(),
|
|
|
|
reblogsCount = 0,
|
|
|
|
favouritesCount = 0,
|
|
|
|
repliesCount = 0,
|
|
|
|
reblogged = false,
|
|
|
|
favourited = false,
|
|
|
|
bookmarked = false,
|
|
|
|
sensitive = false,
|
|
|
|
spoilerText = "",
|
|
|
|
visibility = Status.Visibility.PUBLIC,
|
|
|
|
attachments = arrayListOf(),
|
|
|
|
mentions = listOf(),
|
|
|
|
tags = listOf(),
|
|
|
|
application = null,
|
|
|
|
pinned = pinned,
|
|
|
|
muted = false,
|
|
|
|
poll = null,
|
|
|
|
card = null,
|
|
|
|
language = null,
|
2023-03-13 13:16:39 +01:00
|
|
|
filtered = null
|
2022-11-09 19:30:50 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|