Files
suixinkan_ios_uikit/suixinkan_iosTests/AccountContextLoaderTests.swift
2026-06-26 14:33:31 +08:00

138 lines
4.9 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// AccountContextLoaderTests.swift
// suixinkanTests
//
// Created by Codex on 2026/6/22.
//
import XCTest
@testable import suixinkan_ios
@MainActor
///
final class AccountContextLoaderTests: XCTestCase {
///
func testRefreshLoadsRolePermissionScenicAndStoreContext() async throws {
let accountContext = AccountContext()
let permissionContext = PermissionContext()
let api = MockAccountContextAPI()
api.roles = [
RolePermissionResponse(
role: RoleInfo(id: 7, name: "运营", permission: [PermissionItem(id: 1, name: "首页", uri: "/home")]),
scenic: [ScenicInfo(id: 88, name: "东湖景区")]
)
]
api.scenicResponse = ScenicListAllResponse(total: 1, list: [ScenicListItem(id: 88, name: "东湖景区")])
api.storeResponse = ListPayload(total: 1, list: [StoreItem(id: 66, scenicId: 88, name: "东湖门店")])
try await AccountContextLoader().refresh(
accountContext: accountContext,
permissionContext: permissionContext,
profileAPI: MockUserProfileAPI(),
accountContextAPI: api
)
XCTAssertEqual(accountContext.profile?.displayName, "测试用户")
XCTAssertEqual(permissionContext.currentRole?.id, 7)
XCTAssertTrue(permissionContext.canAccess("/home"))
XCTAssertEqual(accountContext.currentScenic?.id, 88)
XCTAssertEqual(accountContext.currentStore?.id, 66)
}
///
func testRefreshFallsBackToRoleScenicsWhenScenicAPIThrows() async throws {
let accountContext = AccountContext()
let permissionContext = PermissionContext()
let api = MockAccountContextAPI()
api.roles = [
RolePermissionResponse(
role: RoleInfo(id: 7, name: "运营"),
scenic: [
ScenicInfo(id: 88, name: "东湖景区"),
ScenicInfo(id: 88, name: "东湖景区")
]
)
]
api.scenicError = APIError.networkFailed("景区接口失败")
try await AccountContextLoader().refresh(
accountContext: accountContext,
permissionContext: permissionContext,
profileAPI: MockUserProfileAPI(),
accountContextAPI: api
)
XCTAssertEqual(accountContext.scenicScopes.map(\.id), [88])
}
///
func testRefreshAllowsStoreFailure() async throws {
let accountContext = AccountContext()
let permissionContext = PermissionContext()
let api = MockAccountContextAPI()
api.roles = [
RolePermissionResponse(
role: RoleInfo(id: 7, name: "运营"),
scenic: [ScenicInfo(id: 88, name: "东湖景区")]
)
]
api.storeError = APIError.networkFailed("门店接口失败")
try await AccountContextLoader().refresh(
accountContext: accountContext,
permissionContext: permissionContext,
profileAPI: MockUserProfileAPI(),
accountContextAPI: api
)
XCTAssertEqual(accountContext.currentScenic?.id, 88)
XCTAssertTrue(accountContext.storeScopes.isEmpty)
XCTAssertNil(accountContext.currentStore)
}
}
@MainActor
///
private final class MockUserProfileAPI: UserProfileServing {
///
func userInfo() async throws -> UserInfoResponse {
UserInfoResponse(nickname: "测试用户", roleName: "运营")
}
}
@MainActor
///
private final class MockAccountContextAPI: AccountContextServing {
var roles: [RolePermissionResponse] = []
var scenicResponse = ScenicListAllResponse(total: 0, list: [])
var storeResponse = ListPayload(total: 0, list: [StoreItem]())
var scenicError: Error?
var storeError: Error?
///
func rolePermissions() async throws -> [RolePermissionResponse] {
roles
}
///
func scenicListAll() async throws -> ScenicListAllResponse {
if let scenicError {
throw scenicError
}
return scenicResponse
}
///
func storeAll() async throws -> ListPayload<StoreItem> {
if let storeError {
throw storeError
}
return storeResponse
}
///
func scenicSpotListAll(scenicId: Int) async throws -> ListPayload<ScenicSpotItem> {
ListPayload(total: 0, list: [])
}
}