Files
suixinkan_uikit/suixinkanTests/DroneDockShootingTaskTestSupport.swift
lujiuyin e3979e9e32 feat: 完善机巢任务安全控制与状态诊断
同步新版飞控与 WebSocket 回执合同,完善独立指令等待、暂停继续和返航状态;增加真机诊断并优化倒计时与素材库展示。
2026-11-04 17:34:28 +08:00

80 lines
4.8 KiB
Swift

import Foundation
@testable import suixinkan
/// 真实拍摄任务页测试共享的匿名任务、订单和设备快照。
enum DroneDockShootingTaskTestData {
/// 固定测试登录范围,参数可模拟切换景区或凭证。
static func context(scenicID: Int = 128, token: String = "task-read-test-token") -> DroneDockHomeContext {
DroneDockHomeContext(userID: "task-read-user", accountType: "store_user", scenicID: scenicID,
scenicName: "测试景区", storeID: 38, photographerName: "测试员工", token: token)
}
/// 进入页面时的订单摘要与 shooting 套餐名故意不同,验证服务端展示优先级。
static func order() throws -> DroneDockHomeOrder {
try JSONDecoder().decode(DroneDockHomeOrder.self, from: Data(#"{"order_number":"TEST-TASK-PAGE-001","project_id":101,"project_name":"原订单五次套餐","user_phone":"测试游客","shoot_count":5,"shot_count":0,"remaining_count":5}"#.utf8))
}
/// 使用已确认的后端字段结构,测试数据不代表真实飞行结果。
static func payload() -> [String: Any] {
["order_flight_task": ["id": 501, "order_number": "TEST-TASK-PAGE-001", "scenic_id": 128,
"status": 10, "status_label": "待执行"],
"task": ["id": 501, "status": 10, "status_label": "待执行"],
"scenic": ["id": 128, "name": "测试景区"],
"scenic_spot": ["id": 201, "name": "测试点位"],
"air_line": ["id": 301, "name": "测试航线", "scenic_spot_id": 201],
"header": ["order_number": "TEST-TASK-PAGE-001", "title": "测试点位 · 测试航线",
"tourist_label": "测试游客", "package_name": "接口返回单次套餐"],
"current_plane": ["id": 58, "sn_number": "TEST-PLANE-058", "status_text": "空闲 · 电量 0%"],
"plane_status": ["scenic_id": 128, "connected": true, "poll_hint_ms": 5000],
"metrics": [["key": "battery", "value": "0%", "raw": 0], ["key": "env_temp", "value": "0°C"],
["key": "wind_speed", "value": "0m/s"], ["key": "rainfall", "value": NSNull()],
["key": "signal", "value": "—"], ["key": "satellite", "value": "0颗"]],
"execute_gate": ["can_execute": false, "code": "wayline_missing", "message": "测试门禁原因"],
"ui": ["phase": "blocked", "title": "任务尚未关联航线", "message": "测试阶段说明"]]
}
/// 可覆盖顶层字段以测试缺失、未知状态与身份错误。
static func response(changes: [String: Any] = [:]) throws -> DroneDockShootingResponse {
try decode(payload().merging(changes) { _, new in new })
}
/// 新版飞控合同的显式测试数据;调用方自行选择阶段及权限,缺失场景不自动补齐。
static func flightControl(state: String = "executing", batch: Int = 100,
canPause: Bool = true, canResume: Bool = false, canReturn: Bool = true,
resumePhase: String? = nil, returnReason: String? = nil,
droneInDock: String? = "outside", isAirborne: Bool? = true,
pauseReason: String? = nil, pendingCommand: [String: Any]? = nil) -> [String: Any] {
["state": state, "execution_batch": batch, "can_pause": canPause,
"can_resume": canResume, "can_return_home": canReturn,
"resume_phase": resumePhase as Any? ?? NSNull(),
"return_reason": returnReason as Any? ?? NSNull(),
"drone_in_dock": droneInDock as Any? ?? NSNull(),
"is_airborne": isAirborne as Any? ?? NSNull(),
"pause_reason": pauseReason as Any? ?? NSNull(),
"pending_command": pendingCommand as Any? ?? NSNull()]
}
/// 从任意匿名 data 对象构造真实 DTO。
static func decode(_ payload: [String: Any]) throws -> DroneDockShootingResponse {
try JSONDecoder().decode(DroneDockShootingResponse.self, from: JSONSerialization.data(withJSONObject: payload))
}
}
/// 可配置返回值的只读 shooting 替身,只记录任务 ID 与实时电量参数。
@MainActor
final class DroneDockShootingTaskTestService: DroneDockShootingServing {
/// 当前读取结果,测试可模拟失败后恢复。
var result: Result<DroneDockShootingResponse, Error>
/// 所有读取记录,不提供任何创建或飞控能力。
private(set) var requests: [(taskID: Int, liveBattery: Bool)] = []
/// 注入匿名快照或读取错误。
init(result: Result<DroneDockShootingResponse, Error>) { self.result = result }
/// 返回配置结果,不自动重读或发送其他请求。
func shooting(taskID: Int, liveBattery: Bool) async throws -> DroneDockShootingResponse {
requests.append((taskID, liveBattery))
return try result.get()
}
}