Repository Public License
1// ORIX OS Automation2// File: Packages/ORIXCore/Tests/ORIXCoreTests/CoreTests.swift3// Description: Unit tests for JSON values, schema validation, capability coverage, path resolution, redaction, manifests and run-state transitions.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import ORIXCore10import ORIXTestingSupport1112@Suite struct JSONValueTests {13 @Test func roundTripKeepsIntegersAndDoubles() throws {14 let v = try JSONValue.parse(#"{"a":1,"b":1.5,"c":[true,null,"x"],"d":{"e":-3}}"#)15 #expect(v["a"] == .int(1))16 #expect(v["b"] == .number(1.5))17 #expect(v.value(atPath: "c.2") == .string("x"))18 #expect(v.value(atPath: "d.e")?.intValue == -3)19 #expect(try JSONValue.parse(v.serialized()) == v)20 }2122 @Test func truthiness() {23 #expect(!JSONValue.null.isTruthy)24 #expect(!JSONValue.string("").isTruthy)25 #expect(!JSONValue.array([]).isTruthy)26 #expect(JSONValue.int(2).isTruthy)27 }28}2930@Suite struct SchemaTests {31 let schema = JSONSchema.object([32 "path": .string("A path"),33 "limit": .integer(minimum: 1, maximum: 100),34 "mode": .string(enum: ["a", "b"]),35 "tags": .array(of: .string()),36 ], required: ["path"])3738 @Test func acceptsValidInput() {39 #expect(schema.validate(["path": "/tmp", "limit": 5, "mode": "a", "tags": ["x"]]).isEmpty)40 }4142 @Test func reportsEveryViolation() {43 let v = schema.validate(["limit": 500, "mode": "z", "tags": [1], "extra": true])44 let text = v.map(\.description).joined(separator: "|")45 #expect(text.contains("path: required"))46 #expect(text.contains("limit: greater than maximum"))47 #expect(text.contains("mode: value is not one of"))48 #expect(text.contains("tags.0: expected string"))49 #expect(text.contains("extra: unexpected property"))50 }5152 @Test func rejectsWrongRootType() {53 #expect(!schema.validate("not an object").isEmpty)54 }55}5657@Suite struct CapabilityTests {58 @Test func pathCoverageIsPrefixOnWholeComponents() {59 let g = Capability.fileRead(path: "/Users/a/Documents")60 #expect(g.covers(.fileRead(path: "/Users/a/Documents")))61 #expect(g.covers(.fileRead(path: "/Users/a/Documents/x/y.txt")))62 #expect(!g.covers(.fileRead(path: "/Users/a/Documents-private/x")))63 #expect(!g.covers(.fileRead(path: "/Users/a")))64 }6566 @Test func kindsNeverImplyEachOther() {67 #expect(!Capability.fileWrite(path: "/a").covers(.fileRead(path: "/a")))68 #expect(!Capability.fileWrite(path: "/a").covers(.fileDelete(path: "/a/b")))69 #expect(!Capability.network(host: "example.com").covers(.browser(host: "example.com")))70 }7172 @Test func hostWildcards() {73 #expect(Capability.network(host: "*.example.com").covers(.network(host: "api.example.com")))74 #expect(!Capability.network(host: "*.example.com").covers(.network(host: "example.com")))75 #expect(!Capability.network(host: "*.example.com").covers(.network(host: "evilexample.com")))76 #expect(!Capability.network(host: "example.com").covers(.network(host: "example.com.evil.net")))77 }7879 @Test func kindScopeRoundTrip() {80 let caps: [Capability] = [.fileRead(path: "/a"), .processExec(executable: "git"), .clipboardRead, .secret(reference: "x"), .skill(id: "s")]81 for c in caps { #expect(Capability(kind: c.kind, scope: c.scope) == c) }82 }83}8485@Suite struct PathResolverTests {86 @Test func removesDotDotAndExpandsTilde() throws {87 let base = URL(fileURLWithPath: "/tmp")88 #expect(try PathResolver.canonical("~/x/../y", relativeTo: base) == PathResolver.resolveSymlinks(NSHomeDirectory()) + "/y")89 #expect(throws: ToolError.self) { try PathResolver.canonical("~root/x", relativeTo: base) }90 #expect(throws: ToolError.self) { try PathResolver.canonical("", relativeTo: base) }91 }9293 @Test func symlinkEscapeResolvesToRealTarget() throws {94 let tmp = try TempDirectory(); defer { tmp.remove() }95 let outside = try TempDirectory(prefix: "orix-outside"); defer { outside.remove() }96 try outside.write("secret.txt", "s")97 try FileManager.default.createSymbolicLink(at: tmp.file("link"), withDestinationURL: outside.url)98 let resolved = try PathResolver.canonical("link/secret.txt", relativeTo: tmp.url)99 #expect(resolved == outside.url.path + "/secret.txt")100 #expect(!Capability.fileRead(path: tmp.url.path).covers(.fileRead(path: resolved)))101 // A not-yet-existing file below a symlinked directory still resolves through the link.102 let future = try PathResolver.canonical("link/new/file.txt", relativeTo: tmp.url)103 #expect(future == outside.url.path + "/new/file.txt")104 }105106 @Test func protectedLocations() {107 #expect(PathResolver.isProtected(NSHomeDirectory() + "/.ssh/id_ed25519"))108 #expect(PathResolver.isProtected(NSHomeDirectory() + "/Library/Keychains/login.keychain-db"))109 #expect(!PathResolver.isProtected(NSHomeDirectory() + "/Documents/a.txt"))110 }111}112113@Suite struct RedactionTests {114 @Test func scrubsCredentialShapes() {115 let r = Redactor()116 let fakeAnthropic = "sk-ant-" + String(repeating: "a1B2", count: 10)117 let fakeOpenRouter = "sk-or-v1-" + String(repeating: "0f", count: 20)118 let text = "key=\(fakeAnthropic) other \(fakeOpenRouter) Authorization: Bearer abcdefghijklmnopqrstuvwxyz012345"119 let out = r.redact(text)120 #expect(!out.contains(fakeAnthropic))121 #expect(!out.contains(fakeOpenRouter))122 #expect(!out.contains("abcdefghijklmnopqrstuvwxyz012345"))123 #expect(out.contains(Redactor.placeholder))124 }125126 @Test func scrubsRegisteredLiteralsAndSensitiveKeys() {127 let r = Redactor()128 r.register(secret: "correct-horse-battery")129 #expect(r.redact("the value is correct-horse-battery!") == "the value is \(Redactor.placeholder)!")130 let v = r.redact(["password": "hunter2hunter2", "note": "ok", "body": "x"], keys: ["body"])131 #expect(v["password"] == .string(Redactor.placeholder))132 #expect(v["body"] == .string(Redactor.placeholder))133 #expect(v["note"] == "ok")134 }135136 @Test func leavesOrdinaryTextAlone() {137 #expect(Redactor().redact("Moved 14 files to ~/Downloads/Sorted in 0.3 s") == "Moved 14 files to ~/Downloads/Sorted in 0.3 s")138 }139140 @Test func truncationIsHonest() {141 let (t, cut) = String(repeating: "é", count: 100).truncated(maxBytes: 51)142 #expect(cut)143 #expect(t.contains("output truncated"))144 #expect(!"short".truncated(maxBytes: 51).truncated)145 }146}147148@Suite struct ManifestAndStateTests {149 @Test func manifestProblems() {150 let good = ToolManifest(identifier: "filesystem.list", name: "List", summary: "Lists the entries of a directory.", family: "filesystem",151 inputSchema: .object([:]), sideEffect: .read, defaultTier: .readOnly, idempotent: true)152 #expect(good.problems().isEmpty)153 var bad = good154 bad.identifier = "List Files"; bad.sideEffect = .destructive; bad.defaultTier = .readOnly; bad.idempotent = false155 #expect(bad.problems().count >= 2)156 }157158 @Test func terminalStatesAreFinal() {159 for s in RunStatus.allCases where s.isTerminal && s != .recoveryRequired {160 for n in RunStatus.allCases { #expect(!s.canTransition(to: n)) }161 }162 #expect(RunStatus.running.canTransition(to: .waitingApproval))163 #expect(RunStatus.waitingApproval.canTransition(to: .running))164 #expect(!RunStatus.running.canTransition(to: .queued))165 }166167 @Test func costIsNeverInvented() {168 let u = TokenUsage(inputTokens: 1_000_000, outputTokens: 1_000_000)169 #expect(u.costUSD(model: ModelInfo(id: "m", provider: .openai)) == nil)170 #expect(u.costUSD(model: ModelInfo(id: "m", provider: .openai, inputPricePerMTok: 3, outputPricePerMTok: 15)) == 18)171 #expect(TokenUsage(reportedCostUSD: 0.002).costUSD(model: nil) == 0.002)172 }173174 @Test func scriptedProviderAggregates() async throws {175 let p = ScriptedProvider(turns: [ScriptedProvider.tool("filesystem.list", ["path": "/tmp"], id: "t1", text: "Looking")])176 let r = try await p.complete(LLMRequest(model: "scripted-1", messages: [.user("hi")]))177 #expect(r.text == "Looking")178 #expect(r.toolUses.first?.name == "filesystem__list")179 #expect(r.stopReason == .toolUse)180 }181}182