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 829070f8..eb1d88bc 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 @@ -177,6 +177,7 @@ import kr.co.vividnext.sodalive.user.find_password.FindPasswordViewModel import kr.co.vividnext.sodalive.user.login.LoginViewModel import kr.co.vividnext.sodalive.user.signup.SignUpViewModel import kr.co.vividnext.sodalive.v2.creator.channel.CreatorChannelHomeViewModel +import kr.co.vividnext.sodalive.v2.creator.channel.audio.CreatorChannelAudioViewModel 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.live.CreatorChannelLiveViewModel @@ -410,6 +411,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) { viewModel { HomeRecommendationViewModel(get()) } viewModel { CreatorChannelHomeViewModel(get()) } viewModel { CreatorChannelLiveViewModel(get()) } + viewModel { CreatorChannelAudioViewModel(get()) } viewModel { PushNotificationListViewModel(get()) } viewModel { CharacterTabViewModel(get()) } viewModel { CharacterDetailViewModel(get()) } diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/audio/CreatorChannelAudioViewModel.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/audio/CreatorChannelAudioViewModel.kt new file mode 100644 index 00000000..8d93dd0d --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/audio/CreatorChannelAudioViewModel.kt @@ -0,0 +1,234 @@ +package kr.co.vividnext.sodalive.v2.creator.channel.audio + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import com.orhanobut.logger.Logger +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.v2.common.data.ContentSort +import kr.co.vividnext.sodalive.v2.creator.channel.audio.data.CreatorChannelAudioTabResponse +import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelAudioContentResponse +import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository + +class CreatorChannelAudioViewModel( + private val repository: CreatorChannelRepository +) : BaseViewModel() { + + private val _audioStateLiveData = MutableLiveData() + val audioStateLiveData: LiveData + get() = _audioStateLiveData + + private var creatorId: Long = 0L + private var isOwner: Boolean = false + private var selectedSort: ContentSort = ContentSort.LATEST + private var selectedThemeId: Long? = null + private var requestGeneration: Int = 0 + + fun loadAudio(creatorId: Long, isOwner: Boolean) { + if (creatorId <= 0) return + if (this.creatorId == creatorId && _audioStateLiveData.value != null) return + + this.creatorId = creatorId + this.isOwner = isOwner + loadFirstPage(selectedSort, selectedThemeId) + } + + fun changeSort(sort: ContentSort) { + if (sort == selectedSort) return + if (creatorId <= 0) return + + selectedSort = sort + loadFirstPage(sort, selectedThemeId) + } + + fun changeTheme(themeId: Long?) { + if (themeId == selectedThemeId) return + if (creatorId <= 0) return + + selectedThemeId = themeId + loadFirstPage(selectedSort, themeId) + } + + fun retryAudio() { + if (creatorId <= 0) return + + loadFirstPage(selectedSort, selectedThemeId) + } + + fun loadMore() { + val content = _audioStateLiveData.value as? CreatorChannelAudioUiState.Content ?: return + if (!content.hasNext || content.isLoadingMore || creatorId <= 0) return + + val generation = requestGeneration + _audioStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null) + requestAudio( + page = content.page + 1, + sort = content.selectedSort, + themeId = content.selectedThemeId, + generation = generation + ) { response -> + val data = response.data + val current = _audioStateLiveData.value as? CreatorChannelAudioUiState.Content ?: content + if (response.success && data != null) { + _audioStateLiveData.value = current.copy( + audioContents = current.audioContents + data.displayableAudioContents(), + page = data.page, + size = data.size, + hasNext = data.hasNext, + isLoadingMore = false + ) + } else { + _audioStateLiveData.value = current.copy( + isLoadingMore = false, + paginationErrorMessage = response.message + ) + } + } + } + + fun consumePaginationErrorMessage() { + val content = _audioStateLiveData.value as? CreatorChannelAudioUiState.Content ?: return + if (content.paginationErrorMessage == null) return + + _audioStateLiveData.value = content.copy(paginationErrorMessage = null) + } + + private fun loadFirstPage(sort: ContentSort, themeId: Long?) { + val generation = ++requestGeneration + _audioStateLiveData.value = CreatorChannelAudioUiState.Loading + requestAudio(page = FIRST_PAGE, sort = sort, themeId = themeId, generation = generation) { response -> + val data = response.data + if (response.success && data != null) { + val audioContents = data.displayableAudioContents() + _audioStateLiveData.value = if (audioContents.isEmpty() || data.audioContentCount == 0) { + CreatorChannelAudioUiState.Empty + } else { + data.toContentState(audioContents = audioContents) + } + } else { + _audioStateLiveData.value = CreatorChannelAudioUiState.Error(response.message) + } + } + } + + private fun requestAudio( + page: Int, + sort: ContentSort, + themeId: Long?, + generation: Int, + onSuccess: (ApiResponse) -> Unit + ) { + compositeDisposable.add( + repository.getAudio( + creatorId = creatorId, + page = page, + size = DEFAULT_PAGE_SIZE, + sort = sort, + themeId = themeId, + token = authToken() + ) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + { + if (generation == requestGeneration) { + onSuccess(it) + } + }, + { + if (generation != requestGeneration) return@subscribe + + it.message?.let { message -> Logger.e(message) } + val current = _audioStateLiveData.value as? CreatorChannelAudioUiState.Content + _audioStateLiveData.value = if (current != null && page > FIRST_PAGE) { + current.copy(isLoadingMore = false, paginationErrorMessage = it.message) + } else { + CreatorChannelAudioUiState.Error(it.message) + } + } + ) + ) + } + + private fun CreatorChannelAudioTabResponse.displayableAudioContents(): List = + audioContents.filter { it.duration != null } + + private fun CreatorChannelAudioTabResponse.toContentState( + audioContents: List, + isLoadingMore: Boolean = false + ) = CreatorChannelAudioUiState.Content( + audioContentCount = audioContentCount, + themes = toThemeUiModels(), + selectedSort = sort, + selectedThemeId = themeId, + rate = toRateUiModel(), + audioContents = audioContents, + page = page, + size = size, + hasNext = hasNext, + isLoadingMore = isLoadingMore + ) + + private fun CreatorChannelAudioTabResponse.toThemeUiModels(): List = + listOf(CreatorChannelAudioThemeUiModel(themeId = null, title = ALL_THEME_TITLE, isSelected = themeId == null)) + + themes.map { theme -> + CreatorChannelAudioThemeUiModel( + themeId = theme.themeId, + title = theme.themeName, + isSelected = theme.themeId == themeId + ) + } + + private fun CreatorChannelAudioTabResponse.toRateUiModel(): CreatorChannelAudioRateUiModel? = + if (!isOwner && themeId == null) { + CreatorChannelAudioRateUiModel( + ratePercent = purchasedAudioContentRate, + purchasedCount = purchasedAudioContentCount, + paidCount = paidAudioContentCount + ) + } else { + null + } + + private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}" + + companion object { + val DEFAULT_PAGE_SIZE = 20 + private const val FIRST_PAGE = 0 + private const val ALL_THEME_TITLE = "전체" + } +} + +sealed interface CreatorChannelAudioUiState { + data object Loading : CreatorChannelAudioUiState + data object Empty : CreatorChannelAudioUiState + data class Error(val message: String?) : CreatorChannelAudioUiState + data class Content( + val audioContentCount: Int, + val themes: List, + val selectedSort: ContentSort, + val selectedThemeId: Long?, + val rate: CreatorChannelAudioRateUiModel?, + val audioContents: List, + val page: Int, + val size: Int, + val hasNext: Boolean, + val isLoadingMore: Boolean = false, + val paginationErrorMessage: String? = null + ) : CreatorChannelAudioUiState +} + +data class CreatorChannelAudioThemeUiModel( + val themeId: Long?, + val title: String, + val isSelected: Boolean +) + +data class CreatorChannelAudioRateUiModel( + val ratePercent: Double, + val purchasedCount: Int, + val paidCount: Int +) diff --git a/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/audio/CreatorChannelAudioPaginationTest.kt b/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/audio/CreatorChannelAudioPaginationTest.kt new file mode 100644 index 00000000..2e20c2b4 --- /dev/null +++ b/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/audio/CreatorChannelAudioPaginationTest.kt @@ -0,0 +1,261 @@ +package kr.co.vividnext.sodalive.v2.creator.channel.audio + +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.SharedPreferenceManager +import kr.co.vividnext.sodalive.v2.common.data.ContentSort +import kr.co.vividnext.sodalive.v2.creator.channel.audio.data.CreatorChannelAudioTabResponse +import kr.co.vividnext.sodalive.v2.creator.channel.audio.data.CreatorChannelAudioThemeResponse +import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelAudioContentResponse +import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository +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 CreatorChannelAudioPaginationTest { + + private val context: Context = ApplicationProvider.getApplicationContext() + private lateinit var repository: CreatorChannelRepository + private lateinit var viewModel: CreatorChannelAudioViewModel + + @Before + fun setUp() { + setImmediateRxSchedulers() + SharedPreferenceManager.resetForTest() + SharedPreferenceManager.init(context) + SharedPreferenceManager.token = "test-token" + repository = org.mockito.kotlin.mock() + viewModel = CreatorChannelAudioViewModel(repository) + } + + @After + fun tearDown() { + RxJavaPlugins.reset() + RxAndroidPlugins.reset() + SharedPreferenceManager.resetForTest() + } + + @Test + fun `hasNext가 true이면 다음 페이지는 마지막 성공 응답의 page plus 1로 요청하고 append한다`() { + stubGetAudio( + page = 0, + response = Single.just( + ApiResponse(true, audioResponse(page = 0, ids = listOf(1L), hasNext = true), null) + ) + ) + stubGetAudio( + page = 1, + response = Single.just( + ApiResponse(true, audioResponse(page = 1, ids = listOf(2L), hasNext = false), null) + ) + ) + viewModel.loadAudio(100L, isOwner = false) + + viewModel.loadMore() + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(1, state.page) + assertEquals(listOf(1L, 2L), state.audioContents.map { it.audioContentId }) + assertFalse(state.hasNext) + verifyGetAudio(page = 1) + } + + @Test + fun `다음 페이지 로딩 중 중복 load-more 요청은 막는다`() { + val pending = SingleSubject.create>() + stubGetAudio( + page = 0, + response = Single.just( + ApiResponse(true, audioResponse(page = 0, ids = listOf(1L), hasNext = true), null) + ) + ) + stubGetAudio(page = 1, response = pending) + viewModel.loadAudio(100L, isOwner = false) + + viewModel.loadMore() + viewModel.loadMore() + + val loadingState = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertTrue(loadingState.isLoadingMore) + verifyGetAudio(page = 1, times = 1) + pending.onSuccess( + ApiResponse(true, audioResponse(page = 1, ids = listOf(2L), hasNext = false), null) + ) + val loadedState = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(listOf(1L, 2L), loadedState.audioContents.map { it.audioContentId }) + } + + @Test + fun `다음 페이지 실패는 기존 목록을 유지하고 pagination error를 표시한다`() { + stubGetAudio( + page = 0, + response = Single.just( + ApiResponse(true, audioResponse(page = 0, ids = listOf(1L), hasNext = true), null) + ) + ) + stubGetAudio(page = 1, response = Single.just(ApiResponse(false, null, "failed"))) + viewModel.loadAudio(100L, isOwner = false) + + viewModel.loadMore() + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(listOf(1L), state.audioContents.map { it.audioContentId }) + assertFalse(state.isLoadingMore) + assertEquals("failed", state.paginationErrorMessage) + } + + @Test + fun `pagination error message는 표시 후 clear되어 다시 표시되지 않는다`() { + stubGetAudio( + page = 0, + response = Single.just( + ApiResponse(true, audioResponse(page = 0, ids = listOf(1L), hasNext = true), null) + ) + ) + stubGetAudio(page = 1, response = Single.just(ApiResponse(false, null, "failed"))) + viewModel.loadAudio(100L, isOwner = false) + viewModel.loadMore() + + viewModel.consumePaginationErrorMessage() + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(null, state.paginationErrorMessage) + assertEquals(listOf(1L), state.audioContents.map { it.audioContentId }) + } + + @Test + fun `이전 load-more 응답은 이후 정렬 변경 목록에 append되지 않는다`() { + val nextPagePending = SingleSubject.create>() + stubGetAudio( + page = 0, + response = Single.just( + ApiResponse(true, audioResponse(page = 0, ids = listOf(1L), hasNext = true), null) + ) + ) + stubGetAudio(page = 1, response = nextPagePending) + stubGetAudio( + page = 0, + sort = ContentSort.POPULAR, + response = Single.just( + ApiResponse( + true, + audioResponse(page = 0, sort = ContentSort.POPULAR, ids = listOf(10L), hasNext = false), + null + ) + ) + ) + viewModel.loadAudio(100L, isOwner = false) + + viewModel.loadMore() + viewModel.changeSort(ContentSort.POPULAR) + nextPagePending.onSuccess( + ApiResponse(true, audioResponse(page = 1, ids = listOf(2L), hasNext = false), null) + ) + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(ContentSort.POPULAR, state.selectedSort) + assertEquals(listOf(10L), state.audioContents.map { it.audioContentId }) + } + + private fun stubGetAudio( + page: Int, + sort: ContentSort = ContentSort.LATEST, + themeId: Long? = null, + response: Single> + ) { + whenever( + repository.getAudio( + 100L, + page, + CreatorChannelAudioViewModel.DEFAULT_PAGE_SIZE, + sort, + themeId, + "Bearer test-token" + ) + ).thenReturn(response) + } + + private fun verifyGetAudio( + page: Int, + sort: ContentSort = ContentSort.LATEST, + themeId: Long? = null, + times: Int? = null + ) { + val verification = times?.let { verify(repository, times(it)) } ?: verify(repository) + verification.getAudio( + 100L, + page, + CreatorChannelAudioViewModel.DEFAULT_PAGE_SIZE, + sort, + themeId, + "Bearer test-token" + ) + } + + private fun setImmediateRxSchedulers() { + val trampoline = { _: Scheduler -> Schedulers.trampoline() } + RxJavaPlugins.setIoSchedulerHandler(trampoline) + RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() } + RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() } + } + + private fun audioResponse( + page: Int, + sort: ContentSort = ContentSort.LATEST, + ids: List, + hasNext: Boolean + ) = CreatorChannelAudioTabResponse( + audioContentCount = ids.size, + themes = listOf(CreatorChannelAudioThemeResponse(themeId = 10L, themeName = "ASMR")), + themeId = null, + purchasedAudioContentRate = 75.0, + purchasedAudioContentCount = 3, + paidAudioContentCount = 4, + audioContents = ids.map { audioContent(it) }, + sort = sort, + page = page, + size = CreatorChannelAudioViewModel.DEFAULT_PAGE_SIZE, + hasNext = hasNext + ) + + private fun audioContent(id: Long) = CreatorChannelAudioContentResponse( + audioContentId = id, + title = "오디오 $id", + duration = "01:00", + imageUrl = null, + price = 0, + isPointAvailable = true, + isFirstContent = false, + seriesName = null, + isOriginalSeries = null + ) + + 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/audio/CreatorChannelAudioViewModelTest.kt b/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/audio/CreatorChannelAudioViewModelTest.kt new file mode 100644 index 00000000..77b85388 --- /dev/null +++ b/app/src/test/java/kr/co/vividnext/sodalive/v2/creator/channel/audio/CreatorChannelAudioViewModelTest.kt @@ -0,0 +1,305 @@ +package kr.co.vividnext.sodalive.v2.creator.channel.audio + +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.SharedPreferenceManager +import kr.co.vividnext.sodalive.v2.common.data.ContentSort +import kr.co.vividnext.sodalive.v2.creator.channel.audio.data.CreatorChannelAudioTabResponse +import kr.co.vividnext.sodalive.v2.creator.channel.audio.data.CreatorChannelAudioThemeResponse +import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelAudioContentResponse +import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +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 CreatorChannelAudioViewModelTest { + + private val context: Context = ApplicationProvider.getApplicationContext() + private lateinit var repository: CreatorChannelRepository + private lateinit var viewModel: CreatorChannelAudioViewModel + + @Before + fun setUp() { + setImmediateRxSchedulers() + SharedPreferenceManager.resetForTest() + SharedPreferenceManager.init(context) + SharedPreferenceManager.token = "test-token" + repository = org.mockito.kotlin.mock() + viewModel = CreatorChannelAudioViewModel(repository) + } + + @After + fun tearDown() { + RxJavaPlugins.reset() + RxAndroidPlugins.reset() + SharedPreferenceManager.resetForTest() + } + + @Test + fun `최초 로드는 page 0 size 20 최신순 themeId null로 오디오 API를 호출하고 Content를 emit한다`() { + stubGetAudio(response = Single.just(ApiResponse(true, audioResponse(), null))) + + viewModel.loadAudio(100L, isOwner = false) + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(ContentSort.LATEST, state.selectedSort) + assertNull(state.selectedThemeId) + assertEquals(0, state.page) + assertEquals(listOf(1L), state.audioContents.map { it.audioContentId }) + verifyGetAudio(themeId = null) + } + + @Test + fun `응답 themes 앞에 전체 tab을 추가하고 themeId null이면 전체를 선택한다`() { + stubGetAudio(response = Single.just(ApiResponse(true, audioResponse(themeId = null), null))) + + viewModel.loadAudio(100L, isOwner = false) + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(listOf(null, 10L), state.themes.map { it.themeId }) + assertEquals("전체", state.themes.first().title) + assertTrue(state.themes.first().isSelected) + } + + @Test + fun `테마 선택은 page 0과 현재 sort와 선택 themeId로 재조회한다`() { + stubGetAudio(response = Single.just(ApiResponse(true, audioResponse(themeId = null, ids = listOf(1L)), null))) + stubGetAudio( + themeId = 10L, + response = Single.just(ApiResponse(true, audioResponse(themeId = 10L, ids = listOf(2L)), null)) + ) + viewModel.loadAudio(100L, isOwner = false) + + viewModel.changeTheme(10L) + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(10L, state.selectedThemeId) + assertEquals(listOf(2L), state.audioContents.map { it.audioContentId }) + verifyGetAudio(themeId = 10L) + } + + @Test + fun `전체 선택은 themeId null로 재조회한다`() { + stubGetAudio(response = Single.just(ApiResponse(true, audioResponse(themeId = null), null))) + stubGetAudio( + themeId = 10L, + response = Single.just(ApiResponse(true, audioResponse(themeId = 10L), null)) + ) + whenever( + repository.getAudio( + 100L, + 0, + CreatorChannelAudioViewModel.DEFAULT_PAGE_SIZE, + ContentSort.LATEST, + null, + "Bearer test-token" + ) + ).thenReturn( + Single.just(ApiResponse(true, audioResponse(themeId = null), null)), + Single.just(ApiResponse(true, audioResponse(themeId = null, ids = listOf(3L)), null)) + ) + viewModel.loadAudio(100L, isOwner = false) + viewModel.changeTheme(10L) + + viewModel.changeTheme(null) + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertNull(state.selectedThemeId) + assertEquals(listOf(3L), state.audioContents.map { it.audioContentId }) + verifyGetAudio(themeId = null, times = 2) + } + + @Test + fun `정렬 변경은 page 0과 선택 themeId를 유지해 재조회한다`() { + stubGetAudio(response = Single.just(ApiResponse(true, audioResponse(themeId = null), null))) + stubGetAudio(themeId = 10L, response = Single.just(ApiResponse(true, audioResponse(themeId = 10L), null))) + stubGetAudio( + sort = ContentSort.POPULAR, + themeId = 10L, + response = Single.just(ApiResponse(true, audioResponse(sort = ContentSort.POPULAR, themeId = 10L), null)) + ) + viewModel.loadAudio(100L, isOwner = false) + viewModel.changeTheme(10L) + + viewModel.changeSort(ContentSort.POPULAR) + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(ContentSort.POPULAR, state.selectedSort) + assertEquals(10L, state.selectedThemeId) + verifyGetAudio(sort = ContentSort.POPULAR, themeId = 10L) + } + + @Test + fun `같은 정렬 또는 같은 테마를 다시 선택하면 API를 재호출하지 않는다`() { + stubGetAudio(response = Single.just(ApiResponse(true, audioResponse(), null))) + viewModel.loadAudio(100L, isOwner = false) + + viewModel.changeSort(ContentSort.LATEST) + viewModel.changeTheme(null) + + verifyGetAudio(themeId = null, times = 1) + } + + @Test + fun `duration null item은 제외하고 표시 가능한 item이 없으면 Empty를 emit한다`() { + stubGetAudio( + response = Single.just(ApiResponse(true, audioResponse(ids = listOf(1L), nullDurationIds = setOf(1L)), null)) + ) + + viewModel.loadAudio(100L, isOwner = false) + + assertTrue(viewModel.audioStateLiveData.requireValue() is CreatorChannelAudioUiState.Empty) + } + + @Test + fun `audioContentCount가 0이면 표시 가능한 item이 있어도 Empty를 emit한다`() { + stubGetAudio( + response = Single.just(ApiResponse(true, audioResponse(audioContentCount = 0, ids = listOf(1L)), null)) + ) + + viewModel.loadAudio(100L, isOwner = false) + + assertTrue(viewModel.audioStateLiveData.requireValue() is CreatorChannelAudioUiState.Empty) + } + + @Test + fun `소장률은 내 채널이 아니고 전체 테마일 때만 생성된다`() { + stubGetAudio(response = Single.just(ApiResponse(true, audioResponse(themeId = null), null))) + viewModel.loadAudio(100L, isOwner = false) + + val guestState = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertEquals(CreatorChannelAudioRateUiModel(75.0, 3, 4), guestState.rate) + + viewModel = CreatorChannelAudioViewModel(repository) + stubGetAudio(response = Single.just(ApiResponse(true, audioResponse(themeId = null), null))) + viewModel.loadAudio(100L, isOwner = true) + + val ownerState = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertNull(ownerState.rate) + } + + @Test + fun `특정 테마 선택 상태에서는 소장률이 null이다`() { + stubGetAudio(response = Single.just(ApiResponse(true, audioResponse(themeId = null), null))) + stubGetAudio(themeId = 10L, response = Single.just(ApiResponse(true, audioResponse(themeId = 10L), null))) + viewModel.loadAudio(100L, isOwner = false) + + viewModel.changeTheme(10L) + + val state = viewModel.audioStateLiveData.requireValue() as CreatorChannelAudioUiState.Content + assertNull(state.rate) + } + + @Test + fun `creatorId가 0 이하이면 오디오 API를 호출하지 않는다`() { + viewModel.loadAudio(0L, isOwner = false) + + verify(repository, never()).getAudio(any(), any(), any(), any(), any(), any()) + } + + private fun stubGetAudio( + page: Int = 0, + sort: ContentSort = ContentSort.LATEST, + themeId: Long? = null, + response: Single> + ) { + whenever( + repository.getAudio( + 100L, + page, + CreatorChannelAudioViewModel.DEFAULT_PAGE_SIZE, + sort, + themeId, + "Bearer test-token" + ) + ).thenReturn(response) + } + + private fun verifyGetAudio( + page: Int = 0, + sort: ContentSort = ContentSort.LATEST, + themeId: Long?, + times: Int? = null + ) { + val verification = times?.let { verify(repository, times(it)) } ?: verify(repository) + verification.getAudio( + 100L, + page, + CreatorChannelAudioViewModel.DEFAULT_PAGE_SIZE, + sort, + themeId, + "Bearer test-token" + ) + } + + private fun setImmediateRxSchedulers() { + val trampoline = { _: Scheduler -> Schedulers.trampoline() } + RxJavaPlugins.setIoSchedulerHandler(trampoline) + RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() } + RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() } + } + + private fun audioResponse( + page: Int = 0, + sort: ContentSort = ContentSort.LATEST, + themeId: Long? = null, + audioContentCount: Int? = null, + ids: List = listOf(1L), + nullDurationIds: Set = emptySet(), + hasNext: Boolean = false + ) = CreatorChannelAudioTabResponse( + audioContentCount = audioContentCount ?: ids.size, + themes = listOf(CreatorChannelAudioThemeResponse(themeId = 10L, themeName = "ASMR")), + themeId = themeId, + purchasedAudioContentRate = 75.0, + purchasedAudioContentCount = 3, + paidAudioContentCount = 4, + audioContents = ids.map { audioContent(it, duration = if (it in nullDurationIds) null else "01:00") }, + sort = sort, + page = page, + size = CreatorChannelAudioViewModel.DEFAULT_PAGE_SIZE, + hasNext = hasNext + ) + + private fun audioContent(id: Long, duration: String?) = CreatorChannelAudioContentResponse( + audioContentId = id, + title = "오디오 $id", + duration = duration, + imageUrl = null, + price = 0, + isPointAvailable = true, + isFirstContent = false, + seriesName = null, + isOriginalSeries = null + ) + + private fun LiveData.requireValue(): T? { + var value: T? = null + val observer = Observer { value = it } + observeForever(observer) + removeObserver(observer) + return value + } +}