package sbtbiswas.AidenOnTheGo.features.settings import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.advanceTimeBy import kotlinx.coroutines.test.advanceUntilIdle import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder import sbtbiswas.AidenOnTheGo.models.* import sbtbiswas.AidenOnTheGo.persistence.AidenSettingsCache import sbtbiswas.AidenOnTheGo.persistence.AidenSettingsProvider import java.io.IOException import java.util.UUID @OptIn(ExperimentalCoroutinesApi::class) class AidenSpeechSettlementReviewTest { @get:Rule val directory = TemporaryFolder() private fun status(selected: String, downloading: Boolean = false) = AidenSpeechStatus( AidenSpeechEngine(true), selected, listOf( AidenSpeechModel("small", "Small", "Fast", "100 MB", "English", true, true), AidenSpeechModel("large", "Large", "Accurate", "1 GB", "English", false, true, download = if (downloading) AidenSpeechDownload("large", 10, "download", "downloading") else null) ), AidenSpeechInputContract("pcm_s16le", 16000, 1, 60, false) ) private inner class Remote : AidenSettingsRemote { var speech = status("small") var selectionGate: CompletableDeferred? = null var readGate: CompletableDeferred? = null var outcome = "success" var reads = 0 override suspend fun speechStatus(): AidenSpeechStatus { reads++ val captured = speech readGate?.await() return captured } override suspend fun selectSpeechModel(modelId: String): AidenSpeechStatus { selectionGate?.await() when (outcome) { "failure" -> throw IOException("rejected") "cancel" -> throw CancellationException("cancelled") } speech = status(modelId) return speech } override suspend fun canCreateProvider() = false override suspend fun providers() = emptyList() override suspend fun memorySettings() = AidenMemorySettings(true, "r1") override suspend fun updateMemorySettings(revision: String, enabled: Boolean) = AidenMemorySettings(enabled, "r2") override suspend fun readAloudStatus() = AidenReadAloudStatus(true, true, "r1") override suspend fun createProvider(input: AidenProviderCreation, idempotencyKey: UUID) = error("unused") override suspend fun downloadSpeechModel(modelId: String) = error("unused") override suspend fun cancelSpeechModelDownload(modelId: String) = error("unused") } @Test fun duringActionReadCannotUndoConfirmedSelection() = checkRefresh("success") @Test fun duringActionReadCannotUndoRejectedSelectionRollback() = checkRefresh("failure") @Test fun duringActionReadCannotUndoCancelledSelectionRollback() = checkRefresh("cancel") private fun checkRefresh(outcome: String) = runTest { val cache = AidenSettingsCache(directory.root) val store = AidenSettingsStore(cache, this, StandardTestDispatcher(testScheduler)) val remote = Remote() store.bind("mac", remote) store.refreshSpeech() advanceUntilIdle() assertEquals("small", store.state.value.speech?.selectedModelId) remote.outcome = outcome remote.selectionGate = CompletableDeferred() store.selectSpeechModel("large") runCurrent() assertTrue(store.state.value.isSavingSpeech) // A read carries a value distinct from the eventual confirmed/rolled-back value. if (outcome != "success") remote.speech = status("large") remote.readGate = CompletableDeferred() store.refreshSpeech() runCurrent() assertEquals(2, remote.reads) remote.selectionGate!!.complete(Unit) runCurrent() assertFalse(store.state.value.isSavingSpeech) val expected = if (outcome == "success") "large" else "small" assertEquals(expected, store.state.value.speech?.selectedModelId) remote.readGate!!.complete(Unit) advanceUntilIdle() assertEquals(expected, store.state.value.speech?.selectedModelId) assertEquals(expected, AidenSettingsCache(directory.root).load("mac")?.speech?.selectedModelId) if (outcome == "success") assertEquals("large", remote.speech.selectedModelId) } @Test fun pollStartedDuringSelectionCannotUndoConfirmationAndFreshPollingStillWorks() = runTest { val cache = AidenSettingsCache(directory.root) val store = AidenSettingsStore(cache, this, StandardTestDispatcher(testScheduler)) val remote = Remote().apply { speech = status("small", downloading = true) } store.bind("mac", remote) store.refreshSpeech() runCurrent() assertEquals(1, remote.reads) remote.selectionGate = CompletableDeferred() store.selectSpeechModel("large") runCurrent() remote.readGate = CompletableDeferred() advanceTimeBy(1000) runCurrent() assertEquals(2, remote.reads) remote.selectionGate!!.complete(Unit) runCurrent() assertEquals("large", remote.speech.selectedModelId) remote.readGate!!.complete(Unit) advanceUntilIdle() assertEquals("large", store.state.value.speech?.selectedModelId) assertFalse(store.state.value.speech!!.isDownloading) assertEquals("large", AidenSettingsCache(directory.root).load("mac")?.speech?.selectedModelId) remote.readGate = null remote.speech = status("large", downloading = true) store.refreshSpeech() runCurrent() assertTrue(store.state.value.speech!!.isDownloading) remote.speech = status("large") advanceTimeBy(1000) runCurrent() assertFalse(store.state.value.speech!!.isDownloading) advanceUntilIdle() assertFalse(AidenSettingsCache(directory.root).load("mac")!!.speech!!.isDownloading) } }