Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/CoreTests/LabIntegrationTests.swift3// Description: The Quant Lab of backend 6 against the REAL VQuant Next test backend (5058, through the SSH tunnel) with a 2.0 client header: the 19-tool catalogue, the life of a lab job with progress and a `lab/1` result, cancellation (lab and legacy), validation and engine failures. No language model is called.4//5// Run: eval "$(Scripts/dev-session.sh)"; VQ_IT_URL=$VQ_SERVER VQ_IT_TOKEN=$VQ_DEV_TOKEN swift test --filter LabIntegrationTests6// (the session is the development device session of Scripts/dev-session.sh: no passkey enrolment is needed here).7import Foundation8import Testing9@testable import VQuantCore1011private enum LabIT {12 static let url = ProcessInfo.processInfo.environment["VQ_IT_URL"].flatMap(URL.init(string:))13 static let token = ProcessInfo.processInfo.environment["VQ_IT_TOKEN"] ?? ""14 static var enabled: Bool { url != nil && token.hasPrefix("vqn_") }15 static func api() -> VQuantAPI {16 VQuantAPI(client: APIClient(baseURL: url!, identity: ClientIdentity(version: "2.0.0", build: "it", deviceName: "Mac d’essai (lab)"), tokens: MemoryTokenStore(token), transport: URLSessionTransport()))17 }18 /// Polls until the job leaves `running` (lab runs are limited to 10 minutes by the server; these take seconds).19 static func finished(_ api: VQuantAPI, _ id: String, seconds: Int = 120, onState: (QuantJob) -> Void = { _ in }) async throws -> QuantJob {20 var j = try await api.quantJob(id)21 for _ in 0..<(seconds * 2) where j.status == .running { try await Task.sleep(for: .milliseconds(500)); j = try await api.quantJob(id); onState(j) }22 return j23 }24}2526@Suite(.serialized, .enabled(if: LabIT.enabled, "set VQ_IT_URL and VQ_IT_TOKEN (see the file header)"))27struct LabIntegrationTests {28 @Test func l01_catalogueHasNineteenToolsInSevenCategories() async throws {29 let cat = try await LabIT.api().quantCatalogue()30 #expect(cat.tools.count == 19 && cat.maxRunning == 3)31 #expect(cat.tools.filter(\.isLab).count == 13 && cat.tools.filter { $0.resultSchema == "legacy" }.count == 6)32 #expect(Set(cat.tools.compactMap(\.category)) == ["volatility", "risk", "econometrics", "options", "backtest", "portfolio", "simulation"])33 #expect(cat.tools.allSatisfy { $0.version != nil && !$0.title.isEmpty && !$0.summary.isEmpty })34 #expect(Set(cat.tools.flatMap { $0.params.map(\.type) }).isSuperset(of: ["symbol", "symbols", "int", "number", "choice", "bool", "date", "text", "json"]))35 for id in ["volatility_lab", "risk_lab", "regression", "options_lab", "backtest", "portfolio_lab"] { #expect(cat.tools.contains { $0.id == id }) }36 }3738 @Test func l02_labJobLifecycleWithProgressAndLabResult() async throws {39 let api = LabIT.api()40 let job = try await api.submitQuantJob(tool: "backtest", params: ["symbols": ["spy"], "strategy": "ma_cross", "years": 5, "walk_forward": true])41 #expect(job.status == .running && job.params["symbols"] == ["SPY"] && job.params["cost_bps"] == 5) // defaults are recorded42 #expect(job.engineVersion != nil && job.cancellable == true)43 #expect(try await api.quantJobs().jobs.contains { $0.id == job.id && $0.status == .running })44 var seen: Set<Double> = []45 let done = try await LabIT.finished(api, job.id) { if let p = $0.progress { seen.insert(p) } }46 #expect(done.status == .done && done.progress == 1 && (done.durationMs ?? 0) > 0 && done.cancellable == false)47 #expect(seen.allSatisfy { $0 >= 0 && $0 <= 1 })48 let r = try #require(done.result)49 #expect(r["schema"] == "lab/1" && r["success"] == true && r["engine"] == "backtest")50 #expect(!(r["headline"]?.stringValue ?? "").isEmpty && (r["summary"]?.arrayValue?.count ?? 0) > 5 && (r["charts"]?.arrayValue?.count ?? 0) >= 4)51 #expect(r["provenance"]?["data"]?[0]?["symbol"] == "SPY" && !(r["provenance"]?["reproduce"]?.stringValue ?? "").isEmpty)52 // the engine's own integrity flags, which the app shows above every backtest53 let diagnostics = (r["diagnostics"]?.arrayValue ?? []).map { LabIntegrity.Diagnostic(name: $0["name"]?.stringValue ?? "", passed: $0["passed"]?.boolValue, conclusion: $0["conclusion"]?.stringValue ?? "") }54 let statement = LabIntegrity.backtest(diagnostics: diagnostics, warnings: (r["warnings"]?.arrayValue ?? []).compactMap(\.stringValue), assumptions: (r["assumptions"]?.arrayValue ?? []).compactMap(\.stringValue), symbols: 1)55 #expect(statement.lookAhead.count >= 1 && statement.lookAhead.allSatisfy { $0.fromEngine && $0.state == .passed })56 #expect((r["tables"]?.arrayValue ?? []).contains { $0["id"] == "walk_forward" })57 // a finished job cannot be cancelled, and says so58 #expect(try await api.cancelQuantJob(job.id) == .notRunning)59 try await api.deleteQuantJob(job.id)60 await #expect { try await api.quantJob(job.id) } throws: { ($0 as? APIError)?.kind == .notFound }61 }6263 @Test func l03_cancelARunningLabJobAndALegacyJob() async throws {64 let api = LabIT.api()65 let job = try await api.submitQuantJob(tool: "bootstrap", params: ["symbol": "MSFT", "replications": 20000])66 #expect(job.status == .running)67 guard case .cancelled(let c) = try await api.cancelQuantJob(job.id) else { Issue.record("a running lab job is cancellable"); return }68 #expect(c.id == job.id && c.status == .cancelled)69 let after = try await api.quantJob(job.id)70 #expect(after.status == .cancelled && after.result == nil) // a cancelled job keeps no result71 #expect(try await api.cancelQuantJob(job.id) == .notRunning)72 // legacy tools run to completion: 409 not_cancellable73 let legacy = try await api.submitQuantJob(tool: "monte_carlo", params: ["symbol": "AAPL", "num_simulations": 100000, "time_horizon": 2520])74 #expect(legacy.progress == nil)75 let outcome = try await api.cancelQuantJob(legacy.id)76 #expect(outcome == .notCancellable || outcome == .notRunning) // notRunning only if it already finished77 let end = try await LabIT.finished(api, legacy.id)78 #expect(end.status == .done && end.result?["schema"] == nil) // a legacy result keeps its own shape79 await #expect { try await api.cancelQuantJob("does-not-exist") } throws: { ($0 as? APIError)?.kind == .notFound }80 try await api.deleteQuantJob(job.id); try await api.deleteQuantJob(legacy.id)81 }8283 @Test func l04_validationAndEngineFailures() async throws {84 let api = LabIT.api()85 // refused before anything runs, with a sentence86 await #expect { try await api.submitQuantJob(tool: "options_lab", params: ["symbol": "SPY", "mode": "nope"]) } throws: { ($0 as? APIError)?.kind == .rejected && ($0 as? APIError)?.message.contains("Analysis") == true }87 await #expect { try await api.submitQuantJob(tool: "options_lab", params: ["symbol": "SPY", "legs": "not json"]) } throws: { ($0 as? APIError)?.kind == .rejected }88 await #expect { try await api.submitQuantJob(tool: "backtest", params: ["symbols": ["SPY"], "cost_bps": 9999]) } throws: { ($0 as? APIError)?.kind == .rejected }89 // accepted, then fails in the engine: an error with a reason a person can act on, never an empty success90 let bad = try await api.submitQuantJob(tool: "regression", params: ["symbol": "ZZZZNOPE", "regressors": ["SPY"]])91 let failed = try await LabIT.finished(api, bad.id)92 #expect(failed.status == .error && (failed.error ?? "").contains("ZZZZNOPE"))93 #expect(failed.result == nil || failed.result?["success"] == false)94 try await api.deleteQuantJob(bad.id)95 }96}97