package wang.yaojia.webterm.hostregistry

import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test

/**
 * Contract tests for the in-Sources [InMemoryLastSessionStore] double (reused by the
 * A29 cold-start / SessionActivityBridge tests). Mirrors the iOS
 * `LastSessionStoreTests`: set→get, unknown→null, set(null)→clear, host isolation.
 */
class InMemoryLastSessionStoreTest {

    @Test
    fun `set then get returns the same sessionId`() = runTest {
        val store = InMemoryLastSessionStore()
        store.setLastSessionId("session-1", hostId = "host-1")

        assertEquals("session-1", store.lastSessionId("host-1"))
    }

    @Test
    fun `an unset host returns null`() = runTest {
        val store = InMemoryLastSessionStore()

        assertNull(store.lastSessionId("host-1"))
    }

    @Test
    fun `setting null clears an existing sessionId`() = runTest {
        val store = InMemoryLastSessionStore()
        store.setLastSessionId("session-1", hostId = "host-1")

        store.setLastSessionId(null, hostId = "host-1")

        assertNull(store.lastSessionId("host-1"))
    }

    @Test
    fun `distinct hosts are keyed independently`() = runTest {
        val store = InMemoryLastSessionStore()

        store.setLastSessionId("session-a", hostId = "host-a")
        store.setLastSessionId("session-b", hostId = "host-b")

        assertEquals("session-a", store.lastSessionId("host-a"))
        assertEquals("session-b", store.lastSessionId("host-b"))
    }
}
