SPB Git

Repository Public

LabSupportTests.swift 14.6 KB 176 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/CoreTests/LabSupportTests.swift3// Description: The 1.x quant models against payloads RECORDED from the VQuant Next test backend with a 2.0 client header (19 tools, the `cancelled` status, new fields), and the pure logic of the Quant Lab screens (schema hints, row editor, comparison, backtest integrity statement, options modes, slices).4import Foundation5import Testing6@testable import VQuantCore78@Suite struct LabDecodingTests {9    @Test func catalogueOfBackend6DecodesWithThe1xModels() throws {10        let cat = try JSONDecoder().decode(QuantCatalogue.self, from: fixture("quant-tools-next"))11        #expect(cat.tools.count == 19 && cat.maxRunning == 3 && cat.unavailable == [:])12        #expect(cat.tools.filter { !$0.isLab }.map(\.id) == ["garch", "var", "monte_carlo", "options", "portfolio_optimization", "risk_metrics"])13        #expect(cat.tools.filter(\.isLab).count == 13)14        #expect(Set(cat.tools.compactMap(\.category)) == ["volatility", "risk", "econometrics", "options", "backtest", "portfolio", "simulation"])15        let options = try #require(cat.tools.first { $0.id == "options_lab" })16        let legs = try #require(options.params.first { $0.name == "legs" })17        #expect(legs.type == "json" && legs.group == "Strategy" && legs.schemaHint?.contains("expiry_days") == true)18        // bounds stay numbers for every type, a default may be a list19        let backtest = try #require(cat.tools.first { $0.id == "backtest" })20        #expect(backtest.params.first { $0.name == "symbols" }?.default == ["SPY"])21        #expect(backtest.params.first { $0.name == "start" }?.type == "date")22        // the old form builder still accepts a legacy tool of the new catalogue23        let garch = try #require(cat.tools.first { $0.id == "garch" })24        #expect(try QuantForm.build(tool: garch, values: ["symbol": "aapl", "p": "2"], flags: [:]).get() == ["symbol": "AAPL", "p": 2])25    }2627    @Test func jobListWithNewStatusesAndFieldsDecodes() throws {28        let list = try JSONDecoder().decode(QuantJobList.self, from: fixture("quant-jobs-next"))29        #expect(list.jobs.count >= 20)30        #expect(list.jobs.contains { $0.status == .cancelled })31        let lab = try #require(list.jobs.first { $0.tool == "portfolio_lab" && $0.status == .done })32        #expect(lab.progress == 1 && lab.engineVersion == "1.0.0" && (lab.durationMs ?? 0) > 0 && lab.cancellable == false)33        // structured parameters (positions, cash) are summarised, never dropped silently34        #expect(lab.paramSummary.contains("positions = 3 items") && lab.paramSummary.contains("cash = {CAD, USD}"))35    }3637    @Test func unknownStatusIsReadAsInterruptedAndOldPayloadsStillDecode() throws {38        let j = try JSONDecoder().decode(QuantJob.self, from: Data(#"{"id":"a","tool":"x","params":{},"status":"paused_by_admin","startedAt":1}"#.utf8))39        #expect(j.status == .interrupted && j.progress == nil && j.engineVersion == nil)40        // the six-tool catalogue a 1.x header receives41        let old = try JSONDecoder().decode(QuantCatalogue.self, from: fixture("quant-tools"))42        #expect(old.tools.count == 6 && old.unavailable == nil && old.tools.allSatisfy { !$0.isLab })43        #expect(try JSONDecoder().decode(QuantJobList.self, from: fixture("quant-jobs")).jobs.isEmpty == false)44    }45}4647@Suite struct LabJSONHintTests {48    let legsHint = #"[{"type": "call|put|stock", "side": "long|short", "strike": 100, "expiry_days": 30, "quantity": 1, "premium": 2.5}]"#4950    @Test func hintsOfTheRealCatalogueAreRead() throws {51        let cat = try JSONDecoder().decode(QuantCatalogue.self, from: fixture("quant-tools-next"))52        var seen = 053        for tool in cat.tools { for p in tool.params where p.type == "json" {54            seen += 155            #expect(!LabJSONHint.alternatives(p.schemaHint).isEmpty, "\(tool.id).\(p.name): \(p.schemaHint ?? "")")56        } }57        #expect(seen >= 10)58        let params = try #require(cat.tools.first { $0.id == "backtest" }?.params)59        let strategies = LabJSONHint.alternatives(params.first { $0.name == "strategy_params" }?.schemaHint)60        #expect(strategies.compactMap(\.label).prefix(4) == ["buy_hold", "ma_cross", "ts_momentum", "xs_momentum"])61        #expect(LabJSONHint.example(params.first { $0.name == "strategy_params" }?.schemaHint, labelled: "ma_cross") == ["fast": 50, "slow": 200])62        // an ellipsis and an "or" between two examples63        let events = try #require(cat.tools.first { $0.id == "event_study" }?.params.first { $0.name == "events" })64        let alts = LabJSONHint.alternatives(events.schemaHint)65        #expect(alts.count == 2 && alts[0].shape == .rows([.init(name: "symbol", kind: .text, example: "AAPL"), .init(name: "date", kind: .text, example: "2025-05-01")]) && alts[1].shape == .list)66    }6768    @Test func columnsKeepTheHintOrderAndKinds() throws {69        let cols = try #require(LabJSONHint.rowColumns(legsHint))70        #expect(cols.map(\.name) == ["type", "side", "strike", "expiry_days", "quantity", "premium"])71        #expect(cols[0].kind == .choice(["call", "put", "stock"]) && cols[2].kind == .number)72        #expect(LabJSONHint.rowColumns(#"{"fast": [20, 50]}"#) == nil && LabJSONHint.rowColumns(nil) == nil)73    }7475    @Test func checkSeparatesErrorsFromAdvisoryNotes() {76        #expect(LabJSONHint.check("  ", hint: legsHint).isEmpty)77        let broken = LabJSONHint.check(#"[{"type": "call",]"#, hint: legsHint)78        #expect(!broken.isValid && broken.error?.hasPrefix("Not valid JSON") == true)79        #expect(LabJSONHint.check("42", hint: legsHint).error == "A JSON object or array is expected.")80        let ok = LabJSONHint.check(#"[{"type":"call","side":"long","strike":700,"quantity":1},{"type":"put","side":"short","strike":650}]"#, hint: legsHint)81        #expect(ok.isValid && ok.notes.isEmpty && ok.summary == "array of 2 objects")82        let odd = LabJSONHint.check(#"[{"type":"swap","strik":"x","strike":"700"}]"#, hint: legsHint)83        #expect(odd.isValid && odd.notes.count == 3)      // advisory: the value can still be sent84        #expect(odd.notes.contains { $0.contains("“strik” is not in the hint") } && odd.notes.contains { $0.contains("“swap” is not one of call, put, stock") } && odd.notes.contains { $0.contains("strike is a number") })85        #expect(LabJSONHint.check(#"{"a": 1}"#, hint: legsHint).notes == ["The hint shows an array; this is an object."])86        // two object examples: keys are free (symbols, scenario names)87        #expect(LabJSONHint.check(#"{"QQQ": -0.3}"#, hint: #"{"SPY": -0.20, "TLT": 0.05}  or  {"Equity crash": {"SPY": -0.20}}"#).notes.isEmpty)88        // a named alternative is checked by its keys89        let named = LabJSONHint.check(#"{"fast": 20, "slo": 100}"#, hint: #"buy_hold {} · ma_cross {"fast": 50, "slow": 200}"#, selected: "ma_cross")90        #expect(named.notes == ["“slo” is not in the hint for ma_cross (fast, slow)."])91        #expect(LabJSONHint.pretty(#"{"b":1,"a":[1,2]}"#)?.contains("\n") == true && LabJSONHint.pretty("{") == nil)92    }9394    @Test func rowEditorRoundTrip() throws {95        let cols = try #require(LabJSONHint.rowColumns(legsHint))96        var rows = try #require(LabJSONHint.rows(#"[{"type":"call","side":"long","strike":700.5,"quantity":2,"note":"kept"}]"#))97        #expect(rows == [["type": "call", "side": "long", "strike": "700.5", "quantity": "2", "note": "kept"]])98        rows.append(LabJSONHint.newRow(cols))99        #expect(rows[1] == ["type": "call", "side": "long", "strike": "", "expiry_days": "", "quantity": "", "premium": ""])100        rows[1]["strike"] = "650,5"; rows[1]["quantity"] = "1"101        let text = LabJSONHint.text(rows: rows, columns: cols)102        let back = try JSONValue.parse(Data(text.utf8))103        #expect(back[0] == ["type": "call", "side": "long", "strike": 700.5, "quantity": 2, "note": "kept"])104        #expect(back[1] == ["type": "call", "side": "long", "strike": 650.5, "quantity": 1])    // empty cells left out: the engine's defaults105        #expect(LabJSONHint.rows("") == [] && LabJSONHint.rows(#"{"a":1}"#) == nil && LabJSONHint.rows("[1,2]") == nil)106        #expect(LabJSONHint.text(rows: [], columns: cols) == "")107    }108}109110@Suite struct LabLogicTests {111    @Test func comparisonAlignsMetricsByLabel() {112        let rows = LabCompare.rows(a: [.init(label: "Sharpe", value: 0.5, unit: "ratio"), .init(label: "CAGR", value: 0.08, unit: "percent"), .init(label: "Only A", value: nil)],113                                   b: [.init(label: "CAGR", value: 0.10, unit: "percent"), .init(label: "Sharpe", value: 0.75, unit: "ratio"), .init(label: "Only B", value: 3)])114        #expect(rows.map(\.label) == ["Sharpe", "CAGR", "Only A", "Only B"])115        #expect(rows[0].delta == 0.25 && rows[0].relative == 0.5)116        #expect(abs((rows[1].delta ?? 0) - 0.02) < 1e-12)117        #expect(rows[2].delta == nil && rows[2].inA && !rows[2].inB && rows[3].inB && !rows[3].inA && rows[3].delta == nil)118        let diff = LabCompare.paramDifferences(a: ["symbols": ["SPY"], "fast": 50, "same": true], b: ["symbols": ["SPY", "QQQ"], "same": true, "slow": 200])119        #expect(diff == [.init(name: "fast", a: "50", b: "—"), .init(name: "slow", a: "—", b: "200"), .init(name: "symbols", a: "SPY", b: "SPY, QQQ")])120    }121122    @Test func backtestIntegrityStatesTheEngineFlagsAndNothingMore() {123        let reported = LabIntegrity.backtest(124            diagnostics: [.init(name: "No look-ahead (truncation test)", passed: true, conclusion: "Decisions recomputed on histories cut at 5 dates equal the full-run decisions."),125                          .init(name: "Signals traded one bar later", passed: true, conclusion: "Decisions of bar t are executed at the open of bar t+1."),126                          .init(name: "Deflated Sharpe ratio", passed: false, conclusion: "…")],127            warnings: ["Multiple testing: 12 configurations.", "Survivorship bias: the universe is today's list of symbols. Names that were delisted are absent."], assumptions: [], symbols: 5)128        #expect(reported.lookAhead.map(\.state) == [.passed, .passed] && reported.lookAhead.allSatisfy(\.fromEngine))129        #expect(reported.survivorship.state == .warning && reported.survivorship.fromEngine && reported.survivorship.detail.hasPrefix("Survivorship bias"))130        // a failed test is shown as failed131        #expect(LabIntegrity.backtest(diagnostics: [.init(name: "No look-ahead (truncation test)", passed: false, conclusion: "max difference 0.4")], warnings: [], assumptions: [], symbols: 1).lookAhead[0].state == .failed)132        // nothing reported: the screen says so, and claims neither a pass nor a check133        let silent = LabIntegrity.backtest(diagnostics: [.init(name: "Alpha vs benchmark (HAC)", passed: nil, conclusion: "…")], warnings: [], assumptions: [], symbols: 1)134        #expect(silent.lookAhead.count == 1 && silent.lookAhead[0].state == .notReported && !silent.lookAhead[0].fromEngine)135        #expect(silent.lookAhead[0].detail.contains("reported no look-ahead test") && silent.lookAhead[0].detail.contains("nothing else was checked"))136        #expect(silent.survivorship.state == .notReported && !silent.survivorship.fromEngine && silent.survivorship.detail.hasPrefix("Not checked."))137        #expect(!silent.survivorship.detail.lowercased().contains("no survivorship bias"))138    }139140    @Test func optionsModesShapeTheFormOfTheRealEngine() throws {141        let cat = try JSONDecoder().decode(QuantCatalogue.self, from: fixture("quant-tools-next"))142        let params = try #require(cat.tools.first { $0.id == "options_lab" }?.params)143        func shown(_ m: LabOptionsMode) -> [String] { params.filter { m.shows(param: $0.name, group: $0.group) }.map(\.name) }144        #expect(params.first { $0.name == "mode" }?.choices == LabOptionsMode.allCases.map(\.rawValue))145        #expect(shown(.price).contains("strike") && shown(.price).contains("volatility") && !shown(.price).contains("legs") && !shown(.price).contains("max_dte"))146        #expect(shown(.implied).contains("market_price") && !shown(.price).contains("market_price"))147        #expect(shown(.strategy).starts(with: ["symbol"]) && shown(.strategy).contains("legs") && shown(.strategy).contains("template") && shown(.strategy).contains("expiry_days") && !shown(.strategy).contains("strike"))148        #expect(shown(.chain).contains("strike_window") && shown(.chain).contains("rate") && !shown(.chain).contains("legs"))149        for m in LabOptionsMode.allCases { #expect(!shown(m).contains("mode")) }150    }151152    @Test func slicesOfChainAndBacktestResults() {153        let chain = LabSlice.available(engine: "options_lab", mode: "chain",154                                       chartIds: ["smile_strike", "smile_moneyness", "smile_delta", "term_structure", "surface", "vendor_iv", "gex_by_strike", "dex_by_strike", "gex_profile", "open_interest"],155                                       tableIds: ["expiries", "cleaning", "exposure", "quotes"])156        #expect(chain.map(\.id) == ["smile", "term", "surface", "exposure"])157        #expect(chain[3].includesChart("gex_profile") && chain[3].includesTable("exposure") && !chain[3].includesChart("surface"))158        let bt = LabSlice.available(engine: "backtest", mode: nil, chartIds: ["equity", "drawdown", "walk_forward", "sensitivity"], tableIds: ["years", "walk_forward", "sensitivity"])159        #expect(bt.map(\.title) == ["Performance", "Walk-forward", "Sensitivity"])160        #expect(bt[0].includesChart("equity") && !bt[0].includesChart("walk_forward") && bt[0].includesTable("years") && !bt[0].includesTable("sensitivity"))161        // without walk-forward nor sensitivity there is nothing to slice162        #expect(LabSlice.available(engine: "backtest", mode: nil, chartIds: ["equity", "drawdown"], tableIds: ["years"]).isEmpty)163        #expect(LabSlice.available(engine: "options_lab", mode: "price", chartIds: ["price_vs_spot"], tableIds: ["greeks"]).isEmpty)164    }165166    @Test func catalogueSearchAndRememberedParameters() {167        #expect(LabCatalogueSearch.matches("", fields: ["x"]))168        #expect(LabCatalogueSearch.matches("garch vol", fields: ["volatility_lab", "Conditional volatility (EWMA, GARCH, GJR)"]))169        #expect(LabCatalogueSearch.matches("volatility lab", fields: ["volatility_lab"]))170        #expect(!LabCatalogueSearch.matches("garch options", fields: ["volatility_lab", "Conditional volatility (EWMA, GARCH, GJR)"]))171        let data = LabParamMemory.encode(["symbol": "MSFT", "legs": [["type": "call"]], "renamed_since": 3, "empty": .null])172        #expect(LabParamMemory.decode(data, known: ["symbol", "legs", "empty"]) == ["symbol": "MSFT", "legs": [["type": "call"]]])173        #expect(LabParamMemory.decode(nil, known: ["symbol"]) == [:] && LabParamMemory.decode(Data("nope".utf8), known: ["symbol"]) == [:])174    }175}176
Files main
Menu

Theme