- 完善双路 RTC 接收、首帧切换及拍摄结束后的本地播放释放 - 对齐无人机状态枚举与正常拍完返航标记,新增到达后本地倒计时 - 补充准备页电量与前往回执,进入任务时移除准备页路由 - 对齐拍摄中和拍摄完成原型布局,移除详情按钮 - 补充隔离测试、接口记录与项目约定 验证:最近 157 项相关模拟器隔离测试通过,Release 无签名编译通过;未调用真实飞控或图传。
264 lines
15 KiB
Swift
264 lines
15 KiB
Swift
import Foundation
|
|
import XCTest
|
|
@testable import suixinkan
|
|
|
|
/// 验证正常拍完返航标记与真实任务结果独立,HTTP/WS 均只消费隔离快照。
|
|
@MainActor
|
|
final class DroneDockShootDoneReturningTests: XCTestCase {
|
|
/// 新字段保持缺失、null 和真假语义,手动返航结果码也原样保留。
|
|
func testReturningFieldsDecodeMissingNullTrueAndFalse() throws {
|
|
for flag: Bool? in [nil, false, true] {
|
|
var value = payload()
|
|
var ui = try XCTUnwrap(value["ui"] as? [String: Any])
|
|
if let flag {
|
|
value["shoot_complete_returning"] = flag
|
|
ui["shoot_complete_returning"] = flag
|
|
ui["wayline_stage"] = "shoot_done_returning"
|
|
} else {
|
|
value.removeValue(forKey: "shoot_complete_returning")
|
|
ui.removeValue(forKey: "shoot_complete_returning")
|
|
ui.removeValue(forKey: "wayline_stage")
|
|
}
|
|
value["ui"] = ui
|
|
let result = try DroneDockShootingTaskTestData.decode(value)
|
|
XCTAssertEqual(result.shootCompleteReturning, flag)
|
|
XCTAssertEqual(result.ui?.shootCompleteReturning, flag)
|
|
XCTAssertEqual(result.ui?.waylineStage, flag == nil ? nil : "shoot_done_returning")
|
|
}
|
|
var nullable = payload()
|
|
nullable["shoot_complete_returning"] = NSNull()
|
|
nullable["ui"] = ["phase": "post_shoot_uploading", "shoot_complete_returning": NSNull(), "wayline_stage": NSNull()]
|
|
nullable["task"] = ["id": 501, "status": 20, "result_code": "return_home"]
|
|
nullable["order_flight_task"] = ["id": 501, "status": 20, "result_code": "return_home"]
|
|
let result = try DroneDockShootingTaskTestData.decode(nullable)
|
|
XCTAssertNil(result.shootCompleteReturning)
|
|
XCTAssertNil(result.ui?.shootCompleteReturning)
|
|
XCTAssertNil(result.ui?.waylineStage)
|
|
XCTAssertEqual(result.task?.resultCode, "return_home")
|
|
XCTAssertEqual(result.orderFlightTask?.resultCode, "return_home")
|
|
}
|
|
|
|
/// 顶层、UI 布尔或阶段任一明确标记均能展示正常返航说明,不要求任务终态成功。
|
|
func testExplicitReturningMarkersPreserveExecutingTaskAndExposeMaterials() async throws {
|
|
let markers: [(Bool?, Bool?, String?)] = [
|
|
(true, nil, nil), (nil, true, nil), (nil, nil, "shoot_done_returning"),
|
|
(true, true, "shoot_done_returning")
|
|
]
|
|
for (root, nested, stage) in markers {
|
|
for wayline in [20, 24] {
|
|
let fixture = try makeFixture(payload(root: root, nested: nested, stage: stage, wayline: wayline))
|
|
await fixture.model.refresh()
|
|
XCTAssertEqual(fixture.model.presentation.phase, .postShootUploading)
|
|
XCTAssertEqual(fixture.model.presentation.title, "拍摄已完成")
|
|
XCTAssertTrue(fixture.model.presentation.message.contains("无人机正在返航,请留意现场"))
|
|
XCTAssertTrue(fixture.model.isShootCompleteReturning)
|
|
XCTAssertTrue(fixture.model.isRecordingFinished)
|
|
XCTAssertTrue(fixture.model.shouldPresentRecordingCompletion)
|
|
XCTAssertEqual(fixture.model.presentation.action, .materials)
|
|
XCTAssertTrue(fixture.model.presentation.isActionEnabled)
|
|
XCTAssertFalse(fixture.model.canStartShooting)
|
|
XCTAssertFalse(fixture.model.canResummon)
|
|
XCTAssertEqual(fixture.model.response?.task?.status, 20)
|
|
XCTAssertEqual(fixture.model.response?.orderFlightTask?.status, 20)
|
|
XCTAssertEqual(fixture.model.response?.task?.waylineStatus, wayline)
|
|
XCTAssertNil(fixture.model.countdownRemainingSeconds)
|
|
XCTAssertEqual(fixture.actions.startCount + fixture.actions.resummonCount, 0)
|
|
XCTAssertEqual(fixture.service.requests.count, 1)
|
|
XCTAssertTrue(fixture.service.requests.allSatisfy { !$0.liveBattery })
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 明确 false 优先于另一处 true、阶段残值和旧设备返航文案,避免继续展示已撤销标记。
|
|
func testExplicitFalseVetoesConflictingReturningMarkers() async throws {
|
|
for (root, nested) in [(false, true), (true, false), (false, false)] {
|
|
var value = payload(root: root, nested: nested, stage: "shoot_done_returning")
|
|
value["current_plane"] = ["id": 58, "work_status": 2, "work_status_label": "正在返航"]
|
|
let fixture = try makeFixture(value)
|
|
await fixture.model.refresh()
|
|
XCTAssertFalse(fixture.model.isShootCompleteReturning)
|
|
XCTAssertFalse(fixture.model.presentation.message.contains("无人机正在返航"))
|
|
XCTAssertEqual(fixture.model.presentation.phase, .postShootUploading)
|
|
XCTAssertEqual(fixture.model.presentation.action, .materials)
|
|
}
|
|
}
|
|
|
|
/// 真实上传或渲染状态覆盖滞留的返航标记,但不会改写拍摄已经完成的页面阶段。
|
|
func testUploadingAndRenderingOverrideStaleReturningFlags() async throws {
|
|
for wayline in [30, 40] {
|
|
let fixture = try makeFixture(payload(root: true, nested: true, stage: "shoot_done_returning", wayline: wayline))
|
|
await fixture.model.refresh()
|
|
XCTAssertFalse(fixture.model.isShootCompleteReturning)
|
|
XCTAssertFalse(fixture.model.presentation.message.contains("无人机正在返航"))
|
|
XCTAssertEqual(fixture.model.presentation.title, "拍摄已完成")
|
|
XCTAssertEqual(fixture.model.presentation.phase, .postShootUploading)
|
|
XCTAssertTrue(fixture.model.shouldPresentRecordingCompletion)
|
|
XCTAssertEqual(fixture.model.response?.task?.waylineStatus, wayline)
|
|
}
|
|
}
|
|
|
|
/// 同批次切到上传后说明随完整快照更新,旧返航版本不能再次覆盖上传说明。
|
|
func testReturningSnapshotTransitionsToUploadAndRejectsOldVersion() async throws {
|
|
let fixture = try makeFixture(payload())
|
|
await fixture.model.refresh()
|
|
XCTAssertTrue(fixture.model.isShootCompleteReturning)
|
|
fixture.service.result = .success(try DroneDockShootingTaskTestData.decode(
|
|
payload(root: false, nested: false, stage: nil, wayline: 30, version: 2)))
|
|
await fixture.model.refresh()
|
|
XCTAssertFalse(fixture.model.isShootCompleteReturning)
|
|
XCTAssertFalse(fixture.model.presentation.message.contains("无人机正在返航"))
|
|
XCTAssertEqual(fixture.model.response?.stateVersion, 2)
|
|
XCTAssertEqual(fixture.model.response?.task?.waylineStatus, 30)
|
|
XCTAssertEqual(fixture.model.applyTaskSnapshot(try DroneDockShootingTaskTestData.decode(payload())), .outdated)
|
|
XCTAssertFalse(fixture.model.isShootCompleteReturning)
|
|
XCTAssertEqual(fixture.model.response?.task?.waylineStatus, 30)
|
|
}
|
|
|
|
/// 低电等未拍完返航、任务失败与取消优先于错误残留的完成标记。
|
|
func testPartialFailureAndCancellationNeverBecomeSuccessfulRecording() async throws {
|
|
for (status, wayline, phase) in [(40, 29, "failed_restartable"), (40, 91, "failed_restartable"),
|
|
(50, 90, "post_shoot_uploading"), (60, 20, "post_shoot_uploading")] {
|
|
var value = payload()
|
|
value["task"] = ["id": 501, "status": status, "wayline_status": wayline]
|
|
value["order_flight_task"] = ["id": 501, "status": status, "result_code": "wayline_\(wayline)",
|
|
"result_message": "本地模拟:未拍完提前返航"]
|
|
value["ui"] = ["phase": phase, "wayline_stage": "shoot_done_returning", "shoot_complete_returning": true,
|
|
"message": "本地模拟:未拍完提前返航", "commands": []]
|
|
let fixture = try makeFixture(value)
|
|
await fixture.model.refresh()
|
|
XCTAssertFalse(fixture.model.isShootCompleteReturning)
|
|
XCTAssertFalse(fixture.model.shouldPresentRecordingCompletion)
|
|
XCTAssertNotEqual(fixture.model.presentation.action, .materials)
|
|
XCTAssertFalse(fixture.model.canStartShooting)
|
|
XCTAssertEqual(fixture.model.response?.orderFlightTask?.status, status)
|
|
}
|
|
}
|
|
|
|
/// 手动返航结果码即使伴随残留的运行中摘要或乐观 UI,也必须保持中断失败。
|
|
func testManualReturnHomeResultCodeOverridesResidualCompletedUI() async throws {
|
|
for status in [20, 40] {
|
|
for field in ["task", "order_flight_task"] {
|
|
var value = payload()
|
|
value["task"] = ["id": 501, "status": status, "wayline_status": 24]
|
|
value["order_flight_task"] = ["id": 501, "status": status]
|
|
var task = try XCTUnwrap(value[field] as? [String: Any])
|
|
task["result_code"] = "return_home"
|
|
task["result_message"] = "立即返航中断任务"
|
|
value[field] = task
|
|
let fixture = try makeFixture(value)
|
|
await fixture.model.refresh()
|
|
XCTAssertEqual(fixture.model.presentation.phase, .failed)
|
|
XCTAssertFalse(fixture.model.isShootCompleteReturning)
|
|
XCTAssertFalse(fixture.model.shouldPresentRecordingCompletion)
|
|
XCTAssertNotEqual(fixture.model.presentation.action, .materials)
|
|
XCTAssertFalse(fixture.model.canStartShooting)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 标记只细分可信的录制后阶段,不能凭标记把拍摄中或未知阶段提升为完成。
|
|
func testMarkersDoNotPromoteShootingOrUnknownPhaseToCompletion() async throws {
|
|
for phase in ["shooting", "future_phase", "blocked"] {
|
|
var value = payload(wayline: 20)
|
|
value["ui"] = ["phase": phase, "shoot_complete_returning": true, "wayline_stage": "shoot_done_returning"]
|
|
let fixture = try makeFixture(value)
|
|
await fixture.model.refresh()
|
|
XCTAssertFalse(fixture.model.isShootCompleteReturning)
|
|
XCTAssertFalse(fixture.model.shouldPresentRecordingCompletion)
|
|
XCTAssertFalse(fixture.model.isRecordingFinished)
|
|
XCTAssertNotEqual(fixture.model.presentation.action, .materials)
|
|
}
|
|
}
|
|
|
|
/// 缺少新字段的旧快照仍可兼容明确设备返航文案,不凭普通忙碌状态编造返航。
|
|
func testLegacySnapshotsUseOnlyExplicitReturningDeviceLabel() async throws {
|
|
for label in ["返航中", "正在返航", "拍摄中", "空闲"] {
|
|
var value = payload(root: nil, nested: nil, stage: nil)
|
|
value["current_plane"] = ["id": 58, "work_status": 2, "work_status_label": label]
|
|
let fixture = try makeFixture(value)
|
|
await fixture.model.refresh()
|
|
XCTAssertEqual(fixture.model.presentation.message.contains("无人机正在返航"), ["返航中", "正在返航"].contains(label))
|
|
}
|
|
}
|
|
|
|
/// HTTP 和 WS 使用同一个完整快照,在飞行任务仍执行中时呈现一致的返航完成界面。
|
|
func testHTTPAndWebSocketNormalReturningSnapshotsAreEquivalent() async throws {
|
|
let value = payload()
|
|
let http = try makeFixture(value)
|
|
await http.model.refresh()
|
|
let socket = try makeFixture(payload(root: nil, nested: nil, stage: nil, wayline: 20))
|
|
guard case .snapshot(let snapshot) = try DroneDockShootingSocketMessage.decode(RealtimeTestData.push(value)) else {
|
|
return XCTFail("正常返航完整 WS 快照应可解析")
|
|
}
|
|
XCTAssertEqual(socket.model.applyTaskSnapshot(snapshot), .applied)
|
|
XCTAssertEqual(socket.model.presentation, http.model.presentation)
|
|
XCTAssertTrue(socket.model.isShootCompleteReturning)
|
|
XCTAssertEqual(socket.model.response?.shootCompleteReturning, true)
|
|
XCTAssertEqual(socket.model.response?.ui?.waylineStage, "shoot_done_returning")
|
|
XCTAssertTrue(socket.service.requests.isEmpty)
|
|
XCTAssertEqual(socket.actions.startCount + socket.actions.resummonCount, 0)
|
|
}
|
|
|
|
/// 新批次上传阶段不会被旧批次或其他任务的返航标记污染。
|
|
func testOlderBatchAndWrongTaskMarkersCannotRestoreReturningNotice() async throws {
|
|
let fixture = try makeFixture(payload(root: false, nested: false, stage: nil, wayline: 30, batch: 101, version: 3))
|
|
await fixture.model.refresh()
|
|
XCTAssertEqual(fixture.model.applyTaskSnapshot(try DroneDockShootingTaskTestData.decode(
|
|
payload(batch: 100, version: 999))), .outdated)
|
|
var wrongTask = payload(batch: 101, version: 4)
|
|
wrongTask["order_flight_task_id"] = 502
|
|
XCTAssertEqual(fixture.model.applyTaskSnapshot(try DroneDockShootingTaskTestData.decode(wrongTask)), .invalid)
|
|
XCTAssertFalse(fixture.model.isShootCompleteReturning)
|
|
XCTAssertEqual(fixture.model.response?.executionBatch, 101)
|
|
XCTAssertEqual(fixture.model.response?.stateVersion, 3)
|
|
XCTAssertEqual(fixture.model.response?.task?.waylineStatus, 30)
|
|
}
|
|
|
|
private func payload(root: Bool? = true, nested: Bool? = true, stage: String? = "shoot_done_returning",
|
|
wayline: Int = 24, batch: Int = 100, version: Int = 1) -> [String: Any] {
|
|
var value = DroneDockShootingTaskTestData.payload()
|
|
value["order_flight_task_id"] = 501
|
|
value["execution_batch"] = batch
|
|
value["state_version"] = version
|
|
value["snapshot_kind"] = "full"
|
|
value["task"] = ["id": 501, "status": 20, "wayline_status": wayline]
|
|
value["order_flight_task"] = ["id": 501, "status": 20]
|
|
value["current_plane"] = ["id": 58, "work_status": 1, "online_status": 1, "work_status_label": "空闲"]
|
|
value["execute_gate"] = ["can_execute": false, "primary_action": "blocked"]
|
|
if let root { value["shoot_complete_returning"] = root }
|
|
var ui: [String: Any] = ["phase": "post_shoot_uploading", "title": "拍摄已完成", "commands": []]
|
|
if let nested { ui["shoot_complete_returning"] = nested }
|
|
if let stage { ui["wayline_stage"] = stage }
|
|
value["ui"] = ui
|
|
return value
|
|
}
|
|
|
|
private func makeFixture(_ payload: [String: Any]) throws -> (model: DroneDockShootingTaskViewModel,
|
|
service: DroneDockShootingTaskTestService, actions: ShootDoneReturningActions) {
|
|
let context = DroneDockShootingTaskTestData.context()
|
|
let service = DroneDockShootingTaskTestService(result: .success(try DroneDockShootingTaskTestData.decode(payload)))
|
|
let actions = ShootDoneReturningActions()
|
|
let model = try DroneDockShootingTaskViewModel(context: context, taskID: 501,
|
|
order: DroneDockShootingTaskTestData.order(), service: service, startService: actions,
|
|
resummonService: actions, currentContext: { context })
|
|
return (model, service, actions)
|
|
}
|
|
}
|
|
|
|
/// 仅记录潜在误触发的控制调用,不存在真实网络或设备传输。
|
|
@MainActor
|
|
private final class ShootDoneReturningActions: DroneDockStartShooting, DroneDockResummonServing {
|
|
private(set) var startCount = 0
|
|
private(set) var resummonCount = 0
|
|
|
|
func startShooting(taskID: Int) async throws -> DroneDockStartShootResponse {
|
|
startCount += 1
|
|
throw APIError.networkFailed("本地模拟:不应发起开拍")
|
|
}
|
|
|
|
func resummon(taskID: Int) async throws -> DroneDockShootResponse {
|
|
resummonCount += 1
|
|
throw APIError.networkFailed("本地模拟:不应发起重召")
|
|
}
|
|
}
|