package com.tencent.qian.webview.filechooser;

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

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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

    private final List<String> legacy = FileChooserLauncher.DEFAULT_LEGACY_FACE_CAPTURE_MIME_TYPES;

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

    @Test
    public void matchesAny_null_array_returns_false() {
        assertFalse(FileChooserLauncher.matchesAny(null, legacy));
    }

    @Test
    public void matchesAny_empty_target_returns_false() {
        assertFalse(FileChooserLauncher.matchesAny(new String[]{"video/kyc"}, Collections.emptyList()));
    }

    @Test
    public void matchesAny_exact_lowercase_match() {
        assertTrue(FileChooserLauncher.matchesAny(new String[]{"video/kyc"}, legacy));
    }

    @Test
    public void matchesAny_case_insensitive() {
        assertTrue(FileChooserLauncher.matchesAny(new String[]{"VIDEO/KYC"}, legacy));
        assertTrue(FileChooserLauncher.matchesAny(new String[]{"Video/Faceid"}, legacy));
    }

    @Test
    public void matchesAny_tolerates_whitespace() {
        assertTrue(FileChooserLauncher.matchesAny(new String[]{"  video/kyc  "}, legacy));
    }

    @Test
    public void matchesAny_splits_comma_joined_entry() {
        // H5 写法常见:accept="video/kyc, image/png"
        assertTrue(FileChooserLauncher.matchesAny(new String[]{"video/kyc, image/png"}, legacy));
    }

    @Test
    public void matchesAny_no_match_for_image_only() {
        assertFalse(FileChooserLauncher.matchesAny(new String[]{"image/png"}, legacy));
    }

    @Test
    public void matchesAny_custom_legacy_list_livecheck() {
        List<String> custom = Arrays.asList("video/kyc", "video/faceid", "video/livecheck");
        assertTrue(FileChooserLauncher.matchesAny(new String[]{"video/livecheck"}, custom));
    }

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

    @Test
    public void matchesPrefix_detects_image_type() {
        assertTrue(FileChooserLauncher.matchesPrefix(new String[]{"image/png"}, "image/"));
        assertTrue(FileChooserLauncher.matchesPrefix(new String[]{"image/jpeg"}, "image/"));
    }

    @Test
    public void matchesPrefix_detects_video_type() {
        assertTrue(FileChooserLauncher.matchesPrefix(new String[]{"video/mp4"}, "video/"));
    }

    @Test
    public void matchesPrefix_returns_false_for_non_matching_prefix() {
        assertFalse(FileChooserLauncher.matchesPrefix(new String[]{"video/mp4"}, "image/"));
        assertFalse(FileChooserLauncher.matchesPrefix(new String[]{"application/pdf"}, "image/"));
    }

    @Test
    public void matchesPrefix_is_case_insensitive() {
        assertTrue(FileChooserLauncher.matchesPrefix(new String[]{"IMAGE/PNG"}, "image/"));
        assertTrue(FileChooserLauncher.matchesPrefix(new String[]{"Video/MP4"}, "video/"));
    }

    @Test
    public void matchesPrefix_splits_comma_joined_entry() {
        assertTrue(FileChooserLauncher.matchesPrefix(new String[]{"application/pdf, image/png"}, "image/"));
    }

    @Test
    public void matchesPrefix_handles_wildcard_accept() {
        // H5 常见写法:accept="image/*"(注:matchesPrefix 命中前缀,通配符无副作用)
        assertTrue(FileChooserLauncher.matchesPrefix(new String[]{"image/*"}, "image/"));
    }

    @Test
    public void matchesPrefix_null_array_returns_false() {
        assertFalse(FileChooserLauncher.matchesPrefix(null, "image/"));
    }
}
