package com.correx.testing.contracts.model

import com.correx.core.events.types.StageId
import com.correx.core.events.types.TransitionId
import com.correx.core.transitions.graph.StageConfig
import com.correx.core.transitions.graph.TransitionEdge
import com.correx.core.transitions.graph.WorkflowGraph
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class WorkflowGraphTest {

    private val s1 = StageId("s1")
    private val s2 = StageId("s2")

    @Test
    fun `graph stores immutable structure`() {
        val graph = WorkflowGraph(
            id = "workflow-test",
            stages = mapOf(s1 to StageConfig(), s2 to StageConfig()),
            transitions = setOf(
                TransitionEdge(
                    id = TransitionId("t1"),
                    from = s1,
                    to = s2,
                    condition = { true }
                )
            ),
            start = s1
        )

        assertEquals(2, graph.stages.size)
        assertEquals(1, graph.transitions.size)
        assertEquals(s1, graph.start)
    }

    @Test
    fun `map removes duplicate keys deterministically`() {
        val node = s1

        val graph = WorkflowGraph(
            id = "workflow-test",
            stages = mapOf(node to StageConfig()),
            transitions = emptySet(),
            start = s1
        )

        assertEquals(1, graph.stages.size)
        assertTrue(graph.stages.containsKey(node))
    }

    @Test
    fun `graph equality is structural`() {
        val g1 = WorkflowGraph(
            id = "workflow-test",
            stages = mapOf(s1 to StageConfig()),
            transitions = emptySet(),
            start = s1
        )

        val g2 = WorkflowGraph(
            id = "workflow-test",
            stages = mapOf(s1 to StageConfig()),
            transitions = emptySet(),
            start = s1
        )

        assertEquals(g1, g2)
    }
}
