package com.cottondb.messages.warm import android.os.Looper import com.cottondb.messages.chat.Voice import kotlinx.coroutines.MainScope import kotlinx.coroutines.cancel import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config import java.time.Duration /** * THE VOICE PLAYER's behaviour (owner, 23 Nov 2026, item 2: voice notes play but the player looks unfinished; * assets/design/chat/voice-player.html). Before, a tap while playing stopped the note and threw its place away, the * place ran on a clock started at the tap, and there was no speed, loading or failure. Invented notes, a stand-in * player. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], application = android.app.Application::class) class VoicePlayerTest { private val scope = MainScope() private val engines = mutableListOf() private var file: java.io.File? = java.io.File("invented.m4a") private var opens = true private val player = VoicePlayer(scope, engines = { FakeEngine(opens).also { engines += it } }, fileOf = { file }) private val note = Voice(42, "0:42", listOf(10, 80, 40), "msg:a:0") private val other = Voice(9, "0:09", listOf(50), "msg:b:0") @After fun end() = scope.cancel() private fun run(ms: Long = 0) = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms)) @Test fun a_tap_while_playing_pauses_and_the_next_tap_goes_on_from_there() { player.toggle("a", note); run() assertEquals("a", player.playing) val e = engines.single() e.at = 12_000; run(64) player.toggle("a", note); run() assertNull(player.playing) assertFalse(e.running) assertEquals(12_000L, player.places["a"]) assertTrue("paused partway counts as started: the speed stays", player.started("a")) player.toggle("a", note); run() assertEquals("a", player.playing) assertEquals("the same player resumes: nothing fetched again", 1, engines.size) assertTrue(e.running) } @Test fun the_place_is_the_players_own_never_a_clock() { player.toggle("a", note); run() val e = engines.single() // The clock runs on, the player has not moved (a slow start, a buffer): the place stays where the player is. run(5_000) assertEquals(0L, player.places["a"] ?: 0L) e.at = 21_000; run(64) assertEquals(0.5f, player.progress("a", note), 0.001f) } @Test fun another_note_pauses_the_first_where_it_stands() { player.toggle("a", note); run() engines[0].at = 7_000; run(64) player.toggle("b", other); run() assertEquals("b", player.playing) assertEquals(7_000L, player.places["a"]) assertTrue(engines[0].released) // Back to the first: it starts where it was left. player.toggle("a", note); run() assertEquals(7_000L, engines[2].seeked) } @Test fun the_speed_goes_round_and_the_note_keeps_playing() { player.toggle("a", note); run() val e = engines.single() player.nextSpeed(); assertEquals(1.5f, e.times); assertTrue(e.running) player.nextSpeed(); assertEquals(2f, e.times) player.nextSpeed(); assertEquals(1f, e.times) player.nextSpeed() // A new note starts at the speed last chosen. player.toggle("b", other); run() assertEquals(1.5f, engines[1].times) } /** `chat.settings` as the core answers it (`chat_voice.rs`): voiceSpeed 1 until chosen, 1, 1.5 or 2 taken. */ private class Settings(var speed: Number = 1) { val told = mutableListOf() val ask: suspend (kotlinx.serialization.json.JsonObject?) -> kotlinx.serialization.json.JsonObject = { o -> o?.get("voiceSpeed")?.let { v -> val n = v.toString().toDouble(); told += n; speed = n } kotlinx.serialization.json.buildJsonObject { put("voiceSpeed", kotlinx.serialization.json.JsonPrimitive(speed)) } } } private fun prefs(name: String) = org.robolectric.RuntimeEnvironment.getApplication().getSharedPreferences(name, android.content.Context.MODE_PRIVATE) @Test fun the_speed_chosen_is_the_persons_kept_in_their_settings_for_every_phone() { val settings = Settings() val first = VoicePlayer(scope, engines = { FakeEngine(true) }, fileOf = { file }, speeds = VoicePlayer.SpeedKeep.Person(prefs("voice-a"), settings.ask, scope)) run() assertEquals(1f, first.speed) first.nextSpeed(); first.nextSpeed(); run() assertEquals("the pill tells the person's settings", listOf(1.5, 2.0), settings.told.map { it.toDouble() }) // The chat left, the app started again: a new player starts at 2x, here and on their other phone. val next = VoicePlayer(scope, engines = { FakeEngine(true).also { engines += it } }, fileOf = { file }, speeds = VoicePlayer.SpeedKeep.Person(prefs("voice-a"), settings.ask, scope)) assertEquals("held here, so a note starts at it before the core answers", 2f, next.speed) next.toggle("a", note); run() assertEquals(2f, engines.single().times) val elsewhere = VoicePlayer(scope, engines = { FakeEngine(true) }, fileOf = { file }, speeds = VoicePlayer.SpeedKeep.Person(prefs("voice-other-phone"), settings.ask, scope)) run() assertEquals(2f, elsewhere.speed) } @Test fun a_speed_kept_on_this_phone_before_moves_into_the_settings_once() { // d707582691 kept 1.5x in this phone's own preferences; the person's settings hold none chosen. prefs("voice-old").edit().putFloat("voice_speed", 1.5f).commit() val settings = Settings() val player = VoicePlayer(scope, engines = { FakeEngine(true) }, fileOf = { file }, speeds = VoicePlayer.SpeedKeep.Person(prefs("voice-old"), settings.ask, scope)) run() assertEquals(1.5f, player.speed) assertEquals(listOf(1.5), settings.told.map { it.toDouble() }) // Moved once: chosen 1x on another phone afterwards, this one follows and never moves its own again. settings.speed = 1 val again = VoicePlayer(scope, engines = { FakeEngine(true) }, fileOf = { file }, speeds = VoicePlayer.SpeedKeep.Person(prefs("voice-old"), settings.ask, scope)) run() assertEquals(1f, again.speed) assertEquals(1, settings.told.size) } @Test fun a_speed_another_phone_chose_wins_over_this_phones_old_one() { prefs("voice-both").edit().putFloat("voice_speed", 1.5f).commit() val settings = Settings(2) val player = VoicePlayer(scope, engines = { FakeEngine(true).also { engines += it } }, fileOf = { file }, speeds = VoicePlayer.SpeedKeep.Person(prefs("voice-both"), settings.ask, scope)) run() assertEquals(2f, player.speed) assertTrue("nothing told: the settings already hold their choice", settings.told.isEmpty()) } @Test fun at_the_end_the_note_goes_back_to_its_start_and_is_played() { val told = mutableListOf() player.onPlayed = { told += it } player.toggle("b", other); run() engines.single().end() run() assertNull(player.playing) assertNull(player.places["b"]) assertFalse(player.started("b")) assertEquals(listOf("b"), told) assertEquals(true, player.heard["b"]) } @Test fun a_second_of_it_makes_it_played_once() { val told = mutableListOf() player.onPlayed = { told += it } player.toggle("a", note); run() engines.single().at = 400; run(64) assertEquals(emptyList(), told) engines.single().at = 1_100; run(64) engines.single().at = 2_000; run(64) assertEquals(listOf("a"), told) } @Test fun a_file_that_cannot_be_fetched_is_offline_and_a_tap_tries_again() { file = null player.toggle("a", note); run() assertEquals(VoicePlayer.Failure.Offline, player.failed["a"]) assertNull(player.loading) file = java.io.File("invented.m4a") player.toggle("a", note); run() assertNull(player.failed["a"]) assertEquals("a", player.playing) } @Test fun try_again_is_never_held_back_by_the_wait_after_a_failed_try() { // The real picture tries hold a failed key back 30 s; Try again must ask at once. val tries = com.cottondb.messages.chat.ChatPictures.Tries(now = { 0L }) var asked = 0 val withTries = VoicePlayer( scope, engines = { FakeEngine(true).also { engines += it } }, fileOf = { key -> tries.file(key) { asked++; file } }, forget = { tries.forget(it) }, ) file = null withTries.toggle("a", note); run(); Thread.sleep(50); run() assertEquals(VoicePlayer.Failure.Offline, withTries.failed["a"]) file = java.io.File("invented.m4a") withTries.toggle("a", note); run(); Thread.sleep(50); run() assertEquals(2, asked) assertEquals("a", withTries.playing) } @Test fun a_file_that_will_not_play_says_so() { opens = false player.toggle("a", note); run() assertEquals(VoicePlayer.Failure.Unplayable, player.failed["a"]) assertNull(player.playing) assertTrue(engines.single().released) } @Test fun a_second_tap_while_it_loads_stops_the_loading() { val gate = kotlinx.coroutines.CompletableDeferred() // A fetch that does not stop when asked (the core's call runs on): on 5556 the note played anyway, 23 Nov. val slow = VoicePlayer(scope, engines = { FakeEngine(true).also { engines += it } }, fileOf = { kotlinx.coroutines.withContext(kotlinx.coroutines.NonCancellable) { gate.await() } }) slow.toggle("a", note); run() assertEquals("a", slow.loading) slow.toggle("a", note); run() assertNull(slow.loading) gate.complete(java.io.File("invented.m4a")); run() assertNull(slow.playing) assertNull("stopped is not a failure", slow.failed["a"]) assertTrue(engines.isEmpty()) } @Test fun a_tap_on_the_bars_moves_the_place() { player.toggle("a", note); run() player.seek("a", 0.25f, note) assertEquals(10_500L, engines.single().seeked) assertEquals(10_500L, player.places["a"]) } // Owner on iPhone, Comms 26 (FEEDBACK-4 B5): a drag to seek put the note back. MediaPlayer seeks in the background // and answers its old place for a moment; the drag's place stands until it gets there, then the player's own runs on. @Test fun a_drag_while_playing_goes_on_from_where_it_was_dragged_to() { player.toggle("a", note); run() val e = engines.single() e.at = 3_000; run(64) e.lagging = true player.seek("a", 0.25f, note); player.seek("a", 0.5f, note) run(96) assertEquals("the old place never comes back over the drag", 21_000L, player.places["a"]) assertTrue(e.running) e.lagging = false; e.at = 21_200; run(64) assertEquals("then the player's own place, on from there", 21_200L, player.places["a"]) player.toggle("a", note); run() assertEquals(21_200L, player.places["a"]) } @Test fun a_drag_while_paused_resumes_from_there() { player.toggle("a", note); run() val e = engines.single() e.at = 5_000; run(64) player.toggle("a", note); run() player.seek("a", 0.75f, note) assertEquals(31_500L, player.places["a"]) player.toggle("a", note); run(64) assertEquals("the same player, from the drag", 1, engines.size) assertEquals(31_500L, player.places["a"]) } @Test fun bars_are_36_by_peak_scaled_to_the_loudest() { // Before: samples times 100 drawn as if out of 31, so everything above a third was clipped flat. val many = List(72) { if (it == 10) 0.5f else 0.1f } val bars = VoicePlayer.bars(many) assertEquals(36, bars.size) assertEquals(1f, bars[5], 0.0001f) assertEquals(0.2f, bars[0], 0.0001f) val few = VoicePlayer.bars(listOf(0.2f, 0.4f)) assertEquals(36, few.size) assertEquals(0.5f, few[0], 0.0001f) assertEquals(1f, few[35], 0.0001f) assertEquals(List(36) { 0f }, VoicePlayer.bars(List(10) { 0.01f })) assertEquals(emptyList(), VoicePlayer.bars(emptyList())) } @Test fun the_length_is_written_as_the_core_writes_it_rounded_up() { assertEquals("0:42", VoicePlayer.clock(42_000)) assertEquals("0:26", VoicePlayer.clock(25_100)) assertEquals("2:30", VoicePlayer.clock(150_000)) assertEquals("0:00", VoicePlayer.clock(0)) } class FakeEngine(private val opens: Boolean) : VoiceEngine { var at = 0L var running = false var released = false var seeked: Long? = null /** MediaPlayer seeking in the background: the old place is still answered. */ var lagging = false var times = 1f override var onEnd: (() -> Unit)? = null override fun open(file: java.io.File) = opens override fun start() { running = true } override fun pause() { running = false } override fun seekTo(ms: Long) { seeked = ms; if (!lagging) at = ms } override val position: Long get() = at override val duration: Long get() = 42_000 override fun speed(times: Float) { this.times = times } override fun release() { released = true; running = false } fun end() { running = false; onEnd?.invoke() } } }