Yokan / rebuild
Course 1 8624561

NovelTtsControllerTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/novel/NovelTtsControllerTest.kt

test / fixture129 lines
1package eu.friedcutlet.yokan.ui.screens.novel23import eu.friedcutlet.yokan.services.tts.TextToSpeechRequest4import eu.friedcutlet.yokan.services.tts.TextToSpeechService5import kotlin.test.Test6import kotlin.test.assertEquals7import kotlinx.coroutines.channels.Channel8import kotlinx.coroutines.test.runCurrent9import kotlinx.coroutines.test.runTest1011@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)12class NovelTtsControllerTest {13    @Test14    fun extractsReadableTextBlocksInDocumentOrder() {15        val content = parseNovelRichContent(16            "<h2>Heading</h2><p>First <strong>paragraph</strong></p><img src=\"cover.jpg\"><blockquote>Quote</blockquote>",17        )1819        assertEquals(20            listOf("Heading", "First paragraph", "Quote"),21            novelTtsParagraphs(content),22        )23    }2425    @Test26    fun speaksParagraphsInOrderAndReportsProgress() = runTest {27        val service = ControlledTextToSpeechService()28        val controller = NovelTtsController(service, backgroundScope)2930        controller.start(31            paragraphs = listOf("First paragraph", "Second paragraph"),32            request = TextToSpeechRequest(speechRate = 0.8, pitch = 1.2),33        )34        runCurrent()3536        assertEquals(listOf("First paragraph"), service.spokenTexts)37        assertEquals(NovelTtsPlaybackState.Playing, controller.state.value.playback)38        assertEquals(0, controller.state.value.paragraphIndex)39        service.reportProgress(0, 5)40        assertEquals(0, controller.state.value.wordStart)41        assertEquals(5, controller.state.value.wordEnd)4243        service.completeCurrent()44        runCurrent()45        assertEquals(listOf("First paragraph", "Second paragraph"), service.spokenTexts)46        assertEquals(1, controller.state.value.paragraphIndex)47        assertEquals(1f, controller.state.value.progress)4849        service.completeCurrent()50        runCurrent()51        assertEquals(NovelTtsPlaybackState.Stopped, controller.state.value.playback)52    }5354    @Test55    fun pauseAndResumeRestartsAtCurrentWord() = runTest {56        val service = ControlledTextToSpeechService()57        val controller = NovelTtsController(service, backgroundScope)5859        controller.start(listOf("Read this sentence"), TextToSpeechRequest())60        runCurrent()61        service.reportProgress(5, 9)6263        val stopsBeforePause = service.stopCount64        controller.pause()65        assertEquals(NovelTtsPlaybackState.Paused, controller.state.value.playback)66        controller.resume()67        runCurrent()6869        assertEquals(listOf("Read this sentence", "this sentence"), service.spokenTexts)70        assertEquals(stopsBeforePause + 1, service.stopCount)71    }7273    @Test74    fun skipAndLiveSettingsRestartTheSelectedParagraph() = runTest {75        val service = ControlledTextToSpeechService()76        val controller = NovelTtsController(service, backgroundScope)77        controller.start(listOf("One", "Two", "Three"), TextToSpeechRequest())78        runCurrent()7980        controller.skipNext()81        runCurrent()82        assertEquals(1, controller.state.value.paragraphIndex)83        assertEquals("Two", service.spokenTexts.last())8485        controller.updateRequest(TextToSpeechRequest(speechRate = 1.4, pitch = 0.8))86        runCurrent()87        assertEquals("Two", service.spokenTexts.last())88        assertEquals(1.4, service.requests.last().speechRate)89        assertEquals(0.8, service.requests.last().pitch)9091        controller.skipPrevious()92        runCurrent()93        assertEquals(0, controller.state.value.paragraphIndex)94        assertEquals("One", service.spokenTexts.last())95    }96}9798private class ControlledTextToSpeechService : TextToSpeechService {99    val spokenTexts = mutableListOf<String>()100    val requests = mutableListOf<TextToSpeechRequest>()101    var stopCount = 0102    private val completions = Channel<Unit>(Channel.UNLIMITED)103    private var progress: ((Int, Int) -> Unit)? = null104105    override suspend fun voices(language: String?): List<String> = emptyList()106107    override suspend fun speak(108        text: String,109        request: TextToSpeechRequest,110        onProgress: (startOffset: Int, endOffset: Int) -> Unit,111    ) {112        spokenTexts += text113        requests += request114        progress = onProgress115        completions.receive()116    }117118    override suspend fun stop() {119        stopCount += 1120    }121122    fun reportProgress(start: Int, end: Int) {123        progress?.invoke(start, end)124    }125126    fun completeCurrent() {127        completions.trySend(Unit)128    }129}130

Search chapters, reference and source filenames.