订阅明确无缓存时使用新发起的有效 HTTP 全量响应衔接首包,保留任务、批次和版本校验。 移除 Debug/Release 的设备指令、图传和 RTC 构建拦截,保留后端权限、身份、防重复提交及结果未知保护。 更新开发约定并补充隔离传输、飞控状态和页面回归测试。 验证:iPhone 真机 500 项相关测试通过;iOS Release 无签名编译通过。
295 lines
16 KiB
Swift
295 lines
16 KiB
Swift
import AVFoundation
|
|
import UIKit
|
|
import XCTest
|
|
@testable import suixinkan
|
|
|
|
/// 通过隔离模拟入口验证重新召唤与图传区域交互,不连接后端或无人机。
|
|
@MainActor
|
|
final class DroneDockResummonLiveViewTests: XCTestCase {
|
|
private weak var previousKeyWindow: UIWindow?
|
|
|
|
/// 双击重召唤只提交一次原任务,回查更新页面,固定区域与旧读数保持不变。
|
|
func testResummonSuccessUsesSameTaskOnceAndPreservesFixedLayout() async throws {
|
|
let controller = try makeController(kind: "resummon", result: "success")
|
|
let window = makeWindow(controller)
|
|
defer { close(window) }
|
|
let model = controller.viewModel
|
|
try await settle(window) { model.canResummon }
|
|
let fixedIDs = ["top", "stateContainer", "safety"]
|
|
let frames = try fixedIDs.map { try XCTUnwrap(find(controller.view, suffix: $0)).frame }
|
|
let oldTitle = model.presentation.title
|
|
let oldBatch = try XCTUnwrap(model.response?.executionBatch)
|
|
let oldBattery = find(controller.view, suffix: "metric.battery")?.accessibilityValue
|
|
let button = try XCTUnwrap(find(controller.view, suffix: "failureRetry") as? UIButton)
|
|
capture(window, name: "重新召唤-失败任务-本地模拟")
|
|
|
|
button.sendActions(for: .touchUpInside)
|
|
button.sendActions(for: .touchUpInside)
|
|
XCTAssertFalse(button.isEnabled)
|
|
XCTAssertEqual(model.presentation.title, oldTitle)
|
|
XCTAssertEqual(find(controller.view, suffix: "metric.battery")?.accessibilityValue, oldBattery)
|
|
try await settle(window) {
|
|
(model.response?.executionBatch ?? 0) > oldBatch
|
|
&& !model.isResummoning && model.presentation.phase == .ready
|
|
}
|
|
XCTAssertFalse(model.hasSubmittedResummon, "新的执行批次应清除上一批次的重召唤锁")
|
|
XCTAssertFalse(model.canResummon)
|
|
XCTAssertTrue(model.canStartShooting)
|
|
for (suffix, frame) in zip(fixedIDs, frames) {
|
|
XCTAssertEqual(find(controller.view, suffix: suffix)?.frame, frame)
|
|
}
|
|
capture(window, name: "重新召唤-同任务回查成功-本地模拟")
|
|
|
|
let entries = try await records(controller, window: window, name: "重新召唤-成功")
|
|
XCTAssertEqual(entries.compactMap { $0["method"] as? String }, ["GET", "POST", "GET"])
|
|
let posts = entries.filter { $0["method"] as? String == "POST" }
|
|
XCTAssertEqual(posts.count, 1)
|
|
XCTAssertEqual(posts.first?["path"] as? String, "/api/app/airport/shoot")
|
|
let body = try XCTUnwrap(posts.first?["request_body"] as? [String: Any])
|
|
XCTAssertEqual(Set(body.keys), ["id", "restart"])
|
|
XCTAssertEqual(body["id"] as? Int, 901)
|
|
XCTAssertEqual(body["restart"] as? Bool, true)
|
|
XCTAssertEqual(entries.filter { $0["path"] as? String == "/api/app/airport/shooting" }.count, 2)
|
|
XCTAssertFalse(entries.contains { ($0["path"] as? String)?.contains("/tasks/create") == true })
|
|
}
|
|
|
|
/// 明确业务拒绝保留失败快照,只有用户再次点击才重新请求,不偷偷创建任务。
|
|
func testResummonBusinessFailureAllowsExplicitRetry() async throws {
|
|
let controller = try makeController(kind: "resummon", result: "business-failure")
|
|
let window = makeWindow(controller)
|
|
defer { close(window) }
|
|
let model = controller.viewModel
|
|
try await settle(window) { model.canResummon }
|
|
let original = model.response
|
|
let button = try XCTUnwrap(find(controller.view, suffix: "failureRetry") as? UIButton)
|
|
button.sendActions(for: .touchUpInside)
|
|
try await settle(window) { model.resummonErrorMessage != nil && !model.isResummoning }
|
|
XCTAssertFalse(model.resummonOutcomeUnknown)
|
|
XCTAssertFalse(model.hasSubmittedResummon)
|
|
XCTAssertEqual(model.response, original)
|
|
XCTAssertTrue(model.canResummon)
|
|
XCTAssertTrue(button.isEnabled)
|
|
capture(window, name: "重新召唤-业务拒绝-本地模拟")
|
|
|
|
button.sendActions(for: .touchUpInside)
|
|
try await settle(window) { model.resummonErrorMessage != nil && !model.isResummoning }
|
|
let entries = try await records(controller, window: window, name: "重新召唤-明确拒绝后手动重试")
|
|
XCTAssertEqual(entries.compactMap { $0["method"] as? String }, ["GET", "POST", "POST"])
|
|
XCTAssertTrue(entries.filter { $0["method"] as? String == "POST" }.allSatisfy {
|
|
$0["path"] as? String == "/api/app/airport/shoot"
|
|
&& ($0["request_body"] as? [String: Any])?["restart"] as? Bool == true
|
|
})
|
|
XCTAssertFalse(entries.contains { ($0["path"] as? String)?.contains("/tasks/create") == true })
|
|
}
|
|
|
|
/// 超时后锁定重复重召唤,旧失败快照不解锁,只允许手动读取同一任务。
|
|
func testResummonTimeoutKeepsReadRefreshButPreventsSecondPost() async throws {
|
|
let controller = try makeController(kind: "resummon", result: "timeout")
|
|
let window = makeWindow(controller)
|
|
defer { close(window) }
|
|
let model = controller.viewModel
|
|
try await settle(window) { model.canResummon }
|
|
let button = try XCTUnwrap(find(controller.view, suffix: "failureRetry") as? UIButton)
|
|
button.sendActions(for: .touchUpInside)
|
|
try await settle(window) { model.resummonOutcomeUnknown && !model.isResummoning }
|
|
XCTAssertFalse(model.canResummon)
|
|
XCTAssertEqual(model.presentation.action, .refresh)
|
|
capture(window, name: "重新召唤-超时待核对-本地模拟")
|
|
|
|
let refresh = try XCTUnwrap(controller.navigationItem.rightBarButtonItems?.first {
|
|
$0.accessibilityIdentifier == "drone.task.remote.navigationRefresh"
|
|
})
|
|
XCTAssertTrue(refresh.isEnabled)
|
|
send(refresh)
|
|
try await settle(window) { !model.isLoading && model.resummonOutcomeUnknown }
|
|
XCTAssertFalse(model.canResummon)
|
|
let entries = try await records(controller, window: window, name: "重新召唤-超时后只读回查")
|
|
XCTAssertEqual(entries.filter { $0["method"] as? String == "POST" }.count, 1)
|
|
XCTAssertEqual(entries.filter { $0["path"] as? String == "/api/app/airport/shooting" }.count, 2)
|
|
XCTAssertFalse(entries.contains { ($0["path"] as? String)?.contains("/tasks/create") == true })
|
|
}
|
|
|
|
/// 图传只读取一次并播放明确标记的本地样片;离开页面后释放 AVPlayer。
|
|
func testLocalLivePreviewIsLabeledDoesNotPollAndStopsOnLeaving() async throws {
|
|
let controller = try makeController(kind: "live", result: "success")
|
|
let window = makeWindow(controller)
|
|
defer { close(window) }
|
|
try await settle(window) {
|
|
controller.liveViewModel.playbackURL != nil && !controller.viewModel.isLoading
|
|
}
|
|
let url = try XCTUnwrap(controller.liveViewModel.playbackURL)
|
|
XCTAssertTrue(url.isFileURL)
|
|
XCTAssertEqual(controller.liveViewModel.state, .localPreview(url))
|
|
let source = try XCTUnwrap(find(controller.view, suffix: "live.source") as? UILabel)
|
|
XCTAssertFalse(source.isHidden)
|
|
XCTAssertEqual(source.text, "本地模拟画面 · 未连接无人机")
|
|
let feed = try XCTUnwrap(find(controller.view, suffix: "feedPlaceholder"))
|
|
let playerLayer = try XCTUnwrap(feed.layer.sublayers?.compactMap { $0 as? AVPlayerLayer }.first)
|
|
XCTAssertNotNil(playerLayer.player)
|
|
capture(window, name: "图传-明确标记的本地画面")
|
|
try await Task.sleep(nanoseconds: 350_000_000)
|
|
|
|
let entries = try await records(controller, window: window, name: "图传-单次本地请求")
|
|
let liveEntries = entries.filter { $0["path"] as? String == "/api/app/airport/live" }
|
|
XCTAssertEqual(liveEntries.count, 1)
|
|
XCTAssertEqual(liveEntries.first?["method"] as? String, "GET")
|
|
XCTAssertEqual(liveEntries.first?["query"] as? String, "id=901")
|
|
XCTAssertFalse(entries.contains { $0["method"] as? String == "POST" })
|
|
|
|
controller.dismiss(animated: false)
|
|
try await settle(window) { controller.presentedViewController == nil }
|
|
controller.navigationController?.pushViewController(UIViewController(), animated: false)
|
|
try await settle(window) { playerLayer.player == nil }
|
|
XCTAssertNil(controller.liveViewModel.playbackURL)
|
|
}
|
|
|
|
/// 接口只返回设备和任务时显示缺少播放参数,不用演示视频伪装真实流。
|
|
func testLiveMissingPlaybackParametersShowsUnavailableWithoutPlayer() async throws {
|
|
let controller = try makeController(kind: "live", result: "missing-parameters")
|
|
let window = makeWindow(controller)
|
|
defer { close(window) }
|
|
try await settle(window) { controller.liveViewModel.response != nil && !controller.liveViewModel.isLoading }
|
|
XCTAssertEqual(controller.liveViewModel.state, .unavailable("暂未提供图传播放参数"))
|
|
XCTAssertNil(controller.liveViewModel.playbackURL)
|
|
XCTAssertEqual((find(controller.view, suffix: "live.status") as? UILabel)?.text, "暂未提供图传播放参数")
|
|
XCTAssertTrue(try XCTUnwrap(find(controller.view, suffix: "live.badge")).isHidden,
|
|
"实时标识整体隐藏后,内部文案标签不需要单独隐藏")
|
|
let feed = try XCTUnwrap(find(controller.view, suffix: "feedPlaceholder"))
|
|
XCTAssertNil(feed.layer.sublayers?.compactMap { $0 as? AVPlayerLayer }.first?.player)
|
|
capture(window, name: "图传-缺少播放参数")
|
|
let entries = try await records(controller, window: window, name: "图传-缺少播放参数")
|
|
XCTAssertEqual(entries.filter { $0["path"] as? String == "/api/app/airport/live" }.count, 1)
|
|
XCTAssertFalse(entries.contains { $0["method"] as? String == "POST" })
|
|
}
|
|
|
|
/// 图传业务拒绝和超时均显示错误并允许显式重试,快速双击不叠加请求。
|
|
func testLiveFailureAndTimeoutCanRetryWithoutStartingDeviceCommands() async throws {
|
|
for result in ["business-failure", "timeout"] {
|
|
let controller = try makeController(kind: "live", result: result)
|
|
let window = makeWindow(controller)
|
|
defer { close(window) }
|
|
try await settle(window) {
|
|
if case .failed = controller.liveViewModel.state { return !controller.liveViewModel.isLoading }
|
|
return false
|
|
}
|
|
XCTAssertNil(controller.liveViewModel.playbackURL)
|
|
XCTAssertFalse(controller.liveViewModel.message.isEmpty)
|
|
XCTAssertEqual((find(controller.view, suffix: "live.title") as? UILabel)?.text, "图传加载失败")
|
|
let button = try XCTUnwrap(find(controller.view, suffix: "live.request") as? UIButton)
|
|
XCTAssertTrue(button.isEnabled)
|
|
button.sendActions(for: .touchUpInside)
|
|
button.sendActions(for: .touchUpInside)
|
|
try await settle(window) {
|
|
if case .failed = controller.liveViewModel.state { return !controller.liveViewModel.isLoading }
|
|
return false
|
|
}
|
|
capture(window, name: "图传-\(result)-本地模拟")
|
|
let entries = try await records(controller, window: window, name: "图传-\(result)-手动重试")
|
|
XCTAssertEqual(entries.filter { $0["path"] as? String == "/api/app/airport/live" }.count, 2)
|
|
XCTAssertFalse(entries.contains { $0["method"] as? String == "POST" })
|
|
}
|
|
}
|
|
|
|
/// 默认页面不注入图传或重召唤能力,仅用隔离替身读取状态,保护不依赖模拟入口标签。
|
|
func testMissingServicesDoNotEnableLiveOrResummon() async throws {
|
|
let context = DroneDockShootingTaskTestData.context()
|
|
let service = DroneDockShootingTaskTestService(result: .success(try DroneDockShootingTaskTestData.response()))
|
|
let model = DroneDockShootingTaskViewModel(context: context, taskID: 501,
|
|
order: try DroneDockShootingTaskTestData.order(), service: service, currentContext: { context })
|
|
let controller = DroneDockShootingTaskViewController(viewModel: model)
|
|
let window = makeWindow(controller)
|
|
defer { close(window) }
|
|
try await settle(window) { model.response != nil && !model.isLoading }
|
|
XCTAssertEqual(controller.liveViewModel.state, .unavailable("当前页面未配置图传服务"))
|
|
XCTAssertFalse(controller.liveViewModel.canLoad)
|
|
XCTAssertFalse(model.canResummon)
|
|
XCTAssertNil(controller.liveViewModel.playbackURL)
|
|
XCTAssertTrue(try XCTUnwrap(find(controller.view, suffix: "live.request")).isHidden)
|
|
XCTAssertEqual((find(controller.view, suffix: "live.title") as? UILabel)?.text, "图传暂不可用")
|
|
XCTAssertEqual(service.requests.count, 1)
|
|
}
|
|
|
|
private func makeController(kind: String, result: String) throws -> DroneDockShootingTaskViewController {
|
|
try XCTUnwrap(DroneDockStartShootSimulation.makeController(
|
|
arguments: ["--dock-\(kind)-mock", "--dock-\(kind)-result=\(result)"])
|
|
as? DroneDockShootingTaskViewController)
|
|
}
|
|
|
|
private func records(_ controller: UIViewController, window: UIWindow, name: String) async throws -> [[String: Any]] {
|
|
let item = try XCTUnwrap(controller.navigationItem.rightBarButtonItems?.first {
|
|
$0.accessibilityIdentifier == "drone.task.mock.recordsButton"
|
|
})
|
|
XCTAssertTrue(item.isEnabled)
|
|
send(item)
|
|
try await settle(window) { controller.presentedViewController != nil }
|
|
let sheet = try XCTUnwrap(controller.presentedViewController)
|
|
let text = try XCTUnwrap(allViews(sheet.view).first {
|
|
$0.accessibilityIdentifier == "drone.task.mock.records"
|
|
} as? UITextView)
|
|
let data = Data(text.text.utf8)
|
|
let attachment = XCTAttachment(data: data, uniformTypeIdentifier: "public.json")
|
|
attachment.name = name
|
|
attachment.lifetime = .keepAlways
|
|
add(attachment)
|
|
let transcript = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])
|
|
XCTAssertEqual(transcript["source"] as? String, "local_mock")
|
|
XCTAssertEqual(transcript["network_sent"] as? Bool, false)
|
|
return try XCTUnwrap(transcript["records"] as? [[String: Any]])
|
|
}
|
|
|
|
private func capture(_ window: UIWindow, name: String) {
|
|
window.layoutIfNeeded()
|
|
let image = UIGraphicsImageRenderer(bounds: window.bounds).image { _ in
|
|
window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
|
|
}
|
|
let attachment = XCTAttachment(image: image)
|
|
attachment.name = name
|
|
attachment.lifetime = .keepAlways
|
|
add(attachment)
|
|
}
|
|
|
|
private func send(_ item: UIBarButtonItem) {
|
|
guard let action = item.action else { return XCTFail("缺少导航操作") }
|
|
_ = UIApplication.shared.sendAction(action, to: item.target, from: item, for: nil)
|
|
}
|
|
|
|
private func makeWindow(_ controller: UIViewController) -> UIWindow {
|
|
let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
|
|
let scene = scenes.first { $0.activationState == .foregroundActive } ?? scenes.first
|
|
previousKeyWindow = scene?.windows.first { $0.isKeyWindow }
|
|
let window = scene.map { UIWindow(windowScene: $0) } ?? UIWindow(frame: UIScreen.main.bounds)
|
|
window.frame = scene?.coordinateSpace.bounds ?? UIScreen.main.bounds
|
|
window.overrideUserInterfaceStyle = .light
|
|
window.rootViewController = UINavigationController(rootViewController: controller)
|
|
window.makeKeyAndVisible()
|
|
return window
|
|
}
|
|
|
|
private func close(_ window: UIWindow) {
|
|
window.rootViewController?.dismiss(animated: false)
|
|
window.isHidden = true
|
|
window.rootViewController = nil
|
|
previousKeyWindow?.makeKeyAndVisible()
|
|
}
|
|
|
|
private func settle(_ window: UIWindow, until condition: () -> Bool) async throws {
|
|
for _ in 0..<200 {
|
|
window.layoutIfNeeded()
|
|
if condition() {
|
|
try await Task.sleep(nanoseconds: 80_000_000)
|
|
window.layoutIfNeeded()
|
|
if condition() { return }
|
|
}
|
|
try await Task.sleep(nanoseconds: 15_000_000)
|
|
}
|
|
XCTFail("重新召唤或图传页面未在预期时间内完成状态更新")
|
|
throw NSError(domain: "DroneDockResummonLiveViewTests", code: 1)
|
|
}
|
|
|
|
private func find(_ root: UIView, suffix: String) -> UIView? {
|
|
allViews(root).first { $0.accessibilityIdentifier == "drone.task.remote.\(suffix)" }
|
|
}
|
|
|
|
private func allViews(_ root: UIView) -> [UIView] { [root] + root.subviews.flatMap { allViews($0) } }
|
|
}
|