SPB Git

Repository Public MIT

CoreTests.swift 19.8 KB 385 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Tests/AstraCoreTests/CoreTests.swift10// Module           : AstraCoreTests11// 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 Foundation89import Testing90@testable import AstraCore9192@Suite struct DomainTests {93    @Test func modelRefRoundTrip() {94        for ref in [ModelRef(provider: .openRouter, modelID: "meta/llama:free"), ModelRef(provider: .custom, modelID: "a|b", endpointID: "E1"),95                    ModelRef(provider: .mlx, modelID: "qwen3-1.7b-4bit")] {96            #expect(ModelRef(id: ref.id) == ref)97        }98        #expect(ModelRef(id: "nope") == nil)99        #expect(ModelRef(id: "openAI||") == nil)100    }101102    @Test func jsonRoundTripKeepsIntegersExact() throws {103        let v: JSONValue = ["a": 1, "b": [true, nil, 2.5, "x"], "c": ["d": 9007199254740000]]104        let text = v.serialized()105        #expect(text == #"{"a":1,"b":[true,null,2.5,"x"],"c":{"d":9007199254740000}}"#)106        #expect(JSONValue.parse(text) == v)107        #expect(JSONValue.parse("{not json") == nil)108        #expect(v["b"]?[3]?.stringValue == "x")109        #expect(v["a"]?.intValue == 1)110        #expect(JSONValue.number(1e300).intValue == nil)111        #expect(JSONValue.number(-.infinity).intValue == nil)112    }113114    @Test func usageMergeIsFieldwise() {115        let a = TokenUsage(inputTokens: 10, cachedInputTokens: 4)116        let b = TokenUsage(outputTokens: 7, reportedCostUSD: 0.001)117        let m = a.merged(with: b)118        #expect(m.inputTokens == 10 && m.outputTokens == 7 && m.cachedInputTokens == 4 && m.reportedCostUSD == 0.001)119        #expect(m.totalTokens == 17)120        #expect(TokenUsage().totalTokens == nil)121    }122123    @Test func cloudKeyedListsEveryKeyedProvider() {124        #expect(ProviderKind.cloudKeyed == [.openAI, .anthropic, .google, .xAI, .openRouter])125        #expect(ProviderKind.allCases.filter(\.isLocal) == [.mlx, .appleFoundation])126    }127}128129@Suite struct SSETests {130    @Test func parsesEventsCommentsAndMultilineData() {131        var p = SSEParser()132        var out: [SSEEvent] = []133        for line in [": OPENROUTER PROCESSING", "", "event: message_start", "data: {\"a\":1}", "", "data: one", "data: two", "", "data:no-space", "", "id: 7", "retry: 10", ""] {134            if let e = p.feed(line) { out.append(e) }135        }136        #expect(out == [SSEEvent(event: "message_start", data: "{\"a\":1}"), SSEEvent(data: "one\ntwo"), SSEEvent(data: "no-space")])137        #expect(p.comments == 1)138    }139140    @Test func finishFlushesAnUnterminatedEvent() {141        var p = SSEParser()142        #expect(p.feed("data: tail") == nil)143        #expect(p.finish() == SSEEvent(data: "tail"))144        #expect(p.finish() == nil)145    }146147    @Test func eventNameDoesNotLeakIntoTheNextEvent() {148        var p = SSEParser()149        _ = p.feed("event: a"); _ = p.feed("data: 1")150        #expect(p.feed("")?.event == "a")151        _ = p.feed("data: 2")152        #expect(p.feed("")?.event == nil)153    }154}155156@Suite struct ErrorTests {157    @Test func httpStatusMapping() {158        #expect(mapHTTPError(status: 401, code: nil, message: "bad key", retryAfter: nil, model: nil) == .unauthorized("bad key"))159        #expect(mapHTTPError(status: 402, code: nil, message: nil, retryAfter: nil, model: nil) == .quotaExceeded(nil))160        #expect(mapHTTPError(status: 429, code: nil, message: "slow down", retryAfter: 12, model: nil) == .rateLimited(retryAfter: 12, message: "slow down"))161        #expect(mapHTTPError(status: 429, code: "insufficient_quota", message: "You exceeded your current quota", retryAfter: nil, model: nil) == .quotaExceeded("You exceeded your current quota"))162        #expect(mapHTTPError(status: 400, code: "context_length_exceeded", message: "too big", retryAfter: nil, model: nil) == .contextTooLong("too big"))163        #expect(mapHTTPError(status: 404, code: "model_not_found", message: "The model x does not exist", retryAfter: nil, model: "x") == .modelNotFound("x"))164        #expect(mapHTTPError(status: 529, code: nil, message: nil, retryAfter: nil, model: nil) == .overloaded(nil))165        #expect(mapHTTPError(status: 500, code: nil, message: nil, retryAfter: nil, model: nil) == .server(status: 500, message: nil))166        #expect(mapHTTPError(status: 403, code: nil, message: "input flagged by moderation", retryAfter: nil, model: nil) == .contentFiltered("input flagged by moderation"))167    }168169    @Test func transientClassification() {170        #expect(AstraError.rateLimited(retryAfter: nil, message: nil).isTransient)171        #expect(AstraError.server(status: 503, message: nil).isTransient)172        #expect(!AstraError.server(status: 400, message: nil).isTransient)173        #expect(!AstraError.unauthorized(nil).isTransient)174        #expect(!AstraError.cancelled.isTransient)175    }176177    @Test func wrapMapsURLErrors() {178        #expect(AstraError.wrap(URLError(.cancelled)) == .cancelled)179        #expect(AstraError.wrap(URLError(.timedOut)) == .timeout)180        #expect(AstraError.wrap(CancellationError()) == .cancelled)181        #expect(AstraError.wrap(AstraError.timeout) == .timeout)182        if case .network = AstraError.wrap(URLError(.notConnectedToInternet)) {} else { Issue.record("offline should be a network error") }183    }184185    @Test func errorMessagesNeverCarryASecret() {186        let secret = "sk-proj-AbCdEfGhIjKlMnOpQrStUvWxYz0123456789"187        let e = mapHTTPError(status: 401, code: nil, message: "Incorrect API key provided: \(secret).", retryAfter: nil, model: nil)188        #expect(!e.message.contains(secret))189        #expect(e.message.contains("sk-proj-•••"))190    }191192    @Test(arguments: [193        "sk-ant-api03-ABCDEFGHIJKLMNOPQRSTUVWX", "sk-or-v1-0123456789abcdef0123456789abcdef", "sk-proj-ABCDEFGHIJKLMNOP1234",194        "sk-ABCDEFGHIJKLMNOPQRSTUVWX", "hf_ABCDEFGHIJKLMNOPQRSTUV", "xai-ABCDEFGHIJKLMNOPQRSTUVWX", "AIzaSyA1234567890abcdefghijklmnopqrstu",195    ])196    func redactorRemovesKnownKeyShapes(key: String) {197        for text in ["key is \(key) ok", "Authorization: Bearer \(key)", "{\"x-api-key\": \"\(key)\"}", "https://h/v1?key=\(key)&a=1", "api_key=\(key)", "token=\(key)", "session_token: \(key)"] {198            #expect(!Redactor.redact(text).contains(key), "leaked in: \(text)")199        }200    }201202    @Test func redactorLeavesOrdinaryTextAlone() {203        let text = "The sky is blue. Task-based design costs $5. See https://example.com/a?b=c."204        #expect(Redactor.redact(text) == text)205    }206207    @Test func headerRedactionAndMasking() {208        let h = Redactor.redact(headers: ["Authorization": "Bearer abcdefghijkl", "X-Api-Key": "zzz", "Content-Type": "application/json"])209        #expect(h["Authorization"] == "•••" && h["X-Api-Key"] == "•••" && h["Content-Type"] == "application/json")210        let masked = Redactor.mask(key: "sk-ant-api03-ABCDEFGHIJKLMNOP9876")211        #expect(masked.hasPrefix("sk-ant-") && masked.hasSuffix("9876") && !masked.contains("ABCDEFGH"))212        #expect(Redactor.mask(key: "short") == "•••••")213    }214}215216@Suite struct EndpointPolicyTests {217    @Test func httpsIsAccepted() throws {218        #expect(try EndpointPolicy.validate(" https://api.example.com/v1 ", allowInsecure: false).host == "api.example.com")219    }220221    @Test func plainHTTPIsRefusedByDefault() {222        #expect(throws: AstraError.self) { try EndpointPolicy.validate("http://192.168.1.20:8080/v1", allowInsecure: false) }223    }224225    @Test func plainHTTPOnlyForLocalHostsEvenWhenAllowed() throws {226        _ = try EndpointPolicy.validate("http://192.168.1.20:8080/v1", allowInsecure: true)227        _ = try EndpointPolicy.validate("http://studio.local:1234/v1", allowInsecure: true)228        _ = try EndpointPolicy.validate("http://100.80.1.2/v1", allowInsecure: true)229        #expect(throws: AstraError.self) { try EndpointPolicy.validate("http://example.com/v1", allowInsecure: true) }230        #expect(throws: AstraError.self) { try EndpointPolicy.validate("http://8.8.8.8/v1", allowInsecure: true) }231    }232233    @Test func rejectsCredentialsInURLAndOtherSchemes() {234        #expect(throws: AstraError.self) { try EndpointPolicy.validate("https://user:pass@example.com/v1", allowInsecure: false) }235        #expect(throws: AstraError.self) { try EndpointPolicy.validate("ftp://example.com", allowInsecure: true) }236        #expect(throws: AstraError.self) { try EndpointPolicy.validate("not a url", allowInsecure: true) }237        #expect(throws: AstraError.self) { try EndpointPolicy.validate("file:///etc/passwd", allowInsecure: true) }238    }239240    @Test func localHostClassification() {241        for h in ["localhost", "10.0.0.1", "127.0.0.1", "172.16.0.9", "172.31.255.1", "192.168.0.1", "169.254.1.1", "mac.local"] { #expect(EndpointPolicy.isLocalHost(h), "\(h)") }242        for h in ["172.32.0.1", "11.0.0.1", "example.com", "192.169.0.1", "100.128.0.1", "1.2.3"] { #expect(!EndpointPolicy.isLocalHost(h), "\(h)") }243    }244245    @Test func profileRefusesCredentialHeaders() throws {246        var p = EndpointProfile(name: "Lab", baseURL: "https://lab.example.com/v1", extraHeaders: ["X-Team": "astra"])247        _ = try p.validated()248        p.extraHeaders = ["Authorization": "Bearer x"]249        #expect(throws: AstraError.self) { try p.validated() }250        p.extraHeaders = ["X-Ok": "a\r\nInjected: 1"]251        #expect(throws: AstraError.self) { try p.validated() }252        p.extraHeaders = ["bad name": "a"]253        #expect(throws: AstraError.self) { try p.validated() }254    }255}256257@Suite struct BudgetTests {258    private func long(_ n: Int) -> String { String(repeating: "word ", count: n) }259260    @Test func estimateGrowsWithTextAndCountsCJK() {261        #expect(TokenEstimator.estimate("") == 0)262        #expect(TokenEstimator.estimate("hello world") >= 3)263        #expect(TokenEstimator.estimate(long(1000)) > TokenEstimator.estimate(long(100)) * 8)264        #expect(TokenEstimator.estimate("こんにちは世界") >= 4)265    }266267    @Test func everythingFitsUntouched() {268        let b = ContextBudget(window: 8000, reservedForOutput: 1000)269        let msgs: [PromptMessage] = [.user("hi"), .assistant("hello"), .user("bye")]270        let plan = b.plan(system: "be brief", messages: msgs)271        #expect(plan.fits && plan.droppedCount == 0 && plan.messages == msgs)272    }273274    @Test func dropOldestKeepsTheLastTurnAndStartsWithUser() {275        let b = ContextBudget(window: 900, reservedForOutput: 200, strategy: .dropOldest)276        let msgs: [PromptMessage] = [.user(long(150)), .assistant(long(150)), .user(long(150)), .assistant(long(150)), .user("final question")]277        let plan = b.plan(system: nil, messages: msgs)278        #expect(plan.droppedCount > 0)279        #expect(plan.messages.last?.text == "final question")280        #expect(plan.messages.first?.role == .user)281        #expect(plan.estimatedInputTokens <= plan.inputBudget)282    }283284    @Test func keepFirstAndRecentKeepsTheFirstExchange() {285        let b = ContextBudget(window: 1100, reservedForOutput: 100, strategy: .keepFirstAndRecent)286        let msgs: [PromptMessage] = [.user("TASK " + long(60)), .assistant("ack " + long(60)), .user(long(200)), .assistant(long(200)), .user(long(200)), .assistant(long(200)), .user("now")]287        let plan = b.plan(system: nil, messages: msgs)288        #expect(plan.messages.first?.text.hasPrefix("TASK") == true)289        #expect(plan.messages.last?.text == "now")290        #expect(plan.droppedCount > 0 && plan.fits)291    }292293    @Test func strictNeverDropsAndReportsNoFit() {294        let b = ContextBudget(window: 300, reservedForOutput: 100, strategy: .strict)295        let msgs: [PromptMessage] = [.user(long(400)), .assistant("a"), .user("b")]296        let plan = b.plan(system: nil, messages: msgs)297        #expect(!plan.fits && plan.droppedCount == 0 && plan.messages.count == 3)298    }299300    @Test func toolResultsStayWithTheirCall() {301        let call = ToolCall(id: "c1", name: "calculator", arguments: ["expression": "1+1"])302        let msgs: [PromptMessage] = [.user(long(300)), .assistant("", toolCalls: [call]), .tool("2", callID: "c1", name: "calculator"), .assistant(long(10)), .user("next")]303        let units = ContextBudget.units(msgs)304        #expect(units.count == 4 && units[1].count == 2)305        let plan = ContextBudget(window: 400, reservedForOutput: 100).plan(system: nil, messages: msgs)306        let hasCall = plan.messages.contains { !$0.toolCalls.isEmpty }, hasResult = plan.messages.contains { $0.role == .tool }307        #expect(hasCall == hasResult)308    }309310    @Test func aSingleOversizedTurnIsNeverDropped() {311        let plan = ContextBudget(window: 300, reservedForOutput: 100).plan(system: nil, messages: [.user(long(2000))])312        #expect(plan.messages.count == 1 && !plan.fits)313    }314}315316@Suite struct CostTests {317    let pricing = Pricing(inputPerMTok: 2, outputPerMTok: 10, cachedInputPerMTok: 0.2, cacheWritePerMTok: 2.5, asOf: "2026-11-21")318319    @Test func estimatedFromUsage() {320        let c = UsageAccounting.cost(usage: TokenUsage(inputTokens: 1_000_000, outputTokens: 100_000), pricing: pricing, provider: .openAI)321        #expect(c.basis == .estimatedFromUsage && c.pricingAsOf == "2026-11-21")322        #expect(abs((c.amountUSD ?? 0) - 3.0) < 1e-9)323        #expect(c.label.hasPrefix("≈ "))324    }325326    @Test func cachedAndWrittenTokensArePricedAsSubsets() {327        let u = TokenUsage(inputTokens: 1000, outputTokens: 0, cachedInputTokens: 600, cacheWriteTokens: 100)328        let c = UsageAccounting.cost(usage: u, pricing: pricing, provider: .anthropic)329        let expected = (300 * 2 + 600 * 0.2 + 100 * 2.5) / 1_000_000330        #expect(abs((c.amountUSD ?? 0) - expected) < 1e-12)331    }332333    @Test func providerReportedCostWins() {334        let c = UsageAccounting.cost(usage: TokenUsage(inputTokens: 5, outputTokens: 5, reportedCostUSD: 0.00042), pricing: pricing, provider: .openRouter)335        #expect(c.basis == .reportedByProvider && c.amountUSD == 0.00042 && !c.label.contains("≈"))336    }337338    @Test func missingDataIsUnavailableNeverZero() {339        #expect(UsageAccounting.cost(usage: nil, pricing: pricing, provider: .openAI) == .unavailable)340        #expect(UsageAccounting.cost(usage: TokenUsage(inputTokens: 5), pricing: nil, provider: .openAI) == .unavailable)341        #expect(UsageAccounting.cost(usage: TokenUsage(), pricing: pricing, provider: .openAI) == .unavailable)342        #expect(CostEstimate.unavailable.label == "Cost unavailable")343    }344345    @Test func localIsOnDevice() {346        #expect(UsageAccounting.cost(usage: TokenUsage(inputTokens: 5, outputTokens: 5), pricing: nil, provider: .mlx) == .onDevice)347        #expect(UsageAccounting.preflight(inputTokens: 10, maxOutputTokens: 10, pricing: nil, provider: .appleFoundation).label == "On device")348    }349350    @Test func preflightIsAnUpperBound() {351        let c = UsageAccounting.preflight(inputTokens: 1000, maxOutputTokens: 2000, pricing: pricing, provider: .openAI)352        #expect(c.basis == .estimatedFromText)353        #expect(abs((c.amountUSD ?? 0) - (1000 * 2 + 2000 * 10) / 1_000_000) < 1e-12)354    }355356    @Test func formatting() {357        #expect(CostEstimate.format(0) == "$0")358        #expect(CostEstimate.format(0.00001) == "< $0.0001")359        #expect(CostEstimate.format(0.0042) == "$0.0042")360        #expect(CostEstimate.format(0.25) == "$0.250")361        #expect(CostEstimate.format(12.5) == "$12.50")362    }363}364365@Suite struct NegotiationTests {366    @Test func dropsWhatTheModelDoesNotAccept() {367        let model = ModelInfo(ref: ModelRef(provider: .openRouter, modelID: "m"), displayName: "M", maxOutputTokens: 1000, capabilities: [.text],368                              supportedParameters: ["max_tokens"])369        let tool = ToolDefinition(name: "t", summary: "s", parameters: ["type": "object"])370        let r = ParameterNegotiator.negotiate(GenerationParameters(temperature: 0.5, topP: 0.9, maxOutputTokens: 5000, reasoning: .high, seed: 1, stop: ["x"]),371                                              tools: [tool], images: true, for: model)372        #expect(r.parameters.temperature == nil && r.parameters.topP == nil && r.parameters.seed == nil && r.parameters.stop.isEmpty)373        #expect(r.parameters.reasoning == nil && r.parameters.maxOutputTokens == 1000 && r.tools.isEmpty)374        #expect(r.notes.count == 8)375    }376377    @Test func keepsEverythingWhenSupported() {378        let model = ModelInfo(ref: ModelRef(provider: .openRouter, modelID: "m"), displayName: "M", capabilities: [.text, .tools, .reasoning, .vision],379                              supportedParameters: ["temperature", "top_p", "seed", "stop"])380        let p = GenerationParameters(temperature: 0.5, topP: 0.9, reasoning: .low, seed: 1, stop: ["x"])381        let r = ParameterNegotiator.negotiate(p, tools: [ToolDefinition(name: "t", summary: "s", parameters: [:])], images: true, for: model)382        #expect(r.parameters == p && r.tools.count == 1 && r.notes.isEmpty)383    }384}385
Files main
Menu

Theme