package com.erp.tenant;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
 * extractSubdomain() 단위 테스트.
 * - 운영 (3파트, *.invyone.com) 동작 회귀 없음
 * - dev   (2파트, *.localhost) 신규 지원
 * - bare localhost / IP / 베이스 도메인 / 예약어 → null (META)
 */
class SubdomainResolverFilterTest {

    @Test
    void nullOrBlank_returnsNull() {
        assertNull(SubdomainResolverFilter.extractSubdomain(null));
        assertNull(SubdomainResolverFilter.extractSubdomain(""));
        assertNull(SubdomainResolverFilter.extractSubdomain("  "));
    }

    @Test
    void bareLocalhost_returnsNull() {
        assertNull(SubdomainResolverFilter.extractSubdomain("localhost"));
        assertNull(SubdomainResolverFilter.extractSubdomain("localhost:9771"));
    }

    @Test
    void ipv4_returnsNull() {
        assertNull(SubdomainResolverFilter.extractSubdomain("127.0.0.1"));
        assertNull(SubdomainResolverFilter.extractSubdomain("183.99.177.40:8081"));
    }

    @Test
    void baseDomain_returnsNull() {
        assertNull(SubdomainResolverFilter.extractSubdomain("invyone.com"));
        assertNull(SubdomainResolverFilter.extractSubdomain("invyone.com:443"));
    }

    @Test
    void productionThreePartHost_returnsFirstPart() {
        assertEquals("qnc", SubdomainResolverFilter.extractSubdomain("qnc.invyone.com"));
        assertEquals("test02", SubdomainResolverFilter.extractSubdomain("test02.invyone.com:443"));
    }

    @Test
    void devLocalhostHost_returnsFirstPart() {
        assertEquals("test02", SubdomainResolverFilter.extractSubdomain("test02.localhost"));
        assertEquals("test02", SubdomainResolverFilter.extractSubdomain("test02.localhost:9771"));
        assertEquals("qnc", SubdomainResolverFilter.extractSubdomain("QNC.LOCALHOST"));
    }

    @Test
    void reservedSubdomain_returnsNull() {
        assertNull(SubdomainResolverFilter.extractSubdomain("admin.invyone.com"));
        assertNull(SubdomainResolverFilter.extractSubdomain("solution.invyone.com"));
        assertNull(SubdomainResolverFilter.extractSubdomain("admin.localhost:9771"));
        assertNull(SubdomainResolverFilter.extractSubdomain("www.localhost"));
    }

    @Test
    void emptyFirstPart_returnsNull() {
        assertNull(SubdomainResolverFilter.extractSubdomain(".localhost"));
    }
}
