package com.correx.core.events.serialization

import com.correx.core.events.events.EventPayload
import com.correx.core.events.events.StageCheckpointFailedEvent
import com.correx.core.events.events.StageCheckpointPassedEvent
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 StageCheckpointEventSerializationTest {

    @Test
    fun `StageCheckpointPassedEvent round-trips as polymorphic EventPayload`() {
        val sample: EventPayload = StageCheckpointPassedEvent(
            sessionId = SessionId("s"),
            stageId = StageId("plan"),
            expectedArtifacts = setOf("plan.json"),
            producedArtifacts = setOf("plan.json", "notes.md"),
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"StageCheckpointPassed\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }

    @Test
    fun `StageCheckpointFailedEvent round-trips as polymorphic EventPayload`() {
        val sample: EventPayload = StageCheckpointFailedEvent(
            sessionId = SessionId("s"),
            stageId = StageId("plan"),
            expectedArtifacts = setOf("plan.json", "diff.patch"),
            producedArtifacts = setOf("plan.json"),
            missingArtifacts = setOf("diff.patch"),
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"StageCheckpointFailed\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }
}
