TeraSubmissionQualificationSupport.swift (11177B)
1 import XCTest 2 3 extension TeraRemoteQualificationUITests { 4 @MainActor 5 func beginNewDraft(_ action: XCUIElement) -> Bool { 6 let ready = NSPredicate { _, _ in 7 action.exists && action.isEnabled && action.isHittable 8 } 9 guard XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: ready, object: action)], timeout: 10) == .completed else { return false } 10 action.tap() 11 // New may first preserve the old editor through an asynchronous durable 12 // save. Editing before it settles intentionally cancels that replacement. 13 return XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: ready, object: action)], timeout: 60) == .completed 14 } 15 16 @MainActor 17 func enterExactText(_ field: XCUIElement, value: String) { 18 field.typeText(value) 19 XCTAssertEqual(field.value as? String, value, "The fresh form must contain only the supplied field value") 20 } 21 22 @MainActor 23 func openAdd(_ app: XCUIApplication) -> XCUIElement? { 24 let add = app.tabBars.buttons["Add"] 25 guard add.waitForExistence(timeout: 10) else { return nil } 26 let root = app.descendants(matching: .any)["radroots.add.root"] 27 let type = app.descendants(matching: .any)["radroots.add.type"] 28 for _ in 0 ..< 3 { 29 add.tap() 30 guard root.waitForExistence(timeout: 10) else { continue } 31 // Public disclosure precedes the picker in the lazy native Form. 32 // Reveal its actual row before requiring existence and interaction. 33 scrollTo(app, element: type) 34 if type.waitForExistence(timeout: 10), type.isHittable { 35 return type 36 } 37 } 38 return nil 39 } 40 41 @MainActor 42 func recordAccessibilityIssue(_ issue: XCUIAccessibilityAuditIssue) { 43 let description = [ 44 "Audit type: \(issue.auditType.rawValue)", 45 issue.compactDescription, 46 issue.detailedDescription, 47 issue.element?.debugDescription ?? "No associated accessibility element", 48 ].joined(separator: "\n") 49 let attachment = XCTAttachment(string: description) 50 attachment.name = "Accessibility issue details" 51 attachment.lifetime = .keepAlways 52 add(attachment) 53 } 54 55 @MainActor 56 func assertDraftOutboxContainsAtLeast(_ app: XCUIApplication, count: Int) { 57 let sheet = qualificationDraftsSheet(app) 58 let list = sheet.descendants(matching: .collectionView).firstMatch 59 guard list.waitForExistence(timeout: 10) else { 60 return XCTFail("The visible Drafts list was unavailable") 61 } 62 63 // This sheet has just opened at its first page. Swiping downward at the 64 // top invokes native sheet dismissal rather than revealing saved rows. 65 var identifiers = Set<String>() 66 let rows = sheet.descendants(matching: .any).matching( 67 NSPredicate(format: "identifier BEGINSWITH 'tera.add.submission.' AND NOT identifier CONTAINS '.progress'") 68 ) 69 for _ in 0 ..< 12 where identifiers.count < count { 70 for index in 0 ..< rows.count { 71 let row = rows.element(boundBy: index) 72 guard row.frame.height > 0, row.frame.intersects(list.frame) else { continue } 73 let identifier = row.identifier 74 if identifier.range(of: "^tera\\.add\\.submission\\.[0-9a-f]{32}$", options: .regularExpression) != nil { 75 identifiers.insert(identifier) 76 } 77 } 78 if identifiers.count < count { 79 // Overlapping, settled drags keep every row in the measured viewport. 80 let start = list.coordinate(withNormalizedOffset: CGVector(dx: 0.05, dy: 0.75)) 81 let finish = list.coordinate(withNormalizedOffset: CGVector(dx: 0.05, dy: 0.25)) 82 start.press(forDuration: 0.05, thenDragTo: finish, 83 withVelocity: .slow, thenHoldForDuration: 0.3) 84 } 85 } 86 XCTAssertGreaterThanOrEqual( 87 identifiers.count, 88 count, 89 "The visible Drafts list omitted persisted outbox rows; identifiers=\(identifiers.sorted())" 90 ) 91 } 92 93 @MainActor 94 func waitForWorkToFinish( 95 _ app: XCUIApplication, 96 submit: XCUIElement, 97 status: XCUIElement, 98 priorStatusLabel: String?, 99 priorSubmitValue: String? 100 ) -> Bool { 101 let addRoot = app.descendants(matching: .any)["radroots.add.root"] 102 let progress = app.descendants(matching: .any)["radroots.add.progress"] 103 let submissionProgress = app.descendants(matching: .any)["tera.add.submission.progress"] 104 let started = NSPredicate { _, _ in 105 if progress.exists || submissionProgress.exists || addRoot.value as? String == "Working" { 106 return true 107 } 108 if status.exists, !status.label.isEmpty, status.label != priorStatusLabel { 109 return true 110 } 111 return submit.exists && submit.value as? String != priorSubmitValue 112 } 113 let startExpectation = XCTNSPredicateExpectation(predicate: started, object: app) 114 _ = XCTWaiter.wait(for: [startExpectation], timeout: 5) 115 let settled = NSPredicate { _, _ in 116 addRoot.value as? String == "Ready" && !progress.exists && !submissionProgress.exists 117 } 118 let settleExpectation = XCTNSPredicateExpectation(predicate: settled, object: app) 119 return XCTWaiter.wait(for: [settleExpectation], timeout: 180) == .completed 120 } 121 122 @MainActor 123 func terminalControlShowsSettledState(_ submit: XCUIElement) -> Bool { 124 if submit.isEnabled { 125 return true 126 } 127 // Both complete and temporarily deferred work disable a new effect. 128 // Successful qualification still requires actual policy completion below. 129 let recognized = ["Delivery policy complete", "Publication waiting"].contains(submit.label) 130 XCTAssertTrue(recognized, "Unexpected inactive publication control: \(submit.label)") 131 return recognized 132 } 133 134 @MainActor 135 func finishOriginalPublication(_ app: XCUIApplication, submit: XCUIElement) -> Bool { 136 for _ in 0 ..< 3 { 137 scrollTo(app, element: submit) 138 guard submit.waitForExistence(timeout: 10) else { return false } 139 if submit.label == "Delivery policy complete" { 140 return true 141 } 142 guard submit.label == "Publication waiting" else { 143 XCTFail("The original publication did not complete: \(submit.label)") 144 return false 145 } 146 let priorValue = submit.value as? String 147 let check = app.buttons["Check saved submission status"] 148 TeraAccessibilityNavigation.scroll(app, to: check, up: false, test: self) 149 guard check.waitForExistence(timeout: 10), check.isHittable else { return false } 150 check.tap() 151 guard waitForWorkToFinish(app, submit: submit, status: app.staticTexts["tera.add.submission.status"], 152 priorStatusLabel: nil, priorSubmitValue: priorValue) else { return false } 153 scrollTo(app, element: submit) 154 guard submit.waitForExistence(timeout: 10) else { return false } 155 if submit.isEnabled { 156 guard let ready = readySubmit(app), submitAndWait(app, submit: ready) != nil else { return false } 157 } 158 } 159 scrollTo(app, element: submit) 160 let completed = submit.exists && submit.label == "Delivery policy complete" 161 XCTAssertTrue(completed, "The original publication did not complete within three visible status/continuation attempts") 162 return completed 163 } 164 165 @MainActor 166 func continueSavedPersonaSubmission(_ app: XCUIApplication, marker: String) -> Bool { 167 let sheet = qualificationDraftsSheet(app) 168 let view = sheet.buttons["View submission"].firstMatch 169 guard revealSavedSubmissionControl(sheet, element: view) else { return false } 170 view.tap() 171 let captured = sheet.buttons["tera.add.submission.captured"].firstMatch 172 guard revealSavedSubmissionControl(sheet, element: captured) else { return false } 173 captured.tap() 174 guard sheet.staticTexts[marker].waitForExistence(timeout: 20) else { 175 XCTFail("The saved submission must retain the exact captured attempt marker") 176 return false 177 } 178 let continuation = sheet.buttons["tera.add.submission.continue"] 179 guard revealSavedSubmissionControl(sheet, element: continuation, searchingBackward: true), 180 continuation.waitForExistence(timeout: 20), continuation.isEnabled 181 else { 182 XCTFail("The transport-retry submission did not expose its original continuation") 183 return false 184 } 185 continuation.tap() 186 let status = sheet.staticTexts["tera.add.submission.status"] 187 let complete = NSPredicate { _, _ in 188 status.exists && status.label == "Delivery completed for the saved relay policy." 189 } 190 let expectation = XCTNSPredicateExpectation(predicate: complete, object: app) 191 let finished = XCTWaiter.wait(for: [expectation], timeout: 180) == .completed 192 XCTAssertTrue(finished, "The original saved submission did not complete its delivery policy") 193 return finished 194 } 195 196 @MainActor 197 private func qualificationDraftsSheet(_ app: XCUIApplication) -> XCUIElement { 198 app.descendants(matching: .any)["radroots.add.drafts.sheet"] 199 } 200 201 @MainActor 202 private func revealSavedSubmissionControl(_ sheet: XCUIElement, element: XCUIElement, 203 searchingBackward: Bool = false) -> Bool 204 { 205 let list = sheet.descendants(matching: .collectionView).firstMatch 206 let navigation = sheet.navigationBars.firstMatch 207 guard list.waitForExistence(timeout: 10), navigation.exists else { return false } 208 for _ in 0 ..< 12 { 209 guard sheet.exists, list.exists else { return false } 210 let top = max(list.frame.minY, navigation.frame.maxY) 211 if element.exists, element.isHittable, element.frame.height > 0, 212 element.frame.minY >= top, element.frame.maxY <= list.frame.maxY 213 { 214 return true 215 } 216 // The continuation precedes Captured form, even when its lazy row has no frame. 217 let reverse = element.exists && element.frame.height > 0 ? element.frame.minY < top : searchingBackward 218 let start = list.coordinate(withNormalizedOffset: CGVector(dx: 0.05, dy: reverse ? 0.25 : 0.75)) 219 let finish = list.coordinate(withNormalizedOffset: CGVector(dx: 0.05, dy: reverse ? 0.75 : 0.25)) 220 start.press(forDuration: 0.05, thenDragTo: finish, 221 withVelocity: .slow, thenHoldForDuration: 0.3) 222 } 223 XCTFail("The saved submission control was unavailable within the bounded native viewport") 224 return false 225 } 226 227 @MainActor 228 func assertSavedPhotoEditing(_ app: XCUIApplication) { 229 guard openDrafts(app) else { return XCTFail("The saved editing inventory did not open") } 230 let reopen = app.buttons.matching( 231 NSPredicate(format: "identifier BEGINSWITH 'tera.add.composer.' AND label == 'Reopen'") 232 ).firstMatch 233 guard reopen.waitForExistence(timeout: 20) else { 234 return XCTFail("The persisted photo editing inventory omitted its Reopen action") 235 } 236 scrollTo(app, element: reopen) 237 XCTAssertTrue(waitUntilHittable(reopen, timeout: 10)) 238 reopen.tap() 239 XCTAssertTrue(app.navigationBars["Drafts & outbox"].waitForNonExistence(timeout: 20)) 240 let description = app.descendants(matching: .any)["Describe this photo"] 241 scrollTo(app, element: description) 242 XCTAssertEqual(description.value as? String, "A green test image prepared for the photo update", 243 "Reopening persisted photo editing must retain the supplied description") 244 } 245 }