// ─────────────────────────────────────────────────────────────────────────────
// 预热纯逻辑 XCTest 单测：去重单调性、version 重置、环境映射、错配反查、
// Cache-Control 解析、manifest 解析、配置默认值。
// 与 Swift / Android / HarmonyOS PrewarmTest 同一组用例。
// ─────────────────────────────────────────────────────────────────────────────

#import <XCTest/XCTest.h>
#import "TQPrewarmLevel.h"
#import "TQPrewarmEnvironment.h"
#import "TQPrewarmConfig.h"
#import "TQPrewarmDedup.h"
#import "TQManifestStore.h"
#import "TQWarmupManifest.h"
#import "TQWebViewPrewarmer.h"

@interface TQPrewarmTests : XCTestCase
@end

@implementation TQPrewarmTests

#pragma mark - TQPrewarmDedup

- (void)test_dedup_minimal_runsOnce {
    TQPrewarmDedup *d = [[TQPrewarmDedup alloc] init];
    XCTAssertTrue([d shouldRun:TQPrewarmLevelMinimal bizType:@""]);
    [d markDone:TQPrewarmLevelMinimal bizType:@""];
    XCTAssertFalse([d shouldRun:TQPrewarmLevelMinimal bizType:@""]);
}

- (void)test_dedup_sameLevel_isDeduped {
    TQPrewarmDedup *d = [[TQPrewarmDedup alloc] init];
    XCTAssertTrue([d shouldRun:TQPrewarmLevelLight bizType:@"single-sign"]);
    [d markDone:TQPrewarmLevelLight bizType:@"single-sign"];
    XCTAssertFalse([d shouldRun:TQPrewarmLevelLight bizType:@"single-sign"]);
}

- (void)test_dedup_upgrade_runsIncrement {
    TQPrewarmDedup *d = [[TQPrewarmDedup alloc] init];
    [d markDone:TQPrewarmLevelLight bizType:@"single-sign"];
    XCTAssertTrue([d shouldRun:TQPrewarmLevelHeavy bizType:@"single-sign"]);
    [d markDone:TQPrewarmLevelHeavy bizType:@"single-sign"];
    XCTAssertFalse([d shouldRun:TQPrewarmLevelHeavy bizType:@"single-sign"]);
}

- (void)test_dedup_downgrade_isIgnored {
    TQPrewarmDedup *d = [[TQPrewarmDedup alloc] init];
    [d markDone:TQPrewarmLevelHeavy bizType:@"single-sign"];
    XCTAssertFalse([d shouldRun:TQPrewarmLevelLight bizType:@"single-sign"]);
}

- (void)test_dedup_reset_clearsAll {
    TQPrewarmDedup *d = [[TQPrewarmDedup alloc] init];
    [d markDone:TQPrewarmLevelHeavy bizType:@"single-sign"];
    [d markDone:TQPrewarmLevelMinimal bizType:@""];
    [d reset];
    XCTAssertTrue([d shouldRun:TQPrewarmLevelHeavy bizType:@"single-sign"]);
    XCTAssertTrue([d shouldRun:TQPrewarmLevelMinimal bizType:@""]);
}

// 档位增量补做：任意档执行后 DNS 已做过 → 后续 Minimal 应 no-op。
- (void)test_dedup_directLight_marksMinimalDone {
    TQPrewarmDedup *d = [[TQPrewarmDedup alloc] init];
    [d markDone:TQPrewarmLevelLight bizType:@"single-sign"];
    XCTAssertFalse([d shouldRun:TQPrewarmLevelMinimal bizType:@""]);
}

- (void)test_dedup_directHeavy_marksMinimalDone {
    TQPrewarmDedup *d = [[TQPrewarmDedup alloc] init];
    [d markDone:TQPrewarmLevelHeavy bizType:@"single-sign"];
    XCTAssertFalse([d shouldRun:TQPrewarmLevelMinimal bizType:@""]);
}

#pragma mark - 环境映射 & 错配反查

- (void)test_manifestUrl_mapping {
    XCTAssertEqualObjects([TQWebViewPrewarmer manifestUrlForEnvironment:TQPrewarmEnvironmentProd],
                          @"https://res.ess.tencent.cn/cdn/h5-std/warmup-manifest.json");
    XCTAssertEqualObjects([TQWebViewPrewarmer manifestUrlForEnvironment:TQPrewarmEnvironmentBeta],
                          @"https://res.ess.tencent.cn/cdn/h5-std-beta/warmup-manifest.json");
    XCTAssertEqualObjects([TQWebViewPrewarmer manifestUrlForEnvironment:TQPrewarmEnvironmentTest],
                          @"https://res.ess.tencent.cn/cdn/h5-std-dev/warmup-manifest.json");
}

