import com.correx.core.context.DefaultContextReducer
import com.correx.core.context.state.ContextState
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.Assertions.assertFalse
import org.junit.jupiter.api.Test

class DefaultContextReducerTest {

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

    @Test
    fun `WorkflowStartedEvent leaves buildingInProgress unchanged`() {
        val state = reducer.reduce(
            ContextState(),
            stored(payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = stageId))
        )
        assertFalse(state.buildingInProgress)
    }

    @Test
    fun `WorkflowStartedEvent leaves builtPackIds unchanged`() {
        val state = reducer.reduce(
            ContextState(),
            stored(payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = stageId))
        )
        assertEquals(0, state.builtPackIds.size)
    }

    @Test
    fun `WorkflowStartedEvent leaves buildingInProgress false`() {
        val started = reducer.reduce(
            ContextState(),
            stored(payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = stageId))
        )
        val after = reducer.reduce(
            started,
            stored(payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = stageId))
        )
        assertFalse(after.buildingInProgress)
    }

    @Test
    fun `WorkflowStartedEvent leaves interrupted unchanged`() {
        val started = reducer.reduce(
            ContextState(),
            stored(payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = stageId))
        )
        val after = reducer.reduce(
            started,
            stored(payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = stageId))
        )
        assertFalse(after.interrupted)
    }

    @Test
    fun `unrelated events leave state unchanged`() {
        val state = ContextState()
        val after = reducer.reduce(state, stored())
        assertEquals(state, after)
    }
}
