//
//  RolePermissionMatcherTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 角色权限匹配测试。
final class RolePermissionMatcherTests: XCTestCase {

    private func makeItem(code: String, id: Int, name: String) -> RolePermissionResponse {
        RolePermissionResponse(
            role: RoleInfo(id: id, name: name, roleCode: code, notes: nil, permission: []),
            scenic: []
        )
    }

    func testMatchByRoleCodeHasHighestPriority() {
        let list = [
            makeItem(code: "photographer", id: 41, name: "摄影师"),
            makeItem(code: "store_admin", id: 46, name: "店铺管理员"),
        ]
        let matched = RolePermissionMatcher.match(
            in: list,
            roleCode: AppRoleCode.storeAdmin.rawValue,
            roleName: "摄影师"
        )
        XCTAssertEqual(matched?.role.roleCode, AppRoleCode.storeAdmin.rawValue)
    }

    func testMatchByLegacyRoleIdWhenCodeMissing() {
        let list = [makeItem(code: "", id: 53, name: "景区管理员")]
        let matched = RolePermissionMatcher.match(
            in: list,
            roleCode: "",
            roleName: "",
            legacyRoleId: 53
        )
        XCTAssertEqual(matched?.role.id, 53)
    }

    func testMatchByRoleNameWhenCodeAndLegacyMissing() {
        let list = [makeItem(code: "", id: 99, name: "剪辑师")]
        let matched = RolePermissionMatcher.match(
            in: list,
            roleCode: "",
            roleName: "剪辑师"
        )
        XCTAssertEqual(matched?.role.name, "剪辑师")
    }
}
