package org.loculus.backend.service

import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.loculus.backend.api.Organism
import org.loculus.backend.api.SubmittedData
import org.loculus.backend.config.BackendConfig
import org.loculus.backend.controller.DEFAULT_ORGANISM
import org.loculus.backend.service.submission.CompressionDictService
import org.loculus.backend.service.submission.CompressionService
import org.loculus.backend.service.submission.DictEntry

class CompressionServiceTest {
    private val compressionDictServiceMock = mockk<CompressionDictService>()
    private val backendConfigMock = mockk<BackendConfig>()

    private val compressor = CompressionService(
        compressionDictService = compressionDictServiceMock,
        backendConfig = backendConfigMock,
    )

    @Test
    fun `Round trip compress and decompress sequences in submitted data`() {
        val organism = Organism(DEFAULT_ORGANISM)

        every { backendConfigMock.zstdCompressionLevel } returns 10

        val dict = "NNACTGACTGACTGACTGATCGATCGATCGATCGATCGATCGATC".toByteArray()
        every { compressionDictServiceMock.getDictForUnalignedSequence(organism) } returns DictEntry(
            id = 1,
            dict = dict,
        )
        every { compressionDictServiceMock.getDictById(1) } returns dict

        val input =
            "NNACTGACTGACTGACTGATCGATCGATCGATCGATCGATCGATC----NNNNATCGCGATCGATCGATCGATCGGGATCGTAGC--NNNNATGC"

        val segmentName = "main"
        val testData = SubmittedData(
            mapOf("test" to "test"),
            mapOf(segmentName to input),
        )
        val compressed = compressor.compressSequencesInSubmittedData(testData, organism)
        val decompressed = compressor.decompressSequencesInSubmittedData(compressed)

        assertEquals(testData, decompressed)
    }
}
