package de.arxsilex.maptools.converter.worker

import java.nio.file.Files
import kotlin.io.path.createTempFile
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class MapsforgeSourceRequestTest {
    @Test
    fun `parses mapsforge materialization request`() {
        val input = createTempFile(suffix = ".map")
        val output = input.resolveSibling("materialized-${System.nanoTime()}.osm.pbf")
        try {
            val request = MapsforgeSourceRequest.parse(
                arrayOf(
                    "mapsforge-source", "--input", input.toString(), "--output", output.toString(),
                    "--temp-dir", output.resolveSibling("temp").toString(),
                    "--namespace", "4",
                    "--west", "7.0", "--south", "46.0", "--east", "12.0", "--north", "48.0",
                ),
            )

            assertEquals(4, request.namespace)
            assertEquals(7.0, request.bounds.west)
            assertEquals(48.0, request.bounds.north)
            assertEquals(output.resolveSibling("temp").toAbsolutePath().normalize(), request.temporaryDirectory)
        } finally {
            Files.deleteIfExists(input)
        }
    }

    @Test
    fun `rejects invalid synthetic id namespace`() {
        val input = createTempFile(suffix = ".map")
        try {
            assertFailsWith<IllegalArgumentException> {
                MapsforgeSourceRequest(
                    input,
                    input.resolveSibling("output.osm.pbf"),
                    de.arxsilex.maptools.converter.core.GeoBounds(7.0, 46.0, 12.0, 48.0),
                    1_000,
                )
            }
        } finally {
            Files.deleteIfExists(input)
        }
    }
}