- (void)test_environmentForURL_known_and_nonQuick {
    XCTAssertEqualObjects([TQWebViewPrewarmer environmentForURL:@"https://quick.qian.tencent.com/sign"],
                          @(TQPrewarmEnvironmentProd));
    XCTAssertEqualObjects([TQWebViewPrewarmer environmentForURL:@"https://quick.beta.qian.tencent.cn/x"],
                          @(TQPrewarmEnvironmentBeta));
    // 非 quick 域名 → nil → open() 不告警
    XCTAssertNil([TQWebViewPrewarmer environmentForURL:@"https://sign.customer.com/flow"]);
    XCTAssertNil([TQWebViewPrewarmer environmentForURL:nil]);
}

#pragma mark - Cache-Control 解析

- (void)test_parseMaxAge {
    XCTAssertEqualObjects([TQManifestStore parseMaxAge:@"max-age=300"], @(300));
    XCTAssertEqualObjects([TQManifestStore parseMaxAge:@"public, max-age=120, immutable"], @(120));
    XCTAssertNil([TQManifestStore parseMaxAge:@"no-cache"]);
    XCTAssertNil([TQManifestStore parseMaxAge:nil]);
}

#pragma mark - manifest 解析

- (void)test_manifest_parse {
    NSString *json = @"{\"version\":\"1.0\",\"hosts\":[\"https://quick.qian.tencent.com\"],"
                      "\"bizTypes\":{\"single-sign\":{\"warmupUrl\":\"https://quick.qian.tencent.com/warmup.html\","
                      "\"assets\":[\"https://res/a.js\",\"https://res/b.css\"]}}}";
    TQWarmupManifest *m = [TQWarmupManifest parse:[json dataUsingEncoding:NSUTF8StringEncoding]];
    XCTAssertNotNil(m);
    XCTAssertEqualObjects(m.version, @"1.0");
    XCTAssertEqual(m.bizTypes[@"single-sign"].assets.count, 2);
}

- (void)test_manifest_parse_invalidReturnsNil {
    TQWarmupManifest *m = [TQWarmupManifest parse:[@"not-json" dataUsingEncoding:NSUTF8StringEncoding]];
    XCTAssertNil(m);
}

#pragma mark - 配置默认值

- (void)test_config_defaults {
    TQPrewarmConfig *c = [TQPrewarmConfig defaultConfig];
    XCTAssertEqual(c.environment, TQPrewarmEnvironmentProd);
    XCTAssertNil(c.manifestUrl);
    XCTAssertEqual(c.poolSize, 1);
    XCTAssertTrue(c.enablePool);
    XCTAssertNil(c.preferredLanguage);
    // UA 字段默认值（与 TQWebViewConfig 对齐）
    XCTAssertTrue(c.injectUserAgent);
    XCTAssertEqualObjects(c.userAgentExtra, @"");
}

#pragma mark - 语言偏好 appendLang（L4 转发）

- (void)test_appendLang_withExistingQuery_usesAmpersand {
    XCTAssertEqualObjects(
        [TQWebViewPrewarmer appendLang:@"https://quick.qian.tencent.com/warmup.html?bizType=single-sign" lang:@"en-US"],
        @"https://quick.qian.tencent.com/warmup.html?bizType=single-sign&lang=en-US");
}

- (void)test_appendLang_withoutQuery_usesQuestionMark {
    XCTAssertEqualObjects(
        [TQWebViewPrewarmer appendLang:@"https://quick.qian.tencent.com/warmup.html" lang:@"zh-CN"],
        @"https://quick.qian.tencent.com/warmup.html?lang=zh-CN");
}

- (void)test_appendLang_emptyOrNil_returnsUrlUnchanged {
    NSString *url = @"https://quick.qian.tencent.com/warmup.html?bizType=x";
    XCTAssertEqualObjects([TQWebViewPrewarmer appendLang:url lang:nil], url);
    XCTAssertEqualObjects([TQWebViewPrewarmer appendLang:url lang:@""], url);
}

@end
