package com.correx.core.kernel.orchestration

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

class ClosestPathByFilenameTest {

    private val repo = listOf(
        "apps/server/src/main/kotlin/com/correx/apps/server/routes/SessionRoutes.kt",
        "apps/server/src/main/kotlin/com/correx/apps/server/ws/SessionStreamHandler.kt",
        "core/kernel/src/main/kotlin/com/correx/core/kernel/orchestration/SessionOrchestrator.kt",
    )

    @Test
    fun `a wrong package prefix resolves to the real file by shared trailing segments`() {
        // The model dropped the `apps` segment: com/correx/server/routes vs com/correx/apps/server/routes.
        val guess = "apps/server/src/main/kotlin/com/correx/server/routes/SessionRoutes.kt"
        assertEquals(
            "apps/server/src/main/kotlin/com/correx/apps/server/routes/SessionRoutes.kt",
            closestPathByFilename(guess, repo),
        )
    }

    @Test
    fun `a bare filename resolves to its only owner`() {
        assertEquals(
            "apps/server/src/main/kotlin/com/correx/apps/server/ws/SessionStreamHandler.kt",
            closestPathByFilename("SessionStreamHandler.kt", repo),
        )
    }

    @Test
    fun `no filename match yields null rather than a wild guess`() {
        assertNull(closestPathByFilename("com/correx/apps/server/routes/GhostRoutes.kt", repo))
    }

    @Test
    fun `a directory path (no filename) yields null`() {
        assertNull(closestPathByFilename("apps/server/src/main/kotlin/com/correx/server/routes/", repo))
    }
}
