import Foundation /// 离线验证两种编译模式的设备请求策略;不创建 URLSession、不发送网络请求。 @main struct DroneDockBuildPolicyCheck { static func main() throws { #if DEBUG let blocked = true let mode = "Debug" #else let blocked = false let mode = "Release" #endif precondition(DeviceCommandDebugPolicy.blocksFlightRequests == blocked) precondition(DeviceCommandDebugPolicy.blocksLiveRequests == blocked) let paths = ["/api/yf-handset-app/photog/multi-plane/tasks/create", "/api/app/airport/shoot", "/api/app/airport/start-shoot", "/api/app/airport/command"] for path in paths { try check(path, method: "POST", shouldBlock: blocked) try check(path, method: "GET", shouldBlock: true) } try check("/api/app/airport/live", method: "GET", shouldBlock: blocked) try check("/api/app/airport/live", method: "POST", shouldBlock: true) try check("/api/app/airport/future-control", method: "POST", shouldBlock: true) let read = request("/api/app/airport/shooting?id=501&live_battery=1", method: "GET") let prepared = try DeviceCommandDebugPolicy.prepare(read, isLocalSimulation: false) precondition(prepared.url?.query?.contains("live_battery=0") == true) let trace = DroneDockRequestTrace(request: prepared, isSimulation: true)! let logged = trace.message("offline_check", fields: ["token": "TEST_TOKEN", "play": ["url": "TEST_RTC"]]) precondition(logged.contains("TEST_TOKEN") && logged.contains("TEST_RTC")) precondition(logged.contains(mode) && logged.contains("501")) print("PASS \(mode): flight_blocked=\(blocked), unknown operations blocked, cached status, complete diagnostics. No network sent.") } private static func request(_ path: String, method: String) -> URLRequest { var value = URLRequest(url: URL(string: "https://example.invalid" + path)!) value.httpMethod = method return value } private static func check(_ path: String, method: String, shouldBlock: Bool) throws { do { _ = try DeviceCommandDebugPolicy.prepare(request(path, method: method), isLocalSimulation: false) precondition(!shouldBlock, "Unexpected allow: \(method) \(path)") } catch is DeviceCommandDebugBlockedError { precondition(shouldBlock, "Unexpected block: \(method) \(path)") } } }