package com.correx.apps.server.research

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

class SourceListHostsTest {

    @Test
    fun `distinct hosts are extracted from multiple urls`() {
        val hosts = SourceListHosts.extract(
            listOf(
                "https://example.com/a",
                "https://docs.rust-lang.org/book",
                "http://api.github.com/repos",
            ),
        )
        assertEquals(setOf("example.com", "docs.rust-lang.org", "api.github.com"), hosts)
    }

    @Test
    fun `duplicate hosts collapse`() {
        val hosts = SourceListHosts.extract(
            listOf(
                "https://example.com/one",
                "https://example.com/two",
                "http://example.com/three",
            ),
        )
        assertEquals(setOf("example.com"), hosts)
    }

    @Test
    fun `ports are stripped`() {
        val hosts = SourceListHosts.extract(listOf("https://example.com:8443/path"))
        assertEquals(setOf("example.com"), hosts)
    }

    @Test
    fun `host case is normalised to lower case`() {
        val hosts = SourceListHosts.extract(
            listOf("https://Example.COM/a", "https://EXAMPLE.com/b"),
        )
        assertEquals(setOf("example.com"), hosts)
    }

    @Test
    fun `unparseable and relative entries are ignored`() {
        val hosts = SourceListHosts.extract(
            listOf(
                "/local/path",
                "not a url at all",
                "ftp ://broken",
                "",
                "https://kept.com/x",
            ),
        )
        assertEquals(setOf("kept.com"), hosts)
    }

    @Test
    fun `dossier source lines yield their leading url host`() {
        val hosts = SourceListHosts.extract(
            listOf("https://example.com/x — explains Y; bears on sub-question 2"),
        )
        assertEquals(setOf("example.com"), hosts)
        assertTrue("example.com" in hosts)
    }
}
