Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Wallet9// File : UITests/MarketsRiskUITests.swift10// Description : Interface tests of Markets and Risk against the THROWAWAY11// local stack (../SPWalletCore/Scripts/dev-stack.sh up-fork:12// random keys, every live-trading lock off, software13// passkey): the explorer loads or says honestly why not, the14// trading halt is engaged without a passkey and released15// with one, a limit is tightened with the session and16// loosened through the review and the passkey, an API key is17// created (secret shown once) and revoked. Skipped when the18// stack is not running. No test ever touches production.19// Module : UITests20// Repository : https://git.spboucher.ai/sp-wallet-ios21// Created : 2026-11-1822// Last Modified : 2026-11-1823//24// =============================================================================25// AUTHOR & PROFESSIONAL AFFILIATION26// =============================================================================27//28// Author : Simon-Pierre Boucher29// Position : Professor of Finance30// Institution : Université du Québec en Outaouais (UQO)31// Department : Département des sciences administratives32// Organization : SP Boucher33//34// Website : https://spboucher.ai35// Git : https://git.spboucher.ai36//37// =============================================================================38// OFFICIAL CONTACT DIRECTORY39// =============================================================================40//41// General : info@spboucher.ai42// Professional : contact@spboucher.ai43// Direct : simon@spboucher.ai44// Research : research@spboucher.ai45// Development : dev@spboucher.ai46// Support : support@spboucher.ai47// Security/Admin : admin@spboucher.ai48// Notifications : notifications@spboucher.ai49//50// Preferred Contact: support@spboucher.ai51//52// =============================================================================53// COPYRIGHT & INTELLECTUAL PROPERTY54// =============================================================================55//56// Copyright (c) 202657// Simon-Pierre Boucher58//59// All rights reserved where applicable.60//61// This file is governed by the applicable project license,62// copyright notices, institutional agreements and relevant law.63//64// Unless expressly authorized by the applicable license or65// rights holder, copying, redistribution, sublicensing,66// publication and commercial exploitation are not permitted.67//68// Existing third-party rights and notices remain unaffected.69//70// =============================================================================71// CONFIDENTIALITY & SECURITY72// =============================================================================73//74// Classification : Public75// Distribution : Public repository; use is governed by the project license76// Security Contact : admin@spboucher.ai77//78// Access to this source does not independently grant permission79// to redistribute or disclose confidential materials.80//81// Do not embed credentials, private keys, access tokens,82// certificates or confidential personal information.83//84// =============================================================================85// LICENSE & MAINTENANCE86// =============================================================================87//88// License : None specified (no license file in this repository)89// Maintainer : Simon-Pierre Boucher90// Technical Contact: dev@spboucher.ai91//92// =============================================================================93// END OF SP BOUCHER SOURCE HEADER94// =============================================================================9596import XCTest9798@MainActor99final class MarketsRiskUITests: XCTestCase {100 override func setUpWithError() throws {101 continueAfterFailure = false102 try XCTSkipUnless(Stack.isUp, "throwaway stack not running (../SPWalletCore/Scripts/dev-stack.sh up-fork)")103 }104105 private func open(_ app: XCUIApplication, _ section: String) {106 if isPad { go(app, section); return }107 go(app, "explore")108 let row = app.descendants(matching: .any)["explore.\(section)"].firstMatch109 XCTAssertTrue(app.navigationBars["Explorer"].waitForExistence(timeout: 10))110 var n = 0111 while !(row.exists && row.isHittable), n < 6 { app.swipeUp(velocity: .slow); n += 1 }112 XCTAssertTrue(row.exists && row.isHittable, "Explore row \(section)")113 row.tap()114 }115 private func el(_ app: XCUIApplication, _ id: String) -> XCUIElement { app.descendants(matching: .any)[id].firstMatch }116 /** Scrolls until the element can be tapped (cards far down a long screen). */117 private func reach(_ app: XCUIApplication, _ e: XCUIElement, file: StaticString = #filePath, line: UInt = #line) {118 XCTAssertTrue(e.waitForExistence(timeout: 20), "\(e) exists", file: file, line: line)119 var n = 0120 while !e.isHittable, n < 14 { app.swipeUp(velocity: .slow); n += 1 }121 XCTAssertTrue(e.isHittable, "\(e) reachable", file: file, line: line)122 }123 private func replaceText(_ field: XCUIElement, with text: String) {124 field.tap()125 if let v = field.value as? String, !v.isEmpty, v != field.placeholderValue { field.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: v.count)) }126 field.typeText(text)127 }128129 func testMarketsExplorerLoadsOrSaysWhyNotThenDetailPrivateAndOrders() {130 let app = signIn(Stack.app())131 open(app, "markets")132 XCTAssertTrue(app.navigationBars["Marchés"].waitForExistence(timeout: 10))133 let row = el(app, "markets.row"), table = el(app, "markets.table"), error = el(app, "markets.error")134 let deadline = Date().addingTimeInterval(40)135 while Date() < deadline, !(row.exists || table.exists || error.exists) { usleep(300_000) }136 XCTAssertTrue(row.exists || table.exists || error.exists, "the explorer shows markets, or the server's error — never an empty screen")137 sleep(4) // let the quote stream connect so that the screenshot shows the freshness marks138 shot("markets-01-explorer")139 if row.exists, !isPad {140 XCTAssertTrue(el(app, "markets.counts").exists, "the counts of the server's answer are shown")141 row.tap()142 XCTAssertTrue(el(app, "market.mark").waitForExistence(timeout: 20), "the detail shows the price with its freshness mark")143 sleep(3)144 shot("markets-02-underlying")145 app.swipeUp(); shot("markets-03-underlying-venues")146 let run = el(app, "compare.run")147 reach(app, run)148 XCTAssertFalse(run.isEnabled, "no comparison without a side, a notional, a leverage and a horizon typed by the owner")149 shot("markets-04-comparator-empty")150 app.navigationBars.buttons.firstMatch.tap()151 }152 let tabs = el(app, "markets.tab")153 XCTAssertTrue(tabs.waitForExistence(timeout: 10))154 app.buttons["Privés"].firstMatch.tap()155 let notice = el(app, "private.notice"), perror = el(app, "private.error")156 let d2 = Date().addingTimeInterval(40)157 while Date() < d2, !(notice.exists || perror.exists) { usleep(300_000) }158 XCTAssertTrue(notice.exists || perror.exists, "private markets show the display-only notice, or the server's error")159 shot("markets-05-private")160 if notice.exists, !isPad {161 let company = app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH 'private.company.'")).firstMatch162 XCTAssertTrue(company.waitForExistence(timeout: 10), "companies are listed")163 company.tap()164 let sheet = app.staticTexts["Valorisations privées de référence"]165 XCTAssertTrue(sheet.waitForExistence(timeout: 30), "the company page opens with its references")166 XCTAssertTrue(el(app, "private.implied.value").exists || el(app, "private.implied.none").exists || el(app, "private.sheet.expired").exists)167 shot("markets-05b-private-company")168 app.swipeUp(); shot("markets-05c-private-contract")169 app.navigationBars.buttons.firstMatch.tap()170 }171 app.buttons["Ordres"].firstMatch.tap()172 XCTAssertTrue(el(app, "orders.empty").waitForExistence(timeout: 20) || el(app, "order.row").exists || el(app, "orders.loadError").exists)173 shot("markets-06-orders")174 }175176 func testKillSwitchEngagesWithoutPasskeyAndIsReleasedWithIt() {177 let app = signIn(Stack.app())178 open(app, "risk")179 let engage = el(app, "risk.kill.engage"), release = el(app, "risk.kill.release")180 let deadline = Date().addingTimeInterval(30)181 while Date() < deadline, !(engage.exists || release.exists) { usleep(300_000) }182 shot("risk-01-home")183 if !release.exists {184 engage.tap()185 let confirm = app.alerts.buttons["Engager maintenant"].firstMatch186 XCTAssertTrue(confirm.waitForExistence(timeout: 5), "one confirmation, no passkey")187 shot("risk-02-engage-confirm")188 confirm.tap()189 }190 XCTAssertTrue(el(app, "risk.kill.banner").waitForExistence(timeout: 20), "the server confirmed the switch is engaged: the banner says when, by whom, why")191 XCTAssertTrue(release.waitForExistence(timeout: 5))192 shot("risk-03-engaged")193 release.tap()194 XCTAssertTrue(el(app, "risk.review.title").waitForExistence(timeout: 20), "the release goes through the review of the verified intent")195 XCTAssertTrue(app.staticTexts["kill_switch"].exists || el(app, "risk.review.title").label.contains("Arrêt"))196 shot("risk-04-release-review")197 let approve = el(app, "risk.review.approve")198 reach(app, approve)199 shot("risk-05-release-review-bottom")200 approve.tap()201 XCTAssertTrue(engage.waitForExistence(timeout: 30), "after the passkey the server reports the switch released")202 XCTAssertFalse(el(app, "risk.kill.banner").exists)203 shot("risk-06-released")204 }205206 func testALimitIsTightenedWithTheSessionThenLoosenedThroughReviewAndPasskey() {207 let app = signIn(Stack.app())208 open(app, "risk")209 func edit(to value: String) {210 let open = el(app, "risk.limits.paper.edit")211 reach(app, open); open.tap()212 let field = app.textFields["risk.edit.maxOrderNotional"]213 reach(app, field)214 replaceText(field, with: value)215 let prepare = el(app, "risk.edit.prepare")216 if app.keyboards.count > 0 { app.swipeUp() }217 reach(app, prepare)218 XCTAssertTrue(prepare.isEnabled, "a change is ready to be checked by the server")219 prepare.tap()220 }221 // 1 — tightening (below the mode default and below anything a previous run stored): session only222 edit(to: "5000")223 let saveTighten = el(app, "risk.edit.saveTighten")224 XCTAssertTrue(saveTighten.waitForExistence(timeout: 20), "the server found nothing loosened: no passkey")225 shot("risk-07-tighten-confirm")226 reach(app, saveTighten); saveTighten.tap()227 var saved = el(app, "risk.saved.maxOrderNotional")228 XCTAssertTrue(saved.waitForExistence(timeout: 20))229 XCTAssertTrue(saved.label.contains("5000"), saved.label)230 el(app, "risk.edit.close").tap()231232 // 2 — loosening: the review names the limit, from → to, then the passkey233 edit(to: "6000")234 XCTAssertTrue(el(app, "risk.review.loosenedCount").waitForExistence(timeout: 20), "the server named a loosening: review before the passkey")235 let change = el(app, "risk.change.maxOrderNotional")236 XCTAssertTrue(change.exists)237 XCTAssertTrue(change.label.contains("5000") && change.label.contains("6000") && change.label.contains("ASSOUPLIE"), change.label)238 shot("risk-08-loosen-review")239 let approve = el(app, "risk.review.approve")240 reach(app, approve)241 shot("risk-09-loosen-review-bottom")242 approve.tap()243 saved = el(app, "risk.saved.maxOrderNotional")244 XCTAssertTrue(saved.waitForExistence(timeout: 30), "after the passkey the server returns the stored set")245 XCTAssertTrue(saved.label.contains("6000"), saved.label)246 shot("risk-10-loosened-stored")247 el(app, "risk.edit.close").tap()248 let detail = el(app, "risk.limits.paper.detail")249 reach(app, detail); detail.tap()250 let limit = el(app, "risk.limit.maxOrderNotional")251 XCTAssertTrue(limit.waitForExistence(timeout: 10))252 XCTAssertTrue(limit.label.contains("6000"), "the limit page shows what is stored: \(limit.label)")253 shot("risk-11-limits-detail")254 }255256 func testAnAPIKeyIsCreatedItsSecretShownOnceThenRevoked() {257 let app = signIn(Stack.app())258 open(app, "risk")259 let link = el(app, "risk.link.developer")260 reach(app, link); link.tap()261 let new = el(app, "developer.new")262 reach(app, new)263 shot("developer-01-keys")264 new.tap()265 let name = "ui-\(Int(Date().timeIntervalSince1970) % 100000)"266 let nameField = app.textFields["developer.form.name"]267 XCTAssertTrue(nameField.waitForExistence(timeout: 10))268 let prepare = el(app, "developer.form.prepare")269 nameField.tap(); nameField.typeText(name)270 let days = app.textFields["developer.form.days"]; days.tap(); days.typeText("7")271 let scope = el(app, "developer.form.scope.markets:read")272 reach(app, scope); scope.tap()273 let rate = app.textFields["developer.form.rate"]274 reach(app, rate); rate.tap(); rate.typeText("60")275 shot("developer-02-form")276 app.swipeUp()277 reach(app, prepare)278 XCTAssertTrue(prepare.isEnabled)279 prepare.tap()280 XCTAssertTrue(el(app, "developer.review.title").waitForExistence(timeout: 20), "the creation goes through the review of the verified intent")281 let scopes = el(app, "developer.review.scopes")282 XCTAssertTrue(scopes.exists && scopes.label.contains("markets:read"), scopes.label)283 shot("developer-03-review")284 let approve = el(app, "developer.review.approve")285 reach(app, approve); approve.tap()286 XCTAssertTrue(el(app, "developer.secret.title").waitForExistence(timeout: 30), "the secret is shown once, in its own sheet")287 let secret = el(app, "developer.secret.value")288 XCTAssertTrue(secret.waitForExistence(timeout: 5))289 XCTAssertTrue(secret.label.hasPrefix("spw_live_spk_"), "a whole token of the throwaway stack")290 XCTAssertFalse(el(app, "developer.secret.discrepancies").exists, "the key created is the key approved")291 let close = el(app, "developer.secret.close")292 reach(app, close)293 XCTAssertFalse(close.isEnabled, "closing asks first that the secret was put away")294 shot("developer-04-secret-once")295 el(app, "developer.secret.saved").tap()296 close.tap()297 XCTAssertTrue(app.staticTexts[name].waitForExistence(timeout: 20), "the key is in the list")298 XCTAssertFalse(app.staticTexts.matching(NSPredicate(format: "label BEGINSWITH 'spw_live_'")).firstMatch.exists, "the secret is nowhere after the sheet closed")299 // the change form of a key opens (pushed destination), then back300 let editKey = app.buttons.matching(identifier: "developer.key.edit").firstMatch301 reach(app, editKey); editKey.tap()302 XCTAssertTrue(el(app, "developer.form.save").waitForExistence(timeout: 10), "the change form opens")303 XCTAssertFalse(el(app, "developer.form.save").isEnabled, "nothing to save while nothing changed")304 shot("developer-04b-edit-form")305 app.navigationBars.buttons.firstMatch.tap()306 // rotation: review of the verified intent, passkey, a new secret shown once307 let rotate = app.buttons.matching(identifier: "developer.key.rotate").firstMatch308 reach(app, rotate); rotate.tap()309 XCTAssertTrue(el(app, "developer.review.title").waitForExistence(timeout: 20), "the rotation goes through the review")310 shot("developer-04c-rotate-review")311 let approveRotation = el(app, "developer.review.approve")312 reach(app, approveRotation); approveRotation.tap()313 XCTAssertTrue(el(app, "developer.secret.title").waitForExistence(timeout: 30), "the new secret is shown once")314 XCTAssertFalse(el(app, "developer.secret.discrepancies").exists)315 let close2 = el(app, "developer.secret.close")316 reach(app, close2); el(app, "developer.secret.saved").tap(); close2.tap()317 XCTAssertTrue(app.staticTexts[name].waitForExistence(timeout: 20))318 let revoke = app.buttons.matching(identifier: "developer.key.revoke").firstMatch319 reach(app, revoke)320 let before = app.buttons.matching(identifier: "developer.key.revoke").count321 revoke.tap()322 let confirm = app.buttons["Révoquer"].firstMatch323 XCTAssertTrue(confirm.waitForExistence(timeout: 5)); confirm.tap()324 XCTAssertTrue(el(app, "developer.notice").waitForExistence(timeout: 20), "the server answered the revocation")325 let deadline = Date().addingTimeInterval(15)326 while Date() < deadline, app.buttons.matching(identifier: "developer.key.revoke").count >= before { usleep(300_000) }327 XCTAssertLessThan(app.buttons.matching(identifier: "developer.key.revoke").count, before, "a revoked key offers no action any more")328 shot("developer-05-revoked")329 }330331 /** Every pushed destination and sheet of the Risk area opens with its environment (a missing object is a crash). */332 func testEveryRiskDestinationAndSheetOpens() {333 let app = signIn(Stack.app())334 open(app, "risk")335 func back() { app.navigationBars.buttons.firstMatch.tap() }336 let detail = el(app, "risk.limits.live.detail")337 reach(app, detail); detail.tap()338 XCTAssertTrue(el(app, "risk.limit.maxDailyLoss").waitForExistence(timeout: 10), "every limit is named and explained")339 shot("risk-12-limits-live-detail")340 back()341 let edit = el(app, "risk.limits.live.edit")342 reach(app, edit); edit.tap()343 XCTAssertTrue(el(app, "risk.edit.prepare").waitForExistence(timeout: 10))344 XCTAssertFalse(el(app, "risk.edit.prepare").isEnabled, "nothing to check while nothing changed")345 shot("risk-13-limits-editor")346 el(app, "risk.edit.close").tap()347 let journal = el(app, "risk.decisions.all")348 reach(app, journal); journal.tap()349 XCTAssertTrue(app.navigationBars["Journal des décisions"].waitForExistence(timeout: 10))350 shot("risk-14-decisions")351 back()352 let mandates = el(app, "risk.link.mandates")353 reach(app, mandates); mandates.tap()354 let new = el(app, "mandates.new")355 reach(app, new)356 shot("mandates-01-list")357 new.tap()358 let create = el(app, "mandate.new.create")359 XCTAssertTrue(el(app, "mandate.new.principalKind").waitForExistence(timeout: 10), "the creation sheet opens")360 shot("mandates-02-new-empty")361 reach(app, create)362 XCTAssertFalse(create.isEnabled, "no mandate can be created while a limit is missing: nothing is preset")363 XCTAssertTrue(el(app, "mandate.new.problems").exists)364 shot("mandates-03-new-bottom")365 el(app, "mandate.new.close").tap()366 XCTAssertTrue(new.waitForExistence(timeout: 10))367 back()368 let dev = el(app, "risk.link.developer")369 reach(app, dev); dev.tap()370 let hooks = el(app, "developer.webhooks")371 reach(app, hooks); hooks.tap()372 XCTAssertTrue(el(app, "webhook.new.create").waitForExistence(timeout: 20), "the webhooks page opens")373 XCTAssertFalse(el(app, "webhook.new.create").isEnabled)374 shot("developer-06-webhooks")375 back(); back()376 XCTAssertTrue(el(app, "risk.kill.state").waitForExistence(timeout: 10))377 shot("risk-15-home-bottom")378 }379}380