package com.correx.apps.server

import java.nio.file.Path
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.junit.jupiter.api.Test

class BootWorkspaceTest {
    @Test
    fun `workspace root clamps an outside configured working directory`() {
        val resolved = resolveBootWorkspace(
            explicitWorkspaceRoot = Path.of("/tmp/audition"),
            explicitWorkingDir = Path.of("/home/user/repo"),
            processWorkingDir = Path.of("/home/user/repo"),
        )

        assertEquals(Path.of("/tmp/audition"), resolved.workspaceRoot)
        assertEquals(Path.of("/tmp/audition"), resolved.workingDir)
        assertTrue(resolved.workingDirWasClamped)
    }

    @Test
    fun `working directory inside workspace root remains intact`() {
        val resolved = resolveBootWorkspace(
            explicitWorkspaceRoot = Path.of("/tmp/audition"),
            explicitWorkingDir = Path.of("/tmp/audition/frontend"),
            processWorkingDir = Path.of("/home/user/repo"),
        )

        assertEquals(Path.of("/tmp/audition"), resolved.workspaceRoot)
        assertEquals(Path.of("/tmp/audition/frontend"), resolved.workingDir)
        assertFalse(resolved.workingDirWasClamped)
    }

    @Test
    fun `configured working directory supplies the root when workspace root is absent`() {
        val resolved = resolveBootWorkspace(
            explicitWorkspaceRoot = null,
            explicitWorkingDir = Path.of("/tmp/configured"),
            processWorkingDir = Path.of("/home/user/repo"),
        )

        assertEquals(Path.of("/tmp/configured"), resolved.workspaceRoot)
        assertEquals(Path.of("/tmp/configured"), resolved.workingDir)
        assertFalse(resolved.workingDirWasClamped)
    }

}
