package com.correx.core.events.serialization

import com.correx.core.events.events.EventPayload
import com.correx.core.events.events.SessionWorkspaceBoundEvent
import com.correx.core.events.types.SessionId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class SessionWorkspaceBoundEventSerializationTest {

    private val sample = SessionWorkspaceBoundEvent(
        sessionId = SessionId("sess-1"),
        workspaceRoot = "/home/user/project",
        allowedPaths = listOf("/home/user/project", "/home/user/project/src"),
    )

    @Test
    fun `round-trips as polymorphic EventPayload`() {
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"SessionWorkspaceBound\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }

    @Test
    fun `decodes hand-written SessionWorkspaceBound JSON`() {
        val json = """
            {"type":"SessionWorkspaceBound","sessionId":"s-42","workspaceRoot":"/ws","allowedPaths":["/ws","/ws/src"]}
        """.trimIndent()
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), json)
        assertTrue(decoded is SessionWorkspaceBoundEvent)
        decoded as SessionWorkspaceBoundEvent
        assertEquals("s-42", decoded.sessionId.value)
        assertEquals("/ws", decoded.workspaceRoot)
        assertEquals(listOf("/ws", "/ws/src"), decoded.allowedPaths)
    }
}
