Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Wallet9// File : UITests/AlertsResearchUITests.swift10// Description : Interface tests of the alert centre, its settings, « État11// du système » and the research jobs, against the THROWAWAY12// local stack (../SPWalletCore/Scripts/dev-stack.sh13// up-fork). The alert rows are real rows of that stack (the14// restart alert it raises when it starts, then the alert of15// a transfer on the local fork). Never production.16// Module : UITests17// Repository : https://git.spboucher.ai/sp-wallet-ios18// Created : 2026-11-1819// Last Modified : 2026-11-1820//21// =============================================================================22// AUTHOR & PROFESSIONAL AFFILIATION23// =============================================================================24//25// Author : Simon-Pierre Boucher26// Position : Professor of Finance27// Institution : Université du Québec en Outaouais (UQO)28// Department : Département des sciences administratives29// Organization : SP Boucher30//31// Website : https://spboucher.ai32// Git : https://git.spboucher.ai33//34// =============================================================================35// OFFICIAL CONTACT DIRECTORY36// =============================================================================37//38// General : info@spboucher.ai39// Professional : contact@spboucher.ai40// Direct : simon@spboucher.ai41// Research : research@spboucher.ai42// Development : dev@spboucher.ai43// Support : support@spboucher.ai44// Security/Admin : admin@spboucher.ai45// Notifications : notifications@spboucher.ai46//47// Preferred Contact: support@spboucher.ai48//49// =============================================================================50// COPYRIGHT & INTELLECTUAL PROPERTY51// =============================================================================52//53// Copyright (c) 202654// Simon-Pierre Boucher55//56// All rights reserved where applicable.57//58// This file is governed by the applicable project license,59// copyright notices, institutional agreements and relevant law.60//61// Unless expressly authorized by the applicable license or62// rights holder, copying, redistribution, sublicensing,63// publication and commercial exploitation are not permitted.64//65// Existing third-party rights and notices remain unaffected.66//67// =============================================================================68// CONFIDENTIALITY & SECURITY69// =============================================================================70//71// Classification : Public72// Distribution : Public repository; use is governed by the project license73// Security Contact : admin@spboucher.ai74//75// Access to this source does not independently grant permission76// to redistribute or disclose confidential materials.77//78// Do not embed credentials, private keys, access tokens,79// certificates or confidential personal information.80//81// =============================================================================82// LICENSE & MAINTENANCE83// =============================================================================84//85// License : None specified (no license file in this repository)86// Maintainer : Simon-Pierre Boucher87// Technical Contact: dev@spboucher.ai88//89// =============================================================================90// END OF SP BOUCHER SOURCE HEADER91// =============================================================================9293import XCTest9495@MainActor final class AlertsResearchUITests: XCTestCase {96 override func setUpWithError() throws {97 continueAfterFailure = false98 try XCTSkipUnless(Stack.isUp, "throwaway stack not running (../SPWalletCore/Scripts/dev-stack.sh up-fork)")99 }100101 /** Alerts and Research are not tab-bar sections on iPhone: they are reached from Explore (or the Home bell). */102 private func open(_ app: XCUIApplication, _ section: String) {103 if isPad { go(app, section); return }104 go(app, "explore")105 let entry = app.descendants(matching: .any)["explore.\(section)"].firstMatch106 // the Explore list is lazy: an entry lower in the list only exists once scrolled to107 for _ in 0..<5 where !entry.waitForExistence(timeout: 2) || !entry.isHittable { app.swipeUp(velocity: .slow) }108 XCTAssertTrue(entry.exists, "explore entry \(section)")109 entry.tap()110 }111112 /** Throwaway fork only (anvil, stack port + 2): mine blocks so that the server sees the confirmations it requires. */113 private func mineForkBlocks() {114 guard let o = URL(string: Stack.origin), let port = o.port, let u = URL(string: "http://127.0.0.1:\(port + 2)") else { return }115 var r = URLRequest(url: u); r.httpMethod = "POST"; r.setValue("application/json", forHTTPHeaderField: "content-type")116 r.httpBody = Data(#"{"jsonrpc":"2.0","id":1,"method":"anvil_mine","params":["0x40"]}"#.utf8)117 let done = DispatchSemaphore(value: 0)118 URLSession(configuration: .ephemeral).dataTask(with: r) { _, _, _ in done.signal() }.resume()119 _ = done.wait(timeout: .now() + 5)120 }121122 private func wait(_ what: String, timeout: TimeInterval, _ cond: () -> Bool) {123 let end = Date().addingTimeInterval(timeout)124 while Date() < end { if cond() { return }; usleep(400_000) }125 XCTFail("timed out: \(what)")126 }127128 func testAlertCentreDetailReadStateSystemStateAndSettings() {129 let app = signIn(Stack.app())130 // the bell shows the SERVER's unread count once the first poll answered (the stack raised « Service redémarré » at start)131 let bell = app.buttons["home.bell"]132 XCTAssertTrue(bell.waitForExistence(timeout: 10))133 wait("bell shows the unread count", timeout: 15) { bell.label.contains("non lue") }134 shot("ops-01-home-bell")135 open(app, "alerts") // through Explore on iPhone: the path most owners take (the bell itself is tapped in the transfer test)136 XCTAssertTrue(app.navigationBars["Alertes"].waitForExistence(timeout: 8))137 let row = app.descendants(matching: .any)["alerts.row"].firstMatch138 XCTAssertTrue(row.waitForExistence(timeout: 10), "the server's alert rows are listed")139 let unread = app.staticTexts["alerts.unread"]140 XCTAssertTrue(unread.waitForExistence(timeout: 5))141 let before = unread.label142 XCTAssertTrue(before.contains("non lue"), before)143 shot("ops-02-alerts-list")144145 // group filter: asks the server again; « Dérivés » has nothing on this stack and says so146 app.buttons["alerts.filter.derivatives"].tap()147 XCTAssertTrue(app.staticTexts["Aucune alerte pour ce filtre"].waitForExistence(timeout: 8))148 app.buttons["alerts.filter.all"].tap()149 XCTAssertTrue(row.waitForExistence(timeout: 8))150151 // detail: every parameter, the read state as the server returned it, and an in-app destination152 // the « non lues » filter asks the server for unread rows only (they may be far down the unfiltered list)153 app.buttons["alerts.filter.unread"].tap()154 let unreadRow = app.descendants(matching: .any)["alerts.row"].firstMatch155 XCTAssertTrue(unreadRow.waitForExistence(timeout: 8), "an unread row (the bell counted at least one)")156 unreadRow.tap()157 XCTAssertTrue(app.staticTexts["alert.title"].waitForExistence(timeout: 8))158 XCTAssertTrue(app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS 'Type (code du serveur)'")).firstMatch.exists)159 shot("ops-03-alert-detail")160 if !isPad { app.navigationBars.buttons.firstMatch.tap() }161 // opening the detail marked the row read ON THE SERVER; the list shows the server's new count162 wait("unread count changed after reading", timeout: 12) { unread.exists && unread.label != before }163 shot("ops-07-alerts-after-read")164 app.buttons["alerts.filter.unread"].tap()165 XCTAssertTrue(row.waitForExistence(timeout: 8))166167 // a system alert links to « État du système » (navigation only)168 let restarted = app.descendants(matching: .any).matching(NSPredicate(format: "identifier == 'alerts.row' AND label CONTAINS 'Service redémarré'")).firstMatch169 for _ in 0..<4 where !restarted.exists || !restarted.isHittable { app.swipeUp(velocity: .slow) }170 XCTAssertTrue(restarted.exists)171 restarted.tap()172 let openLink = app.buttons["alert.open"]173 XCTAssertTrue(openLink.waitForExistence(timeout: 6))174 XCTAssertTrue(openLink.label.contains("État du système"), openLink.label)175 openLink.tap()176 XCTAssertTrue(app.descendants(matching: .any)["monitor.health"].firstMatch.waitForExistence(timeout: 15))177 XCTAssertTrue(app.staticTexts["OBSERVÉ"].firstMatch.exists, "figures carry their nature")178 shot("ops-04-system-state-health")179 let estimate = app.staticTexts["ESTIMATION"].firstMatch180 for _ in 0..<14 where !estimate.exists { app.swipeUp() }181 XCTAssertTrue(estimate.exists, "capital, PnL and exposure are labelled as estimates")182 shot("ops-05-system-state-capital")183 app.swipeUp(); shot("ops-06-system-state-pnl")184 if !isPad { app.navigationBars.buttons.firstMatch.tap(); app.navigationBars.buttons.firstMatch.tap() }185 XCTAssertTrue(app.navigationBars["Alertes"].waitForExistence(timeout: 8))186187 // settings188 app.buttons["alerts.more"].tap()189 app.buttons["Réglages des alertes"].tap()190 let quiet = app.switches["alertSettings.quiet"]191 XCTAssertTrue(quiet.waitForExistence(timeout: 10))192 shot("ops-08-alert-settings")193 let rule = app.switches["alertSettings.rule.deposit.received"]194 for _ in 0..<8 where !rule.exists || !rule.isHittable { app.swipeUp(velocity: .slow) }195 XCTAssertTrue(rule.exists)196 XCTAssertEqual(rule.value as? String, "1")197 rule.switches.firstMatch.exists ? rule.switches.firstMatch.tap() : rule.tap()198 wait("the server stored the switch off", timeout: 8) { (rule.value as? String) == "0" }199 shot("ops-09-alert-rules")200 rule.switches.firstMatch.exists ? rule.switches.firstMatch.tap() : rule.tap()201 wait("the server stored the switch on again", timeout: 8) { (rule.value as? String) == "1" }202203 // webhook: the secret is shown once, then only « défini »204 let rotate = app.buttons["webhook.rotate"]205 for _ in 0..<14 where !rotate.exists || !rotate.isHittable { app.swipeUp() }206 XCTAssertTrue(rotate.exists)207 let state = app.staticTexts["webhook.secretState"]208 // a fresh stack says « non défini »; on a stack where a previous run generated one, replacing asks for a confirmation209 let hadSecret = state.label == "défini"210 XCTAssertTrue(hadSecret || state.label == "non défini", state.label)211 rotate.tap()212 if hadSecret {213 let confirm = app.buttons["Remplacer : l’ancien secret cesse de fonctionner"].firstMatch214 XCTAssertTrue(confirm.waitForExistence(timeout: 4)); confirm.tap()215 }216 let secret = app.staticTexts["webhook.secret"]217 XCTAssertTrue(secret.waitForExistence(timeout: 10), "the new secret is shown once")218 XCTAssertGreaterThanOrEqual(secret.label.count, 32)219 wait("the server now reports a secret", timeout: 8) { state.label == "défini" }220 shot("ops-10-webhook-secret-once")221 app.buttons["webhook.dismissSecret"].tap()222 XCTAssertFalse(secret.exists, "after dismissal the secret is gone from the screen")223 XCTAssertFalse(app.buttons["webhook.test"].isEnabled, "no test call without a URL")224225 if !isPad { app.navigationBars.buttons.firstMatch.tap() }226 XCTAssertTrue(app.navigationBars["Alertes"].waitForExistence(timeout: 8))227 shot("ops-11-alerts-final")228 }229230 func testATransferOnTheForkBecomesARealAlertRowAndTheBellCounts() throws {231 try XCTSkipUnless(Stack.hasFork, "needs the local fork (dev-stack.sh up-fork)")232 let app = signIn(Stack.app())233 let bell = app.buttons["home.bell"]234 XCTAssertTrue(bell.waitForExistence(timeout: 10))235 // the existing Send flow, end to end, on the local fork (fake ETH)236 if isPad { app.buttons["Envoyer"].firstMatch.tap() } else { app.buttons["quick.send"].tap() }237 let base = app.buttons["network.base"]238 XCTAssertTrue(base.waitForExistence(timeout: 10)); base.tap()239 let to = "0x" + (0..<20).map { _ in String(format: "%02x", UInt8.random(in: 0...255)) }.joined()240 let recipient = app.textViews["send.recipient"].exists ? app.textViews["send.recipient"] : app.textFields["send.recipient"]241 XCTAssertTrue(recipient.waitForExistence(timeout: 5))242 recipient.tap(); recipient.typeText(to)243 let amount = app.textFields["send.amount"]244 amount.tap(); amount.typeText("0.001")245 let prepare = app.buttons["send.prepare"]246 if app.buttons["send.keyboardDone"].exists { app.buttons["send.keyboardDone"].tap() }247 if !prepare.isHittable { app.swipeUp() }248 XCTAssertTrue(prepare.waitForExistence(timeout: 5)); prepare.tap()249 if !app.staticTexts["review.amount"].waitForExistence(timeout: 30) { shot("ops-12a-send-not-prepared") }250 XCTAssertTrue(app.staticTexts["review.amount"].exists)251 app.swipeUp(); app.swipeUp()252 let approve = app.buttons["send.approve"]253 XCTAssertTrue(approve.waitForExistence(timeout: 5)); approve.tap()254 XCTAssertTrue(app.descendants(matching: .any)["tracker.state"].firstMatch.waitForExistence(timeout: 40))255 app.buttons["send.done"].tap()256 mineForkBlocks() // the local fork only mines on demand: give the transfer its confirmations257258 // nothing is shown from the locally observed proposal: the alert appears only when the SERVER has written its row259 open(app, "alerts")260 XCTAssertTrue(app.navigationBars["Alertes"].waitForExistence(timeout: 8))261 app.buttons["alerts.filter.transfers"].tap()262 let confirmed = app.descendants(matching: .any).matching(NSPredicate(format: "identifier == 'alerts.row' AND label CONTAINS 'Transfert confirmé'")).firstMatch263 wait("the server's « Transfert confirmé » row", timeout: 150) {264 if confirmed.exists { return true }265 app.swipeDown() // pull to refresh: asks the server again266 return confirmed.waitForExistence(timeout: 4)267 }268 XCTAssertTrue(confirmed.label.contains("0,001") || confirmed.label.contains("0.001"), "the catalogue wording with the server's parameters: \(confirmed.label)")269 shot("ops-12-transfer-alert")270 go(app, "home")271 wait("the bell counts the unread transfer alert", timeout: 75) { bell.label.contains("non lue") }272 shot("ops-13-bell-after-transfer")273 bell.tap()274 XCTAssertTrue(app.navigationBars["Alertes"].waitForExistence(timeout: 8), "the bell opens the alert centre")275 }276277 func testResearchStatusCreateJobFollowAndCancel() {278 let app = signIn(Stack.app())279 open(app, "research")280 XCTAssertTrue(app.navigationBars["Recherche"].waitForExistence(timeout: 8))281 XCTAssertTrue(app.descendants(matching: .any)["research.status"].firstMatch.waitForExistence(timeout: 10), "runner status as served")282 XCTAssertTrue(app.descendants(matching: .any)["research.footer"].firstMatch.exists)283 shot("ops-20-research-home")284285 app.buttons["research.new"].tap()286 let mission = app.textViews["research.mission"].exists ? app.textViews["research.mission"] : app.textFields["research.mission"]287 XCTAssertTrue(mission.waitForExistence(timeout: 5))288 let create = app.buttons["research.create"]289 func pick(_ id: String, _ label: String, _ option: String) {290 // the form's menu pickers still say « à choisir » until a value is picked: the first one left is the next to fill291 _ = (id, label)292 let p = app.buttons.matching(NSPredicate(format: "label CONTAINS %@ OR value CONTAINS %@", "à choisir", "à choisir")).firstMatch293 XCTAssertTrue(p.waitForExistence(timeout: 4), "picker \(label)")294 p.tap()295 let o = app.buttons[option].firstMatch296 XCTAssertTrue(o.waitForExistence(timeout: 4), option); o.tap()297 }298 func type(_ id: String, _ text: String) {299 let f = app.textFields[id]300 XCTAssertTrue(f.waitForExistence(timeout: 4), id)301 // bring the field between the navigation bar and the keyboard with small drags (a swipe overshoots)302 for _ in 0..<8 {303 let kb = app.keyboards.firstMatch, top = kb.exists ? kb.frame.minY : app.frame.maxY - 60304 if f.frame.minY < 130 { app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.3)).press(forDuration: 0.05, thenDragTo: app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.42))) }305 else if f.frame.maxY > top - 12 { app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.42)).press(forDuration: 0.05, thenDragTo: app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.3))) }306 else { break }307 }308 f.tap(); f.typeText(text)309 }310 shot("ops-21a-research-create-empty")311 pick("research.agent", "Agent", "Aucun agent (brouillon de bibliothèque)")312 pick("research.kind", "Marché", "Comptant (spot)")313 pick("research.interval", "Intervalle", "1d")314 mission.tap(); mission.typeText("Chercher une regle de momentum simple sur BTC")315 type("research.symbols", "BTC")316 type("research.capital", "1000")317 type("research.minutes", "5"); type("research.trials", "10"); type("research.tokens", "1"); type("research.usd", "1")318 shot("ops-21-research-create")319 XCTAssertFalse(create.exists && create.isEnabled, "a budget is still missing: nothing can be sent")320 type("research.disk", "64")321 app.swipeUp()322 for _ in 0..<5 where !create.exists || !create.isHittable { app.swipeUp() }323 XCTAssertTrue(create.isEnabled)324 create.tap()325326 let job = app.descendants(matching: .any)["research.row"].firstMatch327 if !job.waitForExistence(timeout: 15) { shot("ops-21b-create-not-listed") }328 XCTAssertTrue(job.exists, "the created job is listed from the server")329 shot("ops-22-research-list")330 if !isPad { job.tap() }331 let state = app.descendants(matching: .any)["research.state"].firstMatch332 if !state.waitForExistence(timeout: 10) { shot("ops-22b-job-not-open") }333 XCTAssertTrue(state.exists)334 XCTAssertTrue(state.label.contains("En file d’attente"), "no runner on this stack: the job stays queued — \(state.label)")335 XCTAssertTrue(app.descendants(matching: .any)["research.footer"].firstMatch.exists, "the fixed footer")336 shot("ops-23-research-job")337 let cancel = app.buttons["research.cancel"]338 for _ in 0..<4 where !cancel.exists || !cancel.isHittable { app.swipeUp(velocity: .slow) }339 cancel.tap()340 let sheet = app.sheets.buttons["Annuler la tâche"].firstMatch, popover = app.buttons.matching(NSPredicate(format: "label == %@", "Annuler la tâche")).allElementsBoundByIndex341 if sheet.waitForExistence(timeout: 4) { sheet.tap() } else { popover.last?.tap() }342 app.swipeDown()343 wait("the server answered « cancelled »", timeout: 12) { state.exists && state.label.contains("Annulée") }344 XCTAssertFalse(cancel.exists, "a cancelled job offers no cancel")345 shot("ops-24-research-cancelled")346 }347}348