package com.tencent.qian.webview.filechooser

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * 纯 JVM 单测:覆盖 [FileChooserLauncher] 内部 MIME 匹配逻辑。
 *
 * 真实的 Intent 启动 / ActivityResult 链路需要 Android Instrumentation,本测试不覆盖。
 *
 * 覆盖场景:
 * - Requirement: Legacy Face Capture Fallback (人脸核身老录制模式兜底)
 * - Requirement: HTML Media Capture Attribute Support (`<input capture>`)
 */
class FileChooserLauncherMatchTest {

    private val legacy = FileChooserLauncher.DEFAULT_LEGACY_FACE_CAPTURE_MIME_TYPES

    // ────────── matchesAny:老协议 MIME 兜底 ──────────

    @Test fun `matchesAny null array returns false`() {
        assertFalse(FileChooserLauncher.matchesAny(null, legacy))
    }

    @Test fun `matchesAny empty target returns false`() {
        assertFalse(FileChooserLauncher.matchesAny(arrayOf("video/kyc"), emptyList()))
    }

    @Test fun `matchesAny exact lowercase match`() {
        assertTrue(FileChooserLauncher.matchesAny(arrayOf("video/kyc"), legacy))
    }

    @Test fun `matchesAny case insensitive`() {
        assertTrue(FileChooserLauncher.matchesAny(arrayOf("VIDEO/KYC"), legacy))
        assertTrue(FileChooserLauncher.matchesAny(arrayOf("Video/Faceid"), legacy))
    }

    @Test fun `matchesAny tolerates whitespace`() {
        assertTrue(FileChooserLauncher.matchesAny(arrayOf("  video/kyc  "), legacy))
    }

    @Test fun `matchesAny splits comma joined entry`() {
        // H5 写法常见:accept="video/kyc, image/png"
        assertTrue(FileChooserLauncher.matchesAny(arrayOf("video/kyc, image/png"), legacy))
    }

    @Test fun `matchesAny no match for image only`() {
        assertFalse(FileChooserLauncher.matchesAny(arrayOf("image/png"), legacy))
    }

    @Test fun `matchesAny custom legacy list - livecheck`() {
        val custom = listOf("video/kyc", "video/faceid", "video/livecheck")
        assertTrue(FileChooserLauncher.matchesAny(arrayOf("video/livecheck"), custom))
    }

    // ────────── matchesPrefix:capture 属性下选 IMAGE_CAPTURE / VIDEO_CAPTURE ──────────

    @Test fun `matchesPrefix detects image type`() {
        assertTrue(FileChooserLauncher.matchesPrefix(arrayOf("image/png"), "image/"))
        assertTrue(FileChooserLauncher.matchesPrefix(arrayOf("image/jpeg"), "image/"))
    }

    @Test fun `matchesPrefix detects video type`() {
        assertTrue(FileChooserLauncher.matchesPrefix(arrayOf("video/mp4"), "video/"))
    }

    @Test fun `matchesPrefix returns false for non-matching prefix`() {
        assertFalse(FileChooserLauncher.matchesPrefix(arrayOf("video/mp4"), "image/"))
        assertFalse(FileChooserLauncher.matchesPrefix(arrayOf("application/pdf"), "image/"))
    }

    @Test fun `matchesPrefix is case insensitive`() {
        assertTrue(FileChooserLauncher.matchesPrefix(arrayOf("IMAGE/PNG"), "image/"))
        assertTrue(FileChooserLauncher.matchesPrefix(arrayOf("Video/MP4"), "video/"))
    }

    @Test fun `matchesPrefix splits comma joined entry`() {
        assertTrue(FileChooserLauncher.matchesPrefix(arrayOf("application/pdf, image/png"), "image/"))
    }

    @Test fun `matchesPrefix handles wildcard accept`() {
        // H5 常见写法:accept="image/*"(注:matchesPrefix 命中前缀,通配符无副作用)
        assertTrue(FileChooserLauncher.matchesPrefix(arrayOf("image/*"), "image/"))
    }

    @Test fun `matchesPrefix null array returns false`() {
        assertFalse(FileChooserLauncher.matchesPrefix(null, "image/"))
    }
}
