NovelTtsOwnershipTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/novel/NovelTtsOwnershipTest.kt
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)23package eu.friedcutlet.yokan.ui.screens.novel45import eu.friedcutlet.yokan.services.tts.ProfileSpeechService6import eu.friedcutlet.yokan.services.tts.TextToSpeechRequest7import eu.friedcutlet.yokan.services.tts.TextToSpeechService8import kotlin.test.Test9import kotlin.test.assertEquals10import kotlin.test.assertFailsWith11import kotlin.test.assertFalse12import kotlinx.coroutines.CancellationException13import kotlinx.coroutines.CompletableDeferred14import kotlinx.coroutines.CoroutineScope15import kotlinx.coroutines.SupervisorJob16import kotlinx.coroutines.async17import kotlinx.coroutines.awaitCancellation18import kotlinx.coroutines.cancelAndJoin19import kotlinx.coroutines.test.StandardTestDispatcher20import kotlinx.coroutines.test.TestScope21import kotlinx.coroutines.test.runCurrent22import kotlinx.coroutines.test.runTest2324class NovelTtsOwnershipTest {25 @Test fun pausedAndSupersededUtterancesCannotUpdateHighlight() = runTest {26 val f = Fixture(this)27 try {28 f.controller.start(listOf("First paragraph", "Second paragraph"), TextToSpeechRequest())29 runCurrent()30 val first = f.native.progress.single()31 first(0, 5)32 f.controller.pause()33 val paused = f.controller.state.value34 first(7, 9)35 assertEquals(paused, f.controller.state.value)36 f.controller.resume()37 runCurrent()38 val resumed = f.native.progress.last()39 f.controller.skipNext()40 runCurrent()41 first(1, 2); resumed(3, 4)42 assertEquals(1, f.controller.state.value.paragraphIndex)43 assertEquals(-1, f.controller.state.value.wordStart)44 f.native.progress.last()(0, 6)45 assertEquals(6, f.controller.state.value.wordEnd)46 } finally { f.close() }47 }4849 @Test fun retainedCommandsCannotRestartRetiredController() = runTest {50 val f = Fixture(this)51 try {52 f.controller.start(listOf("First", "Second"), TextToSpeechRequest())53 runCurrent()54 val old = f.native.progress.single()55 f.controller.closeAndJoin()56 old(0, 4)57 assertEquals(NovelTtsUiState(), f.controller.state.value)58 assertFailsWith<CancellationException> { f.controller.start(listOf("Late"), TextToSpeechRequest()) }59 assertFailsWith<CancellationException> { f.controller.resume() }60 assertFailsWith<CancellationException> { f.controller.pause() }61 assertFailsWith<CancellationException> { f.controller.skipNext() }62 assertFailsWith<CancellationException> { f.controller.skipPrevious() }63 assertFailsWith<CancellationException> { f.controller.updateRequest(TextToSpeechRequest(pitch = 1.5)) }64 assertFailsWith<CancellationException> { f.controller.stop() }65 assertEquals(listOf("First"), f.native.spoken)66 } finally { f.close() }67 }6869 @Test fun retirementDuringRestartCleanupCannotStartReplacementParagraph() = runTest {70 val f = Fixture(this)71 val release = CompletableDeferred<Unit>()72 f.native.stopping = { release.await() }73 try {74 f.controller.start(listOf("First", "Second"), TextToSpeechRequest())75 runCurrent()76 val restart = async { f.controller.skipNext() }77 runCurrent()78 assertFalse(restart.isCompleted)79 f.controller.retire()80 val close = async { f.controller.closeAndJoin() }81 runCurrent()82 assertFalse(close.isCompleted)83 release.complete(Unit)84 close.await()85 restart.join()86 assertEquals(listOf("First"), f.native.spoken)87 assertEquals(NovelTtsUiState(), f.controller.state.value)88 } finally { release.complete(Unit); f.close() }89 }9091 @Test fun canceledQueuedCommandCannotStartPlaybackAfterCleanup() = runTest {92 val f = Fixture(this)93 val release = CompletableDeferred<Unit>()94 f.native.stopping = { release.await() }95 try {96 f.controller.start(listOf("First"), TextToSpeechRequest())97 runCurrent()98 val stopping = async { f.controller.stop() }99 runCurrent()100 val queued = async { f.controller.start(listOf("Late"), TextToSpeechRequest()) }101 runCurrent()102 queued.cancel()103 runCurrent()104 release.complete(Unit)105 stopping.await()106 queued.join()107 runCurrent()108 assertEquals(listOf("First"), f.native.spoken)109 assertEquals(NovelTtsUiState(), f.controller.state.value)110 } finally { release.complete(Unit); f.close() }111 }112113 private class Fixture(test: TestScope) {114 val owner = SupervisorJob()115 val scope = CoroutineScope(owner + StandardTestDispatcher(test.testScheduler))116 val native = Native()117 val speech = ProfileSpeechService(native, scope)118 val reader = speech.reader()119 val controller = NovelTtsController(reader, scope) { reader.isAvailable }120 suspend fun close() { controller.closeAndJoin(); speech.closeAndJoin(); owner.cancelAndJoin() }121 }122 private class Native : TextToSpeechService {123 val spoken = mutableListOf<String>()124 val progress = mutableListOf<(Int, Int) -> Unit>()125 var stopping: suspend () -> Unit = { }126 override suspend fun voices(language: String?): List<String> = emptyList()127 override suspend fun speak(text: String, request: TextToSpeechRequest, onProgress: (Int, Int) -> Unit) {128 spoken += text; progress += onProgress129 awaitCancellation()130 }131 override suspend fun stop() { stopping() }132 }133}134