diff --git a/app/src/main/java/kr/co/vividnext/sodalive/di/AppDI.kt b/app/src/main/java/kr/co/vividnext/sodalive/di/AppDI.kt index 97187c21..18312eb4 100644 --- a/app/src/main/java/kr/co/vividnext/sodalive/di/AppDI.kt +++ b/app/src/main/java/kr/co/vividnext/sodalive/di/AppDI.kt @@ -183,6 +183,7 @@ import kr.co.vividnext.sodalive.v2.creator.channel.audio.CreatorChannelAudioView import kr.co.vividnext.sodalive.v2.creator.channel.community.CreatorChannelCommunityViewModel import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelApi import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository +import kr.co.vividnext.sodalive.v2.creator.channel.donation.CreatorChannelDonationViewModel import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.CreatorChannelFanTalkViewModel import kr.co.vividnext.sodalive.v2.creator.channel.live.CreatorChannelLiveViewModel import kr.co.vividnext.sodalive.v2.creator.channel.series.CreatorChannelSeriesViewModel @@ -421,6 +422,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) { viewModel { CreatorChannelSeriesViewModel(get()) } viewModel { CreatorChannelCommunityViewModel(get(), get()) } viewModel { CreatorChannelFanTalkViewModel(get(), get()) } + viewModel { CreatorChannelDonationViewModel(get(), get()) } viewModel { PushNotificationListViewModel(get()) } viewModel { CharacterTabViewModel(get()) } viewModel { CharacterDetailViewModel(get()) } diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/donation/CreatorChannelDonationViewModel.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/donation/CreatorChannelDonationViewModel.kt new file mode 100644 index 00000000..dae33b98 --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/donation/CreatorChannelDonationViewModel.kt @@ -0,0 +1,237 @@ +package kr.co.vividnext.sodalive.v2.creator.channel.donation + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers +import io.reactivex.rxjava3.schedulers.Schedulers +import kr.co.vividnext.sodalive.base.BaseViewModel +import kr.co.vividnext.sodalive.common.ApiResponse +import kr.co.vividnext.sodalive.common.SharedPreferenceManager +import kr.co.vividnext.sodalive.common.SodaLiveApplicationHolder +import kr.co.vividnext.sodalive.common.UtcRelativeTimeTextFormatter +import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository +import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationTabResponse +import kr.co.vividnext.sodalive.v2.creator.channel.donation.model.CreatorChannelDonationRankingUiModel +import kr.co.vividnext.sodalive.v2.creator.channel.donation.model.CreatorChannelDonationUiModel +import kr.co.vividnext.sodalive.v2.creator.channel.donation.model.toDonationRankingUiModels +import kr.co.vividnext.sodalive.v2.creator.channel.donation.model.toDonationUiModels + +class CreatorChannelDonationViewModel( + private val repository: CreatorChannelRepository, + private val relativeTimeTextFormatter: UtcRelativeTimeTextFormatter +) : BaseViewModel() { + + private val _donationStateLiveData = MutableLiveData() + val donationStateLiveData: LiveData + get() = _donationStateLiveData + + private val context = SodaLiveApplicationHolder.get() + private var creatorId: Long = 0L + private var isOwner: Boolean = false + private var requestGeneration: Int = 0 + private var isPostChannelDonationInProgress: Boolean = false + private var donationSuccessEvent: Boolean = false + + fun loadDonations(creatorId: Long, isOwner: Boolean) { + if (creatorId <= 0) return + val shouldSkipReload = this.creatorId == creatorId && this.isOwner == isOwner && _donationStateLiveData.value != null + if (shouldSkipReload) return + + this.creatorId = creatorId + this.isOwner = isOwner + loadFirstPage() + } + + fun retryDonations() { + if (creatorId <= 0) return + + loadFirstPage() + } + + fun refreshDonations() { + if (creatorId <= 0) return + + loadFirstPage() + } + + fun loadMore() { + val content = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: return + if (!content.hasNext || content.isLoadingMore || creatorId <= 0) return + + val generation = requestGeneration + _donationStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null) + requestDonations(page = content.page + 1, generation = generation) { response -> + val data = response.data + val current = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: content + if (response.success && data != null) { + _donationStateLiveData.value = current.copy( + donationCount = data.donationCount, + rankings = data.toDonationRankingUiModels(), + donations = current.donations + data.toDonationUiModels(), + page = data.page, + size = data.size, + hasNext = data.hasNext, + isLoadingMore = false + ) + } else { + _donationStateLiveData.value = current.copy( + isLoadingMore = false, + paginationErrorMessage = response.message + ) + } + } + } + + fun postChannelDonation(can: Int, isSecret: Boolean, message: String) { + if (creatorId <= 0 || isPostChannelDonationInProgress) return + + isPostChannelDonationInProgress = true + compositeDisposable.add( + repository.postChannelDonation( + creatorId = creatorId, + can = can, + isSecret = isSecret, + message = message, + token = authToken() + ) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + { + isPostChannelDonationInProgress = false + if (it.success) { + SharedPreferenceManager.can = (SharedPreferenceManager.can - can).coerceAtLeast(0) + donationSuccessEvent = true + loadFirstPage() + } else { + setActionToastMessage(it.message) + } + }, + { + isPostChannelDonationInProgress = false + setActionToastMessage(it.message) + } + ) + ) + } + + fun consumePaginationErrorMessage() { + val content = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: return + if (content.paginationErrorMessage == null) return + + _donationStateLiveData.value = content.copy(paginationErrorMessage = null) + } + + fun consumeActionToastMessage() { + val content = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: return + if (content.actionToastMessage == null) return + + _donationStateLiveData.value = content.copy(actionToastMessage = null) + } + + fun consumeDonationSuccessEvent(): Boolean { + val event = donationSuccessEvent + donationSuccessEvent = false + return event + } + + private fun loadFirstPage() { + val generation = ++requestGeneration + _donationStateLiveData.value = CreatorChannelDonationUiState.Loading + requestDonations(page = FIRST_PAGE, generation = generation) { response -> + val data = response.data + if (response.success && data != null) { + val donations = data.toDonationUiModels() + _donationStateLiveData.value = if (donations.isEmpty() || data.donationCount == 0) { + CreatorChannelDonationUiState.Empty(data.donationCount, isOwner) + } else { + data.toContentState(donations) + } + } else { + _donationStateLiveData.value = CreatorChannelDonationUiState.Error(response.message) + } + } + } + + private fun requestDonations( + page: Int, + generation: Int, + onSuccess: (ApiResponse) -> Unit + ) { + compositeDisposable.add( + repository.getDonations( + creatorId = creatorId, + page = page, + size = DEFAULT_PAGE_SIZE, + token = authToken() + ) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + { + if (generation == requestGeneration) { + onSuccess(it) + } + }, + { + if (generation != requestGeneration) return@subscribe + + val current = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content + _donationStateLiveData.value = if (current != null && page > FIRST_PAGE) { + current.copy(isLoadingMore = false, paginationErrorMessage = it.message) + } else { + CreatorChannelDonationUiState.Error(it.message) + } + } + ) + ) + } + + private fun setActionToastMessage(message: String?) { + val content = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: return + _donationStateLiveData.value = content.copy(actionToastMessage = message) + } + + private fun CreatorChannelDonationTabResponse.toContentState( + donations: List + ) = CreatorChannelDonationUiState.Content( + donationCount = donationCount, + rankings = toDonationRankingUiModels(), + donations = donations, + page = page, + size = size, + hasNext = hasNext, + isOwner = isOwner + ) + + private fun CreatorChannelDonationTabResponse.toDonationRankingUiModels(): List = + rankings.toDonationRankingUiModels() + + private fun CreatorChannelDonationTabResponse.toDonationUiModels(): List = + donations.toDonationUiModels(context, relativeTimeTextFormatter) + + private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}" + + companion object { + const val DEFAULT_PAGE_SIZE = 20 + private const val FIRST_PAGE = 0 + } +} + +sealed interface CreatorChannelDonationUiState { + data object Loading : CreatorChannelDonationUiState + data class Empty(val donationCount: Int, val isOwner: Boolean) : CreatorChannelDonationUiState + data class Error(val message: String?) : CreatorChannelDonationUiState + data class Content( + val donationCount: Int, + val rankings: List, + val donations: List, + val page: Int, + val size: Int, + val hasNext: Boolean, + val isOwner: Boolean, + val isLoadingMore: Boolean = false, + val paginationErrorMessage: String? = null, + val actionToastMessage: String? = null + ) : CreatorChannelDonationUiState +} diff --git a/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/donation/CreatorChannelDonationPaginationTest.kt b/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/donation/CreatorChannelDonationPaginationTest.kt new file mode 100644 index 00000000..bc1ed6a2 --- /dev/null +++ b/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/donation/CreatorChannelDonationPaginationTest.kt @@ -0,0 +1,171 @@ +package kr.co.vividnext.sodalive.v2.creator.channel.donation + +import android.app.Application +import android.content.Context +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import androidx.test.core.app.ApplicationProvider +import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins +import io.reactivex.rxjava3.core.Scheduler +import io.reactivex.rxjava3.core.Single +import io.reactivex.rxjava3.plugins.RxJavaPlugins +import io.reactivex.rxjava3.schedulers.Schedulers +import io.reactivex.rxjava3.subjects.SingleSubject +import kr.co.vividnext.sodalive.common.ApiResponse +import kr.co.vividnext.sodalive.common.AndroidUtcRelativeTimeTextFormatter +import kr.co.vividnext.sodalive.common.SharedPreferenceManager +import kr.co.vividnext.sodalive.common.SodaLiveApplicationHolder +import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository +import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationResponse +import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationTabResponse +import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.MemberDonationRankingResponse +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.times +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], application = Application::class) +class CreatorChannelDonationPaginationTest { + + private val context: Context = ApplicationProvider.getApplicationContext() + private lateinit var repository: CreatorChannelRepository + private lateinit var viewModel: CreatorChannelDonationViewModel + + @Before + fun setUp() { + setImmediateRxSchedulers() + SharedPreferenceManager.resetForTest() + SharedPreferenceManager.init(context) + SodaLiveApplicationHolder.init(context as Application) + SharedPreferenceManager.token = "test-token" + repository = org.mockito.kotlin.mock() + viewModel = CreatorChannelDonationViewModel(repository, AndroidUtcRelativeTimeTextFormatter(context)) + } + + @After + fun tearDown() { + RxJavaPlugins.reset() + RxAndroidPlugins.reset() + SharedPreferenceManager.resetForTest() + } + + @Test + fun `hasNext가 true이면 다음 페이지는 마지막 응답의 page plus 1로 요청하고 append한다`() { + stubGetDonations(0, Single.just(ApiResponse(true, donationResponse(page = 0, ids = listOf(1L), hasNext = true), null))) + stubGetDonations(1, Single.just(ApiResponse(true, donationResponse(page = 1, ids = listOf(2L), hasNext = false), null))) + viewModel.loadDonations(100L, isOwner = false) + + viewModel.loadMore() + + val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content + assertEquals(1, state.page) + assertEquals(listOf("member 1", "member 2"), state.donations.map { it.nickname }) + assertFalse(state.hasNext) + verifyGetDonations(page = 1) + } + + @Test + fun `다음 페이지 로딩 중 중복 load-more 요청은 막고 size 20을 유지한다`() { + val pending = SingleSubject.create>() + stubGetDonations(0, Single.just(ApiResponse(true, donationResponse(page = 0, ids = listOf(1L), hasNext = true), null))) + stubGetDonations(1, pending) + viewModel.loadDonations(100L, isOwner = false) + + viewModel.loadMore() + viewModel.loadMore() + + val loadingState = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content + assertTrue(loadingState.isLoadingMore) + verifyGetDonations(page = 1, times = 1) + pending.onSuccess(ApiResponse(true, donationResponse(page = 1, ids = listOf(2L), hasNext = false), null)) + val loadedState = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content + assertEquals(CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, loadedState.size) + assertEquals(listOf("member 1", "member 2"), loadedState.donations.map { it.nickname }) + } + + @Test + fun `다음 페이지 실패는 기존 목록을 유지하고 pagination error message만 설정한다`() { + stubGetDonations(0, Single.just(ApiResponse(true, donationResponse(page = 0, ids = listOf(1L), hasNext = true), null))) + stubGetDonations(1, Single.just(ApiResponse(false, null, "failed"))) + viewModel.loadDonations(100L, isOwner = false) + + viewModel.loadMore() + + val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content + assertEquals(listOf("member 1"), state.donations.map { it.nickname }) + assertFalse(state.isLoadingMore) + assertEquals("failed", state.paginationErrorMessage) + } + + @Test + fun `pagination error message는 consume 후 null이 된다`() { + stubGetDonations(0, Single.just(ApiResponse(true, donationResponse(page = 0, ids = listOf(1L), hasNext = true), null))) + stubGetDonations(1, Single.just(ApiResponse(false, null, "failed"))) + viewModel.loadDonations(100L, isOwner = false) + viewModel.loadMore() + + viewModel.consumePaginationErrorMessage() + + val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content + assertEquals(null, state.paginationErrorMessage) + assertEquals(listOf("member 1"), state.donations.map { it.nickname }) + } + + private fun stubGetDonations(page: Int, response: Single>) { + whenever(repository.getDonations(100L, page, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token")) + .thenReturn(response) + } + + private fun verifyGetDonations(page: Int, times: Int? = null) { + val verification = times?.let { verify(repository, times(it)) } ?: verify(repository) + verification.getDonations(100L, page, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token") + } + + private fun setImmediateRxSchedulers() { + val trampoline = { _: Scheduler -> Schedulers.trampoline() } + RxJavaPlugins.setIoSchedulerHandler(trampoline) + RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() } + RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() } + } + + private fun donationResponse(page: Int, ids: List, hasNext: Boolean) = CreatorChannelDonationTabResponse( + donationCount = ids.size, + rankings = listOf(ranking()), + donations = ids.map { donation(it) }, + page = page, + size = CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, + hasNext = hasNext + ) + + private fun ranking() = MemberDonationRankingResponse( + userId = 10L, + nickname = "member", + profileImage = "profile.png", + donationCan = 100 + ) + + private fun donation(id: Long) = CreatorChannelDonationResponse( + nickname = "member $id", + profileImageUrl = "profile.png", + can = 50, + message = "응원 $id", + createdAtUtc = "2026-06-21T00:00:00Z" + ) + + private fun LiveData.requireValue(): T? { + var value: T? = null + val observer = Observer { value = it } + observeForever(observer) + removeObserver(observer) + return value + } +} diff --git a/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/donation/CreatorChannelDonationViewModelTest.kt b/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/donation/CreatorChannelDonationViewModelTest.kt new file mode 100644 index 00000000..ea38ca74 --- /dev/null +++ b/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/donation/CreatorChannelDonationViewModelTest.kt @@ -0,0 +1,191 @@ +package kr.co.vividnext.sodalive.v2.creator.channel.donation + +import android.app.Application +import android.content.Context +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import androidx.test.core.app.ApplicationProvider +import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins +import io.reactivex.rxjava3.core.Scheduler +import io.reactivex.rxjava3.core.Single +import io.reactivex.rxjava3.plugins.RxJavaPlugins +import io.reactivex.rxjava3.schedulers.Schedulers +import kr.co.vividnext.sodalive.common.ApiResponse +import kr.co.vividnext.sodalive.common.AndroidUtcRelativeTimeTextFormatter +import kr.co.vividnext.sodalive.common.SharedPreferenceManager +import kr.co.vividnext.sodalive.common.SodaLiveApplicationHolder +import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository +import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationResponse +import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationTabResponse +import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.MemberDonationRankingResponse +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.any +import org.mockito.kotlin.never +import org.mockito.kotlin.times +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], application = Application::class) +class CreatorChannelDonationViewModelTest { + + private val context: Context = ApplicationProvider.getApplicationContext() + private lateinit var repository: CreatorChannelRepository + private lateinit var viewModel: CreatorChannelDonationViewModel + + @Before + fun setUp() { + setImmediateRxSchedulers() + SharedPreferenceManager.resetForTest() + SharedPreferenceManager.init(context) + SodaLiveApplicationHolder.init(context as Application) + SharedPreferenceManager.token = "test-token" + SharedPreferenceManager.can = 200 + repository = org.mockito.kotlin.mock() + viewModel = CreatorChannelDonationViewModel(repository, AndroidUtcRelativeTimeTextFormatter(context)) + } + + @After + fun tearDown() { + RxJavaPlugins.reset() + RxAndroidPlugins.reset() + SharedPreferenceManager.resetForTest() + } + + @Test + fun `최초 로드는 page 0 size 20으로 Donation API를 호출하고 Content를 emit한다`() { + stubGetDonations(response = Single.just(ApiResponse(true, donationResponse(ids = listOf(1L)), null))) + + viewModel.loadDonations(100L, isOwner = false) + + val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Content + assertEquals(0, state.page) + assertEquals(CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, state.size) + assertEquals(listOf("member 1"), state.donations.map { it.nickname }) + verifyGetDonations() + } + + @Test + fun `donationCount가 0이면 Empty 상태가 된다`() { + stubGetDonations(response = Single.just(ApiResponse(true, donationResponse(donationCount = 0, ids = listOf(1L)), null))) + + viewModel.loadDonations(100L, isOwner = false) + + val state = viewModel.donationStateLiveData.requireValue() as CreatorChannelDonationUiState.Empty + assertEquals(0, state.donationCount) + } + + @Test + fun `표시 가능한 donations가 없으면 Empty 상태가 된다`() { + stubGetDonations(response = Single.just(ApiResponse(true, donationResponse(ids = emptyList()), null))) + + viewModel.loadDonations(100L, isOwner = false) + + assertTrue(viewModel.donationStateLiveData.requireValue() is CreatorChannelDonationUiState.Empty) + } + + @Test + fun `creatorId가 0 이하이면 API를 호출하지 않는다`() { + viewModel.loadDonations(0L, isOwner = false) + + verify(repository, never()).getDonations(any(), any(), any(), any()) + } + + @Test + fun `채널 후원 성공은 can을 차감하고 첫 페이지를 다시 로드하며 성공 이벤트를 emit한다`() { + whenever(repository.getDonations(100L, 0, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token")) + .thenReturn(Single.just(ApiResponse(true, donationResponse(ids = listOf(1L)), null))) + .thenReturn(Single.just(ApiResponse(true, donationResponse(ids = listOf(2L)), null))) + whenever(repository.postChannelDonation(100L, 50, true, "응원", "Bearer test-token")).thenReturn( + Single.just(ApiResponse(true, Any(), null)) + ) + viewModel.loadDonations(100L, isOwner = false) + + viewModel.postChannelDonation(can = 50, isSecret = true, message = "응원") + + verify(repository).postChannelDonation(100L, 50, true, "응원", "Bearer test-token") + verify(repository, times(2)).getDonations(100L, 0, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token") + assertEquals(150, SharedPreferenceManager.can) + assertEquals(true, viewModel.consumeDonationSuccessEvent()) + assertEquals(false, viewModel.consumeDonationSuccessEvent()) + } + + @Test + fun `채널 후원 성공 시 보유 can보다 큰 금액 차감은 0으로 보정한다`() { + whenever(repository.getDonations(100L, 0, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token")) + .thenReturn(Single.just(ApiResponse(true, donationResponse(ids = listOf(1L)), null))) + .thenReturn(Single.just(ApiResponse(true, donationResponse(ids = listOf(2L)), null))) + whenever(repository.postChannelDonation(100L, 250, false, "응원", "Bearer test-token")).thenReturn( + Single.just(ApiResponse(true, Any(), null)) + ) + viewModel.loadDonations(100L, isOwner = false) + + SharedPreferenceManager.can = 30 + viewModel.postChannelDonation(can = 250, isSecret = false, message = "응원") + + assertEquals(0, SharedPreferenceManager.can) + } + + private fun stubGetDonations( + page: Int = 0, + response: Single> + ) { + whenever(repository.getDonations(100L, page, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token")) + .thenReturn(response) + } + + private fun verifyGetDonations(page: Int = 0) { + verify(repository).getDonations(100L, page, CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token") + } + + private fun setImmediateRxSchedulers() { + val trampoline = { _: Scheduler -> Schedulers.trampoline() } + RxJavaPlugins.setIoSchedulerHandler(trampoline) + RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() } + RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() } + } + + private fun donationResponse( + page: Int = 0, + donationCount: Int? = null, + ids: List, + hasNext: Boolean = false + ) = CreatorChannelDonationTabResponse( + donationCount = donationCount ?: ids.size, + rankings = listOf(ranking()), + donations = ids.map { donation(it) }, + page = page, + size = CreatorChannelDonationViewModel.DEFAULT_PAGE_SIZE, + hasNext = hasNext + ) + + private fun ranking() = MemberDonationRankingResponse( + userId = 10L, + nickname = "member", + profileImage = "profile.png", + donationCan = 100 + ) + + private fun donation(id: Long) = CreatorChannelDonationResponse( + nickname = "member $id", + profileImageUrl = "profile.png", + can = 50, + message = "응원 $id", + createdAtUtc = "2026-06-21T00:00:00Z" + ) + + private fun LiveData.requireValue(): T? { + var value: T? = null + val observer = Observer { value = it } + observeForever(observer) + removeObserver(observer) + return value + } +}