开放 Release 真实操作并保留 Debug 拦截;恢复前台 shooting 串行轮询;统一打印机巢请求与完整原值响应;优化长点位名称、未拍摄文案、报备失败及已结束任务提示,移除成功回执 UI。补充相关测试、离线策略检查和联调说明。
118 lines
6.6 KiB
Swift
118 lines
6.6 KiB
Swift
import Foundation
|
|
import XCTest
|
|
@testable import suixinkan
|
|
|
|
/// 验证禁飞调试在进入传输层之前拦截真实操作,所有探针本身也不联网。
|
|
@MainActor
|
|
final class DeviceCommandDebugPolicyTests: XCTestCase {
|
|
/// 用户授权 Release 真机操作,当前模拟器 Debug 测试仍必须禁止真实飞控。
|
|
func testDebugKeepsFlightRestriction() {
|
|
XCTAssertTrue(DeviceCommandDebugPolicy.blocksFlightRequests)
|
|
}
|
|
/// 即使切到生产地址或未来新增机场动作,默认传输也不能发送设备操作。
|
|
func testTypedCommandsAreBlockedBeforeTransportInBothEnvironments() async throws {
|
|
XCTAssertTrue(DeviceCommandDebugPolicy.blocksLiveRequests)
|
|
for environment in [APIEnvironment.testing, .production] {
|
|
let session = CommandTransportProbe()
|
|
let client = APIClient(environment: environment, session: session)
|
|
for path in ["/api/app/airport/shoot", "/api/app/airport/start-shoot",
|
|
"/api/app/airport/command", "/api/app/airport/future-control",
|
|
"/api/yf-handset-app/photog/multi-plane/tasks/create"] {
|
|
do {
|
|
let _: EmptyDebugPayload = try await client.send(APIRequest(method: .post, path: path))
|
|
XCTFail("禁飞请求不得进入传输层:\(path)")
|
|
} catch is DeviceCommandDebugBlockedError {} catch { XCTFail("错误类型不应伪装为后端失败:\(error)") }
|
|
}
|
|
XCTAssertTrue(session.requests.isEmpty)
|
|
}
|
|
}
|
|
|
|
/// 更换 HTTP 方法、尾部斜杠或调用图传不能绕过允许读取的接口列表。
|
|
func testReadVerbDoesNotBypassCommandRestriction() async throws {
|
|
let session = CommandTransportProbe()
|
|
let client = APIClient(environment: .testing, session: session)
|
|
for path in ["/api/app/airport/shoot/", "/api/app/airport/command", "/api/app/airport/live"] {
|
|
do {
|
|
let _: EmptyDebugPayload = try await client.send(APIRequest(method: .get, path: path))
|
|
XCTFail("操作接口即使使用 GET 也不允许发送")
|
|
} catch is DeviceCommandDebugBlockedError {}
|
|
}
|
|
XCTAssertTrue(session.requests.isEmpty)
|
|
}
|
|
|
|
/// 裸 JSON 和 multipart 与强类型发送具有相同的保护边界。
|
|
func testRawAndMultipartCannotBypassRestriction() async throws {
|
|
let session = CommandTransportProbe()
|
|
let client = APIClient(environment: .testing, session: session)
|
|
do {
|
|
let _: EmptyDebugPayload = try await client.sendRawJSON(
|
|
method: .post, path: "/api/app/airport/shoot", jsonBody: #"{"id":501}"#)
|
|
XCTFail("裸 JSON 不得发送")
|
|
} catch is DeviceCommandDebugBlockedError {}
|
|
do {
|
|
let _: EmptyDebugPayload = try await client.sendMultipart(
|
|
path: "/api/app/airport/command", fileName: "test.json", mimeType: "application/json", data: Data())
|
|
XCTFail("multipart 不得发送")
|
|
} catch is DeviceCommandDebugBlockedError {}
|
|
XCTAssertTrue(session.requests.isEmpty)
|
|
}
|
|
|
|
/// 只读状态仍可联调,但关闭实时设备采集;任务身份及其他参数不变。
|
|
func testStateReadsDisableLiveCollectionEvenWhenRequestedOrOmitted() async throws {
|
|
let session = CommandTransportProbe()
|
|
let client = APIClient(environment: .testing, session: session)
|
|
for (path, key) in [("plane-status", "live_battery"), ("shooting", "live_battery"), ("status", "live_metrics")] {
|
|
for supplied in [false, true] {
|
|
var query = [URLQueryItem(name: "id", value: "501")]
|
|
if supplied { query += [URLQueryItem(name: key, value: "1"), URLQueryItem(name: key, value: "true")] }
|
|
let _: EmptyDebugPayload = try await client.send(APIRequest(
|
|
method: .get, path: "/api/app/airport/\(path)", queryItems: query))
|
|
let request = try XCTUnwrap(session.requests.last)
|
|
let items = try XCTUnwrap(URLComponents(url: XCTUnwrap(request.url), resolvingAgainstBaseURL: false)?.queryItems)
|
|
XCTAssertEqual(items.filter { $0.name == key }, [URLQueryItem(name: key, value: "0")])
|
|
XCTAssertEqual(items.first { $0.name == "id" }?.value, "501")
|
|
}
|
|
}
|
|
XCTAssertEqual(session.requests.count, 6)
|
|
}
|
|
|
|
/// 普通订单读取保持原有请求,不给所有业务泛化地加禁飞限制。
|
|
func testUnrelatedRequestsKeepTheirPayloadAndQuery() async throws {
|
|
let session = CommandTransportProbe()
|
|
let client = APIClient(environment: .testing, session: session)
|
|
let _: EmptyDebugPayload = try await client.send(APIRequest(
|
|
method: .get, path: "/api/yf-handset-app/photog/multi-plane/orders",
|
|
queryItems: [URLQueryItem(name: "scenic_id", value: "128")]))
|
|
XCTAssertEqual(session.requests.count, 1)
|
|
XCTAssertEqual(session.requests[0].url?.query, "scenic_id=128")
|
|
}
|
|
|
|
/// 纯内存 Mock 可核验原始指令合同,调用不代表服务器或设备实际执行。
|
|
func testLocalSimulationRetainsCommandAndLiveReadContracts() async throws {
|
|
let session = MockURLSession(responses: [Data(#"{"code":100000,"data":{}}"#.utf8),
|
|
Data(#"{"code":100000,"data":{}}"#.utf8)])
|
|
let client = APIClient(environment: .testing, session: session)
|
|
let _: EmptyDebugPayload = try await client.sendRawJSON(
|
|
method: .post, path: "/api/app/airport/shoot", jsonBody: #"{"id":501}"#)
|
|
let _: EmptyDebugPayload = try await client.send(APIRequest(
|
|
method: .get, path: "/api/app/airport/shooting", queryItems: [URLQueryItem(name: "live_battery", value: "1")]))
|
|
XCTAssertEqual(session.requests.count, 2)
|
|
XCTAssertEqual(session.requests[0].httpBody, Data(#"{"id":501}"#.utf8))
|
|
XCTAssertEqual(session.requests[1].url?.query, "live_battery=1")
|
|
}
|
|
}
|
|
|
|
/// 不声明模拟豁免的记录探针,验证默认客户端会在传输前拦截;自身绝不调用 URLSession。
|
|
private final class CommandTransportProbe: URLSessionProtocol {
|
|
private(set) var requests: [URLRequest] = []
|
|
|
|
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
|
|
requests.append(request)
|
|
let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)!
|
|
return (Data(#"{"code":100000,"data":{}}"#.utf8), response)
|
|
}
|
|
}
|
|
|
|
/// 无业务字段的测试响应,不使用真实任务或设备数据。
|
|
private struct EmptyDebugPayload: Decodable {}
|