import { describe, it, expect } from "@normalized:N&&&@ohos/hypium/index&1.0.25";
import { PrewarmLevel } from "@normalized:N&&&tencentqianwebview/src/main/ets/model/PrewarmLevel&1.0.0";
import { PrewarmEnvironment, manifestUrlOf, knownQuickHostOf, environmentOfHost } from "@normalized:N&&&tencentqianwebview/src/main/ets/model/PrewarmEnvironment&1.0.0";
import { PrewarmConfig } from "@normalized:N&&&tencentqianwebview/src/main/ets/model/PrewarmConfig&1.0.0";
import { PrewarmDedup } from "@normalized:N&&&tencentqianwebview/src/main/ets/internal/prewarm/PrewarmDedup&1.0.0";
import { convertDetectedLanguage, resolveLang, pickLocaleAssets } from "@normalized:N&&&tencentqianwebview/src/main/ets/internal/prewarm/LangUtil&1.0.0";
/**
 * 预热纯逻辑单测：去重单调性、version 重置、环境映射、错配反查、配置默认值。
 * 对应 spec.md webview-prewarm 的多条 Scenario。
 */
export default function prewarmTest() {
    describe('Prewarm', () => {
        // ────────── PrewarmDedup ──────────
        it('dedup_minimal_runsOnce', 0, () => {
            const d = new PrewarmDedup();
            expect(d.shouldRun(PrewarmLevel.MINIMAL, '')).assertTrue();
            d.markDone(PrewarmLevel.MINIMAL, '');
            expect(d.shouldRun(PrewarmLevel.MINIMAL, '')).assertFalse();
        });
        it('dedup_sameLevel_isDeduped', 0, () => {
            const d = new PrewarmDedup();
            expect(d.shouldRun(PrewarmLevel.LIGHT, 'single-sign')).assertTrue();
            d.markDone(PrewarmLevel.LIGHT, 'single-sign');
            expect(d.shouldRun(PrewarmLevel.LIGHT, 'single-sign')).assertFalse();
        });
        it('dedup_upgrade_runsIncrement', 0, () => {
            const d = new PrewarmDedup();
            d.markDone(PrewarmLevel.LIGHT, 'single-sign');
            // 升级到 HEAVY 应执行
            expect(d.shouldRun(PrewarmLevel.HEAVY, 'single-sign')).assertTrue();
            d.markDone(PrewarmLevel.HEAVY, 'single-sign');
            expect(d.shouldRun(PrewarmLevel.HEAVY, 'single-sign')).assertFalse();
        });
        it('dedup_downgrade_isIgnored', 0, () => {
            const d = new PrewarmDedup();
            d.markDone(PrewarmLevel.HEAVY, 'single-sign');
            // 已 HEAVY，再调 LIGHT 应被忽略
            expect(d.shouldRun(PrewarmLevel.LIGHT, 'single-sign')).assertFalse();
        });
        it('dedup_differentBizType_isIndependent', 0, () => {
            const d = new PrewarmDedup();
            d.markDone(PrewarmLevel.HEAVY, 'single-sign');
            expect(d.shouldRun(PrewarmLevel.LIGHT, 'batch-sign')).assertTrue();
        });
        it('dedup_reset_clearsAll', 0, () => {
            const d = new PrewarmDedup();
            d.markDone(PrewarmLevel.HEAVY, 'single-sign');
            d.markDone(PrewarmLevel.MINIMAL, '');
            d.reset();
            expect(d.shouldRun(PrewarmLevel.HEAVY, 'single-sign')).assertTrue();
            expect(d.shouldRun(PrewarmLevel.MINIMAL, '')).assertTrue();
        });
        // ────────── 环境映射 ──────────
        it('manifestUrl_prod', 0, () => {
            expect(manifestUrlOf(PrewarmEnvironment.PROD))
                .assertEqual('https://res.ess.tencent.cn/cdn/h5-std/warmup-manifest.json');
        });
        it('manifestUrl_beta', 0, () => {
            expect(manifestUrlOf(PrewarmEnvironment.BETA))
                .assertEqual('https://res.ess.tencent.cn/cdn/h5-std-beta/warmup-manifest.json');
        });
        it('manifestUrl_test', 0, () => {
            expect(manifestUrlOf(PrewarmEnvironment.TEST))
                .assertEqual('https://res.ess.tencent.cn/cdn/h5-std-dev/warmup-manifest.json');
        });
        it('knownQuickHost_prod', 0, () => {
            expect(knownQuickHostOf(PrewarmEnvironment.PROD)).assertEqual('quick.qian.tencent.com');
        });
        // ────────── 错配反查 ──────────
        it('environmentOfHost_knownProd', 0, () => {
            expect(environmentOfHost('quick.qian.tencent.com') === PrewarmEnvironment.PROD).assertTrue();
        });
        it('environmentOfHost_knownBeta', 0, () => {
            expect(environmentOfHost('quick.beta.qian.tencent.cn') === PrewarmEnvironment.BETA).assertTrue();
        });
        it('environmentOfHost_nonQuickDomain_isNull', 0, () => {
            // 非 quick 域名 → null → open() 不告警
            expect(environmentOfHost('sign.customer.com') === null).assertTrue();
        });
        // ────────── 配置默认值 ──────────
        it('config_defaults', 0, () => {
            const c = new PrewarmConfig();
            expect(c.environment === PrewarmEnvironment.PROD).assertTrue();
            expect(c.manifestUrl === null).assertTrue();
            expect(c.poolSize).assertEqual(1);
            expect(c.enablePool).assertTrue();
            expect(c.preferredLanguage === null).assertTrue();
        });
        // ────────── 语言解析 LangUtil（L3 直取） ──────────
        it('convertDetectedLanguage_exactMatch', 0, () => {
            expect(convertDetectedLanguage('en-US', ['zh-CN', 'en-US'])).assertEqual('en-US');
        });
        it('convertDetectedLanguage_prefixMatch', 0, () => {
            // en-GB → en-US；zh-TW → zh-CN（前缀匹配）
            expect(convertDetectedLanguage('en-GB', ['zh-CN', 'en-US'])).assertEqual('en-US');
            expect(convertDetectedLanguage('zh-TW', ['zh-CN', 'en-US'])).assertEqual('zh-CN');
        });
        it('convertDetectedLanguage_fallbackZhCN', 0, () => {
            expect(convertDetectedLanguage('fr-FR', ['zh-CN', 'en-US'])).assertEqual('zh-CN');
            expect(convertDetectedLanguage('', ['zh-CN', 'en-US'])).assertEqual('zh-CN');
        });
        it('resolveLang_preferredWins', 0, () => {
            // i18nAutoDetect=true 时 preferredLanguage 非空优先，忽略系统语言
            expect(resolveLang('en-US', true, 'zh-Hans', ['zh-CN', 'en-US'])).assertEqual('en-US');
        });
        it('resolveLang_autoDetectUsesSystem', 0, () => {
            // 无 preferred + i18nAutoDetect=true → 用系统语言（经前缀归一化）
            expect(resolveLang(null, true, 'en', ['zh-CN', 'en-US'])).assertEqual('en-US');
        });
        it('resolveLang_autoDetectDisabledFallsBackZhCN', 0, () => {
            // 无 preferred + i18nAutoDetect=false → 固定 zh-CN（与 warmup.html 一致）
            expect(resolveLang(null, false, 'en', ['zh-CN', 'en-US'])).assertEqual('zh-CN');
        });
        it('resolveLang_autoDetectDisabled_ignoresPreferred', 0, () => {
            // i18nAutoDetect=false 时无视 preferredLanguage，恒 zh-CN（真实页也恒 zh-CN，与 warmup.html 一致）
            expect(resolveLang('en-US', false, 'en', ['zh-CN', 'en-US'])).assertEqual('zh-CN');
        });
        it('pickLocaleAssets_hitAndFallback', 0, () => {
            const locales: Record<string, string[]> = {
                'zh-CN': ['https://res/zh.json'],
                'en-US': ['https://res/en.json']
            };
            expect(pickLocaleAssets(locales, 'en-US')[0]).assertEqual('https://res/en.json');
            // 缺失语言 → 回退 zh-CN
            expect(pickLocaleAssets(locales, 'fr-FR')[0]).assertEqual('https://res/zh.json');
            // locales 为 undefined → 空数组
            expect(pickLocaleAssets(undefined, 'en-US').length).assertEqual(0);
        });
    });
}
