package com.tencent.qian.webview.internal

import com.tencent.qian.webview.TencentQianWebView
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * [WebSettingsApplier.buildUserAgent] 纯函数单测。
 *
 * 覆盖场景:
 * - 正常拼接
 * - 空 systemUa
 * - 空 userAgentExtra
 * - 带前后空格的输入
 * - SDK 版本号正确嵌入
 */
class WebSettingsApplierBuildUaTest {

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

    @Test
    fun `normal UA appends qianwv version and extra`() {
        val result = WebSettingsApplier.buildUserAgent(
            systemUa = "Mozilla/5.0 (Linux; Android 14)",
            userAgentExtra = "MyApp/2.0"
        )

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

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

    @Test
    fun `empty userAgentExtra does not append trailing space`() {
        val result = WebSettingsApplier.buildUserAgent(
            systemUa = "Mozilla/5.0",
            userAgentExtra = ""
        )

        assertEquals("Mozilla/5.0 qianwv/${TencentQianWebView.sdkVersion()}", result)
        // 不应有尾部空格
        assertTrue(!result.endsWith(" "))
    }

    @Test
    fun `blank userAgentExtra does not append trailing space`() {
        val result = WebSettingsApplier.buildUserAgent(
            systemUa = "Mozilla/5.0",
            userAgentExtra = "   "
        )

        assertEquals("Mozilla/5.0 qianwv/${TencentQianWebView.sdkVersion()}", result)
    }

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

    @Test
    fun `empty systemUa starts with qianwv directly`() {
        val result = WebSettingsApplier.buildUserAgent(
            systemUa = "",
            userAgentExtra = "MyApp/1.0"
        )

        assertEquals("qianwv/${TencentQianWebView.sdkVersion()} MyApp/1.0", result)
    }

    @Test
    fun `blank systemUa starts with qianwv directly`() {
        val result = WebSettingsApplier.buildUserAgent(
            systemUa = "   ",
            userAgentExtra = ""
        )

        assertEquals("qianwv/${TencentQianWebView.sdkVersion()}", result)
    }

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

    @Test
    fun `systemUa is trimmed`() {
        val result = WebSettingsApplier.buildUserAgent(
            systemUa = "  Mozilla/5.0  ",
            userAgentExtra = ""
        )

        assertEquals("Mozilla/5.0 qianwv/${TencentQianWebView.sdkVersion()}", result)
    }

    @Test
    fun `userAgentExtra is trimmed`() {
        val result = WebSettingsApplier.buildUserAgent(
            systemUa = "Mozilla/5.0",
            userAgentExtra = "  MyApp/2.0  "
        )

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

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

    @Test
    fun `SDK_VERSION is embedded correctly`() {
        val result = WebSettingsApplier.buildUserAgent("UA", "")

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

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

    @Test
    fun `both empty produces only qianwv version`() {
        val result = WebSettingsApplier.buildUserAgent("", "")

        assertEquals("qianwv/${TencentQianWebView.sdkVersion()}", result)
    }
}
