fix: 自动开启机巢图传并修复日志崩溃与订阅重连

This commit is contained in:
2026-11-03 10:18:23 +08:00
parent 203799b2f4
commit 5d48e37cd7
12 changed files with 301 additions and 36 deletions
+99 -13
View File
@@ -8,10 +8,11 @@ final class DroneDockRTCPlaybackTests: XCTestCase {
/// 成功获取凭证只进入连接中,必须等首帧回调才能展示实时画面。
func testCredentialReceiptWaitsForFirstFrame() async throws {
let (model, service) = try makeModel()
XCTAssertFalse(model.loadsAutomatically)
XCTAssertEqual(model.actionTitle, "开启实时图传")
XCTAssertTrue(model.loadsAutomatically)
XCTAssertNil(model.actionTitle)
await model.refresh()
XCTAssertEqual(model.state, .connecting)
XCTAssertFalse(model.loadsAutomatically)
XCTAssertNil(model.playbackURL)
let id = try XCTUnwrap(model.playbackSessionID)
model.receivePlaybackEvent(.firstFrame, sessionID: id)
@@ -132,14 +133,15 @@ final class DroneDockRTCPlaybackTests: XCTestCase {
player.stop()
}
/// 状态轮询、页面进场和回前台均不自动调用 live。
func testTaskPageRequiresExplicitTapAndDoesNotRestartOnForeground() async throws {
let (live, service) = try makeModel(response: DroneDockRTCFixture.response(play: ["url_type": "agora"]))
/// 页面进场与回前台自动开启,状态刷新不重复启动;播放器使用无网络替身。
func testTaskPageAutomaticallyStartsAndResumesWithoutPollingLive() async throws {
let (live, service) = try makeModel()
let player = RTCPlayerProbe()
let context = DroneDockShootingTaskTestData.context()
let read = DroneDockShootingTaskTestService(result: .success(try DroneDockShootingTaskTestData.response()))
let model = DroneDockShootingTaskViewModel(context: context, taskID: 501,
order: try DroneDockShootingTaskTestData.order(), service: read, currentContext: { context })
let controller = DroneDockShootingTaskViewController(viewModel: model, liveViewModel: live,
let controller = DroneDockShootingTaskViewController(viewModel: model, liveViewModel: live, livePlayer: player,
shootingPollDelay: { _ in try await Task.sleep(for: .milliseconds(40)) })
let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first
let previous = scene?.windows.first { $0.isKeyWindow }
@@ -148,24 +150,86 @@ final class DroneDockRTCPlaybackTests: XCTestCase {
window.makeKeyAndVisible()
defer { window.isHidden = true; window.rootViewController = nil; previous?.makeKeyAndVisible() }
try await Task.sleep(for: .milliseconds(250))
XCTAssertEqual(service.count, 0)
XCTAssertGreaterThan(read.requests.count, 1, "已发生 shooting 轮询,live 仍未被调用")
XCTAssertEqual(service.count, 1)
XCTAssertEqual(player.starts, 1)
XCTAssertGreaterThan(read.requests.count, 1, "shooting 轮询不能重复请求 live")
XCTAssertTrue(read.requests.allSatisfy { !$0.liveBattery })
let button = try XCTUnwrap(descendants(controller.view).first {
$0.accessibilityIdentifier == "drone.task.remote.live.request"
} as? UIButton)
button.sendActions(for: .touchUpInside)
for _ in 0..<100 where service.count == 0 { await Task.yield() }
XCTAssertTrue(button.isHidden, "自动连接时没有手动开启按钮")
let oldEvent = try XCTUnwrap(player.event)
oldEvent(.firstFrame)
try await wait { live.state == .playing }
controller.viewDidAppear(false)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
try await Task.sleep(for: .milliseconds(80))
XCTAssertEqual(service.count, 1)
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
try await Task.sleep(for: .milliseconds(100))
XCTAssertEqual(service.count, 1)
XCTAssertEqual(player.stops, 1)
XCTAssertNil(live.rtcConfiguration)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
try await wait { player.starts == 2 }
XCTAssertEqual(service.count, 2)
oldEvent(.firstFrame)
try await Task.sleep(for: .milliseconds(20))
XCTAssertEqual(live.state, .connecting, "旧播放器事件不能激活新会话")
controller.viewWillDisappear(false)
XCTAssertEqual(player.stops, 2)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
try await Task.sleep(for: .milliseconds(80))
XCTAssertEqual(service.count, 2, "离页不能自动请求图传")
controller.viewDidAppear(false)
try await wait { player.starts == 3 }
XCTAssertEqual(service.count, 3)
XCTAssertFalse(model.canStartShooting)
XCTAssertFalse(model.canResummon)
}
/// 自动请求失败后不随状态刷新或重复出现回调重试;保留显式重试入口。
func testAutomaticLiveFailureDoesNotLoopAndAllowsRetry() async throws {
let (live, service) = try makeModel()
service.error = URLError(.timedOut)
let (controller, window) = try taskPage(live: live)
defer { window.isHidden = true; window.rootViewController = nil }
try await wait { if case .failed = live.state { return true }; return false }
controller.viewDidAppear(false)
try await Task.sleep(for: .milliseconds(120))
XCTAssertEqual(service.count, 1)
XCTAssertFalse(live.loadsAutomatically)
let button = try XCTUnwrap(descendants(controller.view).first {
$0.accessibilityIdentifier == "drone.task.remote.live.request"
} as? UIButton)
XCTAssertEqual(button.title(for: .normal), "重试")
XCTAssertFalse(button.isHidden)
button.sendActions(for: .touchUpInside)
try await wait { service.count == 2 && !live.isLoading }
XCTAssertEqual(service.count, 2)
}
/// 自动请求期间防重入;离页后返回的旧凭证不能启动播放器。
func testAutomaticInFlightRequestIsExclusiveAndLateResponseIsDiscarded() async throws {
let (live, service) = try makeModel()
service.suspend = true
let player = RTCPlayerProbe()
let (controller, window) = try taskPage(live: live, player: player)
defer { service.finish(); window.isHidden = true; window.rootViewController = nil }
try await wait { service.count == 1 }
controller.viewDidAppear(false)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
try await Task.sleep(for: .milliseconds(30))
XCTAssertEqual(service.count, 1)
controller.viewWillDisappear(false)
service.finish()
try await Task.sleep(for: .milliseconds(30))
XCTAssertNil(live.rtcConfiguration)
XCTAssertEqual(player.starts, 0)
service.suspend = false
controller.viewDidAppear(false)
try await wait { player.starts == 1 }
XCTAssertEqual(service.count, 2)
}
/// 按用户要求完整记录 live 原始回执,SDK 播放流程不受日志调整影响。
func testLiveAPIResponseLogContainsOriginalRTCParameters() async throws {
let payload: [String: Any] = ["order_flight_task_id": 501, "play": ["url": "token=TEST-RTC-TOKEN"]]
@@ -186,6 +250,28 @@ final class DroneDockRTCPlaybackTests: XCTestCase {
allowsDevicePlayback: true, now: { DroneDockRTCFixture.now }, currentContext: { context }), service)
}
private func taskPage(live: DroneDockLiveViewModel, player: RTCPlayerProbe? = nil) throws -> (DroneDockShootingTaskViewController, UIWindow) {
let context = DroneDockShootingTaskTestData.context()
let read = DroneDockShootingTaskTestService(result: .success(try DroneDockShootingTaskTestData.response()))
let model = try DroneDockShootingTaskViewModel(context: context, taskID: 501,
order: DroneDockShootingTaskTestData.order(), service: read, currentContext: { context })
let controller = DroneDockShootingTaskViewController(viewModel: model, liveViewModel: live, livePlayer: player ?? RTCPlayerProbe(),
shootingPollDelay: { _ in try await Task.sleep(for: .milliseconds(40)) })
let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first
let window = scene.map { UIWindow(windowScene: $0) } ?? UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UINavigationController(rootViewController: controller)
window.makeKeyAndVisible()
return (controller, window)
}
private func wait(_ condition: () -> Bool) async throws {
for _ in 0..<200 {
if condition() { return }
try await Task.sleep(for: .milliseconds(5))
}
XCTFail("模拟图传生命周期未按预期完成")
}
private func descendants(_ root: UIView) -> [UIView] { [root] + root.subviews.flatMap(descendants) }
}