//
//  HomeCommonMenuStoreTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 常用应用持久化测试。
final class HomeCommonMenuStoreTests: XCTestCase {

    private var defaults: UserDefaults!
    private var store: HomeCommonMenuStore!

    override func setUp() {
        defaults = UserDefaults(suiteName: "HomeCommonMenuStoreTests")!
        defaults.removePersistentDomain(forName: "HomeCommonMenuStoreTests")
        store = HomeCommonMenuStore(defaults: defaults)
    }

    override func tearDown() {
        defaults.removePersistentDomain(forName: "HomeCommonMenuStoreTests")
        super.tearDown()
    }

    func testDefaultCommonURIsTakeFirstFour() {
        let permissions = (1...6).map {
            HomePermissionItem(id: "\($0)", name: "菜单\($0)", uri: "uri_\($0)")
        }
        let uris = HomeCommonMenuStore.defaultCommonURIs(from: permissions)
        XCTAssertEqual(uris, ["uri_1", "uri_2", "uri_3", "uri_4"])
    }

    func testSavedCommonURIsOverrideDefault() {
        let permissions = [
            HomePermissionItem(id: "1", name: "钱包", uri: "wallet"),
            HomePermissionItem(id: "2", name: "云盘", uri: "cloud_management"),
            HomePermissionItem(id: "3", name: "任务", uri: "task_management"),
        ]
        let allMenus = HomeMenuCatalog.visibleMenus(from: permissions)
        store.saveCommonURIs(["cloud_management"], accountScope: "u1_scenic_user", roleCode: "photographer")
        let common = store.commonMenus(
            from: allMenus,
            permissions: permissions,
            accountScope: "u1_scenic_user",
            roleCode: "photographer"
        )
        XCTAssertEqual(common.map(\.uri), ["cloud_management"])
    }

    func testSplitMenusSeparatesCommonAndMore() {
        let menus = [
            HomeMenuItem(uri: "wallet", title: "钱包", iconName: "wallet.pass"),
            HomeMenuItem(uri: "cloud_management", title: "云盘", iconName: "icloud"),
            HomeMenuItem(uri: "task_management", title: "任务", iconName: "checklist"),
        ]
        let split = HomeCommonMenuStore.splitMenus(from: menus, commonURIs: ["wallet", "task_management"])
        XCTAssertEqual(split.common.map(\.uri), ["wallet", "task_management"])
        XCTAssertEqual(split.more.map(\.uri), ["cloud_management"])
    }

    func testPhotographerReportCatalogItemExists() {
        let item = HomeMenuCatalog.item(for: "report_photographer")

        XCTAssertEqual(item?.title, "举报摄影师")
        XCTAssertEqual(item?.iconName, "exclamationmark.shield")
    }
}
