import UIKit import XCTest @testable import suixinkan /// 使用独立内存服务走完整准备与任务页面,不创建真实订单任务或下发设备指令。 @MainActor final class DroneDockPreparationSimulationTests: XCTestCase { private weak var previousKeyWindow: UIWindow? /// 仅点击后模拟创建并召唤,再读相同任务;开拍仍使用同一个隔离会话。 func testPreparationCreatesSummonsAndOpensSameMockTask() async throws { let preparation = try XCTUnwrap(DroneDockStartShootSimulation.makeController( arguments: ["--dock-preparation-mock"]) as? DroneDockRemotePreparationViewController) let window = makeWindow(preparation) defer { close(window) } let button = try button(in: preparation.view, id: "drone.preparation.summon") try await settle(window) { button.isEnabled && preparation.viewModel.planeStatus != nil } XCTAssertEqual(preparation.viewModel.selectedSpotID, 301) XCTAssertEqual(preparation.viewModel.selectedRouteID, 401) XCTAssertEqual(preparation.viewModel.planeStatus?.recommend?.id, 201) XCTAssertNil(preparation.viewModel.createdTask) XCTAssertNil(preparation.viewModel.shootResponse) button.sendActions(for: .touchUpInside) button.sendActions(for: .touchUpInside) let navigation = try XCTUnwrap(preparation.navigationController) try await settle(window) { (navigation.topViewController as? DroneDockShootingTaskViewController)?.viewModel.canStartShooting == true } let task = try XCTUnwrap(navigation.topViewController as? DroneDockShootingTaskViewController) XCTAssertEqual(navigation.viewControllers.count, 2) XCTAssertEqual(task.viewModel.taskID, 901) XCTAssertEqual(task.viewModel.order.orderNumber, "LOCAL-MOCK-ORDER-901") XCTAssertEqual(task.viewModel.context, preparation.viewModel.context) XCTAssertEqual(preparation.viewModel.createdTask?.taskID, 901) try self.button(in: task.view, id: "drone.task.remote.primary").sendActions(for: .touchUpInside) try await settle(window) { task.viewModel.hasSubmittedStart && task.viewModel.presentation.phase == .shooting } let transcript = try await records(task, window: window) let entries = try XCTUnwrap(transcript["records"] as? [[String: Any]]) let posts = entries.filter { $0["method"] as? String == "POST" } XCTAssertEqual(posts.map { $0["path"] as? String }, [ "/api/yf-handset-app/photog/multi-plane/tasks/create", "/api/app/airport/shoot", "/api/app/airport/start-shoot" ]) let creation = try XCTUnwrap(posts.first?["request_body"] as? [String: Any]) XCTAssertEqual(Set(creation.keys), ["scenic_id", "order_number", "scenic_spot_id", "air_line_id"]) XCTAssertEqual(creation["scenic_id"] as? Int, 128) XCTAssertEqual(creation["order_number"] as? String, "LOCAL-MOCK-ORDER-901") XCTAssertEqual(creation["scenic_spot_id"] as? Int, 301) XCTAssertEqual(creation["air_line_id"] as? Int, 401) let summon = try XCTUnwrap(posts[1]["request_body"] as? [String: Any]) XCTAssertEqual(Set(summon.keys), ["id", "restart"]) XCTAssertEqual(summon["id"] as? Int, 901) XCTAssertEqual(summon["restart"] as? Bool, false) XCTAssertEqual((posts[2]["request_body"] as? [String: Int]), ["id": 901]) let taskReads = entries.filter { $0["path"] as? String == "/api/app/airport/shooting" } XCTAssertEqual(taskReads.count, 2) XCTAssertTrue(taskReads.allSatisfy { ($0["query"] as? String)?.contains("id=901") == true }) XCTAssertEqual(transcript["source"] as? String, "local_mock") XCTAssertEqual(transcript["network_sent"] as? Bool, false) } /// 返回准备页后只查看原模拟任务,不重复创建或召唤,也不会自动跳转。 func testReturningToPreparationReopensTaskWithoutRepeatedWrites() async throws { let preparation = try XCTUnwrap(DroneDockStartShootSimulation.makePreparationController()) let window = makeWindow(preparation) defer { close(window) } let button = try button(in: preparation.view, id: "drone.preparation.summon") try await settle(window) { button.isEnabled } button.sendActions(for: .touchUpInside) let navigation = try XCTUnwrap(preparation.navigationController) try await settle(window) { (navigation.topViewController as? DroneDockShootingTaskViewController)?.viewModel.response != nil } navigation.popViewController(animated: false) try await settle(window) { preparation.viewModel.canOpenShootingTask && button.isEnabled } XCTAssertTrue(navigation.topViewController === preparation) XCTAssertEqual(navigation.viewControllers.count, 1) button.sendActions(for: .touchUpInside) try await settle(window) { (navigation.topViewController as? DroneDockShootingTaskViewController)?.viewModel.response != nil } let task = try XCTUnwrap(navigation.topViewController as? DroneDockShootingTaskViewController) XCTAssertEqual(task.viewModel.taskID, 901) let transcript = try await records(task, window: window) let entries = try XCTUnwrap(transcript["records"] as? [[String: Any]]) XCTAssertEqual(entries.filter { $0["method"] as? String == "POST" }.map { $0["path"] as? String }, [ "/api/yf-handset-app/photog/multi-plane/tasks/create", "/api/app/airport/shoot" ]) XCTAssertEqual(entries.filter { $0["path"] as? String == "/api/app/airport/shooting" }.count, 2) XCTAssertEqual(transcript["network_sent"] as? Bool, false) } private func records(_ controller: UIViewController, window: UIWindow) async throws -> [String: Any] { let item = try XCTUnwrap(controller.navigationItem.rightBarButtonItems?.first { $0.accessibilityIdentifier == "drone.task.mock.recordsButton" }) let action = try XCTUnwrap(item.action) _ = UIApplication.shared.sendAction(action, to: item.target, from: item, for: nil) 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) return try XCTUnwrap(JSONSerialization.jsonObject(with: Data(text.text.utf8)) as? [String: Any]) } private func button(in root: UIView, id: String) throws -> UIButton { try XCTUnwrap(allViews(root).first { $0.accessibilityIdentifier == id } as? UIButton) } 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: 300_000_000) window.layoutIfNeeded() if condition() { return } } try await Task.sleep(nanoseconds: 20_000_000) } XCTFail("本地模拟准备与任务流程未在预期时间内完成") throw NSError(domain: "DroneDockPreparationSimulationTests", code: 1) } private func allViews(_ root: UIView) -> [UIView] { [root] + root.subviews.flatMap { allViews($0) } } }