Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Wallet9// File : UITests/AgentsUITests.swift10// Description : Interface tests of the trading agents against the11// THROWAWAY local stack (dev-stack.sh up-fork: random keys,12// every live lock off, software passkey, Base = local fork13// with fake ETH). They create agents (template, custom perp,14// custom spot), check the mode badges, start and stop on15// paper, run a backtest and approve its version, keep the16// live form's button disabled until every limit is typed,17// and — on the fork only, with fake funds — review and18// approve a spot mandate, then stop. The kill switch test19// runs last (it leaves the switch engaged on the stack).20// Skipped when the stack is not running. No test ever21// touches production.22// Module : UITests23// Repository : https://git.spboucher.ai/sp-wallet-ios24// Created : 2026-11-1825// Last Modified : 2026-11-1826//27// =============================================================================28// AUTHOR & PROFESSIONAL AFFILIATION29// =============================================================================30//31// Author : Simon-Pierre Boucher32// Position : Professor of Finance33// Institution : Université du Québec en Outaouais (UQO)34// Department : Département des sciences administratives35// Organization : SP Boucher36//37// Website : https://spboucher.ai38// Git : https://git.spboucher.ai39//40// =============================================================================41// OFFICIAL CONTACT DIRECTORY42// =============================================================================43//44// General : info@spboucher.ai45// Professional : contact@spboucher.ai46// Direct : simon@spboucher.ai47// Research : research@spboucher.ai48// Development : dev@spboucher.ai49// Support : support@spboucher.ai50// Security/Admin : admin@spboucher.ai51// Notifications : notifications@spboucher.ai52//53// Preferred Contact: support@spboucher.ai54//55// =============================================================================56// COPYRIGHT & INTELLECTUAL PROPERTY57// =============================================================================58//59// Copyright (c) 202660// Simon-Pierre Boucher61//62// All rights reserved where applicable.63//64// This file is governed by the applicable project license,65// copyright notices, institutional agreements and relevant law.66//67// Unless expressly authorized by the applicable license or68// rights holder, copying, redistribution, sublicensing,69// publication and commercial exploitation are not permitted.70//71// Existing third-party rights and notices remain unaffected.72//73// =============================================================================74// CONFIDENTIALITY & SECURITY75// =============================================================================76//77// Classification : Public78// Distribution : Public repository; use is governed by the project license79// Security Contact : admin@spboucher.ai80//81// Access to this source does not independently grant permission82// to redistribute or disclose confidential materials.83//84// Do not embed credentials, private keys, access tokens,85// certificates or confidential personal information.86//87// =============================================================================88// LICENSE & MAINTENANCE89// =============================================================================90//91// License : None specified (no license file in this repository)92// Maintainer : Simon-Pierre Boucher93// Technical Contact: dev@spboucher.ai94//95// =============================================================================96// END OF SP BOUCHER SOURCE HEADER97// =============================================================================9899import XCTest100101final class AgentsUITests: XCTestCase {102 override func setUpWithError() throws {103 continueAfterFailure = false104 try XCTSkipUnless(Stack.isUp, "throwaway stack not running (../SPWalletCore/Scripts/dev-stack.sh up-fork)")105 }106107 /** What was on screen when an assertion failed. */108 override func record(_ issue: XCTIssue) {109 shot("agents-failure")110 super.record(issue)111 }112113 // MARK: helpers114115 private func any(_ app: XCUIApplication, _ id: String) -> XCUIElement { app.descendants(matching: .any)[id].firstMatch }116117 private func openAgents(_ app: XCUIApplication) {118 if isPad { go(app, "agents") }119 else {120 go(app, "explore")121 let row = any(app, "explore.agents")122 XCTAssertTrue(row.waitForExistence(timeout: 10)); row.tap()123 }124 XCTAssertTrue(app.buttons["agents.kill.engage"].waitForExistence(timeout: 20) || any(app, "agents.kill.banner").exists, "the kill switch card heads the section")125 }126127 /** Small upward drag inside the scroll view, above where a keyboard would be (it also dismisses the keyboard of the forms). */128 private func nudge(_ app: XCUIApplication, up: Bool = true) {129 let a = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: up ? 0.50 : 0.28)), b = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: up ? 0.26 : 0.52))130 a.press(forDuration: 0.05, thenDragTo: b)131 }132133 @discardableResult private func reach(_ app: XCUIApplication, _ e: XCUIElement, timeout: TimeInterval = 15, file: StaticString = #filePath, line: UInt = #line) -> XCUIElement {134 // lazy containers only create what is near the screen: scroll while the element is missing or covered135 var n = 0136 if !e.waitForExistence(timeout: 3) { while !e.exists, n < 10 { nudge(app); n += 1 } }137 XCTAssertTrue(e.waitForExistence(timeout: timeout), "\(e) exists", file: file, line: line)138 // "hittable" is not enough: the agent page keeps a bar (Démarrer / Arrêter) over the bottom of the scroll view, and a tap on a139 // control lying under it would land on the bar. Bring the control clearly above it first.140 let height = app.windows.firstMatch.frame.height, bar = app.buttons["agent.stop"]141 let underBar = { bar.exists && bar.isHittable && height > 0 && e.frame.midY > height * 0.74 && !["agent.stop", "agent.start", "agent.pause", "agent.resume"].contains(e.identifier) }142 while n < 18, !e.isHittable || underBar() { nudge(app); n += 1 }143 return e144 }145146 private func fill(_ app: XCUIApplication, _ id: String, _ text: String, file: StaticString = #filePath, line: UInt = #line) {147 let f = reach(app, app.textFields[id], file: file, line: line)148 f.tap(); f.typeText(text)149 }150151 private func tap(_ app: XCUIApplication, _ id: String, timeout: TimeInterval = 15, file: StaticString = #filePath, line: UInt = #line) {152 let e = reach(app, any(app, id), timeout: timeout, file: file, line: line)153 // a control that waits for server data is disabled until it arrives154 _ = XCTWaiter().wait(for: [XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in e.isEnabled }, object: nil)], timeout: 20)155 e.tap()156 }157158 /** The single confirmation of an action. */159 private func confirm(_ app: XCUIApplication, file: StaticString = #filePath, line: UInt = #line) {160 let b = app.buttons["agent.confirm"].firstMatch161 XCTAssertTrue(b.waitForExistence(timeout: 8), "a confirmation is asked", file: file, line: line)162 b.tap()163 }164165 private func scrollToTop(_ app: XCUIApplication) { for _ in 0..<6 { nudge(app, up: false) } }166167 private func label(_ app: XCUIApplication, _ id: String, contains text: String, timeout: TimeInterval = 20) -> Bool {168 let e = any(app, id)169 let p = NSPredicate { _, _ in e.exists && e.label.contains(text) }170 return XCTWaiter().wait(for: [XCTNSPredicateExpectation(predicate: p, object: nil)], timeout: timeout) == .completed171 }172173 private var uniqueName: String { "T" + String(Int(Date().timeIntervalSince1970) % 100_000) }174175 /** Fake ETH for an address on the LOCAL fork only (anvil on the stack's port + 2). Never a real network. */176 private func giveForkEther(_ address: String) -> Bool {177 guard let api = URLComponents(string: Stack.origin), let port = api.port, let url = URL(string: "http://127.0.0.1:\(port + 2)") else { return false }178 var req = URLRequest(url: url)179 req.httpMethod = "POST"; req.setValue("application/json", forHTTPHeaderField: "content-type")180 req.httpBody = Data(#"{"jsonrpc":"2.0","id":1,"method":"anvil_setBalance","params":["\#(address)","0x2386f26fc10000"]}"#.utf8)181 let done = DispatchSemaphore(value: 0)182 nonisolated(unsafe) var ok = false183 URLSession(configuration: .ephemeral).dataTask(with: req) { d, r, _ in ok = (r as? HTTPURLResponse)?.statusCode == 200 && d.map { String(decoding: $0, as: UTF8.self).contains("result") } == true; done.signal() }.resume()184 _ = done.wait(timeout: .now() + 5)185 return ok186 }187188 // MARK: tests (run in name order; the kill switch is last)189190 func test1TemplateAgentStartsInSimulationGoesToPaperThenStops() {191 let app = signIn(Stack.app())192 openAgents(app)193 shot("agents-01-list")194 tap(app, "agents.create")195 tap(app, "create.template.market-making")196 let name = uniqueName + " MM"197 fill(app, "create.template.name", name)198 let submit = app.buttons["create.template.submit"]199 fill(app, "create.template.capital", "1000")200 XCTAssertFalse(reach(app, submit).isEnabled, "limits not typed yet: creation stays disabled")201 for (k, v) in [("maxDailyLossPct", "2"), ("maxDataAgeMs", "5000"), ("maxGrossUsd", "1000"), ("maxPositionUsd", "500"), ("maxTotalLossPct", "5")] { fill(app, "create.template.risk.\(k)", v) }202 XCTAssertFalse(reach(app, submit).isEnabled, "strategy parameters not given yet")203 shot("agents-02-template-form")204 tap(app, "create.template.config.copy")205 XCTAssertTrue(reach(app, submit).isEnabled)206 submit.tap()207 XCTAssertTrue(label(app, "create.result.badge", contains: "SIMULATION"), "a new agent starts in simulation, as the server answered")208 shot("agents-03-created")209 tap(app, "create.open")210211 XCTAssertTrue(label(app, "agent.badge", contains: "SIMULATION"))212 shot("agents-04-page-overview")213 tap(app, "agent.start")214 let paper = app.buttons["En PAPIER (ordres simulés)"]215 XCTAssertTrue(paper.waitForExistence(timeout: 5)); paper.tap()216 confirm(app)217 XCTAssertTrue(label(app, "agent.badge", contains: "PAPIER"), "the PAPIER badge after the server confirmed the start")218 XCTAssertFalse(any(app, "agent.badge").label.contains("RÉEL"))219 XCTAssertTrue(label(app, "agent.state", contains: "En marche"))220 shot("agents-05-paper-running")221222 // market making: the negative evidence as served, and the live mandate that this version does not approve223 tap(app, "agent.tab.marketMaking")224 XCTAssertTrue(any(app, "mm.evidence").waitForExistence(timeout: 20))225 shot("agents-06-mm-evidence")226 tap(app, "agent.tab.live")227 XCTAssertTrue(reach(app, any(app, "agent.live.mm.unavailable")).exists)228 shot("agents-07-mm-live-unavailable")229 tap(app, "agent.tab.paper")230 XCTAssertTrue(any(app, "agent.band.paper").waitForExistence(timeout: 10))231 tap(app, "agent.records.paper.performance")232 XCTAssertTrue(any(app, "agent.performance.label").waitForExistence(timeout: 20), "performance is shown with the server's mode label")233 shot("agents-08-paper-performance")234235 // stop: one confirmation, no passkey, the state shown is the server's236 app.buttons["agent.stop"].tap()237 confirm(app)238 XCTAssertTrue(label(app, "agent.state", contains: "Arrêté"))239 XCTAssertTrue(any(app, "agent.outcome").exists || any(app, "agent.state").exists)240 scrollToTop(app)241 shot("agents-09-stopped")242 }243244 func test2CustomPerpAgentIsCreatedOnPaperNeverLive() {245 let app = signIn(Stack.app())246 openAgents(app)247 tap(app, "agents.create")248 tap(app, "create.custom.perp")249 let name = uniqueName + " perp"250 fill(app, "create.perp.name", name)251 tap(app, "create.perp.instrument.BTC"); tap(app, "create.perp.instrument.ETH"); tap(app, "create.perp.interval.1h")252 let submit = app.buttons["create.perp.submit"]253 for (k, v) in [("capital", "1000"), ("leverage", "2"), ("gross", "1.5"), ("position", "1"), ("dailyLoss", "3"), ("totalLoss", "10"), ("slippage", "30")] {254 fill(app, "create.perp.\(k)", v)255 }256 XCTAssertFalse(reach(app, submit).isEnabled, "one limit is still missing")257 fill(app, "create.perp.minTrade", "2")258 shot("agents-10-perp-form")259 XCTAssertTrue(reach(app, submit).isEnabled)260 submit.tap()261 XCTAssertTrue(label(app, "create.result.badge", contains: "PAPIER"))262 XCTAssertFalse(any(app, "create.result.badge").label.contains("RÉEL"))263 tap(app, "create.open")264 XCTAssertTrue(label(app, "agent.badge", contains: "PAPIER"))265 // starting without a strategy: the server's refusal is shown as it is266 tap(app, "agent.start")267 let paper = app.buttons["En PAPIER (ordres simulés)"]268 XCTAssertTrue(paper.waitForExistence(timeout: 5)); paper.tap()269 confirm(app)270 XCTAssertTrue(any(app, "agent.error").waitForExistence(timeout: 15), "the refusal surfaces")271 XCTAssertTrue(any(app, "agent.error").label.contains("no_strategy"), any(app, "agent.error").label)272 shot("agents-11-perp-start-refused")273 // perp live: the server's checklist ends the flow here (no venue account on the throwaway stack)274 tap(app, "agent.tab.live")275 tap(app, "agent.live.start")276 XCTAssertTrue(any(app, "live.perp.next").waitForExistence(timeout: 20))277 XCTAssertFalse(reach(app, app.buttons["live.perp.prepare"]).isEnabled)278 shot("agents-12-perp-live-checklist")279 scrollToTop(app)280 shot("agents-13-perp-live-top")281 app.buttons["live.close"].tap()282 }283284 func test3SpotAgentBacktestPaperThenLiveFormAndMandateOnTheFork() throws {285 let app = signIn(Stack.app())286 openAgents(app)287 tap(app, "agents.create")288 tap(app, "create.custom.spot")289 let name = uniqueName + " spot"290 fill(app, "create.spot.name", name)291 tap(app, "create.spot.network.base"); tap(app, "create.spot.cash.USDC"); tap(app, "create.spot.symbol.ETH"); tap(app, "create.spot.symbol.BTC"); tap(app, "create.spot.interval.1h")292 for (k, v) in [("capital", "100"), ("exposure", "50"), ("loss", "10"), ("maxOrder", "20"), ("maxDaily", "50"), ("orders", "5"), ("days", "7")] { fill(app, "create.spot.\(k)", v) }293 let submit = app.buttons["create.spot.submit"]294 XCTAssertFalse(reach(app, submit).isEnabled)295 fill(app, "create.spot.slippage", "50")296 shot("agents-20-spot-form")297 XCTAssertTrue(reach(app, submit).isEnabled); submit.tap()298 XCTAssertTrue(label(app, "create.result.badge", contains: "SIMULATION"))299 tap(app, "create.open")300301 // simulation: pin a library version, wait for its backtest, read the report, approve302 tap(app, "agent.tab.simulation")303 XCTAssertTrue(any(app, "agent.band.research").waitForExistence(timeout: 10))304 tap(app, "agent.sim.library")305 tap(app, "agent.sim.register.ma_trend/3", timeout: 30)306 confirm(app)307 scrollToTop(app)308 let approve = app.buttons["agent.sim.approve"]309 XCTAssertTrue(approve.waitForExistence(timeout: 240), "the backtest report arrived")310 XCTAssertTrue(any(app, "agent.sim.report").exists, "the report is labelled as a simulation")311 XCTAssertTrue(any(app, "agent.sim.lookahead").label.contains("pass"), "the look-ahead check as served: \(any(app, "agent.sim.lookahead").label)")312 shot("agents-21-lab-report")313 reach(app, approve).tap(); confirm(app)314 XCTAssertTrue(label(app, "agent.state", contains: "Stratégie approuvée", timeout: 30))315 // a backtest on demand: the job is followed; whatever the server says about it is shown316 tap(app, "agent.sim.backtest")317 XCTAssertTrue(any(app, "agent.sim.job").waitForExistence(timeout: 20) || any(app, "agent.sim.job.refused").waitForExistence(timeout: 2))318 shot("agents-22-backtest-job")319320 tap(app, "agent.start")321 let paper = app.buttons["En PAPIER (ordres simulés)"]322 XCTAssertTrue(paper.waitForExistence(timeout: 5)); paper.tap()323 confirm(app)324 XCTAssertTrue(label(app, "agent.badge", contains: "PAPIER"))325 scrollToTop(app)326 shot("agents-23-spot-paper")327328 // vault: the full address, then fake ETH on the local fork for the gas reserve329 tap(app, "agent.tab.vault")330 let address = app.staticTexts["vault.address"]331 XCTAssertTrue(address.waitForExistence(timeout: 15))332 let vault = address.label333 XCTAssertTrue(vault.hasPrefix("0x") && vault.count == 42, "the vault address is shown in full: \(vault)")334 XCTAssertTrue(any(app, "vault.qr").exists)335 shot("agents-24-vault")336337 // going live: the button stays disabled until every limit is typed and every asset ticked338 tap(app, "agent.tab.live")339 XCTAssertTrue(any(app, "agent.band.live").waitForExistence(timeout: 10))340 tap(app, "agent.live.start")341 let prepare = app.buttons["live.spot.prepare"]342 XCTAssertFalse(reach(app, prepare).isEnabled, "empty form: nothing can be prepared")343 scrollToTop(app)344 shot("agents-25-live-form-empty")345 for (k, v) in [("budget", "100"), ("maxOrder", "20"), ("maxDaily", "50"), ("ordersPerDay", "5"), ("lossPct", "10")] {346 fill(app, "live.spot.\(k)", v)347 XCTAssertFalse(reach(app, prepare).isEnabled, "still disabled after \(k)")348 scrollToTop(app)349 }350 for s in ["USDC", "ETH", "WBTC"] { tap(app, "live.spot.asset.\(s)") }351 XCTAssertFalse(reach(app, prepare).isEnabled, "the expiry is mandatory")352 scrollToTop(app)353 fill(app, "live.spot.days", "9")354 XCTAssertFalse(reach(app, prepare).isEnabled, "a validity that is not the agent's is refused before any request")355 scrollToTop(app)356 let days = app.textFields["live.spot.days"]357 reach(app, days).tap(); days.typeText(XCUIKeyboardKey.delete.rawValue + "7")358 shot("agents-26-live-form-complete")359 XCTAssertTrue(reach(app, prepare).isEnabled, "every limit typed: the mandate can be prepared")360361 try XCTSkipUnless(Stack.hasFork, "the mandate preparation needs the local fork")362 XCTAssertTrue(giveForkEther(vault), "fake ETH on the local fork for the vault's gas reserve")363 prepare.tap()364 let reviewVault = any(app, "live.review.vault"), refused = any(app, "live.error")365 let appeared = XCTWaiter().wait(for: [XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in reviewVault.exists || refused.exists }, object: nil)], timeout: 45)366 XCTAssertEqual(appeared, .completed)367 if refused.exists {368 // the server (or the app's own verification) refused: the refusal is on screen, verbatim, and nothing can be approved369 XCTAssertFalse(refused.label.isEmpty)370 XCTAssertFalse(app.buttons["live.spot.approve"].exists)371 shot("agents-27-live-refused")372 return373 }374 XCTAssertTrue(reviewVault.label.contains(vault), "the review shows the vault in full: \(reviewVault.label)")375 shot("agents-27-live-review-top")376 XCTAssertTrue(reach(app, any(app, "live.review.line.ETH")).label.contains("non recalculable ici"))377 shot("agents-28-live-review-lines")378 XCTAssertTrue(reach(app, any(app, "live.review.engine")).label.contains("moteur"))379 XCTAssertTrue(reach(app, any(app, "live.review.digest")).exists)380 shot("agents-29-live-review-digest")381 reach(app, app.buttons["live.spot.approve"]).tap()382 XCTAssertTrue(label(app, "live.status.badge", contains: "RÉEL", timeout: 45), "after the passkey the server reports the agent live")383 XCTAssertTrue(any(app, "live.status.signer").label.contains("Actif"), "the signer reports the mandate")384 shot("agents-30-live-status")385 app.buttons["live.done"].tap()386 XCTAssertTrue(label(app, "agent.badge", contains: "argent réel"))387 scrollToTop(app)388 shot("agents-31-live-page")389390 // stop: one confirmation, no passkey; the mandate is revoked at the signer391 app.buttons["agent.stop"].tap()392 confirm(app)393 XCTAssertTrue(label(app, "agent.state", contains: "Arrêté", timeout: 30))394 XCTAssertFalse(any(app, "agent.badge").label.contains("argent réel"), "a stopped agent is not shown as trading real money")395 XCTAssertTrue(label(app, "agent.mandate.state", contains: "Révoqué", timeout: 20) || any(app, "agent.mandate.none").exists, "no mandate is in force after the stop")396 scrollToTop(app)397 shot("agents-32-live-stopped")398 }399400 func test9KillSwitchEngageShowsItsBanner() {401 let app = signIn(Stack.app())402 openAgents(app)403 let engage = app.buttons["agents.kill.engage"]404 XCTAssertTrue(engage.waitForExistence(timeout: 20), "the switch is not engaged at the start of this test")405 engage.tap()406 confirm(app)407 XCTAssertTrue(any(app, "agents.kill.banner").waitForExistence(timeout: 20), "the banner appears once the server confirmed")408 XCTAssertTrue(any(app, "agents.kill.state").label.contains("ENGAGÉ"))409 XCTAssertFalse(app.buttons["agents.kill.engage"].exists, "nothing here releases it: the release is in Risk, with the passkey")410 XCTAssertTrue(app.buttons["agents.kill.risk"].exists)411 shot("agents-40-kill-switch")412 }413}414