Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Tests/AstraPersistenceTests/ExportTests.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 AstraPersistence9293@Suite struct SettingsEndpointsBenchmarksTests {94 struct Appearance: Codable, Sendable, Equatable { var theme: String; var fontScale: Double; var compact: Bool }9596 @Test func typedSettings() async throws {97 let store = try makeStore()98 #expect(try await store.setting("missing", as: Int.self) == nil)99 #expect(try await store.setting("missing", default: 7) == 7)100 try await store.setSetting("count", 3)101 try await store.setSetting("name", "Astra")102 try await store.setSetting("flag", true)103 try await store.setSetting("ratio", 0.25)104 try await store.setSetting("when", t(1.5))105 try await store.setSetting("model", gpt)106 try await store.setSetting("appearance", Appearance(theme: "dark", fontScale: 1.1, compact: true))107 try await store.setSetting("list", ["a", "b"])108 #expect(try await store.setting("count", as: Int.self) == 3)109 #expect(try await store.setting("name", as: String.self) == "Astra")110 #expect(try await store.setting("flag", default: false) == true)111 #expect(try await store.setting("ratio", as: Double.self) == 0.25)112 #expect(try await store.setting("when", as: Date.self) == t(1.5))113 #expect(try await store.setting("model", as: ModelRef.self) == gpt)114 #expect(try await store.setting("appearance", as: Appearance.self) == Appearance(theme: "dark", fontScale: 1.1, compact: true))115 #expect(try await store.setting("list", as: [String].self) == ["a", "b"])116 // Asking for the wrong type gives nil rather than a crash.117 #expect(try await store.setting("name", as: Int.self) == nil)118 try await store.setSetting("count", 4)119 #expect(try await store.setting("count", as: Int.self) == 4)120 try await store.setSetting("flag", Bool?.none)121 try await store.removeSetting("ratio")122 #expect(try await store.settingKeys() == ["appearance", "count", "list", "model", "name", "when"])123 }124125 @Test func endpointProfiles() async throws {126 let store = try makeStore()127 var profile = EndpointProfile(name: "Home server", baseURL: "http://192.168.2.50:8080/v1", defaultModelID: "qwen", allowInsecureHTTP: true,128 extraHeaders: ["X-Org": "lab"], createdAt: t(1))129 try await store.saveEndpoint(profile)130 try await store.saveEndpoint(EndpointProfile(name: "cloud", baseURL: "https://api.example.com/v1"))131 #expect(try await store.endpoint(id: profile.id) == profile)132 #expect(try await store.endpoints().map(\.name) == ["cloud", "Home server"])133 profile.name = "Lab server"; profile.defaultModelID = nil134 try await store.saveEndpoint(profile)135 #expect(try await store.endpoints().count == 2)136 #expect(try await store.endpoint(id: profile.id)?.name == "Lab server")137 // Invalid or credential-carrying profiles are refused and nothing is stored.138 await #expect(throws: AstraError.self) { try await store.saveEndpoint(EndpointProfile(name: "bad", baseURL: "http://example.com/v1")) }139 await #expect(throws: AstraError.self) {140 try await store.saveEndpoint(EndpointProfile(name: "leaky", baseURL: "https://example.com/v1", extraHeaders: ["Authorization": "Bearer abcdefghijklmnop"]))141 }142 await #expect(throws: AstraError.self) { try await store.saveEndpoint(EndpointProfile(name: "creds", baseURL: "https://user:pass@example.com/v1")) }143 #expect(try await store.endpoints().count == 2)144 try await store.deleteEndpoint(id: profile.id)145 #expect(try await store.endpoint(id: profile.id) == nil)146 }147148 @Test func benchmarkRecordsAndExports() async throws {149 let store = try makeStore()150 let cold = BenchmarkRecord(model: local, deviceModel: "iPhone18,2", osVersion: "iOS 27.0", modelRevision: "abc123", isColdStart: true, timeToFirstToken: 1.25,151 tokensPerSecond: 31.5, promptTokens: 120, outputTokens: 256, peakMemoryBytes: 3_221_225_472, thermalState: "fair",152 createdAt: t(1), notes: "First run, \"cold\", after reboot")153 let warm = BenchmarkRecord(model: local, deviceModel: "iPhone18,2", osVersion: "iOS 27.0", isColdStart: false, tokensPerSecond: 44, createdAt: t(2))154 let other = BenchmarkRecord(model: gpt, deviceModel: "Mac16,1", osVersion: "macOS 27.0", isColdStart: false, createdAt: t(3), notes: "=cmd")155 for record in [cold, warm, other] { try await store.saveBenchmark(record) }156 #expect(try await store.benchmarks() == [other, warm, cold])157 #expect(try await store.benchmarks(model: local) == [warm, cold])158 var edited = warm159 edited.notes = "warm run"160 try await store.saveBenchmark(edited)161 #expect(try await store.benchmarks().count == 3)162163 let rows = parseCSV(try await store.exportBenchmarks(.csv))164 #expect(rows.count == 4)165 let header = rows[0]166 let coldRow = try #require(rows.first { $0[header.firstIndex(of: "start")!] == "cold" })167 #expect(coldRow[header.firstIndex(of: "notes")!] == "First run, \"cold\", after reboot")168 #expect(coldRow[header.firstIndex(of: "peak_memory_bytes")!] == "3221225472")169 #expect(coldRow[header.firstIndex(of: "tokens_per_second")!] == "31.5")170 #expect(coldRow[header.firstIndex(of: "model_revision")!] == "abc123")171 #expect(coldRow[header.firstIndex(of: "thermal_state")!] == "fair")172 #expect(rows[1][header.firstIndex(of: "notes")!] == "'=cmd")173 #expect(rows[1][header.firstIndex(of: "ttft_s")!] == "")174 let decoded = try ExportCoding.decoder.decode([BenchmarkRecord].self, from: Data(try await store.exportBenchmarks(.json).utf8))175 #expect(decoded.map(\.id) == [other.id, warm.id, cold.id])176 #expect(decoded.last?.peakMemoryBytes == 3_221_225_472)177 try await store.deleteBenchmark(id: other.id)178 #expect(try await store.deleteAllBenchmarks() == 2)179 }180}181182@Suite struct ExportAndDeletionTests {183 /// A store with a little of everything in it.184 static func populated(_ store: AstraStore) async throws -> (conversation: ConversationRecord, branchUser: MessageRecord) {185 let conversation = try await store.createConversation(title: "Tides & \"moons\"", model: gpt, systemPrompt: "Be precise.", at: t(0))186 let image = AttachmentDraft(kind: .image, filename: "chart.png", mimeType: "image/png", data: Data(repeating: 0xAB, count: 2048))187 try await store.appendUserMessage(conversationID: conversation.id, text: "Why two tides?", attachments: [image], at: t(1))188 let first = try await store.beginAssistantMessage(conversationID: conversation.id, model: gpt, at: t(2))189 try await store.finalizeAssistantMessage(id: first.id, MessageCompletion(190 text: "Because of the bulge.\n\n```swift\nlet tides = 2\n```", reasoningSummary: "Recall gravity.\nTwo bulges.",191 toolCalls: [ToolCall(id: "c1", name: "lookup", arguments: ["topic": "tides"])], reportedModel: "gpt-test-2026",192 usage: TokenUsage(inputTokens: 30, outputTokens: 12), cost: CostEstimate(amountUSD: 0.0002, basis: .estimatedFromUsage)), at: t(3))193 try await store.appendToolMessage(conversationID: conversation.id, text: "{\"result\": \"```two```\"}", toolCallID: "c1", toolName: "lookup", at: t(4))194 let original = try await store.appendUserMessage(conversationID: conversation.id, text: "Original follow-up", at: t(5))195 let edited = try await store.editAndResend(messageID: original.id, newText: "Edited follow-up", at: t(6))196 let stopped = try await store.beginAssistantMessage(conversationID: conversation.id, model: claude, at: t(7))197 try await store.finalizeAssistantMessage(id: stopped.id, MessageCompletion(text: "Partial ans", status: .stopped, stopReason: .cancelled), at: t(8))198199 let arena = try await store.createArenaSession(prompt: "p", competitors: [gpt, claude])200 var entry = arena.entries[0]; entry.text = "arena answer"; entry.status = .complete; entry.usage = TokenUsage(inputTokens: 1, outputTokens: 2)201 try await store.finishArenaEntry(entry)202 let profile = AgentProfileRecord(name: "Agent", model: gpt)203 try await store.saveAgentProfile(profile)204 let run = try await store.startAgentRun(profile: profile, goal: "goal")205 try await store.appendAgentStep(runID: run.id, AgentStepDraft(kind: .note, text: "note"))206 try await store.saveTemplate(name: "T", system: "s", userTemplate: "{{x}}")207 try await store.savePreset(PresetRecord(name: "P"))208 try await store.recordDiagnostic(RequestDiagnostic(provider: .openAI, modelID: "gpt-test", httpStatus: 200))209 try await store.addMemory(text: "A memory", sourceConversationID: conversation.id)210 try await store.addDocument(name: "Doc", kind: "text", text: "Document text about estuaries.")211 try await store.saveEndpoint(EndpointProfile(name: "Custom", baseURL: "https://llm.example.com/v1", defaultModelID: "m", extraHeaders: ["X-Team": "astra"]))212 try await store.setSetting("theme", "dark")213 try await store.setSetting("limits", ["daily": 5, "monthly": 100])214 try await store.saveBenchmark(BenchmarkRecord(model: local, deviceModel: "iPhone18,2", osVersion: "iOS 27.0", isColdStart: true))215 return (conversation, edited)216 }217218 @Test func conversationJSONExportHasTheActivePathAndTheBranches() async throws {219 let store = try makeStore()220 let (conversation, edited) = try await Self.populated(store)221 let data = try await store.exportConversationJSON(id: conversation.id)222 let export = try ExportCoding.decoder.decode(ConversationExport.self, from: data)223 #expect(export.format == "orix-astra-conversation/1")224 #expect(export.conversation.title == "Tides & \"moons\"")225 #expect(export.messages.count == 6)226 #expect(export.activePath.count == 5)227 let byID = Dictionary(uniqueKeysWithValues: export.messages.map { ($0.id, $0) })228 #expect(export.activePath.compactMap { byID[$0]?.text } == ["Why two tides?", "Because of the bulge.\n\n```swift\nlet tides = 2\n```", "{\"result\": \"```two```\"}", "Edited follow-up", "Partial ans"])229 #expect(export.branchPoints.count == 1)230 let fork = try #require(export.branchPoints.first)231 #expect(fork.childIDs.compactMap { byID[$0]?.text } == ["Original follow-up", "Edited follow-up"])232 #expect(fork.activeChildID == edited.id)233 #expect(fork.parentID == byID[edited.id]?.parentID)234 #expect(export.attachments.map(\.filename) == ["chart.png"])235 #expect(byID[export.activePath[1]]?.toolCalls.first?.name == "lookup")236 #expect(byID[export.activePath[1]]?.usage == TokenUsage(inputTokens: 30, outputTokens: 12))237 #expect(byID[export.activePath[4]]?.status == .stopped)238 // It is plain, readable JSON with ISO dates.239 let object = try #require(JSONValue.parse(data))240 #expect(object["conversation"]?["createdAt"]?.stringValue?.hasSuffix("Z") == true)241 #expect(object["messages"]?[0]?["role"] == "user")242243 let activeOnly = try ExportCoding.decoder.decode(ConversationExport.self, from: try await store.exportConversationJSON(id: conversation.id, activePathOnly: true))244 #expect(activeOnly.messages.map(\.id) == activeOnly.activePath)245 #expect(activeOnly.branchPoints.count == 1)246 await #expect(throws: AstraStoreError.self) { try await store.exportConversationJSON(id: "missing") }247 }248249 @Test func conversationMarkdownExport() async throws {250 let store = try makeStore()251 let (conversation, _) = try await Self.populated(store)252 let markdown = try await store.exportConversationMarkdown(id: conversation.id)253 #expect(markdown.hasPrefix("# Tides & \"moons\"\n"))254 #expect(markdown.contains("- Model: gpt-test (OpenAI)"))255 #expect(markdown.contains("## System\n\nBe precise."))256 #expect(markdown.contains("## You\n\n- Attachment: chart.png (image/png, 2048 bytes)\n\nWhy two tides?"))257 #expect(markdown.contains("## Assistant (gpt-test-2026)"))258 #expect(markdown.contains("> Recall gravity.\n> Two bulges."))259 #expect(markdown.contains("```swift\nlet tides = 2\n```"))260 #expect(markdown.contains("Tool call `lookup`:\n\n```json"))261 // A tool result containing a code fence is wrapped in a longer fence.262 #expect(markdown.contains("## Tool result (lookup)\n\n````\n{\"result\": \"```two```\"}\n````"))263 #expect(markdown.contains("_One of 2 alternatives at this point._\n\nEdited follow-up"))264 #expect(!markdown.contains("Original follow-up"))265 #expect(markdown.contains("Partial ans\n\n_Stopped before the end._"))266 let order = ["Why two tides?", "Because of the bulge.", "Tool result", "Edited follow-up", "Partial ans"].map { markdown.range(of: $0)!.lowerBound }267 #expect(order == order.sorted())268 }269270 @Test func fullExportWritesOneJSONDocumentAndTheAttachments() async throws {271 let store = try makeStore()272 let (conversation, _) = try await Self.populated(store)273 _ = try store.blobs.store(Data("orphan".utf8)) // an unreferenced file is not exported274 let destination = try temporaryDirectory().appendingPathComponent("export", isDirectory: true)275 defer { try? FileManager.default.removeItem(at: destination.deletingLastPathComponent()) }276 let summary = try await store.exportAll(to: destination, at: t(100))277 #expect(summary.jsonURL.lastPathComponent == "astra-export.json")278 #expect(summary.attachmentFileCount == 1)279 #expect(try FileManager.default.contentsOfDirectory(atPath: destination.path).sorted() == ["astra-export.json", "attachments"])280 let data = try Data(contentsOf: summary.jsonURL)281 #expect(summary.totalBytes == Int64(data.count) + 2048)282 let export = try ExportCoding.decoder.decode(AstraFullExport.self, from: data)283 #expect(export.format == "orix-astra-export/1")284 #expect(export.schemaVersion == AstraSchema.currentVersion)285 #expect(export.exportedAt == t(100))286 #expect(export.conversations.map(\.id) == [conversation.id])287 #expect(export.messages.count == 6)288 #expect(export.attachments.count == 1)289 #expect(export.attachmentFiles.map(\.byteSize) == [2048])290 #expect(export.arenaSessions.count == 1)291 #expect(export.arenaEntries.count == 2)292 #expect(export.agentProfiles.count == 1)293 #expect(export.agentRuns.count == 1)294 #expect(export.agentSteps.count == 1)295 #expect(export.promptTemplates.count == 1)296 #expect(export.promptTemplateVersions.count == 1)297 #expect(export.presets.count == 1)298 #expect(export.usage.count == 2)299 #expect(export.diagnostics.count == 1)300 #expect(export.memories.count == 1)301 #expect(export.documents.count == 1)302 #expect(export.chunks.count == 1)303 #expect(export.endpoints.count == 1)304 #expect(export.settings == ["theme": "dark", "limits": ["daily": 5, "monthly": 100]])305 #expect(export.benchmarks.count == 1)306 // The attachments directory lists exactly the referenced files, each under its hash, with the right bytes.307 let files = try FileManager.default.contentsOfDirectory(atPath: summary.attachmentsDirectory.path)308 #expect(files == export.attachmentFiles.map(\.name))309 #expect(files == export.attachments.map(\.sha256))310 #expect(try Data(contentsOf: summary.attachmentsDirectory.appendingPathComponent(files[0])) == Data(repeating: 0xAB, count: 2048))311 // Exporting twice to the same place overwrites cleanly.312 #expect(try await store.exportAll(to: destination).attachmentFileCount == 1)313 }314315 @Test func exportsNeverContainSecrets() async throws {316 let store = try makeStore()317 _ = try await Self.populated(store)318 // A profile written by some future bug with a credential header must still not leak it.319 let leaky = EndpointProfile(name: "Leaky", baseURL: "https://x.example.com/v1", extraHeaders: ["Authorization": "Bearer sk-live-THISMUSTNOTLEAK123", "X-Note": "key sk-ant-THISMUSTNOTLEAK456789"])320 try await store.write { db in321 try db.execute(sql: "INSERT INTO endpoint(id, name, createdAt, json) VALUES (?, ?, 0, ?)", arguments: [leaky.id, leaky.name, try DBJSON.encode(leaky)])322 }323 let export = try await store.fullExport()324 let json = String(decoding: try ExportCoding.encoder.encode(export), as: UTF8.self)325 #expect(!json.contains("THISMUSTNOTLEAK"))326327 // Endpoint profiles have no field that could hold a key: the exported objects carry exactly these keys.328 let endpoints = try #require(JSONValue.parse(json)?["endpoints"]?.arrayValue)329 #expect(endpoints.count == 2)330 let allowed: Set<String> = ["id", "name", "baseURL", "defaultModelID", "allowInsecureHTTP", "extraHeaders", "createdAt"]331 let forbidden = ["key", "secret", "token", "password", "credential", "authorization", "bearer"]332 for endpoint in endpoints {333 let keys = Set(try #require(endpoint.objectValue).keys)334 #expect(keys.isSubset(of: allowed), "Unexpected endpoint fields: \(keys.subtracting(allowed))")335 for key in keys { #expect(!forbidden.contains { key.lowercased().contains($0) }) }336 }337 // And the same holds for the column list of the endpoint table and for every table of the schema.338 let columns = try await store.read { db in339 try AstraTable.allCases.flatMap { table in try db.columns(in: table.rawValue).map { "\(table.rawValue).\($0.name)" } }340 }341 #expect(columns.contains("endpoint.json"))342 for column in columns {343 let name = column.lowercased()344 #expect(!["apikey", "api_key", "secret", "password", "credential", "bearer"].contains { name.contains($0) }, "Suspicious column \(column)")345 }346 }347348 @Test func granularDeletionOnlyTouchesItsCategory() async throws {349 let directory = try temporaryDirectory()350 defer { try? FileManager.default.removeItem(at: directory) }351 let store = try AstraStore(url: directory.appendingPathComponent("astra.sqlite"))352 _ = try await Self.populated(store)353 for index in 0..<200 { try await store.addDocument(name: "bulk \(index)", kind: "text", text: String(repeating: "estuary sediment \(index) ", count: 200)) }354 func counts() async throws -> [String: Int] {355 try await store.read { db in356 var out: [String: Int] = [:]357 for table in AstraTable.allCases { out[table.rawValue] = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM \"\(table.rawValue)\"") ?? 0 }358 return out359 }360 }361 var expected = try await counts()362 #expect(expected.values.allSatisfy { $0 > 0 })363364 let documents = try await store.deleteAllDocuments()365 #expect(documents.rowsDeleted == expected["document"]! + expected["chunk"]!)366 #expect(documents.bytesFreed > 100_000) // measurable: the pages those chunks and their index used367 expected["document"] = 0; expected["chunk"] = 0368 #expect(try await counts() == expected)369370 #expect(try await store.deleteAllUsage().rowsDeleted == 2)371 expected["usage"] = 0372 #expect(try await store.deleteAllDiagnostics().rowsDeleted == 1)373 expected["diagnostic"] = 0374 #expect(try await store.deleteAllMemory().rowsDeleted == 1)375 expected["memory"] = 0376 #expect(try await counts() == expected)377378 let attachments = try await store.deleteAllAttachments()379 #expect(attachments.rowsDeleted == 1)380 #expect(attachments.bytesFreed >= 2048)381 expected["attachment"] = 0382 #expect(try await counts() == expected)383 #expect(try await store.attachmentStorageBytes() == 0)384385 #expect(try await store.deleteAllArenaSessions().rowsDeleted == 3)386 expected["arenaSession"] = 0; expected["arenaEntry"] = 0387 #expect(try await store.deleteAllAgentRuns().rowsDeleted == 2)388 expected["agentRun"] = 0; expected["agentStep"] = 0389 #expect(try await counts() == expected)390391 let conversations = try await store.deleteAllConversations()392 #expect(conversations.rowsDeleted == 7)393 expected["conversation"] = 0; expected["message"] = 0394 #expect(try await counts() == expected)395 // What was not asked for is still there.396 #expect(try await store.templates().count == 1)397 #expect(try await store.endpoints().count == 1)398 #expect(try await store.setting("theme", as: String.self) == "dark")399 #expect(try await store.searchConversations("tides").isEmpty)400401 let before = try await store.databaseFileBytes()402 let shrunk = try await store.compact()403 #expect(shrunk > 100_000)404 #expect(try await store.databaseFileBytes() <= before - 100_000)405 #expect(try await store.integrityCheck() == [])406 }407408 @Test func deleteEverythingDropsAndRecreatesTheDatabase() async throws {409 let directory = try temporaryDirectory()410 defer { try? FileManager.default.removeItem(at: directory) }411 let store = try AstraStore(url: directory.appendingPathComponent("astra.sqlite"))412 _ = try await Self.populated(store)413 for index in 0..<100 { try await store.addDocument(name: "bulk \(index)", kind: "text", text: String(repeating: "filler text \(index) ", count: 300)) }414 let result = try await store.deleteEverything()415 #expect(result.rowsDeleted > 120)416 #expect(result.bytesFreed > 2048 + 100_000)417 let remaining = try await store.read { db in418 try AstraTable.allCases.reduce(0) { $0 + (try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM \"\($1.rawValue)\"") ?? 0) }419 }420 #expect(remaining == 0)421 #expect(try await store.read { db in try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM message_fts") } == 0)422 #expect(try FileManager.default.contentsOfDirectory(atPath: store.attachmentsDirectory.path).isEmpty)423 #expect(try await store.settingKeys().isEmpty)424 // The store is usable at once, on a fresh, fully migrated schema.425 #expect(try await store.schemaVersion() == AstraSchema.currentVersion)426 let conversation = try await store.createConversation(title: "After the reset", model: gpt)427 let image = AttachmentDraft(kind: .image, filename: "new.png", mimeType: "image/png", data: Data([1, 2, 3, 4]))428 try await store.appendUserMessage(conversationID: conversation.id, text: "hello again", attachments: [image])429 #expect(try await store.searchConversations("again").count == 1)430 #expect(try await store.attachmentStorageBytes() == 4)431 // In memory too.432 let memory = try makeStore()433 _ = try await Self.populated(memory)434 #expect(try await memory.deleteEverything().rowsDeleted > 20)435 #expect(try await memory.conversationCount() == 0)436 #expect(try await memory.schemaVersion() == AstraSchema.currentVersion)437 }438}439440@Suite struct ObservationTests {441 actor Counter {442 var value = 0443 func increment() { value += 1 }444 }445446 /// Number of signals a stream delivers within `duration`.447 static func drain(_ stream: AsyncStream<Void>, for duration: Duration = .milliseconds(400)) async -> Int {448 let counter = Counter()449 let reader = Task { for await _ in stream { await counter.increment() } }450 try? await Task.sleep(for: duration)451 reader.cancel()452 return await counter.value453 }454455 @Test func consumersAreToldToReRead() async throws {456 let store = try makeStore()457 let stream = store.changes(in: AstraTable.conversations)458 var iterator = stream.makeAsyncIterator()459 await iterator.next() // the immediate signal: time for the first read460 #expect(try await store.conversations().isEmpty)461 let conversation = try await store.createConversation(title: "Observed", model: gpt)462 await iterator.next()463 #expect(try await store.conversations().map(\.title) == ["Observed"])464 try await store.renameConversation(id: conversation.id, title: "Renamed")465 await iterator.next()466 #expect(try await store.conversations().map(\.title) == ["Renamed"])467 try await store.deleteConversation(id: conversation.id)468 await iterator.next()469 #expect(try await store.conversations().isEmpty)470 }471472 @Test func onlyWatchedTablesSignal() async throws {473 let store = try makeStore()474 let conversation = try await store.createConversation(model: gpt)475 let messages = store.changes(in: AstraTable.messages)476 let memory = store.changes(in: [.memory])477 async let messageSignals = Self.drain(messages, for: .milliseconds(600))478 async let memorySignals = Self.drain(memory, for: .milliseconds(600))479 try await Task.sleep(for: .milliseconds(150))480 try await store.appendUserMessage(conversationID: conversation.id, text: "hello")481 try await store.updateStreaming(id: try await store.beginAssistantMessage(conversationID: conversation.id, model: gpt).id, text: "partial")482 let seen = await (messages: messageSignals, memory: memorySignals)483 #expect((2...4).contains(seen.messages)) // the initial signal, then the writes (possibly coalesced)484 #expect(seen.memory == 1) // only its initial signal: nothing touched memory485 }486487 @Test func burstsCoalesceAndCascadesAreSeen() async throws {488 let store = try makeStore()489 let conversation = try await store.createConversation(model: gpt)490 try await store.exchange(conversation.id, "q", "a")491 let stream = store.changes(in: [.message])492 var iterator = stream.makeAsyncIterator()493 await iterator.next()494 // Deleting the conversation removes its messages by cascade: watchers of `message` still hear about it.495 try await store.deleteConversation(id: conversation.id)496 await iterator.next()497 #expect(try await store.read { db in try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM message") } == 0)498 // Many writes while nobody is reading leave a single pending signal: no unbounded backlog.499 let other = try await store.createConversation(model: gpt)500 let idle = store.changes(in: [.message])501 for index in 0..<50 { try await store.appendUserMessage(conversationID: other.id, text: "m\(index)") }502 #expect(await Self.drain(idle) == 1)503 #expect(try await store.messageCount(conversationID: other.id) == 50)504 }505}506