Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Tests/AstraAgentsTests/SchemaAndRegistryTests.swift10// Module : AstraAgentsTests11// 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 Testing90import AstraCore91@testable import AstraAgents9293@Suite struct SchemaValidatorTests {94 static let schema: JSONValue = [95 "type": "object",96 "properties": [97 "name": ["type": "string", "minLength": 1, "maxLength": 5],98 "mode": ["type": "string", "enum": ["fast", "slow"]],99 "count": ["type": "integer", "minimum": 1, "maximum": 10],100 "ratio": ["type": "number", "exclusiveMinimum": 0, "exclusiveMaximum": 1],101 "flag": ["type": "boolean"],102 "tags": ["type": "array", "minItems": 1, "maxItems": 3, "items": ["type": "string", "maxLength": 3]],103 "nested": ["type": "object", "properties": ["x": ["type": "number"]], "required": ["x"], "additionalProperties": false],104 "either": ["type": ["string", "null"]],105 ],106 "required": ["name"],107 "additionalProperties": false,108 ]109110 func issues(_ value: JSONValue) -> [String] { SchemaValidator.issues(value, schema: Self.schema) }111112 @Test func acceptsConformingValues() {113 #expect(issues(["name": "abc"]).isEmpty)114 #expect(issues(["name": "abcde", "mode": "fast", "count": 10, "ratio": 0.5, "flag": false, "tags": ["a", "bcd"], "nested": ["x": 1.5], "either": nil]).isEmpty)115 #expect(issues(["name": "a", "count": 1.0, "either": "s"]).isEmpty) // 1.0 is an integer in JSON116 }117118 @Test func types() {119 #expect(issues(["name": 5]) == ["“name” must be of type string, got integer."])120 #expect(issues(["name": "a", "count": "5"]) == ["“count” must be of type integer, got string."]) // no coercion121 #expect(issues(["name": "a", "count": 2.5]) == ["“count” must be of type integer, got number."])122 #expect(issues(["name": "a", "flag": "true"]) == ["“flag” must be of type boolean, got string."])123 #expect(issues(["name": "a", "flag": 1]) == ["“flag” must be of type boolean, got integer."])124 #expect(issues(["name": "a", "tags": "x"]) == ["“tags” must be of type array, got string."])125 #expect(issues(["name": "a", "nested": [1]]) == ["“nested” must be of type object, got array."])126 #expect(issues(["name": nil]) == ["“name” must be of type string, got null."])127 #expect(issues(["name": "a", "either": 3]) == ["“either” must be of type string or null, got integer."])128 #expect(issues(["name": "a", "ratio": .number(.infinity)]).count == 1)129 }130131 @Test func requiredAndAdditional() {132 #expect(issues([:]) == ["name: this required property is missing."])133 #expect(issues(["name": "a", "extra": 1]) == ["“extra” is not a known property (allowed: count, either, flag, mode, name, nested, ratio, tags)."])134 #expect(issues(["name": "a", "nested": [:]]) == ["nested.x: this required property is missing."])135 #expect(issues(["name": "a", "nested": ["x": 1, "y": 2]]) == ["“nested.y” is not a known property (allowed: x)."])136 }137138 @Test func enumsRangesLengths() {139 #expect(issues(["name": "a", "mode": "medium"]) == ["“mode” must be one of \"fast\", \"slow\", got \"medium\"."])140 #expect(issues(["name": "a", "count": 0]) == ["“count” must be at least 1, got 0."])141 #expect(issues(["name": "a", "count": 11]) == ["“count” must be at most 10, got 11."])142 #expect(issues(["name": "a", "ratio": 0]) == ["“ratio” must be greater than 0, got 0."])143 #expect(issues(["name": "a", "ratio": 1]) == ["“ratio” must be less than 1, got 1."])144 #expect(issues(["name": ""]) == ["“name” must not be empty."])145 #expect(issues(["name": "abcdef"]) == ["“name” must have at most 5 characters, got 6."])146 #expect(issues(["name": "a", "tags": []]) == ["“tags” must have at least 1 item(s), got 0."])147 #expect(issues(["name": "a", "tags": ["a", "b", "c", "d"]]) == ["“tags” must have at most 3 item(s), got 4."])148 #expect(issues(["name": "a", "tags": ["ok", "toolong", 3]]) == ["“tags[1]” must have at most 3 characters, got 7.", "“tags[2]” must be of type string, got integer."])149 }150151 @Test func reportsSeveralProblemsAtOnceInAReadableError() {152 let tool = ToolDefinition(name: "demo", summary: "", parameters: Self.schema)153 do { try SchemaValidator.validate(arguments: ["mode": "x", "count": 99, "bogus": true], for: tool); Issue.record("should fail") }154 catch {155 let message = (error as? ToolError)?.message ?? ""156 #expect(message.hasPrefix("Invalid arguments for demo:\n"))157 #expect(message.contains("- name: this required property is missing."))158 #expect(message.contains("- “bogus” is not a known property"))159 #expect(message.contains("- “count” must be at most 10, got 99."))160 #expect(message.contains("- “mode” must be one of"))161 #expect(message.hasSuffix("Correct the arguments and call the tool again."))162 }163 }164165 @Test func argumentsMustBeAnObject() {166 let tool = ToolDefinition(name: "demo", summary: "", parameters: Self.schema)167 for bad: JSONValue in [nil, "{\"name\": \"a\"}", [1, 2], 5, true] {168 #expect(throws: ToolError.self) { try SchemaValidator.validate(arguments: bad, for: tool) }169 }170 #expect(throws: Never.self) { try SchemaValidator.validate(arguments: ["name": "ok"], for: tool) }171 }172173 @Test func issueListIsBounded() {174 let many = JSONValue.object(Dictionary(uniqueKeysWithValues: (0..<200).map { ("k\($0)", JSONValue.number(1)) }))175 let tool = ToolDefinition(name: "demo", summary: "", parameters: Self.schema)176 do { try SchemaValidator.validate(arguments: many, for: tool); Issue.record("should fail") }177 catch { #expect(((error as? ToolError)?.message.count ?? .max) < 2_500) }178 }179180 @Test func everyBuiltInSchemaRejectsUnknownProperties() {181 let registry = ToolRegistry.standard(documents: FakeDocuments(hits: []), files: FakeFiles(), web: FakeWeb(html: ""))182 #expect(registry.names == ["calculator", "datetime", "data_transform", "text_stats", "regex_extract", "unit_convert", "document_search",183 "read_file", "write_file", "web_fetch"])184 for name in registry.names + [PlanTool.name] {185 let definition = name == PlanTool.name ? PlanTool.definition : registry.tool(named: name)!.definition186 #expect(definition.parameters["additionalProperties"] == false, "\(name)")187 #expect(definition.parameters["type"] == "object", "\(name)")188 #expect(!SchemaValidator.issues(["__proto__": 1], schema: definition.parameters).isEmpty, "\(name)")189 }190 }191}192193@Suite struct ToolRegistryTests {194 @Test func standardSetInstallsOnlyWhatHasAService() {195 let bare = ToolRegistry.standard()196 #expect(bare.names == ["calculator", "datetime", "data_transform", "text_stats", "regex_extract", "unit_convert"])197 #expect(bare.tool(named: "web_fetch") == nil && bare.tool(named: "read_file") == nil && bare.tool(named: "document_search") == nil)198 let withFiles = ToolRegistry.standard(files: FakeFiles())199 #expect(withFiles.tool(named: "read_file")?.risk == .readsUserData)200 #expect(withFiles.tool(named: "write_file")?.risk == .writesUserData)201 let full = ToolRegistry.standard(documents: FakeDocuments(hits: []), files: FakeFiles(), web: FakeWeb(html: ""))202 #expect(full.tool(named: "web_fetch")?.risk == .sendsDataExternally)203 #expect(full.tool(named: "document_search")?.risk == .readsUserData)204 for safe in ["calculator", "datetime", "data_transform", "text_stats", "regex_extract", "unit_convert"] { #expect(full.tool(named: safe)?.risk == .safe) }205 }206207 @Test func thereIsNoCodeExecutionTool() {208 let full = ToolRegistry.standard(documents: FakeDocuments(hits: []), files: FakeFiles(), web: FakeWeb(html: ""))209 for name in full.names {210 for word in ["shell", "exec", "python", "script", "eval", "code", "bash", "run_"] { #expect(!name.contains(word), "\(name)") }211 }212 #expect(CodeExecutionPolicy.explanation.contains("calculator") && CodeExecutionPolicy.explanation.contains("data_transform"))213 }214215 @Test func allowlistDecidesWhatIsOfferedAndResolved() throws {216 let registry = ToolRegistry([SpyTool("alpha"), SpyTool("beta"), SpyTool("gamma")])217 #expect(registry.definitions(allowed: ["gamma", "alpha", "missing", "alpha"]).map(\.name) == ["gamma", "alpha"])218 #expect(registry.definitions(allowed: []).isEmpty)219 #expect(try registry.resolve("alpha", allowed: ["alpha"]).name == "alpha")220 do { _ = try registry.resolve("beta", allowed: ["alpha"]); Issue.record("beta is not allowed") }221 catch { #expect((error as? ToolError)?.message == "The tool “beta” is not available to this agent. Available tools: alpha.") }222 do { _ = try registry.resolve("nope", allowed: ["alpha"]); Issue.record("nope does not exist") }223 catch { #expect((error as? ToolError)?.message == "The tool “nope” is not available to this agent. Available tools: alpha.") } // indistinguishable224 #expect(throws: ToolError.self) { try registry.resolve("ghost", allowed: ["ghost"]) } // allowed but not installed225 #expect(throws: ToolError.self) { try registry.resolve("alpha", allowed: []) }226 }227228 @Test func outputLimiter() {229 let short = OutputLimiter.cap("hello", maxCharacters: 100)230 #expect(short.text == "hello" && !short.truncated)231 let long = OutputLimiter.cap(String(repeating: "x", count: 5_000), maxCharacters: 1_000)232 #expect(long.truncated)233 #expect(long.text.hasPrefix(String(repeating: "x", count: 1_000) + "\n"))234 #expect(long.text.hasSuffix("[… output truncated: 1000 of 5000 characters shown]"))235 #expect(long.text.count < 1_100)236 let output = OutputLimiter.cap(ToolOutput(String(repeating: "é", count: 300), isError: true), maxCharacters: 256)237 #expect(output.truncated && output.isError)238 }239}240