Files
suixinkan_ios_new/suixinkanTests/AccountContextLoaderTests.swift
汉秋 5ab9b1b324 修复账号切换后「我的」页当前账号展示错误,并统一举报摄影师首页 URI。
切换账号时以用户选中账号为准写入展示标题与业务作用域,避免沿用旧门店名称;同时将首页举报摄影师入口 URI 对齐为 report_photographer。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-01 17:21:04 +08:00

230 lines
8.9 KiB
Swift
Raw 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
@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])
}
/// PermissionContext
func testRestorePermissionsLoadsRolePermissionsOnly() 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: "东湖景区")]
)
]
try await AccountContextLoader().restorePermissions(
permissionContext: permissionContext,
accountContext: accountContext,
accountContextAPI: api
)
XCTAssertEqual(permissionContext.currentRole?.id, 7)
XCTAssertTrue(permissionContext.canAccess("/home"))
XCTAssertEqual(accountContext.scenicScopes.map(\.id), [88])
XCTAssertNil(accountContext.profile)
}
///
func testRefreshSupplementalContextUpdatesProfileAndScopes() 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.scenicResponse = ScenicListAllResponse(total: 1, list: [ScenicListItem(id: 88, name: "东湖景区")])
api.storeResponse = ListPayload(total: 1, list: [StoreItem(id: 66, scenicId: 88, name: "东湖门店")])
try await AccountContextLoader().restorePermissions(
permissionContext: permissionContext,
accountContext: accountContext,
accountContextAPI: api
)
try await AccountContextLoader().refreshSupplementalContext(
accountContext: accountContext,
permissionContext: permissionContext,
profileAPI: MockUserProfileAPI(),
accountContextAPI: api,
cachedCurrentScenicId: 88,
cachedCurrentStoreId: 66
)
XCTAssertEqual(accountContext.profile?.displayName, "测试用户")
XCTAssertEqual(accountContext.currentScenic?.id, 88)
XCTAssertEqual(accountContext.currentStore?.id, 66)
}
///
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)
}
/// /
func testRefreshSupplementalContextPreservesExistingStoreScopesWhenStoreAPIFails() async throws {
let accountContext = AccountContext()
accountContext.applyLogin(accountType: V9StoreUser.accountTypeValue)
accountContext.replaceScopes(
scenic: [BusinessScope(id: 88, name: "东湖景区", kind: .scenic)],
stores: [BusinessScope(id: 66, name: "东湖门店", kind: .store, parentScenicId: 88)],
currentScenicId: 88,
currentStoreId: 66
)
let permissionContext = PermissionContext()
let api = MockAccountContextAPI()
api.roles = [
RolePermissionResponse(
role: RoleInfo(id: 7, name: "运营"),
scenic: [ScenicInfo(id: 88, name: "东湖景区")]
)
]
api.scenicResponse = ScenicListAllResponse(total: 1, list: [ScenicListItem(id: 88, name: "东湖景区")])
api.storeError = APIError.networkFailed("门店接口失败")
try await AccountContextLoader().refreshSupplementalContext(
accountContext: accountContext,
permissionContext: permissionContext,
profileAPI: MockUserProfileAPI(),
accountContextAPI: api,
cachedCurrentScenicId: 88,
cachedCurrentStoreId: 66
)
XCTAssertEqual(accountContext.storeScopes.map(\.name), ["东湖门店"])
XCTAssertEqual(accountContext.currentStore?.name, "东湖门店")
}
}
@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: [])
}
}