7936251d6b
P2-1: Guard SessionId NPE with safe-call in SessionsReducer
P2-2: Remove 16 dead event classes + reducer branches; replace with live orchestration events in tests
P2-3: Standardize ChatInput divergences — guard CancelSession, single-arg ProtocolError
P2-4: Remove dead TuiToolRecord.diff and ToolDisplayStatus.REQUESTED
P2-5: Remove dead streamLive from SessionEventBridge
P2-6: Extract SessionsReducerContext data class for 6+ param method
P2-7: Replace StageId("none") sentinel with TypeId.NONE
P2-9: Add TypeId.random() factory on all type-alias IDs
P2-10: Add KDoc on schemaVersion documenting reserved-for-migration
P2-8: Verified RouterReducer string-template already correct (TypeId value class toString)
245 lines
7.2 KiB
Kotlin
245 lines
7.2 KiB
Kotlin
import com.correx.core.events.events.OrchestrationPausedEvent
|
|
import com.correx.core.events.events.OrchestrationResumedEvent
|
|
import com.correx.core.events.events.StageCompletedEvent
|
|
import com.correx.core.events.events.StageFailedEvent
|
|
import com.correx.core.events.events.TransitionExecutedEvent
|
|
import com.correx.core.events.events.WorkflowCompletedEvent
|
|
import com.correx.core.events.events.WorkflowFailedEvent
|
|
import com.correx.core.events.events.WorkflowStartedEvent
|
|
import com.correx.core.events.types.SessionId
|
|
import com.correx.core.events.types.StageId
|
|
import com.correx.core.events.types.TransitionId
|
|
import com.correx.core.sessions.DefaultSessionReducer
|
|
import com.correx.core.sessions.SessionState
|
|
import com.correx.core.sessions.SessionStatus
|
|
import com.correx.testing.fixtures.EventFixtures.stored
|
|
import kotlinx.datetime.Instant
|
|
import org.junit.jupiter.api.Assertions.assertEquals
|
|
import org.junit.jupiter.api.Test
|
|
|
|
class DefaultSessionReducerTest {
|
|
|
|
private val reducer = DefaultSessionReducer()
|
|
|
|
private val sessionId = SessionId("session-1")
|
|
|
|
@Test
|
|
fun `WorkflowStartedEvent leaves CREATED status`() {
|
|
val state = initialState()
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
sessionId = sessionId,
|
|
payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = StageId("st-1"))
|
|
)
|
|
)
|
|
|
|
assertEquals(SessionStatus.CREATED, result.status)
|
|
}
|
|
|
|
@Test
|
|
fun `OrchestrationPausedEvent leaves ACTIVE status`() {
|
|
val state = activeState()
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
sessionId = sessionId,
|
|
payload = OrchestrationPausedEvent(sessionId, stageId = StageId("st-1"), reason = "APPROVAL_PENDING")
|
|
)
|
|
)
|
|
|
|
assertEquals(SessionStatus.ACTIVE, result.status)
|
|
}
|
|
|
|
@Test
|
|
fun `OrchestrationResumedEvent leaves PAUSED status`() {
|
|
val state = pausedState()
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
sessionId = sessionId,
|
|
payload = OrchestrationResumedEvent(sessionId, stageId = StageId("st-1"))
|
|
)
|
|
)
|
|
|
|
assertEquals(SessionStatus.PAUSED, result.status)
|
|
}
|
|
|
|
@Test
|
|
fun `WorkflowCompletedEvent leaves ACTIVE status`() {
|
|
val state = activeState()
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
sessionId = sessionId,
|
|
payload = WorkflowCompletedEvent(sessionId, terminalStageId = StageId("st-1"), totalStages = 0)
|
|
)
|
|
)
|
|
|
|
assertEquals(SessionStatus.ACTIVE, result.status)
|
|
}
|
|
|
|
@Test
|
|
fun `WorkflowFailedEvent leaves ACTIVE status`() {
|
|
val state = activeState()
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
sessionId = sessionId,
|
|
payload = WorkflowFailedEvent(sessionId, stageId = StageId("st-1"), reason = "error", retryExhausted = false)
|
|
)
|
|
)
|
|
|
|
assertEquals(SessionStatus.ACTIVE, result.status)
|
|
}
|
|
|
|
@Test
|
|
fun `TransitionExecutedEvent from StageStartedEvent sets ACTIVE status`() {
|
|
val state = pausedState()
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
sessionId = sessionId,
|
|
payload = TransitionExecutedEvent(
|
|
sessionId,
|
|
from = StageId("st-1"),
|
|
to = StageId("st-1"),
|
|
transitionId = TransitionId("transition-a")
|
|
)
|
|
)
|
|
)
|
|
|
|
assertEquals(SessionStatus.ACTIVE, result.status)
|
|
}
|
|
|
|
@Test
|
|
fun `StageCompletedEvent sets ACTIVE status`() {
|
|
val state = pausedState()
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
sessionId = sessionId,
|
|
payload = StageCompletedEvent(
|
|
sessionId,
|
|
stageId = StageId("stage-a"),
|
|
transitionId = TransitionId("transition-a")
|
|
)
|
|
)
|
|
)
|
|
|
|
assertEquals(SessionStatus.ACTIVE, result.status)
|
|
}
|
|
|
|
@Test
|
|
fun `StageFailedEvent sets FAILED status`() {
|
|
val state = activeState()
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
sessionId = sessionId,
|
|
payload = StageFailedEvent(
|
|
sessionId,
|
|
stageId = StageId("stage-a"),
|
|
transitionId = TransitionId("transition-a"),
|
|
reason = "boom"
|
|
)
|
|
)
|
|
)
|
|
|
|
assertEquals(SessionStatus.FAILED, result.status)
|
|
}
|
|
|
|
@Test
|
|
fun `TransitionExecutedEvent sets ACTIVE status`() {
|
|
val state = pausedState()
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
sessionId = sessionId,
|
|
payload = TransitionExecutedEvent(
|
|
sessionId,
|
|
from = StageId("stage-a"),
|
|
to = StageId("stage-b"),
|
|
transitionId = TransitionId("transition-a"),
|
|
)
|
|
)
|
|
)
|
|
|
|
assertEquals(SessionStatus.ACTIVE, result.status)
|
|
}
|
|
|
|
@Test
|
|
fun `createdAt is initialized once`() {
|
|
val timestamp = Instant.parse("2026-01-01T00:00:00Z")
|
|
|
|
val result = reducer.reduce(
|
|
state = initialState(),
|
|
event = stored(
|
|
payload = WorkflowStartedEvent(sessionId, workflowId = "test-wf", startStageId = StageId("st-1")),
|
|
timestamp = timestamp
|
|
)
|
|
)
|
|
|
|
assertEquals(timestamp, result.createdAt)
|
|
}
|
|
|
|
@Test
|
|
fun `createdAt is preserved after initialization`() {
|
|
val createdAt = Instant.parse("2026-01-01T00:00:00Z")
|
|
val updatedAt = Instant.parse("2026-01-02T00:00:00Z")
|
|
|
|
val state = activeState().copy(
|
|
createdAt = createdAt
|
|
)
|
|
|
|
val result = reducer.reduce(
|
|
state = state,
|
|
event = stored(
|
|
payload = OrchestrationPausedEvent(sessionId, stageId = StageId("st-1"), reason = "APPROVAL_PENDING"),
|
|
timestamp = updatedAt
|
|
)
|
|
)
|
|
|
|
assertEquals(createdAt, result.createdAt)
|
|
}
|
|
|
|
@Test
|
|
fun `updatedAt always reflects latest event timestamp`() {
|
|
val timestamp = Instant.parse("2026-01-02T00:00:00Z")
|
|
|
|
val result = reducer.reduce(
|
|
state = activeState(),
|
|
event = stored(
|
|
payload = OrchestrationPausedEvent(sessionId, stageId = StageId("st-1"), reason = "APPROVAL_PENDING"),
|
|
timestamp = timestamp
|
|
)
|
|
)
|
|
|
|
assertEquals(timestamp, result.updatedAt)
|
|
}
|
|
|
|
private fun initialState() =
|
|
SessionState(
|
|
status = SessionStatus.CREATED
|
|
)
|
|
|
|
private fun activeState() =
|
|
SessionState(
|
|
status = SessionStatus.ACTIVE
|
|
)
|
|
|
|
private fun pausedState() =
|
|
SessionState(
|
|
status = SessionStatus.PAUSED
|
|
)
|
|
}
|