package com.correx.infrastructure.inference.llama.cpp

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

// Guards the EncodeDefault.NEVER behavior on the sampling knobs: with encodeDefaults=true (matching
// the provider's Json), an unset (null) top_k/min_p/repeat_penalty must be OMITTED from the body so
// the model keeps its own default; a set value must appear.
class SamplingRequestSerializationTest {
    private val json = Json { encodeDefaults = true }

    @Test
    fun `unset sampling knobs are omitted from the request body`() {
        val body = ChatCompletionRequest(
            model = "m", messages = emptyList(), temperature = 0.7, topP = 1.0, maxTokens = 16,
        )
        val out = json.encodeToString(body)
        assertFalse("top_k" in out, out)
        assertFalse("min_p" in out, out)
        assertFalse("repeat_penalty" in out, out)
    }

    @Test
    fun `set sampling knobs are serialized`() {
        val body = ChatCompletionRequest(
            model = "m", messages = emptyList(), temperature = 0.7, topP = 1.0, maxTokens = 16,
            topK = 40, minP = 0.05, repeatPenalty = 1.1,
        )
        val out = json.encodeToString(body)
        assertTrue("\"top_k\":40" in out, out)
        assertTrue("\"min_p\":0.05" in out, out)
        assertTrue("\"repeat_penalty\":1.1" in out, out)
    }
}
