package de.arxsilex.maptools.converter.desktop

import java.nio.file.Files
import java.nio.file.Path
import kotlin.test.Test
import kotlin.test.assertTrue

class PackagedRuntimeModuleTest {
    @Test
    fun `packaged runtime declares modules needed by dynamic data providers`() {
        val buildFile = findDesktopBuildFile()
        val content = Files.readString(buildFile)

        assertTrue(
            Regex("modules\\([^)]*\\\"java\\.sql\\\"[^)]*\\)", RegexOption.DOT_MATCHES_ALL).containsMatchIn(content),
            "The packaged runtime must include java.sql for Mapsforge POI support.",
        )
        assertTrue(
            Regex("modules\\([^)]*\\\"java\\.naming\\\"[^)]*\\)", RegexOption.DOT_MATCHES_ALL).containsMatchIn(content),
            "The packaged runtime must include java.naming for Apache SIS GeoTIFF discovery.",
        )
    }

    private fun findDesktopBuildFile(): Path {
        var current = Path.of("").toAbsolutePath()
        repeat(8) {
            val candidate = current.resolve("desktopApp").resolve("build.gradle.kts")
            if (Files.isRegularFile(candidate)) return candidate
            val nestedCandidate = current.resolve("application").resolve("desktopApp").resolve("build.gradle.kts")
            if (Files.isRegularFile(nestedCandidate)) return nestedCandidate
            current = current.parent ?: return@repeat
        }
        error("desktopApp/build.gradle.kts was not found from the test working directory.")
    }
}
