SPB Git

Repository Public MIT

FixtureTransport.swift 11.5 KB 244 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Tests/AstraProvidersTests/FixtureTransport.swift10// Module           : AstraProvidersTests11// Repository       : https://git.spboucher.ai/orix-astra12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import AstraCore89import Foundation90import Testing91@testable import AstraProviders9293/// Replays recorded responses and records every request it receives. Replies are served in order; the last one repeats.94final class FixtureTransport: HTTPTransport, @unchecked Sendable {95    struct Reply: Sendable {96        var status = 20097        var headers: [String: String] = [:]98        var body = ""99        /// Never finish the stream after the body: used to observe cancellation.100        var hangs = false101        /// Thrown instead of answering.102        var failure: AstraError?103104        static func sse(_ fixture: String, headers: [String: String] = ["Content-Type": "text/event-stream", "request-id": "req_fixture"]) -> Reply {105            Reply(headers: headers, body: Fixtures.text(fixture))106        }107        static func json(_ status: Int = 200, _ body: String, headers: [String: String] = [:]) -> Reply {108            Reply(status: status, headers: headers.merging(["Content-Type": "application/json"]) { a, _ in a }, body: body)109        }110        static func file(_ status: Int = 200, _ fixture: String, headers: [String: String] = [:]) -> Reply { json(status, Fixtures.text(fixture), headers: headers) }111    }112113    private let lock = NSLock()114    private var replies: [Reply]115    private var recorded: [URLRequest] = []116    private var terminated = 0117118    init(_ replies: Reply...) { self.replies = replies }119    init(replies: [Reply]) { self.replies = replies }120121    var requests: [URLRequest] { lock.withLock { recorded } }122    var cancelledStreams: Int { lock.withLock { terminated } }123124    /// JSON body of the n-th request.125    func body(_ index: Int = 0) -> JSONValue {126        let all = requests127        guard all.indices.contains(index), let data = all[index].httpBody else { return .null }128        return JSONValue.parse(data) ?? .null129    }130131    private func next(_ request: URLRequest) -> Reply {132        lock.withLock {133            recorded.append(request)134            return replies.count > 1 ? replies.removeFirst() : (replies.first ?? Reply())135        }136    }137138    private func response(_ request: URLRequest, _ reply: Reply) -> HTTPURLResponse {139        HTTPURLResponse(url: request.url!, statusCode: reply.status, httpVersion: "HTTP/1.1", headerFields: reply.headers)!140    }141142    func send(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) {143        let reply = next(request)144        if let failure = reply.failure { throw failure }145        return (Data(reply.body.utf8), response(request, reply))146    }147148    func stream(_ request: URLRequest) async throws -> (HTTPURLResponse, AsyncThrowingStream<String, Error>) {149        let reply = next(request)150        if let failure = reply.failure { throw failure }151        let (stream, continuation) = AsyncThrowingStream<String, Error>.makeStream()152        var lines = reply.body.components(separatedBy: "\n")153        if lines.last == "" { lines.removeLast() }154        for line in lines { continuation.yield(line) }155        if reply.hangs {156            continuation.onTermination = { [weak self] reason in157                if case .cancelled = reason { self?.lock.withLock { self?.terminated += 1 } }158            }159        } else {160            continuation.finish()161        }162        return (response(request, reply), stream)163    }164}165166enum Fixtures {167    static func url(_ name: String) -> URL { Bundle.module.resourceURL!.appendingPathComponent("Fixtures").appendingPathComponent(name) }168    static func text(_ name: String) -> String {169        guard let text = try? String(contentsOf: url(name), encoding: .utf8) else { fatalError("missing fixture \(name)") }170        return text171    }172}173174final class RecordingDiagnostics: DiagnosticsSink, @unchecked Sendable {175    struct Record: Sendable { var provider: ProviderKind; var modelID: String; var status: Int; var requestID: String?; var timeToHeaders: TimeInterval; var retryCount: Int }176    private let lock = NSLock()177    private var stored: [Record] = []178    var records: [Record] { lock.withLock { stored } }179    func httpResponse(provider: ProviderKind, modelID: String, status: Int, requestID: String?, timeToHeaders: TimeInterval, retryCount: Int) async {180        lock.withLock { stored.append(Record(provider: provider, modelID: modelID, status: status, requestID: requestID, timeToHeaders: timeToHeaders, retryCount: retryCount)) }181    }182}183184/// Everything a stream produced, checked against the event-order contract while it is collected.185struct Collected {186    var events: [StreamEvent] = []187    var error: AstraError?188    var outcome = GenerationOutcome()189    var contractViolation: String?190191    var text: String { outcome.text }192    var reasoning: String { outcome.reasoningSummary }193    var starts: [MessageStart] { events.compactMap { if case .messageStarted(let s) = $0 { return s }; return nil } }194    var completions: [StopReason] { events.compactMap { if case .messageCompleted(let r) = $0 { return r }; return nil } }195    var toolStarts: [String] { events.compactMap { if case .toolCallStarted(let id, _) = $0 { return id }; return nil } }196    func deltas(for id: String) -> [String] { events.compactMap { if case .toolCallDelta(let i, let f) = $0, i == id { return f }; return nil } }197}198199func collect(_ stream: AsyncThrowingStream<StreamEvent, Error>) async -> Collected {200    var out = Collected()201    var validator = StreamValidator()202    do {203        for try await event in stream {204            out.events.append(event)205            do {206                let forwarded = try validator.accept(event)207                // The validator synthesises a start when the adapter forgot one: that is a contract failure here.208                if forwarded.count != 1 { out.contractViolation = "event \(event) was not forwarded one-to-one" }209            } catch { out.contractViolation = "\(error)" }210            out.outcome.apply(event)211        }212    } catch { out.error = AstraError.wrap(error) }213    if out.error == nil, !validator.finished { out.contractViolation = out.contractViolation ?? "the stream ended without a terminal event" }214    if let first = out.events.first, case .messageStarted = first {} else if !out.events.isEmpty { out.contractViolation = "the first event was not messageStarted" }215    return out216}217218/// Asserts the success contract: one start first, one terminal completion last, nothing malformed in between.219func expectWellFormed(_ c: Collected, sourceLocation: SourceLocation = #_sourceLocation) {220    #expect(c.error == nil, "\(c.error?.title ?? ""): \(c.error?.message ?? "")", sourceLocation: sourceLocation)221    #expect(c.contractViolation == nil, "\(c.contractViolation ?? "")", sourceLocation: sourceLocation)222    #expect(c.starts.count == 1, sourceLocation: sourceLocation)223    #expect(c.completions.count == 1, sourceLocation: sourceLocation)224    if let last = c.events.last { #expect(last.isTerminal, sourceLocation: sourceLocation) }225}226227let testKey = "sk-test-FIXTUREKEY0123456789abcdefFIXTURE"228let weatherTool = ToolDefinition(name: "get_weather", summary: "Get the current weather for a city.",229                                 parameters: ["type": "object", "properties": ["city": ["type": "string"]], "required": ["city"], "additionalProperties": false])230231func request(_ provider: ProviderKind, _ model: String, _ text: String = "Reply with exactly: ASTRA-OK", system: String? = nil, parameters: GenerationParameters = .default,232             tools: [ToolDefinition] = [], endpointID: String? = nil) -> GenerationRequest {233    GenerationRequest(model: ModelRef(provider: provider, modelID: model, endpointID: endpointID), system: system, messages: [.user(text)], parameters: parameters, tools: tools)234}235236/// A user turn, an assistant turn that called a tool, and the tool's result: the shape every adapter must replay.237func toolCycle(callID: String = "call_abc123") -> [PromptMessage] {238    [.user("What is the weather in Paris?"),239     .assistant("Let me check.", toolCalls: [ToolCall(id: callID, name: "get_weather", arguments: ["city": "Paris"])]),240     .tool("18 C, cloudy", callID: callID, name: "get_weather")]241}242243let tinyPNG = ImageInput(data: Data([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]), mimeType: "image/png")244
Files main
Menu

Theme