SPB Git

Repository Public

NetworkTests.swift 15.9 KB 229 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/CoreTests/NetworkTests.swift3// Description: Client behaviour under failure, with a scripted transport: errors, session expiry, stream reconnection, cache fallback, authorization boundaries.4import CryptoKit5import Foundation6import Testing7@testable import VQuantCore89@Suite struct NetworkTests {10    @Test func requestsCarryTokenClientHeaderAndEncodedSymbols() async throws {11        let t = MockTransport(); let (api, _) = makeAPI(t)12        t.on("GET", "/api/fmp/quote/^GSPC", .json(200, "[]"))13        t.on("GET", "/api/fmp/chart/full/BRK-B", .json(200, "[]"))14        #expect(try await api.quote("^GSPC") == nil)15        _ = try await api.dailyBars("BRK-B", from: "2026-01-01", to: "2026-02-01")16        let r = t.requests17        #expect(r[0].headers["Authorization"] == "Bearer vqn_" + String(repeating: "a", count: 43))18        #expect(r[0].headers["X-VQ-Client"] == "vquant-macos/2.0.0")19        #expect(r[1].query == "from=2026-01-01&to=2026-02-01")20        // no cookie is ever sent: the session is the Bearer token only21        #expect(r.allSatisfy { $0.headers["Cookie"] == nil })22    }2324    @Test func errorVocabulary() async throws {25        let t = MockTransport(); let (api, _) = makeAPI(t)26        t.on("GET", "/api/sessions/x", .json(404, #"{"error":"Session not found"}"#))27        t.on("POST", "/api/quant/jobs", .json(400, #"{"error":"p (GARCH lags): must be between 1 and 5."}"#), .json(429, #"{"error":"3 computations are already running. Wait for one to finish."}"#))28        t.on("GET", "/api/fmp/indices", .json(500, #"{"error":"Failed to fetch indices"}"#), .fail(.notConnectedToInternet), .json(200, "<html>gateway</html>"))29        await #expect { try await api.conversation("x") } throws: { ($0 as? APIError)?.kind == .notFound }30        await #expect { try await api.submitQuantJob(tool: "garch", params: [:]) } throws: { ($0 as? APIError)?.message == "p (GARCH lags): must be between 1 and 5." }31        await #expect { try await api.submitQuantJob(tool: "garch", params: [:]) } throws: { ($0 as? APIError)?.kind == .rateLimited }32        await #expect { try await api.indices() } throws: { ($0 as? APIError)?.kind == .server }33        await #expect { try await api.indices() } throws: { ($0 as? APIError)?.isOffline == true }34        await #expect { try await api.indices() } throws: { ($0 as? APIError)?.kind == .decoding }35    }3637    @Test func lockedAnswerReportsSessionEndOnce() async throws {38        let t = MockTransport(); let (api, _) = makeAPI(t)39        let fired = MemoryTokenStore()40        api.client.onUnauthorized { try? fired.write((fired.read() ?? "") + "x") }41        t.on("GET", "/api/workspace", .json(401, #"{"error":"Accès verrouillé"}"#, headers: ["x-vq-locked": "1"]))42        await #expect { try await api.workspace() } throws: { ($0 as? APIError)?.kind == .unauthorized }43        #expect(fired.read() == "x")44    }4546    @Test func assetPathsAreConfinedToServerAssets() async throws {47        let t = MockTransport(); let (api, _) = makeAPI(t)48        t.on("GET", "/figures/fig-1-0.png", .json(200, "PNG"))49        #expect(try await api.asset(path: "/figures/fig-1-0.png") == Data("PNG".utf8))50        for bad in ["/api/workspace", "/figures/../.env", "https://evil.example/figures/x.png", "/figures/a b.png", "/api/download/../../local.db"] {51            await #expect { try await api.asset(path: bad) } throws: { ($0 as? APIError)?.kind == .rejected }52        }53        #expect(t.requests.count == 1)54    }5556    // MARK: Authentication5758    @Test func signInStoresTokenAndReportsRefusals() async throws {59        let t = MockTransport()60        let store = MemoryTokenStore()61        let client = APIClient(baseURL: URL(string: "https://vquant.test")!, identity: testIdentity, tokens: store, transport: t)62        let authn = SoftwareAuthenticator(origin: "https://vquant.test")63        let auth = AuthService(api: client, authenticator: authn)64        t.on("POST", "/api/native/login/options", .json(200, #"{"rpId":"vquant.test","challenge":"AAECAw","userVerification":"required"}"#))65        t.on("POST", "/api/native/login/verify", .json(401, #"{"error":"refused"}"#))66        // no passkey on this device: the ceremony is cancelled before anything is sent to verify67        await #expect { try await auth.signIn() } throws: { ($0 as? AuthError)?.code == .cancelled }68        #expect(t.requests.count == 1 && store.read() == nil)69        // options are fetched without any Authorization header70        #expect(t.requests[0].headers["Authorization"] == nil)71        #expect(String(data: t.requests[0].body ?? Data(), encoding: .utf8) == "{}")72    }7374    @Test func setupLinkParsing() {75        let tok = String(repeating: "Ab3_-", count: 9)76        #expect(AuthService.setupToken(from: "https://vquant.spboucher.ai/setup/\(tok)", host: "vquant.spboucher.ai") == tok)77        #expect(AuthService.setupToken(from: "  \(tok)\n", host: "vquant.spboucher.ai") == tok)78        #expect(AuthService.setupToken(from: "https://evil.example/setup/\(tok)", host: "vquant.spboucher.ai") == nil)   // a link to another host is never sent to ours79        #expect(AuthService.setupToken(from: "https://vquant.spboucher.ai/setup/short", host: "vquant.spboucher.ai") == nil)80        #expect(AuthService.setupToken(from: "https://vquant.spboucher.ai/other/\(tok)", host: "vquant.spboucher.ai") == nil)81    }8283    @Test func sensitiveCallReauthenticatesOnceThenRetries() async throws {84        let t = MockTransport()85        let client = APIClient(baseURL: URL(string: "https://vquant.test")!, identity: testIdentity, tokens: MemoryTokenStore("vqn_" + String(repeating: "b", count: 43)), transport: t)86        let authn = SoftwareAuthenticator(origin: "https://vquant.test")87        _ = try await authn.register(RegistrationRequest(rpId: "vquant.test", challenge: Data([1]), userId: Data("simon".utf8), userName: "S", excludeCredentials: []))88        let auth = AuthService(api: client, authenticator: authn)89        t.on("DELETE", "/api/native/devices/d2", .json(403, #"{"error":"reauth_required"}"#), .json(200, #"{"ok":true}"#))90        t.on("POST", "/api/native/reauth/options", .json(200, #"{"rpId":"vquant.test","challenge":"AAECAw","allowCredentials":[]}"#))91        t.on("POST", "/api/native/reauth/verify", .json(200, #"{"ok":true}"#))92        try await auth.revokeDevice("d2")93        #expect(t.requests.map(\.path) == ["/api/native/devices/d2", "/api/native/reauth/options", "/api/native/reauth/verify", "/api/native/devices/d2"])94        #expect(authn.assertions == 1)95    }9697    @Test func signOutForgetsTokenEvenOffline() async {98        let t = MockTransport(); let (api, store) = makeAPI(t)99        t.on("POST", "/api/native/logout", .fail(.notConnectedToInternet))100        await AuthService(api: api.client, authenticator: SoftwareAuthenticator(origin: "x")).signOut()101        #expect(store.read() == nil)102    }103104    // MARK: Streams105106    @Test func sseParserHandlesIdsPingsAndGarbage() {107        var p = SSEParser()108        #expect(p.feed(": ping") == nil && p.feed("") == nil && p.feed("id: 4") == nil)109        #expect(p.feed(#"data: {"type":"text","content":"a"}"#) == SSEFrame(id: 4, json: ["type": "text", "content": "a"]))110        #expect(p.feed(#"data: {"type":"job","id":"j"}"#)?.id == nil)   // the opening frame carries no id111        #expect(p.feed("data: {not json") == nil && p.malformed == 1)112    }113114    static let run: [String] = [115        #"{"type":"status","message":"Analyse en cours…"}"#, #"{"type":"plan","steps":[{"title":"Données","status":"in_progress"}]}"#,116        #"{"type":"tool_start","tool":"get_stock_quote","toolId":"t1","input":{"symbol":"AAPL"}}"#, #"{"type":"tool_complete","tool":"get_stock_quote","toolId":"t1","duration":120,"empty":false}"#,117        #"{"type":"tool_result","tool":"get_stock_quote","result":[{"price":352.45}]}"#, #"{"type":"text","content":"Apple cote "}"#, #"{"type":"text","content":"352,45 $."}"#,118        #"{"type":"custom_python_figures","figureId":"fig-1","figures":["AAAA"],"figureUrls":["/figures/fig-1-0.png"],"output":"ok","description":"Cours"}"#,119        #"{"type":"something_new","x":1}"#, #"{"type":"sources","sources":[{"title":"FMP","url":"https://fmp.example","snippet":"","rank":1}]}"#,120        #"{"type":"followups","questions":["Et Microsoft ?"]}"#, #"{"type":"done","sessionId":"s-1"}"#,121    ]122    static let opening = #"data: {"type":"job","id":"job-1","sessionId":"s-1","query":"Cours AAPL"}"#123124    func collect(_ s: AsyncStream<GenerationRunner.Update>) async -> [GenerationRunner.Update] { var out: [GenerationRunner.Update] = []; for await u in s { out.append(u) }; return out }125126    @Test func runStateFromACompleteStream() async {127        let t = MockTransport(); let (api, _) = makeAPI(t)128        t.on("POST", "/api/chat", .sse([Self.opening] + events(Self.run)))129        let ups = await collect(GenerationRunner(api: api).start(ChatRequest(query: "Cours AAPL", sessionId: nil, history: [], mode: .fast, model: ResearchModel.default)))130        let s = ups.last!.state131        #expect(ups.last!.phase == .finished)132        #expect(s.jobId == "job-1" && s.sessionId == "s-1" && s.answer == "Apple cote 352,45 $." && s.finished)133        #expect(s.tools == [ToolActivity(id: "t1", tool: "get_stock_quote", input: ["symbol": "AAPL"], state: .done, durationMs: 120)])134        #expect(s.figures.first?.figureUrls == ["/figures/fig-1-0.png"] && s.sources.count == 1 && s.followups == ["Et Microsoft ?"])135        #expect(s.plan == [PlanStep(title: "Données", status: "in_progress")])136        #expect(s.unknownEvents == ["something_new": 1])   // kept and counted, not silently dropped137        #expect(s.nextEventIndex == Self.run.count)138        let body = try? JSONValue.parse(t.requests[0].body ?? Data())139        #expect(body?["mode"] == "fast" && body?["query"] == "Cours AAPL" && body?["sessionId"] == nil)140    }141142    @Test func droppedStreamResumesFromLastEventWithoutDuplicates() async {143        let t = MockTransport(); let (api, _) = makeAPI(t)144        // the first connection is cut after 7 events; the server replays from ?from=7 (and, as it does, repeats the opening frame)145        t.on("POST", "/api/chat", .sse([Self.opening] + events(Array(Self.run.prefix(7))), cut: true))146        t.on("GET", "/api/generations/job-1/stream", .fail(.networkConnectionLost), .sse([Self.opening] + events(from: 5, Array(Self.run.dropFirst(5)))))147        var runner = GenerationRunner(api: api); runner.backoff = { _ in .milliseconds(1) }148        let ups = await collect(runner.start(ChatRequest(query: "Cours AAPL", sessionId: nil, history: [], mode: .fast, model: ResearchModel.default)))149        let s = ups.last!.state150        #expect(ups.last!.phase == .finished && s.answer == "Apple cote 352,45 $." && s.figures.count == 1 && s.tools.count == 1)151        let watch = t.requests.filter { $0.path == "/api/generations/job-1/stream" }152        #expect(watch.count == 2 && watch.allSatisfy { $0.query == "from=7" })153        #expect(t.requests.filter { $0.path == "/api/chat" }.count == 1)   // the analysis is never started twice154        #expect(ups.contains { $0.phase == .reconnecting(attempt: 1) })155    }156157    @Test func refusedStartIsReportedWithTheServersWords() async {158        let t = MockTransport(); let (api, _) = makeAPI(t)159        t.on("POST", "/api/chat", .json(429, #"{"error":"4 analyses tournent déjà. Attendez la fin de l'une d'elles ou arrêtez-en une."}"#))160        let ups = await collect(GenerationRunner(api: api).start(ChatRequest(query: "q", sessionId: nil, history: [], mode: .fast, model: "m")))161        #expect(ups.last!.phase == .lost("4 analyses tournent déjà. Attendez la fin de l'une d'elles ou arrêtez-en une."))162        #expect(t.requests.count == 1)163    }164165    @Test func viewerGivesUpAfterBoundedAttemptsAndSaysTheJobContinues() async {166        let t = MockTransport(); let (api, _) = makeAPI(t)167        t.on("POST", "/api/chat", .sse([Self.opening] + events(Array(Self.run.prefix(3))), cut: true))168        t.on("GET", "/api/generations/job-1/stream", .fail(.notConnectedToInternet))169        var runner = GenerationRunner(api: api); runner.backoff = { _ in .milliseconds(1) }; runner.maxAttempts = 3170        let ups = await collect(runner.start(ChatRequest(query: "q", sessionId: nil, history: [], mode: .fast, model: "m")))171        if case .lost(let m) = ups.last!.phase { #expect(m.contains("continues on the server")) } else { Issue.record("expected .lost") }172        #expect(t.requests.filter { $0.path.hasSuffix("/stream") }.count == 3 && !ups.last!.state.finished)173    }174175    @Test func jobGoneFromServerMemory() async {176        let t = MockTransport(); let (api, _) = makeAPI(t)177        t.on("GET", "/api/generations/old/stream", .json(404, #"{"error":"Cette analyse n'est plus en mémoire."}"#))178        let ups = await collect(GenerationRunner(api: api).attach(jobId: "old"))179        if case .lost(let m) = ups.last!.phase { #expect(m.contains("no longer in the server's memory")) } else { Issue.record("expected .lost") }180    }181182    // MARK: Cache183184    func cache() throws -> DiskCache { try DiskCache(directory: FileManager.default.temporaryDirectory.appendingPathComponent("vq-cache-\(UUID().uuidString)"), key: SymmetricKey(size: .bits256)) }185186    @Test func cacheServesLastValueOnlyWhenOfflineAndMarksIt() async throws {187        let c = try cache()188        let t = MockTransport(); let (api, _) = makeAPI(t)189        t.on("GET", "/api/fmp/indices", .json(200, String(data: try fixture("indices"), encoding: .utf8)!), .fail(.notConnectedToInternet), .json(500, #"{"error":"Failed to fetch indices"}"#))190        let live = try await c.fetch("indices") { try await api.indices() }191        #expect(!live.fromCache && live.value.count == 12)192        let off = try await c.fetch("indices") { try await api.indices() }193        #expect(off.fromCache && off.value == live.value && off.fetchedAt == live.fetchedAt)194        // a server refusal is an error, not a reason to show old data as if nothing happened195        await #expect { try await c.fetch("indices") { try await api.indices() } } throws: { ($0 as? APIError)?.kind == .server }196        // nothing cached for this key: offline is reported197        await #expect { try await c.fetch("other") { throw APIError(.network, "offline") } as Cached<[Quote]> } throws: { ($0 as? APIError)?.isOffline == true }198    }199200    @Test func cacheIsEncryptedAtRestAndBoundToItsKey() throws {201        let c = try cache()202        c.write(["secret": "position AAPL 1000"], for: "workspace")203        let files = try FileManager.default.contentsOfDirectory(at: c.directory, includingPropertiesForKeys: nil)204        #expect(files.count == 1)205        let raw = try Data(contentsOf: files[0])206        #expect(raw.range(of: Data("AAPL".utf8)) == nil && raw.range(of: Data("secret".utf8)) == nil)207        #expect(c.read([String: String].self, for: "workspace")?.value["secret"] == "position AAPL 1000")208        // another key (another Mac, another account) cannot read it; a file moved to another cache key is rejected too209        let other = try DiskCache(directory: c.directory, key: SymmetricKey(size: .bits256))210        #expect(other.read([String: String].self, for: "workspace") == nil)211        #expect(c.usage().files == 1)212        c.removeAll()213        #expect(c.read([String: String].self, for: "workspace") == nil && c.usage().files == 0)214    }215216    @Test func localStoresRoundTrip() throws {217        let url = FileManager.default.temporaryDirectory.appendingPathComponent("vq-\(UUID().uuidString)/watchlists.json")218        let f = JSONFile<[Watchlist]>(url: url)219        #expect(f.load() == nil)220        let lists = [Watchlist(name: "Tech", symbols: ["AAPL", "MSFT"])]221        try f.save(lists)222        #expect(f.load() == lists)223        let ws = SavedWorkspace(name: "Desk", windows: [.init(section: "markets", symbol: nil, frame: [0, 0, 1200, 800], kind: "main"), .init(section: "chart", symbol: "AAPL", frame: nil, kind: "chart")])224        let g = JSONFile<[SavedWorkspace]>(url: url.deletingLastPathComponent().appendingPathComponent("ws.json"))225        try g.save([ws])226        #expect(g.load()?.first?.windows == ws.windows)227    }228}229
Files main
Menu

Theme