feat: 接入景区空境库真实素材并统一交付入口
接通场次详情、分页、视频时长与大小,修复日期筛选越界并增加脱敏日志。统一订单与素材库交付方式弹窗,保留任务预选和上传批次,补充模拟器回归测试与接口记录。
This commit is contained in:
@@ -0,0 +1,355 @@
|
||||
import Foundation
|
||||
import XCTest
|
||||
@testable import suixinkan
|
||||
|
||||
/// 基于实测字段的隔离分页测试,覆盖非空场次、详情并发和筛选后的迟到响应。
|
||||
@MainActor
|
||||
final class ScenicMaterialsRemotePagingTests: XCTestCase {
|
||||
/// 非空列表映射真实航线与完整片段,首屏不再被占位错误阻断。
|
||||
func testNonemptySessionsLoadRealGroupsAndAllRouteOptions() async throws {
|
||||
let api = ScenicMaterialsPagingAPI(pages: [1: [.success(try fixture.page(ids: [501, 502], total: 3))]])
|
||||
let model = makeModel()
|
||||
|
||||
await model.loadRemoteSessions(api: api)
|
||||
|
||||
XCTAssertNil(model.loadError)
|
||||
XCTAssertFalse(model.isLoading)
|
||||
XCTAssertEqual(model.allGroups.map(\.id), ["501", "502"])
|
||||
XCTAssertEqual(model.routes.map(\.id), ["287", "273"])
|
||||
XCTAssertEqual(model.filteredClips.count, 2)
|
||||
XCTAssertTrue(model.filteredClips.allSatisfy { $0.isRemote && $0.remoteURL != nil })
|
||||
XCTAssertEqual(model.remoteTotalCount, 3)
|
||||
XCTAssertEqual(model.loadedRemoteSessionCount, 2)
|
||||
XCTAssertTrue(model.hasMoreRemoteSessions)
|
||||
XCTAssertEqual(Set(api.detailRequests), Set([501, 502]))
|
||||
model.toggleGroup(id: "501")
|
||||
XCTAssertEqual(model.selectedCount, 1)
|
||||
XCTAssertTrue(model.canStartDownload)
|
||||
}
|
||||
|
||||
/// 分页去重且不清空选择;列表终点由真实场次总数决定。
|
||||
func testNextPageDeduplicatesSessionsAndKeepsSelection() async throws {
|
||||
let api = ScenicMaterialsPagingAPI(pages: [
|
||||
1: [.success(try fixture.page(ids: [501, 502], total: 3))],
|
||||
2: [.success(try fixture.page(ids: [502, 503], total: 3, page: 2))],
|
||||
])
|
||||
let model = makeModel()
|
||||
await model.loadRemoteSessions(api: api)
|
||||
model.toggleGroup(id: "501")
|
||||
let selected = model.selectedIDs
|
||||
|
||||
await model.loadMoreRemoteSessions(api: api)
|
||||
|
||||
XCTAssertNil(model.loadMoreError)
|
||||
XCTAssertFalse(model.isLoadingMore)
|
||||
XCTAssertEqual(model.allGroups.map(\.id), ["501", "502", "503"])
|
||||
XCTAssertEqual(model.selectedIDs, selected)
|
||||
XCTAssertEqual(api.detailRequests.filter { $0 == 502 }.count, 1)
|
||||
XCTAssertEqual(api.queries.map(\.page), [1, 2])
|
||||
XCTAssertFalse(model.hasMoreRemoteSessions)
|
||||
await model.loadMoreRemoteSessions(api: api)
|
||||
XCTAssertEqual(api.queries.count, 2)
|
||||
}
|
||||
|
||||
/// 下一页详情未完成时不能启动下载,完成后仍可下载首屏保留的选择。
|
||||
func testDownloadCannotStartWhileNextPageDetailsAreLoading() async throws {
|
||||
let api = ScenicMaterialsPagingAPI(pages: [
|
||||
1: [.success(try fixture.page(ids: [501], total: 2))],
|
||||
2: [.success(try fixture.page(ids: [502], total: 2, page: 2))],
|
||||
])
|
||||
api.heldDetailIDs = [502]
|
||||
let pendingDetail = expectation(description: "下一页详情挂起")
|
||||
api.onDetailRequest = { if $0 == 502 { pendingDetail.fulfill() } }
|
||||
let model = makeModel()
|
||||
await model.loadRemoteSessions(api: api)
|
||||
model.toggleGroup(id: "501")
|
||||
XCTAssertTrue(model.canStartDownload)
|
||||
let selected = model.selectedIDs
|
||||
let task = Task { await model.loadMoreRemoteSessions(api: api) }
|
||||
await fulfillment(of: [pendingDetail], timeout: 1)
|
||||
|
||||
XCTAssertTrue(model.isLoadingMore)
|
||||
XCTAssertFalse(model.canStartDownload)
|
||||
XCTAssertFalse(model.startDownload())
|
||||
XCTAssertNil(model.batchID)
|
||||
XCTAssertEqual(model.phase, .idle)
|
||||
XCTAssertEqual(model.selectedIDs, selected)
|
||||
|
||||
api.releaseHeldDetails()
|
||||
await task.value
|
||||
|
||||
XCTAssertFalse(model.isLoadingMore)
|
||||
XCTAssertTrue(model.canStartDownload)
|
||||
XCTAssertEqual(model.selectedIDs, selected)
|
||||
XCTAssertTrue(model.startDownload())
|
||||
XCTAssertEqual(Set(model.batchItemIDs), selected)
|
||||
}
|
||||
|
||||
/// 第二页失败保留首屏和选择,重试同一页而不是跳页。
|
||||
func testPageFailureRetainsContentAndRetriesSamePage() async throws {
|
||||
let api = ScenicMaterialsPagingAPI(pages: [
|
||||
1: [.success(try fixture.page(ids: [501], total: 2))],
|
||||
2: [.failure(.networkFailed("离线")), .success(try fixture.page(ids: [502], total: 2, page: 2))],
|
||||
])
|
||||
let model = makeModel()
|
||||
await model.loadRemoteSessions(api: api)
|
||||
model.toggleGroup(id: "501")
|
||||
|
||||
await model.loadMoreRemoteSessions(api: api)
|
||||
|
||||
XCTAssertEqual(model.allGroups.map(\.id), ["501"])
|
||||
XCTAssertEqual(model.selectedCount, 1)
|
||||
XCTAssertNotNil(model.loadMoreError)
|
||||
XCTAssertNil(model.loadError)
|
||||
XCTAssertTrue(model.hasMoreRemoteSessions)
|
||||
|
||||
await model.loadMoreRemoteSessions(api: api)
|
||||
|
||||
XCTAssertEqual(api.queries.map(\.page), [1, 2, 2])
|
||||
XCTAssertNil(model.loadMoreError)
|
||||
XCTAssertEqual(model.allGroups.count, 2)
|
||||
XCTAssertFalse(model.hasMoreRemoteSessions)
|
||||
}
|
||||
|
||||
/// 详情失败不把不完整场次当作成功空组,重新加载能恢复。
|
||||
func testDetailFailureIsRetryableWithoutPartialPageOrDemoData() async throws {
|
||||
let response = try fixture.page(ids: [501, 502], total: 2)
|
||||
let api = ScenicMaterialsPagingAPI(pages: [1: [.success(response), .success(response)]])
|
||||
api.failingDetailIDs = [502]
|
||||
let model = makeModel()
|
||||
|
||||
await model.loadRemoteSessions(api: api)
|
||||
|
||||
XCTAssertNotNil(model.loadError)
|
||||
XCTAssertEqual(model.remoteSessions?.total, 2)
|
||||
XCTAssertTrue(model.allGroups.isEmpty)
|
||||
XCTAssertFalse(model.hasMoreRemoteSessions)
|
||||
api.failingDetailIDs = []
|
||||
|
||||
await model.loadRemoteSessions(api: api)
|
||||
|
||||
XCTAssertNil(model.loadError)
|
||||
XCTAssertEqual(model.allGroups.count, 2)
|
||||
}
|
||||
|
||||
/// 切换航线和日期作废旧页码及选择,下一页仍携带完整筛选。
|
||||
func testFiltersResetPaginationAndNextPageKeepsExactQuery() async throws {
|
||||
let api = ScenicMaterialsPagingAPI(pages: [
|
||||
1: [.success(try fixture.page(ids: [501], total: 2)),
|
||||
.success(try fixture.page(ids: [601], total: 2, routeID: 273))],
|
||||
2: [.success(try fixture.page(ids: [602], total: 2, page: 2, routeID: 273))],
|
||||
])
|
||||
api.routeIDBySession = [601: 273, 602: 273]
|
||||
let model = makeModel()
|
||||
await model.loadRemoteSessions(api: api)
|
||||
model.toggleGroup(id: "501")
|
||||
|
||||
XCTAssertTrue(model.setRoute(id: "273"))
|
||||
XCTAssertTrue(model.setDateRange(start: fixture.date(2026, 10, 28), end: fixture.date(2026, 10, 30)))
|
||||
XCTAssertTrue(model.selectedIDs.isEmpty)
|
||||
XCTAssertTrue(model.allGroups.isEmpty)
|
||||
XCTAssertFalse(model.hasMoreRemoteSessions)
|
||||
await model.loadRemoteSessions(api: api)
|
||||
await model.loadMoreRemoteSessions(api: api)
|
||||
|
||||
XCTAssertEqual(api.queries.map(\.page), [1, 1, 2])
|
||||
for query in api.queries.dropFirst() {
|
||||
XCTAssertEqual(query.scenicID, 271)
|
||||
XCTAssertEqual(query.airLineID, 273)
|
||||
XCTAssertEqual(query.dateFrom, "2026-10-28")
|
||||
XCTAssertEqual(query.dateTo, "2026-10-30")
|
||||
XCTAssertEqual(query.pageSize, 20)
|
||||
XCTAssertEqual(query.retentionDays, 90)
|
||||
}
|
||||
XCTAssertEqual(model.filteredGroups.map(\.id), ["601", "602"])
|
||||
}
|
||||
|
||||
/// 10 月 30 日的实测 90 天窗口包含 8 月 1 日,客户端不能再次裁掉边界素材。
|
||||
func testServerRetentionBoundaryIsVisibleAndSelectable() async throws {
|
||||
let api = ScenicMaterialsPagingAPI(pages: [
|
||||
1: [.success(try fixture.page(ids: [501], total: 1, capturedAt: "2026-08-01 12:00:00"))],
|
||||
])
|
||||
let model = makeModel()
|
||||
|
||||
await model.loadRemoteSessions(api: api)
|
||||
|
||||
XCTAssertEqual(model.minimumDate, fixture.date(2026, 8, 1))
|
||||
XCTAssertEqual(model.filteredGroups.map(\.id), ["501"])
|
||||
XCTAssertTrue(model.hasAnyMaterials)
|
||||
model.toggleGroup(id: "501")
|
||||
XCTAssertEqual(model.selectedCount, 1)
|
||||
XCTAssertTrue(model.setDateRange(start: fixture.date(2026, 8, 1), end: fixture.date(2026, 8, 1)))
|
||||
}
|
||||
|
||||
/// 开始四个详情请求后等待回执,不向服务端一次性并发发出整页请求。
|
||||
func testDetailsHaveAtMostFourConcurrentRequests() async throws {
|
||||
let ids = Array(501...508)
|
||||
let api = ScenicMaterialsPagingAPI(pages: [1: [.success(try fixture.page(ids: ids, total: ids.count))]])
|
||||
api.heldDetailIDs = Set(ids)
|
||||
let firstBatch = expectation(description: "首批四个详情已发出")
|
||||
let secondBatch = expectation(description: "第二批四个详情已发出")
|
||||
api.onDetailRequest = { _ in
|
||||
if api.detailRequests.count == 4 { firstBatch.fulfill() }
|
||||
if api.detailRequests.count == 8 { secondBatch.fulfill() }
|
||||
}
|
||||
let model = makeModel()
|
||||
let task = Task { await model.loadRemoteSessions(api: api) }
|
||||
await fulfillment(of: [firstBatch], timeout: 1)
|
||||
|
||||
XCTAssertEqual(api.detailRequests.count, 4)
|
||||
XCTAssertEqual(api.maximumConcurrentDetails, 4)
|
||||
api.releaseHeldDetails()
|
||||
await fulfillment(of: [secondBatch], timeout: 1)
|
||||
XCTAssertEqual(api.maximumConcurrentDetails, 4)
|
||||
api.releaseHeldDetails()
|
||||
await task.value
|
||||
|
||||
XCTAssertNil(model.loadError)
|
||||
XCTAssertEqual(model.allGroups.map(\.id), ids.map(String.init))
|
||||
}
|
||||
|
||||
/// 刷新期间上一页详情即使晚返回,也不能覆盖已完成的新查询结果。
|
||||
func testRefreshRejectsLateDetailsAndResetsPagination() async throws {
|
||||
let api = ScenicMaterialsPagingAPI(pages: [
|
||||
1: [.success(try fixture.page(ids: [501], total: 2)), .success(try fixture.page(ids: [601], total: 1))],
|
||||
])
|
||||
api.heldDetailIDs = [501]
|
||||
let oldDetail = expectation(description: "旧详情挂起")
|
||||
api.onDetailRequest = { if $0 == 501 { oldDetail.fulfill() } }
|
||||
let model = makeModel()
|
||||
let oldTask = Task { await model.loadRemoteSessions(api: api) }
|
||||
await fulfillment(of: [oldDetail], timeout: 1)
|
||||
|
||||
await model.loadRemoteSessions(api: api)
|
||||
XCTAssertEqual(model.allGroups.map(\.id), ["601"])
|
||||
api.releaseHeldDetails()
|
||||
await oldTask.value
|
||||
|
||||
XCTAssertEqual(model.allGroups.map(\.id), ["601"])
|
||||
XCTAssertEqual(model.remoteTotalCount, 1)
|
||||
XCTAssertFalse(model.hasMoreRemoteSessions)
|
||||
XCTAssertFalse(model.isLoading)
|
||||
XCTAssertNil(model.loadError)
|
||||
}
|
||||
|
||||
/// 无新数据却仍声称有下一页时保留可重试错误,不静默显示“全部加载完成”。
|
||||
func testRepeatedPageDoesNotAdvanceOrClaimCompletion() async throws {
|
||||
let api = ScenicMaterialsPagingAPI(pages: [
|
||||
1: [.success(try fixture.page(ids: [501], total: 2))],
|
||||
2: [.success(try fixture.page(ids: [501], total: 2, page: 2))],
|
||||
])
|
||||
let model = makeModel()
|
||||
await model.loadRemoteSessions(api: api)
|
||||
|
||||
await model.loadMoreRemoteSessions(api: api)
|
||||
|
||||
XCTAssertNotNil(model.loadMoreError)
|
||||
XCTAssertTrue(model.hasMoreRemoteSessions)
|
||||
XCTAssertEqual(model.allGroups.map(\.id), ["501"])
|
||||
XCTAssertEqual(api.detailRequests, [501])
|
||||
}
|
||||
|
||||
private let fixture = ScenicMaterialsPagingFixture()
|
||||
|
||||
/// 正式上下文固定为本次接口联调的景区及上海业务日期,不访问真实登录态。
|
||||
private func makeModel() -> ScenicMaterialsViewModel {
|
||||
ScenicMaterialsViewModel(context: ScenicMaterialContext(userID: "paging-test", scenicID: "271", scenicName: "测试景区"),
|
||||
now: fixture.date(2026, 10, 30), calendar: fixture.calendar, isRemote: true)
|
||||
}
|
||||
}
|
||||
|
||||
/// 按已观察字段构造可变页码、场次和详情,仅作为隔离测试数据。
|
||||
private struct ScenicMaterialsPagingFixture {
|
||||
var calendar: Calendar {
|
||||
var value = Calendar(identifier: .gregorian)
|
||||
value.timeZone = TimeZone(identifier: "Asia/Shanghai")!
|
||||
return value
|
||||
}
|
||||
|
||||
/// 生成固定业务日期。
|
||||
func date(_ year: Int, _ month: Int, _ day: Int) -> Date {
|
||||
calendar.date(from: DateComponents(year: year, month: month, day: day))!
|
||||
}
|
||||
|
||||
/// 创建真实格式的场次页,筛选回显由 API 层既有测试单独验证。
|
||||
func page(ids: [Int], total: Int, page: Int = 1, routeID: Int = 287,
|
||||
capturedAt: String = "2026-10-29 11:01:56") throws -> ScenicMaterialsSessionsResponse {
|
||||
let payload: [String: Any] = [
|
||||
"list": ids.map { ["scenic_media_id": $0, "scenic_id": 271, "air_line_id": routeID,
|
||||
"route_label": "航线\(routeID)", "log_stime": capturedAt, "material_count": 1] },
|
||||
"groups": [], "total": total, "page": page, "page_size": 20, "retention_days": 90,
|
||||
"retention_tips": "素材保留 90 天,请及时下载",
|
||||
"filter": ["air_line_id": 0, "date_from": "", "date_to": ""],
|
||||
"filter_options": ["air_lines": [287, 273].map { ["air_line_id": $0, "route_label": "航线\($0)"] }],
|
||||
]
|
||||
return try JSONDecoder().decode(ScenicMaterialsSessionsResponse.self, from: JSONSerialization.data(withJSONObject: payload))
|
||||
}
|
||||
|
||||
/// 完整详情不包含网络资源,URL 仅用于检查真实片段的传递。
|
||||
func detail(id: Int, routeID: Int = 287) throws -> ScenicMaterialsSessionDetailResponse {
|
||||
let payload: [String: Any] = [
|
||||
"session": ["scenic_media_id": id, "scenic_id": 271, "air_line_id": routeID],
|
||||
"material_count": 1,
|
||||
"materials": [["id": id + 10000, "scenic_media_id": id, "clip_no": "01", "file_label": "片段1",
|
||||
"file_type": 2, "size_bytes": 123456, "selectable": true, "downloadable": true,
|
||||
"oss_url": "https://example.com/\(id).mp4", "cover_url": "https://example.com/\(id).jpg"]],
|
||||
]
|
||||
return try JSONDecoder().decode(ScenicMaterialsSessionDetailResponse.self, from: JSONSerialization.data(withJSONObject: payload))
|
||||
}
|
||||
}
|
||||
|
||||
/// 内存服务可控制分页失败和详情返回时机,不使用 URLSession 或真实接口。
|
||||
@MainActor
|
||||
private final class ScenicMaterialsPagingAPI: ScenicMaterialsServing {
|
||||
var pages: [Int: [Result<ScenicMaterialsSessionsResponse, APIError>]]
|
||||
var routeIDBySession: [Int: Int] = [:]
|
||||
var failingDetailIDs: Set<Int> = []
|
||||
var heldDetailIDs: Set<Int> = []
|
||||
var onDetailRequest: ((Int) -> Void)?
|
||||
private(set) var queries: [ScenicMaterialsQuery] = []
|
||||
private(set) var detailRequests: [Int] = []
|
||||
private(set) var maximumConcurrentDetails = 0
|
||||
private var activeDetails = 0
|
||||
private var held: [Int: CheckedContinuation<ScenicMaterialsSessionDetailResponse, Error>] = [:]
|
||||
private let fixture = ScenicMaterialsPagingFixture()
|
||||
|
||||
/// 按页码注入独立响应队列,重试消耗同一页下一个响应。
|
||||
init(pages: [Int: [Result<ScenicMaterialsSessionsResponse, APIError>]]) { self.pages = pages }
|
||||
|
||||
/// 记录查询并返回对应页的预设结果。
|
||||
func sessions(query: ScenicMaterialsQuery) async throws -> ScenicMaterialsSessionsResponse {
|
||||
queries.append(query)
|
||||
guard var responses = pages[query.page], !responses.isEmpty else {
|
||||
throw APIError.networkFailed("测试未提供该页")
|
||||
}
|
||||
let result = responses.removeFirst()
|
||||
pages[query.page] = responses
|
||||
return try result.get()
|
||||
}
|
||||
|
||||
/// 详情按场次生成,可挂起以断言并发上限与异步结果隔离。
|
||||
func sessionDetail(scenicMediaID: Int) async throws -> ScenicMaterialsSessionDetailResponse {
|
||||
detailRequests.append(scenicMediaID)
|
||||
activeDetails += 1
|
||||
maximumConcurrentDetails = max(maximumConcurrentDetails, activeDetails)
|
||||
defer { activeDetails -= 1 }
|
||||
if failingDetailIDs.contains(scenicMediaID) { throw APIError.networkFailed("详情暂不可用") }
|
||||
if heldDetailIDs.contains(scenicMediaID) {
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
held[scenicMediaID] = continuation
|
||||
onDetailRequest?(scenicMediaID)
|
||||
}
|
||||
}
|
||||
onDetailRequest?(scenicMediaID)
|
||||
return try fixture.detail(id: scenicMediaID, routeID: routeIDBySession[scenicMediaID] ?? 287)
|
||||
}
|
||||
|
||||
/// 释放当前挂起的一批详情,保留后续场次的挂起规则。
|
||||
func releaseHeldDetails() {
|
||||
let pending = held
|
||||
held.removeAll()
|
||||
for (id, continuation) in pending {
|
||||
continuation.resume(with: Result { try fixture.detail(id: id, routeID: routeIDBySession[id] ?? 287) })
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user