test: добавить unit тесты для helper функций записи голоса, очистка старого UI

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-11 20:47:18 +05:00
parent 620200ca44
commit 3e3f501b9b
2 changed files with 67 additions and 2 deletions

View File

@@ -108,7 +108,7 @@ private fun bytesToHexLower(bytes: ByteArray): String {
return out.toString() return out.toString()
} }
private fun compressVoiceWaves(source: List<Float>, targetLength: Int): List<Float> { internal fun compressVoiceWaves(source: List<Float>, targetLength: Int): List<Float> {
if (targetLength <= 0) return emptyList() if (targetLength <= 0) return emptyList()
if (source.isEmpty()) return List(targetLength) { 0f } if (source.isEmpty()) return List(targetLength) { 0f }
if (source.size == targetLength) return source if (source.size == targetLength) return source
@@ -142,7 +142,7 @@ private fun compressVoiceWaves(source: List<Float>, targetLength: Int): List<Flo
} }
} }
private fun formatVoiceRecordTimer(elapsedMs: Long): String { internal fun formatVoiceRecordTimer(elapsedMs: Long): String {
val safeTenths = (elapsedMs.coerceAtLeast(0L) / 100L).toInt() val safeTenths = (elapsedMs.coerceAtLeast(0L) / 100L).toInt()
val totalSeconds = safeTenths / 10 val totalSeconds = safeTenths / 10
val tenths = safeTenths % 10 val tenths = safeTenths % 10

View File

@@ -0,0 +1,65 @@
package com.rosetta.messenger.ui.chats.input
import org.junit.Assert.*
import org.junit.Test
class VoiceRecordHelpersTest {
@Test
fun `formatVoiceRecordTimer formats zero`() {
assertEquals("0:00,0", formatVoiceRecordTimer(0L))
}
@Test
fun `formatVoiceRecordTimer formats 12300ms`() {
assertEquals("0:12,3", formatVoiceRecordTimer(12300L))
}
@Test
fun `formatVoiceRecordTimer formats 61500ms`() {
assertEquals("1:01,5", formatVoiceRecordTimer(61500L))
}
@Test
fun `formatVoiceRecordTimer handles negative`() {
assertEquals("0:00,0", formatVoiceRecordTimer(-100L))
}
@Test
fun `compressVoiceWaves empty source returns zeros`() {
val result = compressVoiceWaves(emptyList(), 5)
assertEquals(5, result.size)
assertTrue(result.all { it == 0f })
}
@Test
fun `compressVoiceWaves same size returns same`() {
val source = listOf(0.1f, 0.5f, 0.9f)
assertEquals(source, compressVoiceWaves(source, 3))
}
@Test
fun `compressVoiceWaves downsamples by max`() {
val source = listOf(0.1f, 0.8f, 0.3f, 0.9f, 0.2f, 0.7f)
val result = compressVoiceWaves(source, 3)
assertEquals(3, result.size)
assertEquals(0.8f, result[0], 0.01f)
assertEquals(0.9f, result[1], 0.01f)
assertEquals(0.7f, result[2], 0.01f)
}
@Test
fun `compressVoiceWaves target zero returns empty`() {
assertEquals(emptyList<Float>(), compressVoiceWaves(listOf(1f), 0))
}
@Test
fun `compressVoiceWaves upsamples via interpolation`() {
val source = listOf(0.0f, 1.0f)
val result = compressVoiceWaves(source, 3)
assertEquals(3, result.size)
assertEquals(0.0f, result[0], 0.01f)
assertEquals(0.5f, result[1], 0.01f)
assertEquals(1.0f, result[2], 0.01f)
}
}