package eu.friedcutlet.yokan.ui.screens.novel import eu.friedcutlet.yokan.services.tts.TextToSpeechRequest import eu.friedcutlet.yokan.services.tts.TextToSpeechService import kotlin.test.Test import kotlin.test.assertEquals import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest @OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) class NovelTtsControllerTest { @Test fun extractsReadableTextBlocksInDocumentOrder() { val content = parseNovelRichContent( "

Heading

First paragraph

Quote
", ) assertEquals( listOf("Heading", "First paragraph", "Quote"), novelTtsParagraphs(content), ) } @Test fun speaksParagraphsInOrderAndReportsProgress() = runTest { val service = ControlledTextToSpeechService() val controller = NovelTtsController(service, backgroundScope) controller.start( paragraphs = listOf("First paragraph", "Second paragraph"), request = TextToSpeechRequest(speechRate = 0.8, pitch = 1.2), ) runCurrent() assertEquals(listOf("First paragraph"), service.spokenTexts) assertEquals(NovelTtsPlaybackState.Playing, controller.state.value.playback) assertEquals(0, controller.state.value.paragraphIndex) service.reportProgress(0, 5) assertEquals(0, controller.state.value.wordStart) assertEquals(5, controller.state.value.wordEnd) service.completeCurrent() runCurrent() assertEquals(listOf("First paragraph", "Second paragraph"), service.spokenTexts) assertEquals(1, controller.state.value.paragraphIndex) assertEquals(1f, controller.state.value.progress) service.completeCurrent() runCurrent() assertEquals(NovelTtsPlaybackState.Stopped, controller.state.value.playback) } @Test fun pauseAndResumeRestartsAtCurrentWord() = runTest { val service = ControlledTextToSpeechService() val controller = NovelTtsController(service, backgroundScope) controller.start(listOf("Read this sentence"), TextToSpeechRequest()) runCurrent() service.reportProgress(5, 9) val stopsBeforePause = service.stopCount controller.pause() assertEquals(NovelTtsPlaybackState.Paused, controller.state.value.playback) controller.resume() runCurrent() assertEquals(listOf("Read this sentence", "this sentence"), service.spokenTexts) assertEquals(stopsBeforePause + 1, service.stopCount) } @Test fun skipAndLiveSettingsRestartTheSelectedParagraph() = runTest { val service = ControlledTextToSpeechService() val controller = NovelTtsController(service, backgroundScope) controller.start(listOf("One", "Two", "Three"), TextToSpeechRequest()) runCurrent() controller.skipNext() runCurrent() assertEquals(1, controller.state.value.paragraphIndex) assertEquals("Two", service.spokenTexts.last()) controller.updateRequest(TextToSpeechRequest(speechRate = 1.4, pitch = 0.8)) runCurrent() assertEquals("Two", service.spokenTexts.last()) assertEquals(1.4, service.requests.last().speechRate) assertEquals(0.8, service.requests.last().pitch) controller.skipPrevious() runCurrent() assertEquals(0, controller.state.value.paragraphIndex) assertEquals("One", service.spokenTexts.last()) } } private class ControlledTextToSpeechService : TextToSpeechService { val spokenTexts = mutableListOf() val requests = mutableListOf() var stopCount = 0 private val completions = Channel(Channel.UNLIMITED) private var progress: ((Int, Int) -> Unit)? = null override suspend fun voices(language: String?): List = emptyList() override suspend fun speak( text: String, request: TextToSpeechRequest, onProgress: (startOffset: Int, endOffset: Int) -> Unit, ) { spokenTexts += text requests += request progress = onProgress completions.receive() } override suspend fun stop() { stopCount += 1 } fun reportProgress(start: Int, end: Int) { progress?.invoke(start, end) } fun completeCurrent() { completions.trySend(Unit) } }