package com.tencent.qian.webview.prewarm;

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

import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.tencent.qian.webview.model.PrewarmConfig;
import com.tencent.qian.webview.model.PrewarmEnvironment;
import com.tencent.qian.webview.model.PrewarmLevel;

import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * 预热纯逻辑单测：去重单调性、version 重置、环境映射、错配反查、manifest 解析、配置默认值。
 * 与 Kotlin / iOS / HarmonyOS PrewarmTest 同一组用例。
 */
@RunWith(AndroidJUnit4.class)
public class PrewarmTest {

    // ────────── PrewarmDedup ──────────

    @Test public void dedup_minimal_runsOnce() {
        PrewarmDedup d = new PrewarmDedup();
        assertTrue(d.shouldRun(PrewarmLevel.MINIMAL, ""));
        d.markDone(PrewarmLevel.MINIMAL, "");
        assertFalse(d.shouldRun(PrewarmLevel.MINIMAL, ""));
    }

    @Test public void dedup_sameLevel_isDeduped() {
        PrewarmDedup d = new PrewarmDedup();
        assertTrue(d.shouldRun(PrewarmLevel.LIGHT, "single-sign"));
        d.markDone(PrewarmLevel.LIGHT, "single-sign");
        assertFalse(d.shouldRun(PrewarmLevel.LIGHT, "single-sign"));
    }

    @Test public void dedup_upgrade_runsIncrement() {
        PrewarmDedup d = new PrewarmDedup();
        d.markDone(PrewarmLevel.LIGHT, "single-sign");
        assertTrue(d.shouldRun(PrewarmLevel.HEAVY, "single-sign"));
        d.markDone(PrewarmLevel.HEAVY, "single-sign");
        assertFalse(d.shouldRun(PrewarmLevel.HEAVY, "single-sign"));
    }

    @Test public void dedup_downgrade_isIgnored() {
        PrewarmDedup d = new PrewarmDedup();
        d.markDone(PrewarmLevel.HEAVY, "single-sign");
        assertFalse(d.shouldRun(PrewarmLevel.LIGHT, "single-sign"));
    }

    @Test public void dedup_reset_clearsAll() {
        PrewarmDedup d = new PrewarmDedup();
        d.markDone(PrewarmLevel.HEAVY, "single-sign");
        d.markDone(PrewarmLevel.MINIMAL, "");
        d.reset();
        assertTrue(d.shouldRun(PrewarmLevel.HEAVY, "single-sign"));
        assertTrue(d.shouldRun(PrewarmLevel.MINIMAL, ""));
    }

    // 档位增量补做：任意档执行后 DNS 已做过 → 后续 MINIMAL 应 no-op。
    @Test public void dedup_directLight_marksMinimalDone() {
        PrewarmDedup d = new PrewarmDedup();
        d.markDone(PrewarmLevel.LIGHT, "single-sign");
        assertFalse(d.shouldRun(PrewarmLevel.MINIMAL, ""));
    }

    @Test public void dedup_directHeavy_marksMinimalDone() {
        PrewarmDedup d = new PrewarmDedup();
        d.markDone(PrewarmLevel.HEAVY, "single-sign");
        assertFalse(d.shouldRun(PrewarmLevel.MINIMAL, ""));
    }

    // ────────── 环境映射 & 错配反查 ──────────

    @Test public void manifestUrl_mapping() {
        assertEquals("https://res.ess.tencent.cn/cdn/h5-std/warmup-manifest.json",
                PrewarmEnvironment.manifestUrlOf(PrewarmEnvironment.PROD));
        assertEquals("https://res.ess.tencent.cn/cdn/h5-std-beta/warmup-manifest.json",
                PrewarmEnvironment.manifestUrlOf(PrewarmEnvironment.BETA));
        assertEquals("https://res.ess.tencent.cn/cdn/h5-std-dev/warmup-manifest.json",
                PrewarmEnvironment.manifestUrlOf(PrewarmEnvironment.TEST));
    }

    @Test public void environmentOfUrl_known_and_nonQuick() {
        assertEquals(PrewarmEnvironment.PROD, PrewarmEnvironment.environmentOfUrl("https://quick.qian.tencent.com/sign"));
        assertEquals(PrewarmEnvironment.BETA, PrewarmEnvironment.environmentOfUrl("https://quick.beta.qian.tencent.cn/x"));
        // 非 quick 域名 → null → open() 不告警
        assertNull(PrewarmEnvironment.environmentOfUrl("https://sign.customer.com/flow"));
        assertNull(PrewarmEnvironment.environmentOfUrl(null));
    }

    // ────────── manifest 解析 ──────────

    @Test public void manifest_parse_extractsBizTypes() {
        String json = "{\"version\":\"1.0\",\"hosts\":[\"https://quick.qian.tencent.com\"],"
                + "\"bizTypes\":{\"single-sign\":{\"warmupUrl\":\"https://quick.qian.tencent.com/warmup.html?bizType=single-sign\","
                + "\"assets\":[\"https://res/a.js\",\"https://res/b.css\"]}}}";
        WarmupManifest m = WarmupManifest.parse(json);
        assertNotNull(m);
        assertEquals("1.0", m.version);
        assertEquals(1, m.hosts.size());
        assertEquals(2, m.bizTypes.get("single-sign").assets.size());
    }

    @Test public void manifest_parse_invalidReturnsNull() {
        assertNull(WarmupManifest.parse("not-json"));
    }

    // ────────── 配置默认值 ──────────

    @Test public void config_defaults() {
        PrewarmConfig c = PrewarmConfig.DEFAULT;
        assertEquals(PrewarmEnvironment.PROD, c.getEnvironment());
        assertNull(c.getManifestUrl());
        assertEquals(1, c.getPoolSize());
        assertTrue(c.isEnablePool());
        assertNull(c.getPreferredLanguage());
        // UA 字段默认值（与 TencentQianWebViewConfig 对齐）
        assertTrue(c.isInjectUserAgent());
        assertEquals("", c.getUserAgentExtra());
    }
}
