//
//  HomeAllFunctionsBuilderTests.swift
//  suixinkanTests
//
//  Created by Codex on 2026/6/30.
//

import XCTest
@testable import suixinkan

/// 「全部功能」页菜单构建测试，覆盖 Android 对齐的过滤、顺序与拆分规则。
final class HomeAllFunctionsBuilderTests: XCTestCase {
    /// 测试仅保留顶层权限中属于 menuList 白名单的 URI。
    func testBuildFiltersToAndroidMenuListWhitelist() {
        let snapshot = HomeAllFunctionsBuilder.build(
            topLevelPermissions: [
                permission(name: "基本信息", uri: "basic_info"),
                permission(name: "位置上报", uri: "location_report"),
                permission(name: "举报摄影师", uri: "report_photographer"),
                permission(name: "相册管理", uri: "album_list"),
                permission(name: "我的钱包", uri: "wallet")
            ],
            commonURIs: []
        )

        XCTAssertEqual(snapshot.allFunctions.map(\.uri), ["location_report", "report_photographer", "wallet"])
    }

    /// 测试保持 API 顶层顺序，不使用 preferredOrder 重排。
    func testBuildPreservesTopLevelAPIOrder() {
        let snapshot = HomeAllFunctionsBuilder.build(
            topLevelPermissions: [
                permission(name: "消息中心", uri: "message_center"),
                permission(name: "空间设置", uri: "space_settings"),
                permission(name: "我的钱包", uri: "wallet")
            ],
            commonURIs: []
        )

        XCTAssertEqual(snapshot.allFunctions.map(\.uri), ["message_center", "space_settings", "wallet"])
    }

    /// 测试不会展开子权限节点。
    func testBuildIgnoresChildPermissions() {
        let snapshot = HomeAllFunctionsBuilder.build(
            topLevelPermissions: [
                permission(
                    name: "基本信息",
                    uri: "basic_info",
                    children: [permission(name: "消息中心", uri: "message_center")]
                ),
                permission(name: "我的钱包", uri: "wallet")
            ],
            commonURIs: []
        )

        XCTAssertEqual(snapshot.allFunctions.map(\.uri), ["wallet"])
    }

    /// 测试常用/更多按精确 URI 拆分。
    func testBuildSplitsCommonAndMoreByExactURI() {
        let snapshot = HomeAllFunctionsBuilder.build(
            topLevelPermissions: [
                permission(name: "位置上报", uri: "location_report"),
                permission(name: "注册邀请", uri: "registration_invitation"),
                permission(name: "店铺管理", uri: "store"),
                permission(name: "飞行管理", uri: "fly")
            ],
            commonURIs: ["registration_invitation", "location_report"]
        )

        XCTAssertEqual(snapshot.commonFunctions.map(\.uri), ["location_report", "registration_invitation"])
        XCTAssertEqual(snapshot.moreFunctions.map(\.uri), ["store", "fly"])
    }

    /// 测试常用区顺序跟随 allFunctions 列表顺序，而非 commonURIs 存储顺序。
    func testCommonFunctionsFollowAllFunctionsOrder() {
        let snapshot = HomeAllFunctionsBuilder.build(
            topLevelPermissions: [
                permission(name: "位置上报", uri: "location_report"),
                permission(name: "注册邀请", uri: "registration_invitation"),
                permission(name: "店铺管理", uri: "store")
            ],
            commonURIs: ["store", "location_report"]
        )

        XCTAssertEqual(snapshot.commonFunctions.map(\.uri), ["location_report", "store"])
    }

    /// 测试与 Android 日志样例一致的过滤结果。
    func testBuildMatchesAndroidSampleAccountShape() {
        let topLevelURIs = [
            "location_report",
            "basic_info",
            "registration_invitation",
            "store",
            "fly",
            "space_settings",
            "photographer_stats",
            "photographer_orders",
            "asset_management",
            "schedule_management",
            "system_settings",
            "message_center",
            "location_info",
            "checkin_points",
            "sample_management",
            "live_album",
            "live_stream_management",
            "cloud_management",
            "cooperation_order",
            "scan_qr",
            "payment_qr",
            "wallet",
            "pilot_cert",
            "task_management_editor",
            "pm_manager",
            "pilot_controller",
            "travel_album"
        ]
        let snapshot = HomeAllFunctionsBuilder.build(
            topLevelPermissions: topLevelURIs.map { permission(name: $0, uri: $0) },
            commonURIs: ["location_report", "registration_invitation", "store"]
        )

        XCTAssertEqual(snapshot.allFunctions.count, 21)
        XCTAssertEqual(snapshot.allFunctions.first?.uri, "location_report")
        XCTAssertEqual(snapshot.commonFunctions.map(\.uri), ["location_report", "registration_invitation", "store"])
        XCTAssertEqual(snapshot.moreFunctions.first?.uri, "fly")
        XCTAssertEqual(snapshot.moreFunctions.count, 18)
        XCTAssertTrue(snapshot.moreFunctions.contains { $0.uri == "travel_album" })
    }

    private func permission(
        name: String,
        uri: String,
        children: [PermissionItem] = []
    ) -> PermissionItem {
        PermissionItem(id: uri.hashValue, name: name, uri: uri, children: children)
    }
}
