//
//  HomeViewModelTests.swift
//  suixinkanTests
//
//  Created by Codex on 2026/6/22.
//

import XCTest
@testable import suixinkan

@MainActor
/// 首页 ViewModel 测试，覆盖权限菜单构建、排序和去重。
final class HomeViewModelTests: XCTestCase {
    /// 测试当前角色缺失时清空菜单，不回退到其他角色。
    func testBuildMenusClearsStaleMenusWhenCurrentRoleIsMissing() {
        let viewModel = HomeViewModel()
        let permissions = [
            rolePermission(roleId: 1, permissions: [
                permission(id: 1, name: "消息中心", uri: "message_center")
            ])
        ]

        viewModel.buildMenus(from: permissions, currentRoleId: 1)
        XCTAssertEqual(viewModel.menuItems.map(\.uri), ["message_center"])

        viewModel.buildMenus(from: permissions, currentRoleId: 99)

        XCTAssertTrue(viewModel.menuItems.isEmpty)
    }

    /// 测试当前角色为空时才回退到第一个角色。
    func testBuildMenusFallsBackToFirstRoleOnlyWhenCurrentRoleIsNil() {
        let viewModel = HomeViewModel()
        let permissions = [
            rolePermission(roleId: 1, permissions: [
                permission(id: 1, name: "任务管理", uri: "task_management")
            ]),
            rolePermission(roleId: 2, permissions: [
                permission(id: 2, name: "消息中心", uri: "message_center")
            ])
        ]

        viewModel.buildMenus(from: permissions, currentRoleId: nil)

        XCTAssertEqual(viewModel.menuItems.map(\.uri), ["task_management"])
    }

    /// 测试权限树会递归展开为菜单。
    func testBuildMenusFlattensPermissionTree() {
        let viewModel = HomeViewModel()
        let permissions = [
            rolePermission(roleId: 1, permissions: [
                permission(id: 1, name: "任务管理", uri: "task_management", children: [
                    permission(id: 2, name: "消息中心", uri: "message_center")
                ])
            ])
        ]

        viewModel.buildMenus(from: permissions, currentRoleId: 1)

        XCTAssertEqual(viewModel.menuItems.map(\.uri), ["task_management", "message_center"])
    }

    /// 测试同义 URI 会去重，只保留首次出现的菜单。
    func testBuildMenusDeduplicatesKnownAndroidAliases() {
        let viewModel = HomeViewModel()
        let permissions = [
            rolePermission(roleId: 1, permissions: [
                permission(id: 1, name: "项目管理", uri: "pm_manager"),
                permission(id: 2, name: "项目管理旧入口", uri: "pm"),
                permission(id: 3, name: "收款码", uri: "payment_code"),
                permission(id: 4, name: "收款码旧入口", uri: "payment_qr"),
                permission(id: 5, name: "押金订单详情", uri: "deposit_order_detail"),
                permission(id: 6, name: "押金订单", uri: "deposit_order")
            ])
        ]

        viewModel.buildMenus(from: permissions, currentRoleId: 1)

        XCTAssertEqual(viewModel.menuItems.map(\.uri), ["deposit_order_detail", "pm_manager", "payment_code"])
    }

    /// 测试菜单按旧工程 preferred order 排序。
    func testBuildMenusSortsByPreferredOrder() {
        let viewModel = HomeViewModel()
        let permissions = [
            rolePermission(roleId: 1, permissions: [
                permission(id: 1, name: "消息中心", uri: "message_center"),
                permission(id: 2, name: "空间设置", uri: "space_settings"),
                permission(id: 3, name: "钱包", uri: "wallet")
            ])
        ]

        viewModel.buildMenus(from: permissions, currentRoleId: 1)

        XCTAssertEqual(viewModel.menuItems.map(\.uri), ["space_settings", "wallet", "message_center"])
    }

    /// 创建角色权限测试实体。
    private func rolePermission(roleId: Int, permissions: [PermissionItem]) -> RolePermissionResponse {
        RolePermissionResponse(
            role: RoleInfo(id: roleId, name: "角色\(roleId)", permission: permissions),
            scenic: []
        )
    }

    /// 创建权限节点测试实体。
    private func permission(id: Int, name: String, uri: String, children: [PermissionItem] = []) -> PermissionItem {
        PermissionItem(id: id, name: name, uri: uri, children: children)
    }
}
