#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
模块名称：test_sut_url_validation.py
模块描述：被测系统 SUT URL 校验单元测试

覆盖：_validate_sut_url 拒绝无效地址（如 192.168.5.4202 段越界 / 非法端口 /
非法字符 / 缺主机名），放行合法地址（内网 IP / 域名 / 带端口）。防止无效
SUT 地址被保存后导致全部 UI 用例执行瘫痪（Cannot navigate to invalid URL）。

作者：czj
创建日期：2026-08-19
"""

import pytest

from app.routers.system import _validate_sut_url


class TestSutUrlValidation:
    """被测系统 URL 校验"""

    def test_rejects_invalid_ip_octet(self):
        """纯数字点分主机非合法 IPv4（段 4202 > 255）必须拒绝"""
        assert _validate_sut_url("https://192.168.5.4202") is not None
        assert _validate_sut_url("https://192.168.5.4202/") is not None

    def test_rejects_bad_scheme(self):
        assert _validate_sut_url("ftp://192.168.5.44") is not None
        assert _validate_sut_url("192.168.5.44") is not None

    def test_rejects_missing_host(self):
        assert _validate_sut_url("https://") is not None

    def test_rejects_invalid_port(self):
        assert _validate_sut_url("https://192.168.5.44:abc") is not None
        assert _validate_sut_url("https://192.168.5.44:99999") is not None

    def test_rejects_invalid_host_chars(self):
        assert _validate_sut_url("https://exa mple.com/") is not None
        assert _validate_sut_url("https://exa*mple.com/") is not None

    def test_accepts_valid_ipv4(self):
        assert _validate_sut_url("https://192.168.5.44") is None
        assert _validate_sut_url("https://192.168.5.44/") is None

    def test_accepts_ipv4_with_port(self):
        assert _validate_sut_url("https://192.168.5.44:4202") is None
        assert _validate_sut_url("http://192.168.5.44:8080/") is None

    def test_accepts_domain_and_localhost(self):
        assert _validate_sut_url("https://example.com") is None
        assert _validate_sut_url("https://meeting.ubains.com.cn/#/Home") is None
        assert _validate_sut_url("https://localhost:3000") is None
