feat: 自动刷新订单交付确认状态

This commit is contained in:
2026-11-04 10:33:11 +08:00
parent 082ecf96f2
commit 7a63ea3432
5 changed files with 212 additions and 36 deletions
@@ -154,7 +154,83 @@ final class DroneDockConfirmationViewTests: XCTestCase {
XCTAssertNil(controller.viewModel.code)
}
private func makeController(responses: [Data], images: DroneDockPaymentImageLoaderStub) throws -> (DroneDockConfirmationViewController, MockURLSession) {
/// 出码后每轮只读详情,自动失败保留二维码,读取完成回执后停止。
func testAutomaticStatusChecksUseFiveSecondsAndStopAfterReceipt() async throws {
let clock = DroneDockPollTestClock()
let images = DroneDockPaymentImageLoaderStub()
let failure = try TestJSON.errorEnvelope(code: 160001, msg: "测试自动查询失败")
let (controller, session) = try makeController(responses: [
detail(), tasks(), detail(), code(), failure, detail(completed: true, confirmStatus: 2)
], images: images, clock: clock)
let window = show(controller)
defer { close(window) }
try await settle(window) { controller.viewModel.hasLoadedTasks }
XCTAssertTrue(clock.intervals.isEmpty)
try await generateCode(controller, window: window)
try await settle(window) { clock.pendingCount == 1 }
XCTAssertEqual(clock.intervals, [5_000_000_000])
XCTAssertEqual(session.requests.count, 4)
XCTAssertEqual(controller.viewModel.order.confirmStatus, 0)
clock.advance()
try await settle(window) { session.requests.count == 5 && clock.pendingCount == 1 }
XCTAssertNil(controller.viewModel.errorMessage)
XCTAssertNotNil(controller.viewModel.code)
XCTAssertEqual(images.requests.count, 1)
XCTAssertTrue(try button(controller.view, "drone.confirmation.primary").isEnabled)
clock.advance()
try await settle(window) { controller.viewModel.isCompleted && self.texts(controller.view).contains("游客已确认收货") }
XCTAssertNil(controller.viewModel.code)
XCTAssertEqual(clock.pendingCount, 0)
XCTAssertEqual(clock.intervals, [5_000_000_000, 5_000_000_000])
XCTAssertEqual(session.requests.map { $0.url?.lastPathComponent }, ["detail", "tasks", "detail", "confirm-code", "detail", "detail"])
}
/// 已有待确认订单即使本地无码也会查询;后台、离页和交付弹窗暂停,恢复时立即读取。
func testPendingOrderResumesStatusCheckOnlyWhenVisibleAndActive() async throws {
let clock = DroneDockPollTestClock()
let pending = try detail(confirmStatus: 1)
let (controller, session) = try makeController(responses: [
pending, tasks(), pending, pending, detail(completed: true, confirmStatus: 2)
], images: DroneDockPaymentImageLoaderStub(), clock: clock)
let window = show(controller)
defer { close(window) }
try await settle(window) { clock.pendingCount == 1 }
XCTAssertNil(controller.viewModel.code)
XCTAssertEqual(session.requests.count, 2)
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
try await settle(window) { clock.pendingCount == 0 }
let navigation = try XCTUnwrap(controller.navigationController)
navigation.pushViewController(UIViewController(), animated: false)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
try await settle(window) { navigation.topViewController !== controller }
XCTAssertEqual(session.requests.count, 2)
XCTAssertEqual(clock.pendingCount, 0)
navigation.popViewController(animated: false)
try await settle(window) { session.requests.count == 3 && clock.pendingCount == 1 }
try button(controller.view, "drone.confirmation.back").sendActions(for: .touchUpInside)
try await settle(window) { controller.presentedViewController != nil && clock.pendingCount == 0 }
XCTAssertEqual(session.requests.count, 3)
let sheet = try XCTUnwrap(controller.presentedViewController as? UINavigationController)
let methods = try XCTUnwrap(sheet.topViewController)
try button(methods.view, "drone.delivery.upload.close").sendActions(for: .touchUpInside)
try await settle(window) { session.requests.count == 4 && clock.pendingCount == 1 }
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
try await settle(window) { clock.pendingCount == 0 }
XCTAssertEqual(session.requests.count, 4)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
try await settle(window) { controller.viewModel.isCompleted }
XCTAssertEqual(session.requests.map { $0.url?.lastPathComponent }, ["detail", "tasks", "detail", "detail", "detail"])
XCTAssertTrue(session.requests.allSatisfy { $0.httpMethod == "GET" })
XCTAssertEqual(clock.pendingCount, 0)
}
private func makeController(responses: [Data], images: DroneDockPaymentImageLoaderStub,
clock: DroneDockPollTestClock? = nil) throws -> (DroneDockConfirmationViewController, MockURLSession) {
let context = DroneDockHomeContext(userID: "confirm-ui-user", accountType: "store_user", scenicID: 128,
scenicName: "测试景区", storeID: 37, photographerName: "测试工作人员", token: "confirm-ui-token")
let order = try JSONDecoder().decode(DroneDockHomeOrder.self, from: JSONSerialization.data(withJSONObject: ["order_number": number]))
@@ -163,7 +239,12 @@ final class DroneDockConfirmationViewTests: XCTestCase {
client.bindAuthTokenProvider { "confirm-ui-token" }
let model = DroneDockConfirmationViewModel(context: context, order: order,
service: DroneDockConfirmationAPI(client: client), currentContext: { context })
return (DroneDockConfirmationViewController(viewModel: model, imageLoader: images, allowsRemoteMedia: false), session)
let controller = DroneDockConfirmationViewController(viewModel: model, imageLoader: images, allowsRemoteMedia: false,
statusPollDelay: { interval in
if let clock { try await clock.wait(interval) }
else { try await Task.sleep(nanoseconds: interval) }
})
return (controller, session)
}
private func detail(completed: Bool = false, confirmStatus: Int = 0) throws -> Data {