import com.correx.core.context.ContextProjector
import com.correx.core.context.DefaultContextReducer
import com.correx.core.events.events.WorkflowStartedEvent
import com.correx.core.events.types.SessionId
import com.correx.core.events.types.StageId
import com.correx.testing.fixtures.EventFixtures.stored
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class ContextProjectorTest {

    private val projector = ContextProjector(DefaultContextReducer())
    private val sessionId = SessionId("s1")
    private val stageId = StageId("stage-1")

    @Test
    fun `initial state has no packs and is not building`() {
        val state = projector.initial()
        assertEquals(0, state.builtPackIds.size)
        assertEquals(false, state.buildingInProgress)
    }

    @Test
    fun `replay is deterministic`() {
        val events = listOf(
            stored(payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = stageId)),
            stored(payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = stageId))
        )
        val state1 = events.fold(projector.initial()) { s, e -> projector.apply(s, e) }
        val state2 = events.fold(projector.initial()) { s, e -> projector.apply(s, e) }
        assertEquals(state1, state2)
    }
}
