package com.correx.core.events.serialization

import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class AnyMapSerializerTest {

    @Test
    fun `deserializes a map whose value is JSON null by dropping that key`() {
        // The exact shape that used to 500 the whole session's /events: a persisted tool-arg map
        // with a null value. Reading it back must succeed, not throw.
        val decoded = Json.decodeFromString(AnyMapSerializer, """{"path":"a.txt","mode":null}""")
        assertEquals("a.txt", decoded["path"])
        assertFalse(decoded.containsKey("mode"))
    }

    @Test
    fun `round-trips scalars, nested objects, and arrays`() {
        val decoded = Json.decodeFromString(
            AnyMapSerializer,
            """{"s":"x","n":3,"b":true,"arr":[1,2],"obj":{"k":"v"}}""",
        )
        assertEquals("x", decoded["s"])
        assertEquals(3L, decoded["n"])
        assertEquals(true, decoded["b"])
        assertEquals(listOf(1L, 2L), decoded["arr"])
        assertEquals(mapOf("k" to "v"), decoded["obj"])
    }

    @Test
    fun `does not write null values`() {
        val json = Json.encodeToString(AnyMapSerializer, mapOf("a" to "keep"))
        assertFalse(json.contains("null"))
        assertTrue(json.contains("\"a\":\"keep\""))
    }
}
