package com.correx.core.events.serialization

import com.correx.core.events.events.EventPayload
import com.correx.core.events.events.PlanCompileCheckedEvent
import com.correx.core.events.types.SessionId
import com.correx.core.events.types.StageId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class PlanCompileCheckedEventSerializationTest {

    @Test
    fun `PlanCompileCheckedEvent round-trips as polymorphic EventPayload`() {
        val sample: EventPayload = PlanCompileCheckedEvent(
            sessionId = SessionId("s"),
            stageId = StageId("architect"),
            passed = false,
            error = "edge from unknown stage 'ghost'",
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"PlanCompileChecked\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }

    @Test
    fun `PlanCompileCheckedEvent round-trips on success with null error`() {
        val sample: EventPayload = PlanCompileCheckedEvent(
            sessionId = SessionId("s"),
            stageId = StageId("architect"),
            passed = true,
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }
}
