package wang.yaojia.webterm.wire

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.net.URI

/**
 * [HostEndpoint.fromBaseUrl] is the SINGLE derivation point for the stored `baseUrl` and the
 * Origin/WS derivations. These vectors lock the trim fix: the stored `baseUrl` must carry no
 * surrounding whitespace, so every downstream `URI(endpoint.baseUrl)` re-parse succeeds.
 */
class HostEndpointTest {
    @Test
    fun `fromBaseUrl stores a trimmed baseUrl so downstream URI re-parsing never sees whitespace`() {
        val padded = requireNotNull(HostEndpoint.fromBaseUrl(" http://192.168.1.5:3000 "))
        val clean = requireNotNull(HostEndpoint.fromBaseUrl("http://192.168.1.5:3000"))

        assertEquals("http://192.168.1.5:3000", padded.baseUrl)
        assertEquals(padded.baseUrl, padded.baseUrl.trim(), "stored baseUrl has no surrounding whitespace")
        // The padded input yields an endpoint identical to the trimmed-input variant.
        assertEquals(clean, padded)
        // And a downstream re-parse (PairingProbe.httpBaseUrl / HostClassifier.hostOf) now succeeds.
        assertEquals("192.168.1.5", URI(padded.baseUrl).host)
    }

    @Test
    fun `trimming does not disturb the Origin and WS derivations`() {
        val padded = requireNotNull(HostEndpoint.fromBaseUrl("  https://mac.tailnet.ts.net  "))

        assertEquals("https://mac.tailnet.ts.net", padded.baseUrl)
        assertEquals("https://mac.tailnet.ts.net", padded.originHeader)
        assertEquals("wss://mac.tailnet.ts.net/term", padded.wsUrl)
    }
}
