Files
Sybil-2/ios/Tests/SybilUITests/QuickQuestionSelectionUITests.swift
T

104 lines
4.5 KiB
Swift
Raw Normal View History

import XCTest
/// Exercises Quick Question through its real home-screen action and shared SwiftUI field.
@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 testQuickQuestionKeepsKeyboardVisibleAcrossSeparateKeypressesAndStaysCompact() {
app.launch()
openQuickQuestionFromHomeScreen()
let prompt = quickQuestionPrompt()
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
XCTAssertLessThan(prompt.frame.height, 80, "Quick Question should retain its original compact single-line field.")
prompt.typeText("a")
XCTAssertTrue(app.keyboards.firstMatch.exists, "Keyboard should remain visible after the first keypress.")
prompt.typeText("b")
XCTAssertTrue(app.keyboards.firstMatch.exists, "Keyboard should remain visible after the second keypress.")
prompt.typeText("c")
XCTAssertTrue(app.keyboards.firstMatch.exists, "Keyboard should remain visible after the third keypress.")
XCTAssertEqual(prompt.value as? String, "abc")
}
func testQuickQuestionDismissalAndReappearanceSelectsRetainedDraftForReplacement() {
app.launch()
openQuickQuestionFromHomeScreen()
let prompt = quickQuestionPrompt()
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
prompt.typeText("retained draft")
XCTAssertEqual(prompt.value as? String, "retained draft")
dismissQuickQuestion()
let dismissed = XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == false"), object: prompt)
wait(for: [dismissed], timeout: 5)
// Reopen through the same home-screen Quick Question route after the
// sheet has actually disappeared.
openQuickQuestionFromHomeScreen()
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
XCTAssertEqual(prompt.value as? String, "retained draft")
prompt.typeText("replacement")
XCTAssertEqual(prompt.value as? String, "replacement")
}
private func quickQuestionPrompt() -> XCUIElement {
app.textFields["Ask anything..."]
}
private func dismissQuickQuestion() {
let header = app.staticTexts["Quick question"]
XCTAssertTrue(header.waitForExistence(timeout: 5))
let dismissStart = header.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
let dismissEnd = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.9))
dismissStart.press(forDuration: 0.1, thenDragTo: dismissEnd)
}
private func openQuickQuestionFromHomeScreen() {
XCUIDevice.shared.press(.home)
let sybilIcon = springboard.icons.matching(identifier: "Sybil").firstMatch
XCTAssertTrue(sybilIcon.waitForExistence(timeout: 10))
let homeScreen = springboard.otherElements["Home screen icons"]
if !sybilIcon.isHittable, homeScreen.waitForExistence(timeout: 5) {
for _ in 0..<2 where !sybilIcon.isHittable {
homeScreen.swipeLeft()
}
}
XCTAssertTrue(sybilIcon.isHittable, "Navigate to the home-screen page containing Sybil before opening its quick action.")
sybilIcon.press(forDuration: 1.2)
let quickQuestion = springboard.buttons["Quick question"]
XCTAssertTrue(quickQuestion.waitForExistence(timeout: 5))
quickQuestion.tap()
}
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)
}
}