SPB Git

Repository Public

FixtureDecodingTests.swift 21.0 KB 313 lines swift

1import Foundation2import Testing3@testable import VQuantKit4import VQuantKitTesting56/// Decoding against responses recorded from the frozen test backend (M2U64:5058, Scripts/record-fixtures.sh).7@Suite struct FixtureDecodingTests {8    @Test func modelCatalog() throws {9        let c = VQModelCatalog(json: try Fixture.json("models.json"))10        #expect(c.defaultModel == "claude-fable-5-2")11        #expect(c.auto.id == "auto")12        #expect(c.providers.map(\.id) == ["anthropic", "openai", "google", "openrouter", "local"])13        let fable = try #require(c.model("claude-fable-5-2"))14        #expect(fable.tier == .frontier && fable.selectable && fable.capabilities.tools)15        #expect(fable.pricing?.inputPerMTok == 8 && fable.contextWindow == 1_000_000)16        #expect(c.model("gpt-5.6-sol")?.contextWindow == nil)            // not stated: null, never filled in17        #expect(c.provider("openrouter")?.configured == false && c.provider("openrouter")?.checkedAt == nil)18        #expect(!c.selectableModels.isEmpty && c.selectableModels.allSatisfy { $0.available && $0.capabilities.tools })19        #expect(c.allModels.contains { !$0.selectable })20    }2122    @Test func routePreview() throws {23        let r = VQRouteDecision(json: try Fixture.json("route.json"))24        #expect(r.requested == "auto" && r.provider == "anthropic" && r.strategy == "balanced")25        #expect(!r.reason.isEmpty && r.candidates.count >= 2 && r.candidates[0].model == r.model)26    }2728    @Test func marketOverview() throws {29        let o = VQMarketOverview(json: try Fixture.json("market_overview.json"))30        #expect(o.sections.map(\.id) == ["indices", "sectors", "rates", "fx", "commodities", "crypto", "volatility", "movers"])31        #expect(o.status.session == .post && !o.status.isOpen)32        let sp = try #require(o.section("indices")?.items.first)33        #expect(sp.symbol == "^GSPC" && sp.unit == .points && sp.price != nil)34        let sector = try #require(o.section("sectors")?.items.first)35        #expect(sector.symbol == nil && sector.price == nil && sector.unit == .pctChange)36        #expect(o.section("movers")?.items.first?.group == "gainers")37        #expect(o.section("rates")?.items.first?.unit == .yieldPct)38        #expect(o.section("indices")?.freshness?.kind == .polled)39        let curve = try #require(o.yieldCurve)40        #expect(curve.points.count == 12 && curve.points[0].tenor == "1M" && curve.previous?.points.count == 12)41        #expect(curve.freshness?.kind == .eod)42        #expect(o.calendar.count > 10 && o.calendar[0].when != nil)43        #expect(o.calendarFreshness?.provider == "fmp")44    }4546    @Test func dataHealth() throws {47        let h = VQDataHealth(json: try Fixture.json("data_health.json"))48        #expect(h.providers.map(\.id) == ["fmp", "findata", "eodhd"])49        let fmp = h.providers[0]50        #expect(fmp.configured && fmp.ok && fmp.capabilities.count >= 10 && fmp.capabilities[0].status == .ok)51        #expect(fmp.notes?.isEmpty == false && fmp.calls != nil)52    }5354    @Test func bars() throws {55        let aapl = VQBarSeries(json: try Fixture.json("bars_aapl_1d.json"))56        #expect(aapl.symbol == "AAPL" && aapl.interval == "1d" && aapl.bars.count > 100)57        #expect(zip(aapl.bars, aapl.bars.dropFirst()).allSatisfy { $0.t < $1.t })58        #expect(aapl.bars.allSatisfy { $0.high! >= $0.low! })59        let p = aapl.provenance60        #expect(p.provider == "fmp" && p.adjust == "split_div" && p.assetClass == "equity" && p.currency == "USD" && p.timezone == "America/New_York")61        #expect(p.freshness?.kind == .eod && p.gaps?.calendar == "exchange" && p.gaps?.missingSessions == 0 && !p.notes.isEmpty)62        let fx = VQBarSeries(json: try Fixture.json("bars_eurusd_1d.json"))63        #expect(fx.provenance.adjust == "none" && fx.provenance.adjustRequested == "split_div" && fx.provenance.assetClass == "fx" && fx.provenance.gaps?.calendar == "weekdays")64        let intraday = VQBarSeries(json: try Fixture.json("bars_aapl_5m.json"))65        #expect(intraday.interval == "5m" && !intraday.bars.isEmpty)66        // the error body of an unknown symbol67        let body = try Fixture.json("bars_error.json")68        let err = VQError(.notFound, status: 404, message: body.str("error"), body: body)69        #expect(VQDataClient.providerReasons(of: err).map(\.provider) == ["fmp", "findata"])70    }7172    @Test func quotes() throws {73        let q = VQQuotes(json: try Fixture.json("quotes.json"))74        #expect(q.missing == ["ZZZZNOPE"])75        let aapl = try #require(q["AAPL"])76        #expect(aapl.currency == "USD" && aapl.freshness == .polled && aapl.providerTimestamp != nil && aapl.marketCap != nil)77        #expect(q["EURUSD"]?.assetClass == "fx")78        #expect(q.freshness?.kind == .polled)79        // changePct is in percentage points80        #expect(VQFormat.percentValue(aapl.changePct, signed: true).hasSuffix("%"))81    }8283    @Test func statements() throws {84        let s = VQStatements(json: try Fixture.json("statements_aapl.json"))85        #expect(s.symbol == "AAPL" && s.period == "annual" && s.periods.count == 4 && s.periods[0].label == "FY 2026")86        #expect(s.periods[0].date > s.periods[1].date)                   // newest first87        let revenue = try #require(s.income.first { $0.key == "revenue" })88        #expect(revenue.unit == .currency && revenue.values.count == 4 && revenue.formula == nil)89        let margin = try #require(s.ratios.first { $0.key == "grossMargin" })90        #expect(margin.unit == .percent && margin.formula == "grossProfit / revenue")91        #expect((margin.values[0] ?? 2) < 1)                              // a fraction92        #expect(VQFormat.byUnit(margin.values[0], margin.valueUnit).hasSuffix("%"))93        #expect(!s.growth.isEmpty && !s.commonSizeIncome.isEmpty && !s.commonSizeBalance.isEmpty)94        #expect(s.provenance?.freshness?.kind == .historical)95    }9697    @Test func search() throws {98        let r = VQSearchResults(json: try Fixture.json("search_apple.json"))99        #expect(r.results.first?.type == .symbol && r.results.first?.symbol == "AAPL")100        #expect(r.results.contains { $0.type == .conversation })101        #expect(zip(r.results, r.results.dropFirst()).allSatisfy { $0.score >= $1.score })102        let nav = VQSearchResults(json: try Fixture.json("search_empty.json"))103        #expect(nav.results.allSatisfy { $0.type == .action })104        #expect(nav.results.first?.action == .navigate("home"))105        #expect(VQSearchResult(json: ["type": "action", "id": "engine:risk_lab", "title": "Risk Lab"]).action == .openEngine("risk_lab"))106        #expect(VQSearchResult(json: ["type": "dataset", "id": "d1", "title": "x"]).type.known == nil)107    }108109    @Test func toolCatalogue() throws {110        let c = VQLabCatalog(json: try Fixture.json("quant_tools.json"))111        #expect(c.tools.count == 19)112        #expect(c.labEngines.count == 13 && c.maxRunning == 3 && c.unavailable.isEmpty)113        #expect(Set(c.byCategory.map(\.category)) == ["volatility", "risk", "simulation", "options", "portfolio", "econometrics", "backtest"])114        let kinds = Set(c.tools.flatMap(\.params).compactMap(\.type.known))115        #expect(kinds == [.symbol, .symbols, .int, .number, .numbers, .choice, .bool, .date, .text, .json])116        #expect(c.tools.flatMap(\.params).allSatisfy { $0.type.known != nil })117        let risk = try #require(c.tool("risk_lab"))118        #expect(risk.isLab && risk.category == .risk && risk.version == "1.0.0")119        let shocks = try #require(risk.params.first { $0.name == "shocks" })120        #expect(shocks.type == .json && shocks.schemaHint != nil && shocks.maxBytes != nil)121        #expect(risk.groups.count > 1 && risk.groups.flatMap(\.params).count == risk.params.count)122        // a 1.x client header gets the six legacy tools only123        let legacy = VQLabCatalog(json: try Fixture.json("quant_tools_legacy_client.json"))124        #expect(legacy.tools.count == 6 && legacy.tools.allSatisfy { !$0.isLab })125    }126127    @Test(arguments: Fixture.labJobNames)128    func finishedLabJob(_ name: String) throws {129        let job = VQQuantJob(json: try Fixture.json("job_\(name).json").dict("job").isEmpty ? .null : try Fixture.json("job_\(name).json")["job"]!)130        #expect(job.status == .done && job.progress == 1 && job.durationMs != nil && !job.cancellable)131        let r = try #require(job.labResult)132        #expect(r.success && r.schema == "lab/1" && r.engine == job.tool && !r.title.isEmpty && !r.headline.isEmpty)133        #expect(!r.summary.isEmpty && !r.charts.isEmpty && !r.methodology.isEmpty)134        for m in r.summary {135            #expect(!m.label.isEmpty && m.kind.known != nil)136            #expect(!m.formatted().isEmpty)137        }138        for t in r.tables {139            #expect(!t.columns.isEmpty)140            for row in t.rows.prefix(5) { for c in t.columns { _ = t.cell(row, c) } }141        }142        for c in r.charts {143            #expect(c.type.known != nil, "chart type \(c.type.rawValue)")144            #expect(c.x.type.known != nil)145            if c.type == .heatmap || c.type == .surface {146                let m = try #require(c.matrix)147                #expect(m.z.count == m.y.count && m.z.allSatisfy { $0.count == m.x.count } && m.range != nil)148            } else {149                #expect(!c.series.isEmpty)150                for s in c.series {151                    #expect(s.x.count == s.y.count && s.kind.known != nil)152                    if let lo = s.lower, let hi = s.upper { #expect(lo.count == s.x.count && hi.count == s.x.count) }153                    if c.x.type == .time { #expect(s.x.allSatisfy { $0.stringValue.flatMap(VQISODate.parse) != nil }) }154                }155            }156        }157        for d in r.diagnostics { #expect(!d.name.isEmpty && !d.conclusion.isEmpty) }158        let p = try #require(r.provenance)159        #expect(!p.data.isEmpty && p.software.first?.name == "python" && p.computedAt.flatMap(VQISODate.parse) != nil && p.reproduce?.isEmpty == false && !p.spec.isEmpty)160    }161162    @Test func everyChartTypeOfTheSchemaIsCoveredByAFixture() throws {163        var seen = Set<String>()164        for name in Fixture.labJobNames {165            let r = VQLabResult(json: try Fixture.json("job_\(name).json")["job"]!["result"]!)166            for c in r.charts { seen.insert(c.type.rawValue) }167        }168        // `candles` is in the schema but no engine emits it today169        #expect(seen == ["line", "area", "bar", "scatter", "histogram", "heatmap", "surface", "payoff", "fan"])170    }171172    @Test func jobLifecycleStates() throws {173        let running = VQQuantJob(json: try Fixture.json("job_running_volatility_lab.json")["job"]!)174        #expect(running.isRunning && running.cancellable && running.result == nil && running.durationMs == nil && running.progress == 0)175        let cancelled = VQQuantJob(json: try Fixture.json("job_cancelled.json")["job"]!)176        #expect(cancelled.status == .cancelled && cancelled.isFinished && cancelled.result == nil && cancelled.failureMessage == "Cancelled.")177        let failed = VQQuantJob(json: try Fixture.json("job_failure.json")["job"]!)178        #expect(failed.status == .error && failed.labResult == nil && failed.failureMessage?.isEmpty == false)179        let legacy = VQQuantJob(json: try Fixture.json("job_legacy_garch.json")["job"]!)180        #expect(legacy.status == .done && legacy.labResult == nil && legacy.result != nil && legacy.code != nil)   // legacy results are left to the apps181        let list = VQQuantJobList(json: try Fixture.json("quant_jobs_list.json"))182        #expect(!list.jobs.isEmpty && list.jobs.allSatisfy { $0.result == nil })183        #expect(VQQuantJob(json: ["id": "x", "tool": "t", "status": "queued_on_gpu"]).isFinished)     // unknown status: never throws184    }185186    @Test func objectsOfEveryKind() throws {187        let w = try #require(VQObjectEnvelope(json: try Fixture.json("object_watchlist.json")["object"]!).typed(VQWatchlist.self))188        #expect(w.id == "kit-test-watchlist" && w.rev == 2 && w.data.symbols.count == 5 && w.data.note == "fixture")189        #expect(w.data.unknownKeys(known: ["name", "symbols", "note"]) == ["kitFutureField"])190191        func first<T: VQSyncData>(_ file: String, _ type: T.Type, id: String) throws -> VQObject<T> {192            let list = try Fixture.json(file).models("objects", as: VQObjectEnvelope.self)193            return try #require(list.first { $0.id == id }?.typed(T.self))194        }195        let p = try first("objects_portfolio.json", VQPortfolio.self, id: "kit-test-portfolio")196        #expect(p.data.baseCurrency == "USD" && p.data.benchmark == "SPY" && p.data.cash.count == 2 && p.data.positions.count == 2)197        #expect(p.data.position("AAPL")?.costBasis == 150.25 && p.data.position("AAPL")?.openedAt != nil)198        #expect(p.data.position("AAPL")?.raw["updatedAt"] == .number(1_700_000_001_000))        // the legacy bridge's own key199200        let ws = try first("objects_workspace.json", VQWorkspace.self, id: "kit-test-workspace")201        #expect(ws.data.layout.kind == .split && ws.data.layout.direction == .row && ws.data.layout.panelIds == ["p1", "p2", "p3"])202        #expect(ws.data.layout.children[1].direction == .column && ws.data.panels["p1"]?.type == .chart && ws.data.validationIssues().isEmpty)203204        let ch = try first("objects_chart.json", VQChart.self, id: "kit-test-chart")205        #expect(ch.data.symbol == "AAPL" && ch.data.chartType == .candles && ch.data.indicators.count == 2 && ch.data.compare == ["MSFT"] && ch.data.events)206        #expect(ch.data.drawings[0].points.count == 2 && ch.data.drawings[0].points[0].p == 220.5 && ch.data.drawings[0].text == "trend")207208        let pr = try first("objects_project.json", VQProject.self, id: "kit-test-project")209        #expect(pr.data.status == .active && pr.data.hypotheses[0].status == .open && pr.data.items.count == 4 && pr.data.items[0].type == .note)210        #expect(pr.data.items[3].addedAt == nil && pr.data.dataSources[0].provider == "fmp")211212        let n = try first("objects_note.json", VQNote.self, id: "kit-test-note")213        #expect(n.data.projectId == "kit-test-project" && n.data.body.contains("$r_t$"))214215        let reviews = try Fixture.json("objects_review.json").models("objects", as: VQObjectEnvelope.self)216        let review = try #require(reviews.first?.typed(VQReview.self))217        #expect(review.device == "server" && review.data.status == .done && review.data.automated && review.data.target?.type == .quantJob)218        #expect(review.data.checks.count >= 5 && review.data.checks[0].category == .math && review.data.checks.contains { $0.isFromReviewerModel })219        #expect(review.data.usage?.byModel.first?.role == .reviewer && review.data.disclaimer?.contains("not a human peer review") == true)220        #expect(try Fixture.json("objects_preference.json").models("objects", as: VQObjectEnvelope.self).isEmpty)221    }222223    @Test func storeResponses() throws {224        let tomb = VQObjectEnvelope(json: try Fixture.json("delete_watchlist.json")["object"]!)225        #expect(tomb.deleted && tomb.data == .object([:]) && tomb.typed(VQWatchlist.self) == nil)226        let conflict = try Fixture.json("conflict_stale.json")227        let e = VQError(.conflict, status: 409, message: "conflict", body: conflict)228        #expect(VQObjectStoreClient.current(of: e)?.rev == 2)229        let page = VQChangesPage(json: try Fixture.json("sync_changes_page.json"))230        #expect(page.changes.count == 3 && page.more && page.seq >= page.changes.last!.seq)231        #expect(zip(page.changes, page.changes.dropFirst()).allSatisfy { $0.seq < $1.seq })232        let all = VQChangesPage(json: try Fixture.json("sync_changes_all.json"))233        #expect(!all.more && all.changes.contains { $0.deleted } && all.changes.last?.seq == all.seq)234        let kinds = VQChangesPage(json: try Fixture.json("sync_changes_kinds.json"))235        #expect(Set(kinds.changes.map(\.kind)).isSubset(of: [.watchlist, .project]))236        #expect(try Fixture.json("versions_note.json").models("versions", as: VQObjectVersion.self).isEmpty)237    }238239    @Test func projectBundle() throws {240        let b = VQProjectBundle(json: try Fixture.json("project_bundle.json"))241        #expect(b.data.title == "Kit test project" && b.items.count == 4)242        #expect(b.items[0].resolved?.title == "Kit test note" && b.items[0].item.type == .note)243        #expect(b.items[3].resolved == nil && b.items[3].item.type == .notebook)            // no server-side store244    }245246    @Test func usageAuditMemory() throws {247        let u = VQUsageReport(json: try Fixture.json("usage.json"))248        #expect(u.days == 30 && u.totals.calls == 7 && u.totals.costUsd != nil && u.byDay.first?.date.count == 10 && u.byModel.contains { $0.role == .reviewer })249        let events = try Fixture.json("audit.json").models("events", as: VQAuditEvent.self)250        #expect(events.count == 4 && events[0].action == "project.export" && events[0].id > events[1].id && events[0].detail?.str("format") == "zip")251        let m = VQMemoryOverview(json: try Fixture.json("memory.json"))252        #expect(m.settings.enabled && m.settings.retentionDays == nil && m.groups.first?.scope == .user && m.count == 1)253        #expect(m.groups[0].memories[0].kind == .preference && m.groups[0].memories[0].source == .agent && !m.groups[0].memories[0].pinned)254        #expect(VQMemoryScope("project:p1") == .project("p1") && VQMemoryScope.project("p1").rawValue == "project:p1")255    }256257    @Test func sessionWithProvenanceAndTrace() throws {258        let session = try Fixture.json("session_with_provenance.json")259        let extras = VQMessageExtras.ofSession(session)            // `messages` is a JSON string in the legacy route260        #expect(extras.count == 1)261        #expect(extras[0].provenance.first?.provider == .quantEngine && extras[0].provenance.first?.symbols == ["SPY"])262        #expect(extras[0].model == "gpt-5-nano" && extras[0].generationId != nil && extras[0].usage?.byModel.first?.role == .lead)263        // the same payload with messages as an array264        let asArray: VQJSON = ["messages": .array(VQMessageExtras.messages(of: session))]265        #expect(VQMessageExtras.ofSession(asArray) == extras)266267        let trace = VQGenerationTrace(json: try Fixture.json("generation_trace.json"))268        #expect(trace.status == .done && trace.requestedModel == "auto" && trace.model == "gpt-5-nano" && trace.mode == "fast")269        #expect(trace.toolCalls.count == 3 && trace.toolCalls[0].ok == false && trace.toolCalls[0].error != nil && trace.toolCalls[2].ok)270        #expect(trace.usage?.costUsd != nil && trace.generationId == extras[0].generationId)271    }272273    @Test func recordedGenerationStream() throws {274        var parser = VQSSEParser()275        let events = parser.feed(try Fixture.data("generation_stream.txt")).map(VQGenerationEvent.init)276        var routed: VQRouteDecision?, provenance: [VQProvenanceSource]?, usage: VQUsage?, others = 0277        for e in events {278            switch e {279            case .modelRouted(let r): routed = r280            case .provenance(let p): provenance = p281            case .done(_, let u): usage = u282            case .other: others += 1283            case .budget, .quantJob: break284            }285        }286        #expect(routed?.requested == "auto" && routed?.model == "claude-haiku-5-5" && (routed?.candidates.count ?? 0) > 1)287        #expect(provenance?.first?.tool == "get_stock_quote" && provenance?.first?.provider == .fmp && provenance?.first?.ok == true)288        #expect(usage?.byModel.first?.role == .lead && usage?.costUsd != nil)289        #expect(others > 5 && parser.malformed == 0)          // the v5 events stay with the apps290    }291292    /// `budget` and `quant_job` did not occur in the recorded run: these payloads are written from CONTRACT §2.4.293    @Test func streamEventsFromTheContract() {294        guard case .budget(let b) = VQGenerationEvent(json: ["type": "budget", "kind": "cost", "limit": 0.5, "spent": 0.61]) else { Issue.record("budget"); return }295        #expect(b.kind == .cost && b.limit == 0.5 && b.spent == 0.61)296        guard case .quantJob(let q) = VQGenerationEvent(json: ["type": "quant_job", "tool": "run_quant_engine", "toolId": "t1", "agent": "lead", "jobId": "j1", "engine": "risk_lab", "status": "cancelled"]) else { Issue.record("quant_job"); return }297        #expect(q.jobId == "j1" && q.engine == "risk_lab" && q.status == .cancelled)298        // unknown event types and unknown enum values never throw299        guard case .other(let type, _) = VQGenerationEvent(json: ["type": "research_cycle", "n": 3]) else { Issue.record("other"); return }300        #expect(type == "research_cycle")301        guard case .budget(let odd) = VQGenerationEvent(json: ["type": "budget", "kind": "tokens", "limit": "a lot"]) else { Issue.record("budget2"); return }302        #expect(odd.kind.known == nil && odd.kind.rawValue == "tokens" && odd.limit == nil)303        guard case .other = VQGenerationEvent(json: "not an object") else { Issue.record("scalar"); return }304        #expect(VQGenerationEvent.agentRole(of: ["type": "agent_start", "role": "risk_analyst"]) == "risk_analyst")305    }306307    @Test func failureResult() {308        let r = VQLabResult(json: ["success": false, "schema": "lab/1", "engine": "regression", "error": "No data for ZZZZ."])309        #expect(!r.success && r.error == "No data for ZZZZ." && r.charts.isEmpty && r.provenance == nil)310        #expect(VQLabResult.ifLab(["success": true, "symbol": "AAPL"]) == nil)311    }312}313
Files main
Menu

Theme