package wang.yaojia.webterm.nav

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test

/**
 * The new pure route mappers added in app-assembly (the "spawn a new session" seam). The cold-start and
 * deep-link mappers are covered in `DeepLinkRouterTest`; this pins the [NavRoutes.NEW_SESSION_SENTINEL]
 * round-trip: [NavRoutes.terminalNew] builds a route whose session slot is the sentinel, and
 * [NavRoutes.attachSessionId] maps that slot back to `null` (`attach(null)`) while passing a real id
 * through verbatim. Plain-string inputs — no Compose, no device.
 */
class NavRoutesTest {

    private val hostId = "11111111-2222-4333-8444-555555555555"
    private val sessionId = "0f5a1f2e-3b4c-4d5e-8f6a-7b8c9d0e1f2a"

    @Test
    fun `terminalNew builds a route carrying the new-session sentinel`() {
        assertEquals("terminal/$hostId/${NavRoutes.NEW_SESSION_SENTINEL}", NavRoutes.terminalNew(hostId))
    }

    @Test
    fun `attachSessionId maps the sentinel to null (a fresh spawn)`() {
        assertNull(NavRoutes.attachSessionId(NavRoutes.NEW_SESSION_SENTINEL))
    }

    @Test
    fun `attachSessionId passes a real session id through verbatim`() {
        assertEquals(sessionId, NavRoutes.attachSessionId(sessionId))
    }

    @Test
    fun `attachSessionId maps a null arg to null`() {
        assertNull(NavRoutes.attachSessionId(null))
    }

    @Test
    fun `terminalNew route and attachSessionId round-trip to a null spawn id`() {
        // The session slot the new-session route puts on the wire must decode back to "spawn a new one".
        val sessionArg = NavRoutes.terminalNew(hostId).substringAfterLast('/')
        assertNull(NavRoutes.attachSessionId(sessionArg))
    }
}
