import XCTest /// Uses the repository's 127.0.0.1 contract fixture and the real iOS home-screen shortcut. @MainActor final class QuickQuestionSelectionUITests: XCTestCase { private let app = XCUIApplication() private let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard") override func setUp() async throws { continueAfterFailure = false try await control(["action": "reset", "count": 0]) app.launchArguments = [ "-sybil.ios.apiBaseURL", "http://127.0.0.1:8799/api", "-sybil.ios.adminToken", "fixture-token" ] } override func tearDown() async throws { app.terminate() } func testPrefilledDraftIsSelectedOnReopenAndTypingReplacesIt() async throws { app.launch() openQuickQuestionFromHomeScreen() let prompt = app.textFields["quick-question-prompt"] XCTAssertTrue(prompt.waitForExistence(timeout: 10)) XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10)) prompt.typeText("retained draft") XCTAssertEqual(prompt.value as? String, "retained draft") dismissQuickQuestion(prompt) openQuickQuestionFromHomeScreen() XCTAssertTrue(prompt.waitForExistence(timeout: 10)) XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10)) XCTAssertEqual(prompt.value as? String, "retained draft") // The field is already focused with its initial draft selected. Do not tap // it here: typing immediately is the behavior under test. prompt.typeText("replacement") XCTAssertEqual(prompt.value as? String, "replacement") } private func openQuickQuestionFromHomeScreen() { XCUIDevice.shared.press(.home) let sybilIcon = springboard.icons.matching(identifier: "Sybil").firstMatch XCTAssertTrue(sybilIcon.waitForExistence(timeout: 10)) sybilIcon.press(forDuration: 1.2) let quickQuestion = springboard.buttons["Quick question"] XCTAssertTrue(quickQuestion.waitForExistence(timeout: 5)) quickQuestion.tap() } private func dismissQuickQuestion(_ prompt: XCUIElement) { if app.keyboards.firstMatch.exists, app.scrollViews.firstMatch.exists { app.scrollViews.firstMatch.swipeDown() } let grabber = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.08)) let lowerSheet = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.92)) grabber.press(forDuration: 0.1, thenDragTo: lowerSheet) let dismissed = expectation(for: NSPredicate(format: "exists == false"), evaluatedWith: prompt) wait(for: [dismissed], timeout: 5) } private func control(_ body: [String: Any]) async throws { var request = URLRequest(url: URL(string: "http://127.0.0.1:8799/control")!) request.httpMethod = "POST" request.httpBody = try JSONSerialization.data(withJSONObject: body) request.setValue("application/json", forHTTPHeaderField: "Content-Type") let (_, response) = try await URLSession.shared.data(for: request) XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 200) } }