适配上传中订单并完善机巢操作与订阅兜底
支持订单状态 25 和持续拍摄、交付确认;安全指令点击后直接提交;鉴权超时保留 HTTP 兜底并退避换票重连。补充对应隔离回归测试。
This commit is contained in:
@@ -54,6 +54,55 @@ final class DroneDockShootingRealtimeTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/// 鉴权超时由正式失败回执的明确 hint 区分,不猜测错误码或要求失败包带任务 ID。
|
||||
func testFormalAuthenticationTimeoutUsesTrimmedHintForDifferentBusinessCodes() throws {
|
||||
for code in [100000, 100050, 100099] {
|
||||
let message = try DroneDockShootingSocketMessage.decode(
|
||||
RealtimeTestData.authenticationTimeout(code: code, hint: " 鉴权超时,请稍后重试 \n"))
|
||||
guard case .subscriptionTimedOut(let reason) = message else {
|
||||
return XCTFail("正式鉴权超时应保留 HTTP 兜底,不按 \(code) 误判永久无权限")
|
||||
}
|
||||
XCTAssertEqual(reason, "鉴权超时,请稍后重试")
|
||||
}
|
||||
}
|
||||
|
||||
/// 就绪/入站提示及非明确失败字段不能冒充正式鉴权超时。
|
||||
func testTimeoutHintOnDiagnosticOrMalformedAckDoesNotBecomeFormalTimeout() throws {
|
||||
let variants: [[String: Any]] = [
|
||||
["type": 310, "ack": "ready", "subscribed": false],
|
||||
["type": 310, "ack": "received", "subscribed": false],
|
||||
["type": 311, "ack": "subscribe", "subscribed": false],
|
||||
["type": 310, "ack": "subscribe", "subscribed": true, "order_flight_task_id": 501]
|
||||
]
|
||||
for variant in variants {
|
||||
let data = try JSONSerialization.data(withJSONObject: ["code": 100000, "data":
|
||||
variant.merging(["hint": "鉴权超时"]) { old, _ in old }])
|
||||
if case .subscriptionTimedOut = try DroneDockShootingSocketMessage.decode(data) {
|
||||
XCTFail("只有 type310 + subscribe + JSON false 能识别正式超时:\(variant)")
|
||||
}
|
||||
}
|
||||
for subscribed in [0, 1] {
|
||||
let data = try JSONSerialization.data(withJSONObject: ["code": 100000, "data": [
|
||||
"type": 310, "ack": "subscribe", "subscribed": subscribed, "hint": "鉴权超时"
|
||||
]])
|
||||
XCTAssertThrowsError(try DroneDockShootingSocketMessage.decode(data), "数值不能替代 JSON false")
|
||||
}
|
||||
}
|
||||
|
||||
/// 入站回执可能携带 subscribed=false,只有正式 subscribe 才能声明订阅被拒绝。
|
||||
func testFailedDiagnosticAckCannotBecomeFormalSubscriptionRejection() throws {
|
||||
for ack in ["ready", "received", "subscribe_meta"] {
|
||||
let data = try JSONSerialization.data(withJSONObject: ["code": 100099, "msg": "暂不可用", "data": [
|
||||
"type": 310, "ack": ack, "subscribed": false, "hint": "鉴权超时"
|
||||
]])
|
||||
guard case .rejected(let code, let message) = try DroneDockShootingSocketMessage.decode(data) else {
|
||||
return XCTFail("非正式失败回执不能按权限永久停止读取")
|
||||
}
|
||||
XCTAssertEqual(code, 100099)
|
||||
XCTAssertEqual(message, "暂不可用")
|
||||
}
|
||||
}
|
||||
|
||||
/// 下行就绪和订阅入站回执只提供诊断,不能冒充正式订阅结果或吞掉错误码。
|
||||
func testReadyAndReceivedAreDiagnosticMessagesOnly() throws {
|
||||
for ack in ["ready", "received"] {
|
||||
@@ -201,6 +250,55 @@ final class DroneDockShootingRealtimeTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/// 服务端三秒鉴权失败立刻结束本轮等待;保留只读兜底并换票,旧连接不能恢复实时态。
|
||||
func testAuthenticationTimeoutImmediatelyFallsBackAndReconnectsWithoutAccessDenial() async throws {
|
||||
for code in [100000, 100050, 100099] {
|
||||
let harness = RealtimeHarness()
|
||||
let model = try model()
|
||||
await model.refresh()
|
||||
var acceptedSnapshots = 0
|
||||
var states: [DroneDockShootingRealtimeSession.State] = []
|
||||
harness.session.onSnapshot = { _ in acceptedSnapshots += 1; return .applied }
|
||||
harness.session.onConnectionState = { state in
|
||||
states.append(state)
|
||||
if case .denied(let reason) = state { model.stopReading(message: reason) }
|
||||
}
|
||||
harness.session.start()
|
||||
defer { harness.session.stop() }
|
||||
await settle { harness.sockets.count == 1 && harness.sockets[0].sent.count == 1 && harness.clock.has(12) }
|
||||
let first = harness.sockets[0]
|
||||
first.emit(try RealtimeTestData.transportAck("received"))
|
||||
harness.time = 3
|
||||
first.emit(try RealtimeTestData.authenticationTimeout(code: code))
|
||||
await settle { harness.clock.has(1) && !harness.clock.has(12) }
|
||||
XCTAssertEqual(harness.session.state, .fallback("鉴权超时,请稍后重试"))
|
||||
XCTAssertEqual(harness.clock.waitCount(seconds: 12), 1)
|
||||
XCTAssertEqual(first.closeCount, 1)
|
||||
XCTAssertTrue(model.canContinuePolling)
|
||||
XCTAssertNil(model.readAccessError)
|
||||
XCTAssertNotNil(model.response)
|
||||
XCTAssertFalse(states.contains { if case .denied = $0 { return true }; return false })
|
||||
|
||||
harness.clock.fire(seconds: 1)
|
||||
await settle { harness.sockets.count == 2 && harness.sockets[1].sent.count == 1 }
|
||||
XCTAssertEqual(harness.tickets.count, 2)
|
||||
XCTAssertTrue(harness.sockets[1].sent[0].contains("MOCK-TICKET-2"))
|
||||
first.emit(try RealtimeTestData.ack())
|
||||
first.emit(try RealtimeTestData.push(RealtimeTestData.payload()))
|
||||
for _ in 0..<10 { await Task.yield() }
|
||||
XCTAssertEqual(harness.session.state, .connecting)
|
||||
XCTAssertEqual(acceptedSnapshots, 0, "旧连接迟到正式回执及快照不能补齐新连接")
|
||||
|
||||
let second = harness.sockets[1]
|
||||
second.emit(try RealtimeTestData.ack())
|
||||
await settle { second.receiveCount >= 3 }
|
||||
XCTAssertNotEqual(harness.session.state, .live, "新连接仍需有效完整快照")
|
||||
second.emit(try RealtimeTestData.push(RealtimeTestData.payload(version: 2)))
|
||||
await settle { harness.session.state == .live }
|
||||
XCTAssertEqual(acceptedSnapshots, 1)
|
||||
}
|
||||
}
|
||||
|
||||
/// 新版无 ID 的票据失败只换票重连,不停止任务读取或误判 App 登录失效。
|
||||
func testExpiredSubscriptionFailureAckRenewsTicket() async throws {
|
||||
let harness = RealtimeHarness()
|
||||
@@ -940,6 +1038,11 @@ enum RealtimeTestData {
|
||||
if ack == "received" { body["type"] = 310 }
|
||||
return try JSONSerialization.data(withJSONObject: ["code": code, "data": body])
|
||||
}
|
||||
/// 只注入文档明确的超时 hint;错误码可变化,不能用于推断永久无权限。
|
||||
static func authenticationTimeout(code: Int, hint: String = "鉴权超时,请稍后重试") throws -> Data {
|
||||
try JSONSerialization.data(withJSONObject: ["code": code, "msg": "订阅失败",
|
||||
"data": ["type": 310, "ack": "subscribe", "subscribed": false, "hint": hint]])
|
||||
}
|
||||
static func subscriptionFailure(_ code: Int, _ message: String) throws -> Data {
|
||||
try JSONSerialization.data(withJSONObject: ["code": code, "msg": message,
|
||||
"data": ["type": 310, "ack": "subscribe", "subscribed": false]])
|
||||
|
||||
Reference in New Issue
Block a user