package de.arxsilex.maptools.converter.core.pbf

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class PbfDecompressionTest {
    @Test
    fun `selects available accelerated provider over cpu fallback`() {
        val provider = fakeProvider(available = true, priority = 100, accelerated = true)

        val selected = PbfDecompression.select(PbfCompression.ZLIB, listOf(provider))

        assertEquals("test-gpu", selected.id)
        assertTrue(selected.accelerated)
    }

    @Test
    fun `uses cpu fallback if optional provider is unavailable`() {
        val selected = PbfDecompression.select(
            PbfCompression.ZLIB,
            listOf(fakeProvider(available = false, priority = 100, accelerated = true)),
        )

        assertEquals("cpu-zlib", selected.id)
        assertFalse(selected.accelerated)
    }

    @Test
    fun `cuda probe recognizes configured toolkit`() {
        assertTrue(CudaRuntimeProbe.candidateFound(mapOf("CUDA_PATH" to "C:\\CUDA"), "Windows 11"))
        assertFalse(CudaRuntimeProbe.candidateFound(emptyMap(), "Windows 11"))
    }

    private fun fakeProvider(
        available: Boolean,
        priority: Int,
        accelerated: Boolean,
    ) = object : PbfDecompressionProvider {
        override val id = "test-gpu"
        override val priority = priority
        override val accelerated = accelerated
        override fun isAvailable() = available
        override fun supports(compression: PbfCompression) = compression == PbfCompression.ZLIB
        override fun decompress(compressed: ByteArray, expectedSize: Int, maximumSize: Int) = compressed
    }
}
