package com.aletheia.app.data

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class QBooksUrlPolicyTest {
    @Test
    fun allowsExternalHttps() {
        val validation = QBooksUrlPolicy.validate("https://books.example/")

        assertTrue(validation.isAllowed)
        assertEquals("https://books.example", validation.normalizedUrl)
        assertEquals("https", validation.scheme)
    }

    @Test
    fun allowsBundledCatalogOverHttp() {
        val validation = QBooksUrlPolicy.validate("http://m.flibusta.is/opds/")

        assertTrue(validation.isAllowed)
        assertEquals("http://m.flibusta.is/opds", validation.normalizedUrl)
        assertEquals("http", validation.scheme)
    }

    @Test
    fun allowsExplicitLocalHttpHosts() {
        val localUrls = listOf(
            "http://localhost:8080",
            "http://127.0.0.1:8080",
            "http://10.0.2.2:8080",
            "http://192.168.0.185:8080",
            "http://books.local:8080"
        )

        localUrls.forEach { url ->
            val validation = QBooksUrlPolicy.validate(url)
            assertTrue("$url should be accepted", validation.isAllowed)
            assertEquals("http", validation.scheme)
        }
    }

    @Test
    fun rejectsExternalHttp() {
        val validation = QBooksUrlPolicy.validate("http://books.example")

        assertFalse(validation.isAllowed)
        assertEquals(QBooksUrlPolicy.Reason.ExternalCleartext, validation.reason)
    }

    @Test
    fun rejectsUnsupportedSchemes() {
        val validation = QBooksUrlPolicy.validate("ftp://books.example")

        assertFalse(validation.isAllowed)
        assertEquals(QBooksUrlPolicy.Reason.UnsupportedScheme, validation.reason)
    }
}
