//
//  UmengBootstrapTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 友盟 SDK 初始化 gate 测试。
final class UmengBootstrapTests: XCTestCase {

    private var defaults: UserDefaults!
    private var appStore: AppStore!

    override func setUp() {
        super.setUp()
        UmengBootstrap.resetForTesting()
        defaults = UserDefaults(suiteName: "UmengBootstrapTests")!
        defaults.removePersistentDomain(forName: "UmengBootstrapTests")
        appStore = AppStore(defaults: defaults)
    }

    override func tearDown() {
        UmengBootstrap.resetForTesting()
        super.tearDown()
    }

    func testConfigureReturnsFalseWhenPrivacyNotAccepted() {
        var callCount = 0
        UmengBootstrap.setConfigureHandlerForTesting { _, _ in
            callCount += 1
        }
        appStore.session.privacyAgreementAccepted = false

        XCTAssertFalse(UmengBootstrap.configureIfNeeded(appStore: appStore))
        XCTAssertFalse(UmengBootstrap.isConfigured)
        XCTAssertEqual(callCount, 0)
    }

    func testConfigureIsIdempotentWhenPrivacyAccepted() {
        var calls: [(appKey: String, channel: String)] = []
        UmengBootstrap.setConfigureHandlerForTesting { appKey, channel in
            calls.append((appKey, channel))
        }
        appStore.session.privacyAgreementAccepted = true

        XCTAssertTrue(UmengBootstrap.configureIfNeeded(appStore: appStore))
        XCTAssertTrue(UmengBootstrap.isConfigured)
        XCTAssertTrue(UmengBootstrap.configureIfNeeded(appStore: appStore))
        XCTAssertEqual(calls.count, 1)
        XCTAssertEqual(calls.first?.appKey, UmengConfig.appKey)
        XCTAssertEqual(calls.first?.channel, UmengConfig.channel)
    }
}
