Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX OS Automation9// File : Packages/ORIXAppleAutomation/Tests/ORIXAppleAutomationTests/Helpers.swift10// Description : Test helpers: an executor-like invocation (schema11// validation, requirements, execute, error conversion), skip12// reporting and image rendering.13// Module : ORIXAppleAutomationTests14// Repository : https://git.spboucher.ai/orix-automation15// Created : 2026-11-2116// Last Modified : 2026-11-2117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : Proprietary, all rights reserved (see license file)83// License File : LICENSE84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import CoreGraphics92import CoreText93import Foundation94import ImageIO95import ORIXCore96import ORIXTestingSupport97import Testing98@testable import ORIXAppleAutomation99100struct Outcome {101 var result: ToolResult102 var error: ToolError?103 var requirements: ToolRequirements?104}105106/// Mirrors the first steps of the real ToolExecutor: input schema, requirements, execute, ToolError conversion, output schema.107func invoke(_ tool: any ORIXTool, _ arguments: JSONValue, in dir: TempDirectory, dryRun: Bool = false, timeout: Double = 30) async -> Outcome {108 let violations = tool.manifest.inputSchema.validate(arguments)109 if !violations.isEmpty {110 let e = ToolError.invalidInput(violations.map(\.description).joined(separator: "; "))111 return Outcome(result: .failure(e.status, e.failure), error: e)112 }113 var context = dir.toolContext()114 context.timeoutSeconds = timeout115 let input = ToolInput(arguments: arguments, dryRun: dryRun)116 var requirements: ToolRequirements?117 do {118 requirements = try tool.requirements(for: input, context: context)119 let result = try await tool.execute(input: input, context: context)120 if result.status == .succeeded {121 let bad = tool.manifest.outputSchema.validate(result.output)122 #expect(bad.isEmpty, "output of \(tool.identifier) violates its schema: \(bad)")123 }124 return Outcome(result: result, requirements: requirements)125 } catch let e as ToolError {126 return Outcome(result: .failure(e.status, e.failure), error: e, requirements: requirements)127 } catch {128 Issue.record("\(tool.identifier) threw a non-ToolError: \(error)")129 return Outcome(result: .failure(.failed, ToolFailure(code: "internal", message: "\(error)")))130 }131}132133func skipped(_ reason: String) {134 print("SKIPPED(\(reason))")135}136137func verified(_ what: String) {138 print("VERIFIED(\(what))")139}140141enum TestImages {142 /// White canvas with black text lines, written as PNG.143 static func text(_ lines: [String], to url: URL, width: Int = 1100, height: Int = 320, fontSize: CGFloat = 56) throws {144 let ctx = try context(width: width, height: height)145 ctx.setFillColor(CGColor(gray: 1, alpha: 1))146 ctx.fill(CGRect(x: 0, y: 0, width: width, height: height))147 let font = CTFontCreateWithName("Helvetica" as CFString, fontSize, nil)148 for (i, line) in lines.enumerated() {149 let attributed = NSAttributedString(string: line, attributes: [150 NSAttributedString.Key(kCTFontAttributeName as String): font,151 NSAttributedString.Key(kCTForegroundColorAttributeName as String): CGColor(gray: 0, alpha: 1),152 ])153 ctx.textPosition = CGPoint(x: 40, y: CGFloat(height) - CGFloat(i + 1) * (fontSize * 1.6))154 CTLineDraw(CTLineCreateWithAttributedString(attributed), ctx)155 }156 try write(ctx, to: url)157 }158159 /// Solid background with optional filled rectangles (top-left origin, pixels).160 static func blocks(width: Int, height: Int, background: CGFloat, rects: [(CGRect, CGFloat)], to url: URL) throws {161 let ctx = try context(width: width, height: height)162 ctx.setFillColor(CGColor(gray: background, alpha: 1))163 ctx.fill(CGRect(x: 0, y: 0, width: width, height: height))164 for (r, gray) in rects {165 ctx.setFillColor(CGColor(gray: gray, alpha: 1))166 ctx.fill(CGRect(x: r.minX, y: CGFloat(height) - r.maxY, width: r.width, height: r.height))167 }168 try write(ctx, to: url)169 }170171 private static func context(width: Int, height: Int) throws -> CGContext {172 guard let space = CGColorSpace(name: CGColorSpace.sRGB),173 let ctx = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width * 4, space: space,174 bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { throw ToolError.internalError("no bitmap context") }175 return ctx176 }177178 private static func write(_ ctx: CGContext, to url: URL) throws {179 guard let image = ctx.makeImage(), let dest = CGImageDestinationCreateWithURL(url as CFURL, "public.png" as CFString, 1, nil) else {180 throw ToolError.internalError("no image")181 }182 CGImageDestinationAddImage(dest, image, nil)183 guard CGImageDestinationFinalize(dest) else { throw ToolError.io("could not write \(url.path)") }184 }185}186