package com.swoosh.microblog.data

import org.junit.Assert.*
import org.junit.Test

class UrlNormalizerTest {

    @Test
    fun `adds https prefix when no protocol`() {
        assertEquals("https://example.com/", UrlNormalizer.normalize("example.com"))
    }

    @Test
    fun `preserves http prefix`() {
        assertEquals("http://example.com/", UrlNormalizer.normalize("http://example.com"))
    }

    @Test
    fun `preserves https prefix`() {
        assertEquals("https://example.com/", UrlNormalizer.normalize("https://example.com"))
    }

    @Test
    fun `adds trailing slash when missing`() {
        assertEquals("https://example.com/", UrlNormalizer.normalize("https://example.com"))
    }

    @Test
    fun `preserves trailing slash when present`() {
        assertEquals("https://example.com/", UrlNormalizer.normalize("https://example.com/"))
    }

    @Test
    fun `trims leading whitespace`() {
        assertEquals("https://example.com/", UrlNormalizer.normalize("  example.com"))
    }

    @Test
    fun `trims trailing whitespace`() {
        assertEquals("https://example.com/", UrlNormalizer.normalize("example.com  "))
    }

    @Test
    fun `trims both leading and trailing whitespace`() {
        assertEquals("https://example.com/", UrlNormalizer.normalize("  example.com  "))
    }

    @Test
    fun `handles url with path`() {
        assertEquals("https://blog.example.com/ghost/", UrlNormalizer.normalize("blog.example.com/ghost"))
    }

    @Test
    fun `handles url with path and trailing slash`() {
        assertEquals("https://blog.example.com/ghost/", UrlNormalizer.normalize("blog.example.com/ghost/"))
    }

    @Test
    fun `handles subdomain urls`() {
        assertEquals("https://my-blog.ghost.io/", UrlNormalizer.normalize("my-blog.ghost.io"))
    }

    @Test
    fun `handles http url with trailing slash`() {
        assertEquals("http://localhost:2368/", UrlNormalizer.normalize("http://localhost:2368/"))
    }

    @Test
    fun `handles url with port`() {
        assertEquals("https://localhost:2368/", UrlNormalizer.normalize("localhost:2368"))
    }

    @Test
    fun `preserves http with port`() {
        assertEquals("http://localhost:2368/", UrlNormalizer.normalize("http://localhost:2368"))
    }

    @Test
    fun `handles HTTPS in mixed case`() {
        // Should not add another https prefix
        assertEquals("https://example.com/", UrlNormalizer.normalize("https://example.com"))
    }
}
