Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/CoreTests/BackendIntegrationTests.swift3// Description: The client against a REAL VQuant backend (the test copy on its node, reached through an SSH tunnel): passkey ceremonies, device sessions, market data, quant jobs, a real analysis. Run by Scripts/integration-test.sh.4import Foundation5import Testing6@testable import VQuantCore78/// Shared across the serialized suite: one enrolment, then everything else with the session it opened.9private actor IT {10 static let shared = IT()11 let url = ProcessInfo.processInfo.environment["VQ_IT_URL"].flatMap(URL.init(string:))12 let setup = ProcessInfo.processInfo.environment["VQ_IT_SETUP"] ?? ""13 let analysis = ProcessInfo.processInfo.environment["VQ_IT_ANALYSIS"] == "1"14 nonisolated static var enabled: Bool { ProcessInfo.processInfo.environment["VQ_IT_URL"] != nil }1516 private var built: (api: VQuantAPI, auth: AuthService, authn: SoftwareAuthenticator, tokens: MemoryTokenStore)?17 func env() -> (api: VQuantAPI, auth: AuthService, authn: SoftwareAuthenticator, tokens: MemoryTokenStore) {18 if let built { return built }19 let tokens = MemoryTokenStore()20 let client = APIClient(baseURL: url!, identity: ClientIdentity(version: "2.0.0", build: "1", deviceName: "Mac d’essai (intégration)"), tokens: tokens, transport: URLSessionTransport())21 let authn = SoftwareAuthenticator(origin: url!.absoluteString.trimmingCharacters(in: CharacterSet(charactersIn: "/")))22 let b = (VQuantAPI(client: client), AuthService(api: client, authenticator: authn), authn, tokens)23 built = b24 return b25 }26 func second(deviceName: String) -> (api: VQuantAPI, auth: AuthService, tokens: MemoryTokenStore) {27 let e = env()28 let tokens = MemoryTokenStore()29 let client = APIClient(baseURL: url!, identity: ClientIdentity(version: "2.0.0", build: "1", deviceName: deviceName), tokens: tokens, transport: URLSessionTransport())30 return (VQuantAPI(client: client), AuthService(api: client, authenticator: e.authn), tokens)31 }32}3334@Suite(.serialized, .enabled(if: IT.enabled, "set VQ_IT_URL (Scripts/integration-test.sh)"))35struct BackendIntegrationTests {36 @Test func a01_lockedWithoutSession() async throws {37 let e = await IT.shared.env()38 #expect(try await e.api.health().uptime >= 0)39 await #expect { try await e.api.workspace() } throws: { ($0 as? APIError)?.kind == .unauthorized }40 await #expect { try await e.api.indices() } throws: { ($0 as? APIError)?.kind == .unauthorized }41 // an unknown passkey is refused by the server42 let stranger = SoftwareAuthenticator(origin: e.authn.origin)43 _ = try await stranger.register(RegistrationRequest(rpId: "localhost", challenge: Data([1]), userId: Data("simon".utf8), userName: "x", excludeCredentials: []))44 let auth = AuthService(api: e.api.client, authenticator: stranger)45 await #expect { try await auth.signIn() } throws: { ($0 as? AuthError)?.code == .refused }46 #expect(e.tokens.read() == nil)47 }4849 @Test func a02_enrolWithSetupLinkThenLinkIsSpent() async throws {50 let e = await IT.shared.env()51 let link = await IT.shared.setup52 try #require(!link.isEmpty, "VQ_IT_SETUP missing")53 let account = try await e.auth.enrol(setupLink: link)54 #expect(account.id == "simon")55 #expect(e.tokens.read()?.hasPrefix("vqn_") == true)56 let s = try await e.auth.session()57 #expect(s.user == account && s.device.platform == "macos" && s.reauthFresh)58 #expect(s.device.label == "Mac d’essai (intégration) · VQuant macOS 2.0.0")59 // single use60 let again = await IT.shared.second(deviceName: "Autre")61 await #expect { try await again.auth.enrol(setupLink: link) } throws: { ($0 as? AuthError)?.code == .expiredLink }62 }6364 @Test func a03_signInOnASecondDeviceThenRevokeIt() async throws {65 let e = await IT.shared.env()66 let other = await IT.shared.second(deviceName: "Deuxième Mac")67 #expect(try await other.auth.signIn().id == "simon")68 let before = try await e.auth.devices()69 #expect(before.sessions.count >= 2 && before.sessions.filter { $0.current == true }.count == 1)70 #expect(before.passkeys.contains { $0.label.hasPrefix("Mac d’essai") })71 let target = try #require(before.sessions.first { $0.label.hasPrefix("Deuxième Mac") })72 try await e.auth.revokeDevice(target.id)73 // the revoked device is locked out at once, and is told so74 await #expect { try await other.api.workspace() } throws: { ($0 as? APIError)?.kind == .unauthorized }75 #expect(try await e.auth.devices().sessions.contains { $0.id == target.id } == false)76 // this device's own session cannot be revoked through the "other device" route77 let me = try #require(before.sessions.first { $0.current == true })78 await #expect { try await e.auth.revokeDevice(me.id) } throws: { ($0 as? APIError)?.kind == .rejected }79 // the only passkey of the account that this authenticator holds may not be the last: deleting it must not lock the person out80 try await e.auth.reauthenticate()81 #expect(try await e.auth.session().reauthFresh)82 }8384 @Test func a04_passkeyWithWrongOriginIsRefused() async throws {85 let e = await IT.shared.env()86 // same key material would be needed to sign in; a ceremony bound to another origin must fail verification87 let phish = SoftwareAuthenticator(origin: "https://vquant.example.com")88 let tokens = MemoryTokenStore()89 let client = APIClient(baseURL: e.api.client.baseURL, identity: e.api.client.identity, tokens: tokens, transport: URLSessionTransport())90 _ = try await phish.register(RegistrationRequest(rpId: "localhost", challenge: Data([1]), userId: Data("simon".utf8), userName: "x", excludeCredentials: []))91 await #expect { try await AuthService(api: client, authenticator: phish).signIn() } throws: { ($0 as? AuthError)?.code == .refused }92 #expect(tokens.read() == nil)93 }9495 @Test func b01_marketData() async throws {96 let api = await IT.shared.env().api97 let idx = try await api.indices()98 #expect(idx.count >= 8 && idx.allSatisfy { $0.price > 0 })99 let q = try #require(try await api.quote("AAPL"))100 #expect(q.symbol == "AAPL" && q.price > 0 && q.asOf != nil)101 #expect(try await api.quote("ZZZZNOTATICKER") == nil)102 let to = CSV.day.string(from: Date()), from = CSV.day.string(from: Date().addingTimeInterval(-120 * 86400))103 let daily = try await api.dailyBars("AAPL", from: from, to: to)104 #expect(daily.bars.count > 60 && daily.dropped == 0)105 #expect(zip(daily.bars, daily.bars.dropFirst()).allSatisfy { $0.time < $1.time })106 let intra = try await api.intradayBars("AAPL", interval: .m5, from: CSV.day.string(from: Date().addingTimeInterval(-6 * 86400)), to: to)107 #expect(intra.bars.count > 50)108 #expect(try await api.movers(.gainers).isEmpty == false)109 #expect(try await api.search("AAPL").contains { $0.symbol == "AAPL" })110 #expect(try await api.profile("AAPL")?.companyName == "Apple Inc.")111 #expect(try await api.marketHours().isEmpty == false)112 let income = DataTable(rows: try await api.dataset(.income, symbol: "AAPL", quarterly: true, limit: 4))113 #expect(income.rows.count == 4 && income.columns.contains("revenue"))114 for d in Dataset.allCases { #expect(try await api.dataset(d, symbol: "AAPL").isEmpty == false, "\(d.rawValue) returned nothing") }115 }116117 @Test func b02_workspaceRoundTrip() async throws {118 let api = await IT.shared.env().api119 let before = try await api.workspace()120 #expect(before.user.id == "simon")121 let p = try await api.savePosition(symbol: "zzit", quantity: 12.5, costBasis: 3.25, note: "integration")122 #expect(p.symbol == "ZZIT" && p.quantity == 12.5 && p.costBasis == 3.25)123 #expect(try await api.workspace().positions.contains(p))124 try await api.deletePosition("ZZIT")125 let m = try await api.addMemory(content: "Integration test memory \(UUID().uuidString.prefix(6))", category: "suivi")126 #expect(try await api.workspace().memories.contains { $0.id == m.id })127 try await api.deleteMemory(m.id)128 let w = try await api.createWatch(title: "IT watch", prompt: "Résumé du marché", time: "08:30", days: "weekdays", deep: false, model: ResearchModel.default)129 #expect(try await api.updateWatch(w.id, ["enabled": false]).enabled == false)130 await #expect { try await api.createWatch(title: "bad", prompt: "x", time: "25:99", days: "daily", deep: false, model: "m") } throws: { ($0 as? APIError)?.kind == .rejected }131 try await api.deleteWatch(w.id)132 let after = try await api.workspace()133 #expect(after.positions == before.positions && after.memories == before.memories && after.watches == before.watches)134 }135136 @Test func b03_quantJobLifecycle() async throws {137 let api = await IT.shared.env().api138 let cat = try await api.quantCatalogue()139 // a client that announces 2.0.0 receives every lab engine (CONTRACT §4.1); the six 1.x engines are among them140 #expect(cat.tools.count == 19)141 #expect(Set(["garch", "var", "risk_metrics"]).isSubset(of: Set(cat.tools.map(\.id))))142 await #expect { try await api.submitQuantJob(tool: "garch", params: ["symbol": "AAPL", "p": 9]) } throws: { ($0 as? APIError)?.kind == .rejected }143 await #expect { try await api.submitQuantJob(tool: "nope", params: [:]) } throws: { ($0 as? APIError)?.kind == .rejected }144 let job = try await api.submitQuantJob(tool: "var", params: ["symbol": "msft", "confidence_levels": [0.95]])145 #expect(job.status == .running && job.params["symbol"] == "MSFT" && job.params["portfolio_value"] == 100000) // defaults are recorded146 #expect(try await api.quantJobs().jobs.contains { $0.id == job.id })147 var done = job148 for _ in 0..<60 { done = try await api.quantJob(job.id); if done.status != .running { break }; try await Task.sleep(for: .seconds(1)) }149 #expect(done.status == .done)150 #expect(done.result?["var_by_confidence"]?["95%"]?["historical"]?["var_dollar"]?.doubleValue ?? 0 > 0)151 #expect(done.code?.contains("import") == true && done.params == job.params)152 // a job that cannot succeed ends as an error with the engine's reason, never as an empty success153 let bad = try await api.submitQuantJob(tool: "risk_metrics", params: ["symbol": "ZZZZNOTATICKER"])154 var failed = bad155 for _ in 0..<60 { failed = try await api.quantJob(bad.id); if failed.status != .running { break }; try await Task.sleep(for: .seconds(1)) }156 #expect(failed.status == .error && failed.result == nil && !(failed.error ?? "").isEmpty)157 try await api.deleteQuantJob(job.id); try await api.deleteQuantJob(bad.id)158 await #expect { try await api.quantJob(job.id) } throws: { ($0 as? APIError)?.kind == .notFound }159 }160161 @Test func b04_conversationsAndDocuments() async throws {162 let api = await IT.shared.env().api163 let list = try await api.sessions()164 if let first = list.first {165 let c = try await api.conversation(first.sessionId)166 #expect(c.sessionId == first.sessionId && !c.turns.isEmpty)167 }168 await #expect { try await api.conversation("does-not-exist") } throws: { ($0 as? APIError)?.kind == .notFound }169 _ = try await api.sharedReports()170 _ = try await api.generations()171 #expect(try await api.usage().sessions.total >= 0)172 let pdf = try await api.renderPDF(question: "Test d'intégration", answer: "# Titre\n\nUn paragraphe.\n\n| a | b |\n|---|---|\n| 1 | 2 |")173 #expect(pdf.prefix(5) == Data("%PDF-".utf8) && pdf.count > 2000)174 }175176 @Test(.enabled(if: ProcessInfo.processInfo.environment["VQ_IT_ANALYSIS"] == "1", "set VQ_IT_ANALYSIS=1 (calls the model provider)"))177 func c01_realAnalysisStreamReplayAndCancel() async throws {178 let api = await IT.shared.env().api179 let req = ChatRequest(query: "Donne le dernier cours de l'action Apple (AAPL) en une phrase, sans graphique.", sessionId: nil, history: [], mode: .fast, model: "claude-haiku-5-5")180 var last: GenerationRunner.Update?181 for await u in GenerationRunner(api: api).start(req) { last = u }182 let s = try #require(last?.state)183 #expect(last?.phase == .finished && s.finished && s.error == nil)184 #expect(!s.answer.isEmpty && s.jobId != nil && s.sessionId != nil)185 #expect(s.tools.contains { $0.state == .done })186 // a second viewer replaying the job from its first event reconstructs exactly the same state187 var replay: GenerationRunner.Update?188 for await u in GenerationRunner(api: api).attach(jobId: s.jobId!) { replay = u }189 #expect(replay?.state.answer == s.answer && replay?.state.tools == s.tools && replay?.state.nextEventIndex == s.nextEventIndex)190 // and one that joins midway (from event 5) is given only what it has not seen191 var partial = RunState(); partial.nextEventIndex = 5192 var mid: GenerationRunner.Update?193 for await u in GenerationRunner(api: api).attach(jobId: s.jobId!, state: partial) { mid = u }194 #expect(mid?.state.nextEventIndex == s.nextEventIndex && mid?.state.finished == true)195 // the answer was saved in the conversation by the server196 let sid = try #require(s.sessionId)197 let conv = try await api.conversation(sid)198 #expect(conv.turns.last?.answer == s.answer && conv.turns.last?.question == req.query)199 #expect(try await api.generations().generations.contains { $0.id == s.jobId && $0.status == .done })200 try await api.renameSession(sid, title: "IT renamed"); try await api.pinSession(sid, pinned: true)201 #expect(try await api.sessions().first { $0.sessionId == sid }?.title == "IT renamed")202 try await api.deleteSession(sid)203204 // Stop: the job ends as cancelled and the viewer is told it is over205 let long = ChatRequest(query: "Analyse détaillée de Microsoft avec trois graphiques.", sessionId: nil, history: [], mode: .standard, model: "claude-haiku-5-5")206 var cancelled: GenerationRunner.Update?207 var asked = false208 for await u in GenerationRunner(api: api).start(long) {209 cancelled = u210 if !asked, let id = u.state.jobId { asked = true; try await api.cancelGeneration(id) }211 }212 #expect(cancelled?.state.finished == true)213 let job = try #require(try await api.generations().generations.first { $0.id == cancelled?.state.jobId })214 #expect(job.status == .cancelled)215 if let sid = cancelled?.state.sessionId { try? await api.deleteSession(sid) }216 }217218 @Test func z01_signOutRevokesOnTheServer() async throws {219 let e = await IT.shared.env()220 let token = try #require(e.tokens.read())221 await e.auth.signOut()222 #expect(e.tokens.read() == nil)223 // the old token is dead on the server, not merely forgotten here224 try e.tokens.write(token)225 await #expect { try await e.auth.session() } throws: { ($0 as? APIError)?.kind == .unauthorized }226 e.tokens.clear()227 // and the passkey still signs in: a new session, same account228 #expect(try await e.auth.signIn().id == "simon")229 await e.auth.signOut()230 }231}232233/// Not a test of behaviour: opens a device session on the TEST backend for Scripts/run-dev.sh (visual checks of the234/// Debug app, which cannot use system passkeys) and writes its token to a private file.235@Suite(.enabled(if: ProcessInfo.processInfo.environment["VQ_DEV_TOKEN_FILE"] != nil, "used by Scripts/dev-session.sh"))236struct DevSession {237 @Test func openDevSession() async throws {238 let env = ProcessInfo.processInfo.environment239 let url = try #require(env["VQ_IT_URL"].flatMap(URL.init(string:)))240 try #require(["localhost", "127.0.0.1"].contains(url.host ?? ""), "development sessions are opened on a loopback test server only")241 let tokens = MemoryTokenStore()242 let client = APIClient(baseURL: url, identity: ClientIdentity(version: "2.0.0", build: "1", deviceName: "Mac d’essai (run-dev)"), tokens: tokens, transport: URLSessionTransport())243 let auth = AuthService(api: client, authenticator: SoftwareAuthenticator(origin: url.absoluteString.trimmingCharacters(in: CharacterSet(charactersIn: "/"))))244 _ = try await auth.enrol(setupLink: try #require(env["VQ_IT_SETUP"]))245 let path = try #require(env["VQ_DEV_TOKEN_FILE"])246 FileManager.default.createFile(atPath: path, contents: Data(try #require(tokens.read()).utf8), attributes: [.posixPermissions: 0o600])247 }248}249