//
//  SettingViewModelTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 设置中心 ViewModel 测试。
final class SettingViewModelTests: XCTestCase {
    func testAppVersionUsesClientInfoFormatting() {
        let viewModel = SettingViewModel(
            infoDictionary: [
                "CFBundleShortVersionString": "1.2",
                "CFBundleVersion": "9",
            ],
            environment: .testing
        )

        XCTAssertEqual(viewModel.appVersion, "1.2.9")
    }

    func testAppDownloadURLUsesEnvironmentBaseURL() {
        let viewModel = SettingViewModel(infoDictionary: [:], environment: .testing)

        XCTAssertEqual(viewModel.appDownloadURL.absoluteString, "https://api-test.zhifly.cn/h5/app/download")
    }

    func testVersionCodeComparison() {
        XCTAssertTrue(SettingViewModel.isNewerVersionCode(11, than: 10))
        XCTAssertFalse(SettingViewModel.isNewerVersionCode(10, than: 10))
        XCTAssertFalse(SettingViewModel.isNewerVersionCode(9, than: 10))
        XCTAssertFalse(SettingViewModel.isNewerVersionCode(11, than: nil))
    }

    func testAgreementDestinationsMapToAndroidPaths() {
        let viewModel = SettingViewModel(infoDictionary: [:], environment: .testing)

        let about = viewModel.agreementDestination(for: .aboutUs)
        let userAgreement = viewModel.agreementDestination(for: .userAgreement)
        let privacy = viewModel.agreementDestination(for: .privacyPolicy)

        XCTAssertEqual(about.title, "关于我们")
        XCTAssertEqual(about.url.absoluteString, "https://api-test.zhifly.cn/h5/app/about-us")
        XCTAssertEqual(userAgreement.title, "用户协议")
        XCTAssertEqual(userAgreement.url.absoluteString, "https://api-test.zhifly.cn/h5/app/user-agreement")
        XCTAssertEqual(privacy.title, "隐私政策")
        XCTAssertEqual(privacy.url.absoluteString, "https://api-test.zhifly.cn/h5/app/privacy-policy")
    }

    @MainActor
    func testCheckLatestVersionMarksNewVersionWhenVersionCodeIsHigher() async throws {
        let json = """
        {
          "code": 100000,
          "msg": "success",
          "data": {
            "id": 1,
            "type": 1,
            "version": "1.0.1",
            "operator": "admin",
            "description": "update",
            "status": 1,
            "created_at": "2026-07-09 00:00:00",
            "updated_at": "2026-07-09 00:00:00",
            "deleted_at": null,
            "version_code": 8,
            "apk_url": "https://example.com/app.apk",
            "is_force_update": 0,
            "approve_notes": ""
          }
        }
        """.data(using: .utf8)!
        let session = MockURLSession(responses: [json])
        let client = APIClient(environment: .testing, session: session)
        let api = SettingAPI(client: client)
        let viewModel = SettingViewModel(
            infoDictionary: [
                "CFBundleShortVersionString": "1.0.0",
                "CFBundleVersion": "7",
            ],
            environment: .testing
        )

        await viewModel.checkLatestVersion(api: api)

        XCTAssertEqual(viewModel.latestVersion?.versionCode, 8)
        XCTAssertTrue(viewModel.hasNewVersion)
        XCTAssertEqual(session.requests.first?.url?.path, "/api/app/latest-version")
    }
}
