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)
    }
}
