package com.tencent.qian.webview.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.tencent.qian.webview.TencentQianWebView;

import org.junit.Test;

/**
 * {@link WebSettingsApplier#buildUserAgent} 纯函数单测。
 *
 * <p>覆盖场景:
 * <ul>
 *   <li>正常拼接</li>
 *   <li>空 systemUa</li>
 *   <li>空 userAgentExtra</li>
 *   <li>带前后空格的输入</li>
 *   <li>SDK 版本号正确嵌入</li>
 * </ul>
 */
public class WebSettingsApplierBuildUaTest {

    // ────────── 1. 正常拼接 ──────────

    @Test
    public void normal_UA_appends_qianwv_version_and_extra() {
        String result = WebSettingsApplier.buildUserAgent(
                "Mozilla/5.0 (Linux; Android 14)",
                "MyApp/2.0"
        );

        assertEquals(
                "Mozilla/5.0 (Linux; Android 14) qianwv/" + TencentQianWebView.sdkVersion() + " MyApp/2.0",
                result
        );
    }

    // ────────── 2. 空 userAgentExtra 不追加多余空格 ──────────

    @Test
    public void empty_userAgentExtra_does_not_append_trailing_space() {
        String result = WebSettingsApplier.buildUserAgent(
                "Mozilla/5.0",
                ""
        );

        assertEquals("Mozilla/5.0 qianwv/" + TencentQianWebView.sdkVersion(), result);
        // 不应有尾部空格
        assertFalse(result.endsWith(" "));
    }

    @Test
    public void blank_userAgentExtra_does_not_append_trailing_space() {
        String result = WebSettingsApplier.buildUserAgent(
                "Mozilla/5.0",
                "   "
        );

        assertEquals("Mozilla/5.0 qianwv/" + TencentQianWebView.sdkVersion(), result);
    }

    // ────────── 3. 空 systemUa ──────────

    @Test
    public void empty_systemUa_starts_with_qianwv_directly() {
        String result = WebSettingsApplier.buildUserAgent(
                "",
                "MyApp/1.0"
        );

        assertEquals("qianwv/" + TencentQianWebView.sdkVersion() + " MyApp/1.0", result);
    }

    @Test
    public void blank_systemUa_starts_with_qianwv_directly() {
        String result = WebSettingsApplier.buildUserAgent(
                "   ",
                ""
        );

        assertEquals("qianwv/" + TencentQianWebView.sdkVersion(), result);
    }

    // ────────── 4. 带前后空格的输入被 trim ──────────

    @Test
    public void systemUa_is_trimmed() {
        String result = WebSettingsApplier.buildUserAgent(
                "  Mozilla/5.0  ",
                ""
        );

        assertEquals("Mozilla/5.0 qianwv/" + TencentQianWebView.sdkVersion(), result);
    }

    @Test
    public void userAgentExtra_is_trimmed() {
        String result = WebSettingsApplier.buildUserAgent(
                "Mozilla/5.0",
                "  MyApp/2.0  "
        );

        assertEquals("Mozilla/5.0 qianwv/" + TencentQianWebView.sdkVersion() + " MyApp/2.0", result);
    }

    // ────────── 5. SDK 版本号正确 ──────────

    @Test
    public void SDK_VERSION_is_embedded_correctly() {
        String result = WebSettingsApplier.buildUserAgent("UA", "");

        assertTrue("应包含 qianwv/", result.contains("qianwv/"));
        assertTrue("应包含版本号", result.contains(TencentQianWebView.sdkVersion()));
    }

    // ────────── 6. 两者都为空 ──────────

    @Test
    public void both_empty_produces_only_qianwv_version() {
        String result = WebSettingsApplier.buildUserAgent("", "");

        assertEquals("qianwv/" + TencentQianWebView.sdkVersion(), result);
    }
}
