Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Tests/AstraPersistenceTests/Support.swift10// Module : AstraPersistenceTests11// 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 AstraPersistence9293let gpt = ModelRef(provider: .openAI, modelID: "gpt-test")94let claude = ModelRef(provider: .anthropic, modelID: "claude-test")95let local = ModelRef(provider: .mlx, modelID: "mlx-community/test-4bit")9697func makeStore(_ options: AstraStore.Options = .init()) throws -> AstraStore { try AstraStore(inMemory: options) }9899func temporaryDirectory() throws -> URL {100 let url = FileManager.default.temporaryDirectory.appendingPathComponent("AstraPersistenceTests-" + UUID().uuidString, isDirectory: true)101 try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)102 return url103}104105/// A fixed instant plus an offset, so tests control ordering exactly.106func t(_ seconds: Double) -> Date { Date(timeIntervalSinceReferenceDate: 800_000_000 + seconds) }107108extension AstraStore {109 /// Appends a user turn and a finished assistant turn; returns both.110 @discardableResult111 func exchange(_ conversationID: String, _ question: String, _ answer: String, at seconds: Double = 0) async throws -> (user: MessageRecord, assistant: MessageRecord) {112 let user = try await appendUserMessage(conversationID: conversationID, text: question, at: t(seconds))113 let draft = try await beginAssistantMessage(conversationID: conversationID, model: gpt, at: t(seconds + 0.5))114 let done = try await finalizeAssistantMessage(id: draft.id, MessageCompletion(text: answer, usage: TokenUsage(inputTokens: 10, outputTokens: 20)),115 at: t(seconds + 1))116 return (user, done)117 }118}119120/// Minimal RFC 4180 reader used to prove that exported CSV round-trips.121func parseCSV(_ text: String) -> [[String]] {122 var rows: [[String]] = []; var row: [String] = []; var field = ""; var quoted = false123 var iterator = text.unicodeScalars.makeIterator()124 var pending = iterator.next()125 while let scalar = pending {126 pending = iterator.next()127 if quoted {128 if scalar == "\"" {129 if pending == "\"" { field.unicodeScalars.append("\""); pending = iterator.next() } else { quoted = false }130 } else { field.unicodeScalars.append(scalar) }131 } else if scalar == "\"" { quoted = true }132 else if scalar == "," { row.append(field); field = "" }133 else if scalar == "\r" { continue }134 else if scalar == "\n" { row.append(field); rows.append(row); row = []; field = "" }135 else { field.unicodeScalars.append(scalar) }136 }137 if !field.isEmpty || !row.isEmpty { row.append(field); rows.append(row) }138 return rows139}140