package com.tencent.qian.webview

import org.junit.Assert.assertEquals
import org.junit.Test

/**
 * 渲染进程崩溃恢复 URL 优先级单测（P3）。
 *
 * 验证 [TencentQianWebViewActivity.resolveRecoveryUrl] 的优先级：
 * currentUrl（有效） > lastVisitedUrl（崩溃前当前页） > initialUrl（首页兜底）。
 * 与 Java 版 RecoveryUrlResolveTest 同一组用例。
 */
class RecoveryUrlResolveTest {

    @Test
    fun currentUrlValid_preferred() {
        // 崩溃后 WebView.url 仍有效 → 直接用它（最精准）
        val url = TencentQianWebViewActivity.resolveRecoveryUrl(
            currentUrl = "https://x/page3",
            lastVisitedUrl = "https://x/page2",
            initialUrl = "https://x/home"
        )
        assertEquals("https://x/page3", url)
    }

    @Test
    fun currentUrlNull_fallsBackToLastVisited() {
        // 崩溃后 url 为 null → 回退到崩溃前记录的当前页（而非首页）
        val url = TencentQianWebViewActivity.resolveRecoveryUrl(
            currentUrl = null,
            lastVisitedUrl = "https://x/page2",
            initialUrl = "https://x/home"
        )
        assertEquals("https://x/page2", url)
    }

    @Test
    fun currentUrlBlank_fallsBackToLastVisited() {
        // url 为 about:blank（销毁残留）→ 同样回退到 lastVisitedUrl
        val url = TencentQianWebViewActivity.resolveRecoveryUrl(
            currentUrl = "about:blank",
            lastVisitedUrl = "https://x/page2",
            initialUrl = "https://x/home"
        )
        assertEquals("https://x/page2", url)
    }

    @Test
    fun currentAndLastEmpty_fallsBackToInitial() {
        // 崩溃发生在首页加载完成前（无 PageStarted 记录）→ 兜底初始 URL
        val url = TencentQianWebViewActivity.resolveRecoveryUrl(
            currentUrl = null,
            lastVisitedUrl = "",
            initialUrl = "https://x/home"
        )
        assertEquals("https://x/home", url)
    }

    @Test
    fun allEmpty_returnsEmpty() {
        // 全空 → 返回空串（调用方据此 finish 关闭容器）
        val url = TencentQianWebViewActivity.resolveRecoveryUrl(
            currentUrl = null,
            lastVisitedUrl = "",
            initialUrl = null
        )
        assertEquals("", url)
    }
}
