Repository Public
1// SP Wallet Core2// File: Tests/SPWalletCoreTests/OpsTests.swift3// Description: Alerts, monitoring, dashboard aggregates and research: decoding of every response shape (fixtures copied in shape4// from the TS route code — each fixture names its source), the request contract of every endpoint, the alert5// catalogue against vectors generated from the backend's own TypeScript, and the server-sent-events parser.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import Testing11@testable import SPWalletCore1213private func text(_ v: JSONValue?) throws -> String { String(decoding: try JSONEncoder().encode(try #require(v)), as: UTF8.self) }14private func ms(_ v: Double) -> Date { Date(timeIntervalSince1970: v / 1000) }1516@Suite struct AlertCatalogTests {17 // Vectors/ops-alert-catalog.json is generated from packages/core/src/alert-catalog.ts (ALERT_DEFS, renderAlert, safeParams, alertPath)18 static let vectors = try! TestResources.json("ops-alert-catalog.json", in: "Vectors")1920 @Test func theCatalogueIsTheBackends() throws {21 let defs = try #require(Self.vectors["defs"]?.arrayValue)22 #expect(defs.count == 66 && AlertCatalog.definitions.count == defs.count && AlertKind.allKnown.count == defs.count)23 for (d, mine) in zip(defs, AlertCatalog.definitions) {24 #expect(mine.kind.rawValue == d["kind"]?.stringValue)25 #expect(mine.kind.isKnown && AlertKind(rawValue: mine.kind.rawValue) == mine.kind)26 #expect(mine.group.rawValue == d["group"]?.stringValue)27 #expect(mine.severity.rawValue == d["severity"]?.stringValue)28 #expect(mine.defaultOn == d["on"]?.boolValue && mine.fixed == d["fixed"]?.boolValue)29 }30 #expect(AlertCatalog.safeParams == Self.vectors["safeParams"]?.arrayValue?.compactMap(\.stringValue))31 #expect(AlertCatalog.definitions.filter { $0.severity == .critical }.count == defs.filter { $0["severity"]?.stringValue == "critical" }.count)32 #expect(AlertCatalog.definition(.derivPositionOpened)?.defaultOn == false)33 #expect(AlertCatalog.definitions.filter(\.fixed).map(\.kind) == [.securityKillSwitchOn, .systemAlertsCapped, .systemTest])34 }3536 @Test func renderingMatchesTheBackendInBothLanguagesWithAndWithoutDetails() throws {37 let cases = try #require(Self.vectors["render"]?.arrayValue)38 #expect(cases.count == 66 * 4 + 3)39 for c in cases {40 let kind = AlertKind(rawValue: try #require(c["kind"]?.stringValue))41 let locale = try #require(AlertLocale(rawValue: c["locale"]?.stringValue ?? ""))42 let r = AlertCatalog.render(kind, params: c["params"]?.objectValue ?? [:], locale: locale, detail: c["detail"]?.boolValue ?? true)43 #expect(r.title == c["title"]?.stringValue, "title of \(kind) \(locale)")44 #expect(r.body == c["body"]?.stringValue, "body of \(kind) \(locale) detail=\(c["detail"]?.boolValue ?? true)")45 }46 #expect(AlertCatalog.safe(Self.vectors["render"]?[0]?["params"]?.objectValue ?? [:]) == Self.vectors["safe"]?.objectValue)47 }4849 @Test func withoutDetailsNoAmountPriceAddressOrReasonLeaves() {50 let p: [String: JSONValue] = ["network": "Base", "symbol": "USDC", "amount": "25", "reason": "nonce too low", "ip": "203.0.113.7", "price": "65000"]51 for d in AlertCatalog.definitions {52 let body = AlertCatalog.render(d.kind, params: p, locale: .en, detail: false).body53 for secret in ["25", "nonce too low", "203.0.113.7", "65000"] { #expect(!body.contains(secret), "\(d.kind)") }54 }55 }5657 @Test func aKindOfANewerServerIsKeptVerbatimAndNeverGuessed() throws {58 let k = try JSONDecoder().decode(AlertKind.self, from: Data(#""future.kind""#.utf8))59 #expect(k == .unknown("future.kind") && !k.isKnown && k.rawValue == "future.kind")60 #expect(String(decoding: try JSONEncoder().encode(k), as: UTF8.self) == #""future.kind""#)61 let r = AlertCatalog.render(k, params: ["amount": "1"], locale: .fr)62 #expect(r.title == "future.kind" && r.body == "")63 #expect(AlertCatalog.definition(k) == nil)64 #expect(AlertSeverity(rawValue: "urgent") == .unknown("urgent"))65 #expect(AlertSeverity.info < .warning && AlertSeverity.warning < .critical && !(AlertSeverity.unknown("x") < .info) && AlertSeverity.unknown("x") < .critical)66 }6768 @Test func deepLinksMatchTheBackend() throws {69 for c in try #require(Self.vectors["paths"]?.arrayValue) {70 let s = c["subject"].flatMap { $0.isNull ? nil : AlertSubject(type: $0["type"]?.stringValue ?? "", id: $0["id"]?.stringValue, network: nil) }71 #expect(AlertCatalog.path(kind: AlertKind(rawValue: c["kind"]?.stringValue ?? ""), subject: s) == c["path"]?.stringValue)72 }73 }74}7576@Suite struct AlertsAPITests {77 static let fx = try! TestResources.json("ops-alerts.json")7879 @Test func listDecodesRowsReadStateDeliveryAndUnknowns() async throws {80 let (client, t, _) = MockTransport.client()81 t.enqueue(json: try text(Self.fx["list"]))82 let page = try await AlertsAPI(client).list(AlertListQuery(limit: 500, before: 43, severity: .warning, group: .security, unreadOnly: true))83 let r = try #require(t.last)84 #expect(r.method == "GET" && r.path == "/api/v1/alerts")85 #expect(r.query == ["limit": "200", "before": "43", "severity": "warning", "group": "security", "unread": "1"])86 #expect(r.headers["Cookie"] == "__Host-spw=test-session-token" && r.headers["X-SPW-CSRF"] == nil)8788 #expect(page.items.count == 4 && page.counters == AlertUnread(unread: 3, critical: 0, lastId: 42) && page.nextBefore == 39)89 let a = page.items[0]90 #expect(a.id == 42 && a.kind == .transferConfirmed && a.severity == .info && a.group == .transfers && a.ts == ms(1_795_000_000_000))91 #expect(!a.isRead && a.delivery == .sent && a.channels?.push?.sent == 1 && a.channels?.webhook?.status == 200)92 #expect(a.subject == AlertSubject(type: "proposal", id: "6f1c2f0e-5a3b-4c44-9d0a-0f6f0c1d2e3f", network: "base") && a.path == "/activity")93 #expect(a.text(locale: .en).body == "25 USDC sent on Base." && a.text(locale: .fr, detail: false).body == "Sur Base.")94 let b = page.items[1]95 #expect(b.isRead && b.readAt == ms(1_794_999_500_000) && b.subject == nil && b.delivery == .quiet && b.count == 3 && b.channels?.push == nil)96 #expect(b.text(locale: .en).body == "3 attempt(s) refused, last from 203.0.113.7.")97 // the row's own severity is kept (a rule lowered this one below the kind's default)98 let c = page.items[2]99 #expect(c.kind == .derivLossLimit && c.severity == .warning && AlertCatalog.definition(c.kind)?.severity == .critical && c.delivery == .inApp)100 #expect(c.text(locale: .en).body == "Paper 1 (paper, simulated): daily loss limit reached; trading halted.")101 let d = page.items[3]102 #expect(d.kind == .unknown("future.kind_of_a_newer_server") && d.severity == .unknown("urgent") && d.delivery == .unknown("deferred"))103 #expect(d.text(locale: .en).title == "future.kind_of_a_newer_server" && d.path == "/alerts")104 }105106 @Test func defaultListSendsNoFilter() async throws {107 let (client, t, _) = MockTransport.client()108 t.enqueue(json: #"{"items":[],"unread":0,"critical":0,"lastId":0}"#)109 let page = try await AlertsAPI(client).list()110 #expect(t.last?.query == [:] && page.items.isEmpty && page.nextBefore == nil)111 }112113 @Test func unreadAndMarkRead() async throws {114 let (client, t, _) = MockTransport.client()115 let api = AlertsAPI(client)116 t.enqueue(json: try text(Self.fx["unread"]))117 #expect(try await api.unread() == AlertUnread(unread: 3, critical: 1, lastId: 42))118 #expect(t.last?.method == "GET" && t.last?.path == "/api/v1/alerts/unread")119120 t.enqueue(json: try text(Self.fx["empty"]))121 #expect(try await api.markRead(ids: [42, 40]).unread == 0)122 var r = try #require(t.last)123 #expect(r.method == "POST" && r.path == "/api/v1/alerts/read" && r.body == ["ids": [42, 40]])124 #expect(r.headers["Origin"] == "https://wallet.example.test" && r.headers["X-SPW-CSRF"] == "1")125126 t.enqueue(json: try text(Self.fx["empty"]))127 _ = try await api.markAllRead()128 r = try #require(t.last)129 #expect(r.path == "/api/v1/alerts/read" && r.body == ["all": true])130 }131132 @Test func settingsNeverCarryAWebhookSecret() async throws {133 let (client, t, _) = MockTransport.client()134 t.enqueue(json: try text(Self.fx["settings"]))135 let v = try await AlertsAPI(client).settings()136 #expect(t.last?.method == "GET" && t.last?.path == "/api/v1/alerts/settings")137 #expect(v.settings.isOn(.derivPositionOpened) == false && v.settings.isOn(.transferConfirmed) == true && v.settings.isOn(.systemTest) == nil)138 #expect(v.settings.quiet.tz == "America/Toronto" && v.settings.capPerHour == 20 && !v.settings.showDetails)139 #expect(v.settings.thresholds.stuckMinutes == 30 && v.settings.thresholds.fundingUsd == 5 && v.settings.thresholds.unknownOrderSec == 60)140 #expect(v.settings.webhook == AlertWebhookSettings(enabled: false, url: "", details: false))141 #expect(v.webhook.secretSet)142 #expect(v.kinds.count == 4 && v.kinds[1].defaultOn == false && v.kinds[2].fixed && v.kinds[3].kind == .unknown("future.kind_of_a_newer_server") && v.kinds[3].group == .unknown("future"))143 #expect(v.webhookSample?["body"]?.stringValue == "On Base.")144 // what the store would persist of the settings contains no secret field at all145 let stored = String(decoding: try APICoding.encoder().encode(v), as: UTF8.self)146 #expect(stored.contains(#""secretSet":true"#) && !stored.contains(#""secret":"#))147 }148149 @Test func updateSendsOnlyWhatChanged() async throws {150 let (client, t, _) = MockTransport.client()151 let api = AlertsAPI(client)152 t.enqueue(json: try text(Self.fx["settings"]))153 _ = try await api.update(.rule(.derivPositionOpened, on: true))154 var r = try #require(t.last)155 #expect(r.method == "PUT" && r.path == "/api/v1/alerts/settings" && r.body == ["rules": ["deriv.position_opened": true]])156157 var th = AlertSettingsPatch.Thresholds(); th.stuckMinutes = 45158 t.enqueue(json: try text(Self.fx["settings"]))159 _ = try await api.update(AlertSettingsPatch(quiet: .init(enabled: true), showDetails: true, capPerHour: 10, thresholds: th, webhook: .init(enabled: true, url: "https://hooks.example.test/spw")))160 r = try #require(t.last)161 #expect(r.body == ["quiet": ["enabled": true], "showDetails": true, "capPerHour": 10, "thresholds": ["stuckMinutes": 45], "webhook": ["enabled": true, "url": "https://hooks.example.test/spw"]])162163 t.enqueue(status: 400, json: #"{"error":"bad_request","message":"generate the webhook secret first"}"#)164 do { _ = try await api.update(AlertSettingsPatch(webhook: .init(enabled: true))); Issue.record("expected a refusal") }165 catch let e as APIError { #expect(e.code == "bad_request" && e.message == "generate the webhook secret first") }166 }167168 @Test func theWebhookSecretIsShownOnceAndTheTestSaysWhatTheReceiverAnswered() async throws {169 let (client, t, _) = MockTransport.client()170 let api = AlertsAPI(client)171 t.enqueue(json: #"{"secret":"c2VjcmV0LXNob3duLW9uY2U"}"#)172 #expect(try await api.rotateWebhookSecret().secret == "c2VjcmV0LXNob3duLW9uY2U")173 #expect(t.last?.method == "POST" && t.last?.path == "/api/v1/alerts/webhook/secret" && t.last?.body == [:])174 t.enqueue(json: try text(Self.fx["webhookTest"]))175 #expect(try await api.testWebhook() == AlertWebhookTestResult(ok: false, status: 0, error: "fetch failed"))176 #expect(t.last?.path == "/api/v1/alerts/webhook/test")177 }178179 @Test func webPushDevices() async throws {180 let (client, t, _) = MockTransport.client()181 let api = AlertsAPI(client)182 let id = "3d0c8a2e-aaaa-4bbb-8ccc-ddddeeeeffff"183 t.enqueue(json: #"{"publicKey":"BPublicVapidKey"}"#)184 #expect(try await api.pushPublicKey().publicKey == "BPublicVapidKey")185 #expect(t.last?.path == "/api/v1/push/key")186187 t.enqueue(json: try text(Self.fx["subscriptions"]))188 let subs = try await api.pushSubscriptions()189 #expect(t.last?.method == "GET" && t.last?.path == "/api/v1/push/subscriptions")190 #expect(subs.count == 1 && subs[0].service == "web.push.apple.com" && subs[0].endpointTail == "AbCdEf123456" && subs[0].lastOkAt == nil && subs[0].createdAt == ms(1_794_000_000_000))191192 t.enqueue(json: try text(Self.fx["subscriptions"]?[0]))193 _ = try await api.subscribeWebPush(.init(endpoint: "https://web.push.apple.com/abc", keys: .init(p256dh: "P", auth: "A"), label: "Safari Mac"))194 var r = try #require(t.last)195 #expect(r.method == "POST" && r.path == "/api/v1/push/subscriptions")196 #expect(r.body == ["endpoint": "https://web.push.apple.com/abc", "keys": ["p256dh": "P", "auth": "A"], "label": "Safari Mac"])197198 t.enqueue(json: try text(Self.fx["subscriptions"]?[0]))199 _ = try await api.updatePushSubscription(id: id, locale: .en)200 r = try #require(t.last)201 #expect(r.method == "PATCH" && r.path == "/api/v1/push/subscriptions/\(id)" && r.body == ["locale": "en"] && r.headers["X-SPW-CSRF"] == "1")202203 t.enqueue(json: #"{"ok":true}"#)204 try await api.deletePushSubscription(id: id)205 #expect(t.last?.method == "DELETE" && t.last?.path == "/api/v1/push/subscriptions/\(id)")206207 t.enqueue(json: try text(Self.fx["pushTest"]))208 let res = try await api.testPush(id: id)209 #expect(t.last?.path == "/api/v1/push/test" && t.last?.body == ["id": .string(id)])210 #expect(res.results == [.init(id: id, ok: false, status: 410, removed: true, error: "push service answered 410")])211 t.enqueue(json: #"{"results":[]}"#)212 _ = try await api.testPush()213 #expect(t.last?.body == [:])214 }215216 @Test func theUnreadStreamIsAnSSERequestWithTheSessionCookie() async throws {217 let (client, _, _) = MockTransport.client()218 let req = try #require(await AlertsAPI(client).streamRequest())219 #expect(req.url?.absoluteString == "https://wallet.example.test/api/v1/alerts/stream" && req.httpMethod == "GET")220 #expect(req.value(forHTTPHeaderField: "Cookie") == "__Host-spw=test-session-token" && req.value(forHTTPHeaderField: "Accept") == "text/event-stream" && !req.httpShouldHandleCookies)221 let (out, _, _) = MockTransport.client(token: nil)222 #expect(await AlertsAPI(out).streamRequest() == nil)223 }224}225226@Suite struct ServerSentEventsTests {227 @Test func parsesTheAlertStreamAsTheServerWritesIt() {228 // alerts/routes.ts: `retry: 5000\nevent: unread\ndata: {...}\n\n`, then `: keep-alive\n\n`229 var p = ServerSentEventParser()230 let wire = "retry: 5000\nevent: unread\ndata: {\"unread\":2,\"critical\":1,\"lastId\":9}\n\n: keep-alive\n\nevent: unread\ndata: {\"unread\":0,\"critical\":0,\"lastId\":9}\n\nevent: other\ndata: {}\n\n"231 var events: [ServerSentEvent] = []232 for chunk in stride(from: 0, to: wire.utf8.count, by: 7) { events += p.feed(Data(Array(wire.utf8)[chunk..<min(chunk + 7, wire.utf8.count)])) } // arbitrary chunking233 #expect(events.count == 3 && events[0].retryMs == 5000 && events[0].event == "unread")234 #expect(events.compactMap(AlertStreamEvent.init) == [.unread(.init(unread: 2, critical: 1, lastId: 9)), .unread(.init(unread: 0, critical: 0, lastId: 9))])235 }236237 @Test func parsesTheResearchLogStreamWithIdsCRLFAndMultilineData() {238 // research/routes.ts: `retry: 3000\n\n`, `id: 11\nevent: log\ndata: {...}\n\n`, `event: state\n…`, `event: end\ndata: {}\n\n`239 var p = ServerSentEventParser()240 let wire = "retry: 3000\r\n\r\nid: 11\r\nevent: log\r\ndata: {\"id\":11,\"ts\":1795000001000,\"s\":\"out\",\"t\":\"trial 1 done\"}\r\n\r\nevent: state\ndata: {\"state\":\"over_budget\",\"reason\":\"wall clock\"}\n\nevent: end\ndata: {}\n\ndata: a\ndata: b\n\n"241 let events = p.feed(Data(wire.utf8))242 #expect(events.count == 4 && p.lastEventId == "11" && events[0].id == "11")243 let typed = events.compactMap(ResearchLogStreamEvent.init)244 #expect(typed.count == 3)245 guard case .log(let line) = typed[0] else { Issue.record("expected a log line"); return }246 #expect(line.id == 11 && line.stream == "out" && line.text == "trial 1 done" && !line.isServerLine)247 #expect(typed[1] == .state(.overBudget, reason: "wall clock") && typed[2] == .end)248 #expect(events[3].event == "message" && events[3].data == "a\nb")249 }250}251252@Suite struct MonitorAPITests {253 static let fx = try! TestResources.json("ops-monitor.json")254255 @Test func capitalKeepsRealAndSimulatedApartAndNeverTurnsMissingIntoZero() async throws {256 let (client, t, _) = MockTransport.client()257 t.enqueue(json: try text(Self.fx["capital"]))258 let c = try await MonitorAPI(client).capital()259 #expect(t.last?.method == "GET" && t.last?.path == "/api/v1/monitor/capital")260 #expect(c.asOf == ms(1_795_000_000_000) && c.agents.count == 2 && c.derivatives.count == 2)261 #expect(c.agents[0].real && c.agents[0].equityUsd == 512.34 && c.agents[0].mandateExpiresAt == ms(1_795_600_000_000))262 #expect(!c.agents[1].real && c.agents[1].equityUsd == nil && c.agents[1].asOf == nil && c.agents[1].stopReason == "loss limit")263 #expect(c.derivatives[0].kind == "paper" && !c.derivatives[0].real && c.derivatives[0].equity == 10000)264 #expect(c.derivatives[1].real && c.derivatives[1].equity == nil && c.derivatives[1].halt == "owner" && c.derivatives[1].openOrders == 2)265 #expect(c.totals.agentsLiveUsd == 512.34 && c.totals.derivPaperUsd == 10000 && c.totals.derivVenueUsd == 0)266 #expect(c.counts.agents == ["LIVE": 1, "PAPER_TRADING": 1] && c.counts.openPositions.derivatives == 1)267 #expect(c.wallet.usd == nil && c.wallet.unavailable == 1 && !c.wallet.isComplete)268 }269270 @Test func pnlAndExposure() async throws {271 let (client, t, _) = MockTransport.client()272 t.enqueue(json: try text(Self.fx["pnl"]))273 let p = try await MonitorAPI(client).pnl()274 #expect(t.last?.path == "/api/v1/monitor/pnl" && p.tz == "America/Toronto" && p.note == "flows_included")275 #expect(p.agents[0].today == MonitorPnlFigure(abs: 1.25, pct: 0.24, partial: false, from: ms(1_794_960_000_000)))276 #expect(p.agents[0].d7?.pct == nil && p.agents[0].d7?.partial == true && p.agents[0].d30 == nil)277 #expect(p.derivatives[0].today == nil && p.derivatives[0].equity == nil && !p.derivatives[0].real)278279 t.enqueue(json: try text(Self.fx["exposure"]))280 let e = try await MonitorAPI(client).exposure()281 #expect(t.last?.path == "/api/v1/monitor/exposure" && e.caveat == "spot_and_perp_differ")282 #expect(e.real.map(\.asset) == ["BTC"] && e.real[0].netUsd == 50.5 && e.simulated[0].asset == "ETH" && !e.simulated[0].priced)283 }284285 @Test func healthSaysNotMeasuredAndUnknownAsSuch() async throws {286 let (client, t, _) = MockTransport.client()287 t.enqueue(json: try text(Self.fx["health"]))288 let h = try await MonitorAPI(client).health()289 #expect(t.last?.path == "/api/v1/monitor/health")290 #expect(h.connectors.map(\.ok) == [true, nil, false] && h.connectors[1].lastSuccessAt == nil && h.connectors[2].ageSec == 10000)291 #expect(h.loops[0].name == "tracker" && !h.loops[0].late && h.loops[0].lastErrorAt == nil)292 #expect(h.quotes?.venues.map(\.status) == [.connected, .idle] && h.quotes?.lagMs.p50 == nil && h.quotes?.messagesPerSec == 12.5)293 #expect(h.marketData?.apibricks?["needsKey"]?.boolValue == true)294 #expect(!h.backup.visible && h.backup.lastAt == nil && h.disk == nil)295 #expect(h.audit.intact && h.audit.total == 1234 && h.signer == SignerReachability(reachable: true, halted: false))296 #expect(h.versions.apiCommit == nil && h.versions.signerPolicyVersion == 4)297 #expect(h.capability("deriv_live_mainnet")?.on == false && h.capability("deriv_live_mainnet")?.missing == ["SPW_DERIV_LIVE_MAINNET"] && h.capability("nope") == nil)298 #expect(h.lab?.mode == "sandbox-exec" && h.lab?.ok == true)299300 // a server before the quote engine and with an unreadable sandbox: both are absent, not healthy301 var o = try #require(Self.fx["health"]?.objectValue)302 o["quotes"] = .null; o["lab"] = .null; o["signer"] = ["reachable": false, "halted": nil]303 t.enqueue(json: try text(.object(o)))304 let h2 = try await MonitorAPI(client).health()305 #expect(h2.quotes == nil && h2.lab == nil && h2.signer.halted == nil && !h2.signer.reachable)306 }307308 @Test func quoteStreamStats() async throws {309 let (client, t, _) = MockTransport.client()310 t.enqueue(json: try text(Self.fx["streamStats"]))311 let s = try await MonitorAPI(client).quoteStreamStats()312 #expect(t.last?.method == "GET" && t.last?.path == "/api/v1/stream/stats")313 #expect(s.protocol == 1 && s.connections.byPrincipal == ["owner": 1] && s.backpressure.conflated == 3 && s.clients.unauthorizedClosed == 1)314 #expect(s.limits.maxSubscriptions == 300 && s.limits.heartbeatMs == 15000 && s.limits.maxFrameBytes == 16384 && s.lagMs.p95 == 120)315 #expect(s.hub["channels"]?.intValue == 4 && s.metered == nil)316 }317}318319@Suite struct DashboardAPITests {320 static let fx = try! TestResources.json("ops-dashboard.json")321322 @Test func statusSectionsFailOnTheirOwn() async throws {323 let (client, t, _) = MockTransport.client()324 t.enqueue(json: try text(Self.fx["status"]))325 let s = try await DashboardAPI(client).status()326 #expect(t.last?.method == "GET" && t.last?.path == "/api/v1/dashboard/status")327 #expect(s.switches.data?.readOnly == .init(on: true, at: ms(1_794_990_000_000)) && s.switches.data?.killSwitch.at == nil && s.switches.asOf == ms(1_795_000_000_000))328 #expect(s.liveLimits == .failed(code: "timeout", message: "no answer within 8000 ms") && s.liveLimits.data == nil && s.liveLimits.errorCode == "timeout")329 }330331 @Test func summaryKeepsThreeBooksApartAndAFailedBookUnavailable() async throws {332 let (client, t, _) = MockTransport.client()333 t.enqueue(json: try text(Self.fx["summary"]))334 let s = try await DashboardAPI(client).summary()335 #expect(t.last?.path == "/api/v1/dashboard/summary" && t.last?.query == [:])336 let paper = try #require(s.books.paper.data)337 #expect(paper.book == "paper" && paper.equity == "10000" && paper.grossExposure == "6500.05" && paper.isComplete && !paper.isEmpty)338 #expect(paper.positions[0].markPrice == "65000.5" && paper.positions[0].liquidationDistanceBps == "954" && paper.pnl?.unrealizedToday == "-1.5")339 #expect(s.books.live.data == nil && s.books.live.errorCode == "signer_unreachable") // real money: unavailable, not zero340 let testnet = try #require(s.books.liveTestnet.data)341 #expect(testnet.isEmpty && !testnet.isComplete && testnet.leverage == nil)342 #expect(s.risk.data?.switches.killSwitch.on == true && s.risk.data?.switches.killSwitch.reason == "owner" && s.risk.data?.live.missing == ["maxLeverage"])343 #expect(s.agents.data?.byMode["live"]?.active == 1 && s.agents.data?.total.stopped == 1)344 let pnl = try #require(s.agentsPnl.data)345 #expect(pnl.today["research"]?.abs == nil && pnl.today["research"]?.reason == "research_no_pnl" && pnl.today["live"]?.abs == 1.25 && pnl.today["live"]?.partial == true)346 #expect(s.health.data?.signer.halted == nil && s.health.data?.connectors.failing.first?.id == "signer" && s.health.data?.loops.late == ["tracker"])347 #expect(s.audit.data?.intact == false && s.audit.data?.brokenAt == 77)348 // sections survive the offline store unchanged349 let again = try APICoding.decoder().decode(DashboardSummary.self, from: try APICoding.encoder().encode(s))350 #expect(again == s)351 }352353 @Test func activityPageFiltersCursorAndSourceErrors() async throws {354 let (client, t, _) = MockTransport.client()355 t.enqueue(json: try text(Self.fx["activity"]))356 let from = try #require(ISO8601.parse("2026-11-01T00:00:00.000Z"))357 let page = try await DashboardAPI(client).activity(TimelineQuery(kind: "order", mode: "paper", venue: "hyperliquid", principal: "agent", from: from), limit: 999, cursor: "abc_-1")358 let r = try #require(t.last)359 #expect(r.method == "GET" && r.path == "/api/v1/dashboard/activity")360 #expect(r.query == ["kind": "order", "mode": "paper", "venue": "hyperliquid", "principal": "agent", "from": "2026-11-01T00:00:00.000Z", "limit": "200", "cursor": "abc_-1"])361 #expect(page.items.count == 3 && page.nextCursor == "WzE3OTUwMDAwMDAwMDAsImE6MDAwMDAwMDAwMDc3Il0" && !page.isComplete)362 #expect(page.errors == [.init(source: "plans", code: "source_failed", message: "status 500")] && page.bounds == .init(perSource: 500, plans: 50) && page.audit?.intact == true)363 let w = page.items[0]364 #expect(w.kind == "transfer" && w.mode == "live" && !w.isSimulated && w.amount == .init(value: "25", symbol: "USDC") && w.ref == .init(type: "proposal", id: "6f1c2f0e-5a3b-4c44-9d0a-0f6f0c1d2e3f"))365 #expect(w.walletItem?.amount == "25000000" && w.walletItem?.isSettled == true && w.at == ISO8601.parse("2026-11-18T12:00:00.000Z"))366 let o = page.items[1]367 #expect(o.mode == "paper" && o.isSimulated && o.principal == .init(kind: "agent", id: "a2") && o.params["price"]?.stringValue == "65000" && o.side == "buy" && o.walletItem == nil)368 #expect(page.items[2].mode == nil && page.items[2].code == "device.added" && page.items[2].amount == nil)369370 t.enqueue(json: #"{"items":[],"nextCursor":null,"errors":[]}"#)371 let last = try await DashboardAPI(client).activity()372 #expect(t.last?.query == [:] && last.nextCursor == nil && last.isComplete && last.audit == nil)373 }374375 @Test func activityCSVIsTheServersOwnBytes() async throws {376 let (client, t, _) = MockTransport.client()377 let csv = "time_utc,kind,mode\n\"2026-11-18T12:00:00.000Z\",\"transfer\",\"live\"\n"378 t.enqueue(data: Data(csv.utf8), headers: ["Content-Type": "text/csv; charset=utf-8"])379 let d = try await DashboardAPI(client).activityCSV(TimelineQuery(mode: "live"))380 #expect(String(decoding: d, as: UTF8.self) == csv)381 #expect(t.last?.path == "/api/v1/dashboard/activity.csv" && t.last?.query == ["mode": "live"] && t.last?.headers["Accept"] == "text/csv")382 }383}384385@Suite struct ResearchAPITests {386 static let fx = try! TestResources.json("ops-research.json")387388 @Test func statusAndCreate() async throws {389 let (client, t, _) = MockTransport.client()390 let api = ResearchAPI(client)391 t.enqueue(json: try text(Self.fx["status"]))392 let s = try await api.status()393 #expect(t.last?.method == "GET" && t.last?.path == "/api/v1/research/status")394 #expect(s.runnerConfigured && s.openJobs == 1 && s.maxOpenJobs == 6 && s.lastRunnerContact == ISO8601.parse("2026-11-18T11:59:00.000Z"))395396 t.enqueue(json: try text(Self.fx["created"]))397 let job = try await api.create(ResearchJobRequest(mission: "Find a trend strategy on BTC and ETH.", universe: ["BTC", "ETH"], budgets: .init(maxTrials: 50)))398 let r = try #require(t.last)399 #expect(r.method == "POST" && r.path == "/api/v1/research/jobs" && r.headers["X-SPW-CSRF"] == "1")400 // only what the owner chose is sent: every other field takes the SERVER's default401 #expect(r.body == ["mission": "Find a trend strategy on BTC and ETH.", "universe": ["BTC", "ETH"], "budgets": ["maxTrials": 50]])402 #expect(job.state == .queued && !job.state.isFinal && job.state.isCancellable && job.runnerConfigured == false && job.usage == nil && job.budgets.maxModelUsd == 10 && job.strategy == "rj_9a1b2c3d0000/1")403404 t.enqueue(status: 429, json: #"{"error":"too_many_jobs","message":"at most 6 research jobs at once"}"#)405 do { _ = try await api.create(ResearchJobRequest(mission: "0123456789", universe: ["BTC"])); Issue.record("expected a refusal") }406 catch let e as APIError { #expect(e.status == 429 && e.code == "too_many_jobs") }407 }408409 @Test func listDecodesServerVerdictsAndUnknownStates() async throws {410 let (client, t, _) = MockTransport.client()411 t.enqueue(json: try text(Self.fx["jobs"]))412 let jobs = try await ResearchAPI(client).jobs(agentId: "a1", limit: 0)413 #expect(t.last?.path == "/api/v1/research/jobs" && t.last?.query == ["agentId": "a1", "limit": "1"])414 #expect(jobs.count == 2)415 #expect(jobs[0].state == .submitted && jobs[0].state.isFinal && !jobs[0].state.isCancellable && jobs[0].serverStatus == "ok" && jobs[0].trialsTotal == 33 && jobs[0].deflatedSharpe == 0.412 && jobs[0].claimsDiffer == true)416 #expect(jobs[0].usage?.modelUsd == nil && jobs[0].usage?.sandbox == "none" && jobs[0].usage?.trials == 31)417 #expect(jobs[1].state == .unknown("paused_by_a_newer_server") && !jobs[1].state.isFinal && !jobs[1].state.isCancellable && jobs[1].deflatedSharpe == nil && jobs[1].cancelRequested)418 t.enqueue(json: #"{"jobs":[]}"#)419 _ = try await ResearchAPI(client).jobs()420 #expect(t.last?.query == [:])421 }422423 @Test func everyPartOfAResultCarriesItsNatureAndTheyAreNeverMerged() async throws {424 let (client, t, _) = MockTransport.client()425 t.enqueue(json: try text(Self.fx["detail"]))426 let id = "9a1b2c3d-0000-4000-8000-000000000002"427 let d = try await ResearchAPI(client).job(id)428 #expect(t.last?.method == "GET" && t.last?.path == "/api/v1/research/jobs/\(id)")429 #expect(d.id == id && d.job.state == .submitted && d.dataReady == true && d.agent?.name == "Momentum 1" && d.version?.status == "draft" && d.version?.approvedAt == nil)430431 // observed data — how the recorded candles were split, and what the runner measured from outside the agent432 #expect(d.holdout == ResearchHoldout(start: "2025-01-01T00:00:00.000Z", devEnd: "2026-06-01T00:00:00.000Z", end: "2026-11-01T00:00:00.000Z", devBars: 3096, testBars: 918))433 #expect(d.job.usage?.ledgerTampered == true && d.job.usage?.modelUsd == 1.75)434 // generated analysis — the agent's statements435 let sub = try #require(d.submission)436 #expect(sub.claims?.train?.sharpe == 3.1 && sub.claims?.innerTest?.sharpe == 2.8 && sub.claims?.innerTest?.maxDrawdown == nil && sub.claims?.summary == "It works very well." && sub.files["README.md"] == "# Idea\n")437 // simulation — what the server measured on data the job never had; estimates next to it438 let srv = try #require(d.server)439 #expect(srv.isOK && srv.metrics?.test?.sharpe == -0.1 && srv.metrics?.test?.trades == nil && srv.benchmark?.totalReturn == 0.11 && srv.trials == .init(job: 31, server: 2, total: 33, ledgerTampered: true))440 #expect(srv.deflatedSharpe == 0.412 && srv.sharpeCi == [-0.3, nil] && srv.replication?.innerTest?.trades == 9 && srv.checks?["costs"]?.stringValue == "fail")441 // the claimed number and the measured one stay two numbers442 #expect(srv.comparison?[0] == ResearchClaimComparison(split: "train", metric: "sharpe", claimed: 3.1, measured: 1.9, differs: true) && srv.comparison?[1].claimed == nil && srv.claimsDiffer == true)443 #expect(d.report?.equityCurve?[1]?[1]?.doubleValue == 4990.5 && d.report?.researchJob?["trials_job"]?.intValue == 31)444445 let labels = d.results.map { "\($0.provenance.nature.rawValue)/\($0.provenance.author.rawValue)" }446 #expect(labels == ["observedData/server", "observedData/runner", "generatedAnalysis/agent", "simulation/server", "simulation/server"])447 #expect(d.results.filter { $0.provenance.isServerVerified }.count == 3)448 #expect(ResearchSubmission.provenance.nature == .generatedAnalysis && !ResearchSubmission.provenance.isServerVerified)449 #expect(ResearchServerReport.estimateProvenance == ResearchProvenance(nature: .estimate, author: .server))450 // a round trip through the offline store keeps the parts apart451 let again = try APICoding.decoder().decode(ResearchJobDetail.self, from: try APICoding.encoder().encode(d))452 #expect(again == d && again.results.count == 5)453 }454455 @Test func aFailedEvaluationHasNoMeasurement() throws {456 var o = try #require(Self.fx["detail"]?.objectValue)457 o["state"] = "failed"; o["reason"] = "strategy.py fails the purity check"458 o["server"] = ["status": "failed", "reason": "strategy.py fails the purity check"]; o["report"] = .null; o["submission"] = .null; o["holdout"] = .null; o["dataReady"] = .null459 let d: ResearchJobDetail = try JSONValue.object(o).decode()460 #expect(d.job.state == .failed && d.server?.isOK == false && d.server?.metrics == nil && d.server?.deflatedSharpe == nil && d.report == nil && d.submission == nil && d.dataReady == nil)461 #expect(d.results.map(\.provenance.nature) == [.observedData, .simulation])462 }463464 @Test func cancelShowsTheStateTheServerReturnsNotAnAssumedOne() async throws {465 let (client, t, _) = MockTransport.client()466 let id = "9a1b2c3d-0000-4000-8000-000000000004"467 t.enqueue(json: try text(Self.fx["running"]))468 let j = try await ResearchAPI(client).cancel(id)469 #expect(t.last?.method == "POST" && t.last?.path == "/api/v1/research/jobs/\(id)/cancel" && t.last?.body == [:])470 #expect(j.state == .running && j.cancelRequested) // the runner kills the agent at its next report471 t.enqueue(status: 409, json: #"{"error":"bad_state","message":"this job is already submitted"}"#)472 do { _ = try await ResearchAPI(client).cancel(id); Issue.record("expected a refusal") } catch let e as APIError { #expect(e.code == "bad_state") }473 }474475 @Test func logAndItsStream() async throws {476 let (client, t, _) = MockTransport.client()477 let id = "9a1b2c3d-0000-4000-8000-000000000004"478 t.enqueue(json: try text(Self.fx["log"]))479 let log = try await ResearchAPI(client).log(id, after: 9, limit: 5000)480 #expect(t.last?.path == "/api/v1/research/jobs/\(id)/log" && t.last?.query == ["after": "9", "limit": "1000"])481 #expect(log.state == .running && log.lastId == 11 && log.lines[0].isServerLine && log.lines[1].stream == "out" && log.lines[1].ts == ms(1_795_000_001_000))482483 let req = try #require(await ResearchAPI(client).logStreamRequest(id, after: 11, lastEventId: "11"))484 #expect(req.url?.absoluteString == "https://wallet.example.test/api/v1/research/jobs/\(id)/log/stream?after=11")485 #expect(req.value(forHTTPHeaderField: "Last-Event-ID") == "11" && req.value(forHTTPHeaderField: "Cookie") == "__Host-spw=test-session-token")486 }487488 @Test func pathSegmentsOfIdsAreEncoded() async throws {489 let (client, t, _) = MockTransport.client()490 t.enqueue(status: 404, json: #"{"error":"not_found","message":"research job not found"}"#)491 await #expect(throws: APIError.self) { _ = try await ResearchAPI(client).job("../runner/claim") }492 #expect(t.last?.path == "/api/v1/research/jobs/..%2Frunner%2Fclaim" || t.last?.path == "/api/v1/research/jobs/../runner/claim")493 #expect(t.requests.allSatisfy { !$0.path.hasPrefix("/api/v1/research/runner") })494 }495}496