Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/LocalChatDataTests/ConversationsStoreTests.swift3// Description: Conversation store tests: CRUD, branching, full-text search, observation, export/import, sync, attachments.4import Foundation5import LocalChatConversations6import LocalChatCore7import Testing89@Suite struct ConversationsStoreTests {10 func seeded() throws -> (ConversationStore, Conversation, [ChatMessage]) {11 let store = try ConversationStore.inMemory()12 let c = try store.saveConversation(Conversation(title: "Résumé du café"))13 let u1 = try store.appendMessage(ChatMessage(conversationID: c.id, parentID: nil, role: .user, content: "Bonjour, parle-moi du café éthiopien"))14 let a1 = try store.appendMessage(ChatMessage(conversationID: c.id, parentID: nil, role: .assistant, content: "Le café éthiopien est fruité.", modelName: "Qwen"))15 let u2 = try store.appendMessage(ChatMessage(conversationID: c.id, parentID: nil, role: .user, content: "And the roasting temperature?"))16 let a2 = try store.appendMessage(ChatMessage(conversationID: c.id, parentID: nil, role: .assistant, content: "Around 200 degrees Celsius.", modelName: "Qwen"))17 return (store, c, [u1, a1, u2, a2])18 }1920 @Test func conversationCRUDAndFilters() throws {21 let store = try ConversationStore.inMemory()22 let folder = try store.save(ChatFolder(name: "Work"))23 var a = Conversation(title: "Alpha", folderID: folder.id, tags: ["swift", "mlx"], isFavorite: true)24 a = try store.saveConversation(a)25 let b = try store.saveConversation(Conversation(title: "Béta", tags: ["swift"]))26 _ = try store.saveConversation(Conversation(title: "Old", isArchived: true))2728 #expect(try store.conversation(a.id) == a)29 #expect(try store.conversations().count == 2)30 #expect(try store.conversations(.init(archived: nil)).count == 3)31 #expect(try store.conversations(.init(archived: true)).map(\.title) == ["Old"])32 #expect(try store.conversations(.init(folder: .folder(folder.id))).map(\.id) == [a.id])33 #expect(try store.conversations(.init(folder: .unfiled)).map(\.id) == [b.id])34 #expect(try store.conversations(.init(favoritesOnly: true)).map(\.id) == [a.id])35 #expect(try store.conversations(.init(tag: "mlx")).map(\.id) == [a.id])36 #expect(try store.conversations(.init(text: "beta")).map(\.id) == [b.id])37 #expect(try store.conversations(.init(sort: .title)).map(\.title) == ["Alpha", "Béta"])38 #expect(try store.tags() == [TagCount(tag: "swift", count: 2), TagCount(tag: "mlx", count: 1)])3940 try store.delete(ChatFolder.self, id: folder.id)41 #expect(try store.folders().isEmpty)42 #expect(try store.conversation(a.id)?.folderID == nil)4344 try store.deleteConversation(a.id)45 #expect(try store.conversation(a.id) == nil)46 #expect(try store.conversations().map(\.id) == [b.id])47 }4849 @Test func entitiesTemplatesAndPreferences() throws {50 let store = try ConversationStore.inMemory()51 #expect(try store.templates().filter(\.isBuiltIn).count == BuiltInTemplates.all.count)52 let t = try store.save(PromptTemplate(name: "Mine", kind: .user, body: "Hi {{name}}"))53 #expect(try store.templates().first?.id == t.id) // user templates sort first54 let w = try store.save(ModelWorkspace(name: "Research"))55 let agent = try store.save(AgentConfiguration(name: "Helper"))56 #expect(try store.workspaces() == [w])57 #expect(try store.agents() == [agent])58 #expect(try store.get(AgentConfiguration.self, id: agent.id) == agent)59 try store.delete(AgentConfiguration.self, id: agent.id)60 #expect(try store.agents().isEmpty)61 #expect(try store.all(AgentConfiguration.self, includeDeleted: true).count == 1)6263 #expect(try store.preferences().defaultModelID == nil)64 var p = try store.preferences(); p.defaultModelID = "mlx:x"; p.appLockEnabled = true65 try store.savePreferences(p)66 #expect(try store.preferences().defaultModelID == "mlx:x")67 #expect(try store.preferences().appLockEnabled)68 }6970 @Test func appendBuildsALinearBranch() throws {71 let (store, c, m) = try seeded()72 let branch = try store.activeBranch(c.id)73 #expect(branch.map(\.id) == m.map(\.id))74 #expect(m[0].parentID == nil && m[1].parentID == m[0].id && m[3].parentID == m[2].id)75 #expect(try store.conversation(c.id)?.activeLeafID == m[3].id)76 }7778 @Test func editCreatesSiblingAndKeepsOriginal() throws {79 let (store, c, m) = try seeded()80 let edited = try store.editUserMessage(m[2].id, newContent: "And the grind size?")81 #expect(edited.parentID == m[1].id && edited.id != m[2].id && edited.editedAt != nil)82 #expect(try store.message(m[2].id)?.content == "And the roasting temperature?")83 #expect(try store.activeBranch(c.id).map(\.id) == [m[0].id, m[1].id, edited.id])84 let tree = try store.tree(c.id)85 #expect(tree.branchPosition(of: edited.id).index == 2 && tree.branchPosition(of: edited.id).count == 2)86 #expect(tree.branchCount == 2)87 #expect(throws: ConversationStoreError.self) { try store.editUserMessage(m[1].id, newContent: "x") }8889 // switch back to the original branch, then forward again90 #expect(try store.switchBranch(from: edited.id, offset: -1) == m[3].id)91 #expect(try store.activeBranch(c.id).map(\.id) == m.map(\.id))92 #expect(try store.switchBranch(from: m[2].id, offset: 1) == edited.id)93 #expect(try store.switchBranch(from: edited.id, offset: 1) == nil)94 }9596 @Test func regenerateCreatesAssistantSibling() throws {97 let (store, c, m) = try seeded()98 let fresh = try store.regenerate(m[3].id, modelID: "mlx:other", modelName: "Other")99 #expect(fresh.parentID == m[2].id && fresh.status == .streaming && fresh.modelName == "Other")100 try store.updateStreamingMessage(id: fresh.id, content: "About 205", reasoning: "thinking")101 try store.updateStreamingMessage(id: fresh.id, content: "About 205 °C \"medium\"")102 let streamed = try #require(try store.message(fresh.id))103 #expect(streamed.content == "About 205 °C \"medium\"" && streamed.reasoning == "thinking" && streamed.createdAt == fresh.createdAt)104 #expect(try store.search("medium").isEmpty) // not indexed while streaming105 var done = streamed; done.status = .complete106 try store.saveMessage(done)107 #expect(try store.search("medium").map(\.messageID) == [fresh.id])108 #expect(try store.activeBranch(c.id).last?.id == fresh.id)109 #expect(try store.message(m[3].id) != nil)110 }111112 @Test func subtreeDeleteAndFork() throws {113 let (store, c, m) = try seeded()114 let edited = try store.editUserMessage(m[2].id, newContent: "Other question")115 let fork = try store.forkConversation(from: m[1].id)116 let forked = try store.activeBranch(fork.id)117 #expect(forked.map(\.content) == [m[0].content, m[1].content])118 #expect(Set(forked.map(\.id)).isDisjoint(with: Set(m.map(\.id))))119 #expect(forked[1].parentID == forked[0].id && fork.activeLeafID == forked[1].id)120121 try store.deleteMessageSubtree(edited.id) // active leaf was on the deleted branch122 #expect(try store.activeBranch(c.id).map(\.id) == m.map(\.id))123 try store.deleteMessageSubtree(m[2].id)124 #expect(try store.messages(in: c.id).map(\.id) == [m[0].id, m[1].id])125 #expect(try store.conversation(c.id)?.activeLeafID == m[1].id)126 #expect(try store.search("roasting").isEmpty)127 #expect(try store.activeBranch(fork.id).count == 2) // the fork is independent128129 let dup = try store.duplicateConversation(fork.id)130 #expect(try store.messages(in: dup.id).count == 2 && dup.id != fork.id)131 }132133 @Test func summaryDoesNotTouchMessages() throws {134 let (store, c, m) = try seeded()135 try store.setSummary(ContextSummary(text: "coffee talk", coversThroughMessageID: m[1].id, messageCount: 2, modelID: nil), for: c.id)136 #expect(try store.conversation(c.id)?.summary?.text == "coffee talk")137 #expect(try store.messages(in: c.id) == m)138 }139140 @Test func fullTextSearch() throws {141 let (store, c, m) = try seeded()142 #expect(try store.search("ethiopien").count == 2) // diacritics folded143 #expect(try store.search("ÉTHIOP").count == 2) // prefix + case144 #expect(try store.search("cafe resume").map(\.kind) == [.title]) // title hit, all terms required145 let hit = try #require(try store.search("roasting").first)146 #expect(hit.messageID == m[2].id && hit.role == .user && hit.conversationID == c.id && hit.conversationTitle == c.title)147 #expect(hit.snippetRuns.contains { $0.isMatch && $0.text.lowercased() == "roasting" })148 #expect(hit.plainSnippet.contains("roasting temperature"))149 // injection-like and syntax-breaking input must neither throw nor match everything150 for q in ["\"", "roasting\" OR \"x", "NEAR(", "* ) AND", "content:roasting -x", "'; DROP TABLE message; --", " "] { _ = try store.search(q) }151 #expect(try store.search("\"").isEmpty)152 #expect(try store.search("zzzznotthere OR roasting").isEmpty) // OR is a plain word, not an operator153 #expect(SearchQuery.ftsExpression("a-b c") == "\"a\"* \"b\"* \"c\"*")154155 var changed = m[2]; changed.content = "And the brewing ratio?"156 try store.saveMessage(changed)157 #expect(try store.search("roasting").isEmpty)158 #expect(try store.search("brewing").count == 1)159 try store.rebuildSearchIndex()160 #expect(try store.search("brewing").count == 1 && store.search("ethiopien").count == 2)161 try store.deleteConversation(c.id)162 #expect(try store.search("brewing").isEmpty && store.search("cafe").isEmpty)163 }164165 @Test func observationFires() async throws {166 let store = try ConversationStore.inMemory()167 var lists = store.observeConversations().makeAsyncIterator()168 #expect(await lists.next() == [])169 let c = try store.saveConversation(Conversation(title: "Watched"))170 #expect(await lists.next()?.map(\.id) == [c.id])171172 var msgs = store.observeMessages(in: c.id).makeAsyncIterator()173 #expect(await msgs.next() == [])174 let m = try store.appendMessage(ChatMessage(conversationID: c.id, parentID: nil, role: .assistant, content: "", status: .streaming))175 #expect(await msgs.next()?.map(\.id) == [m.id])176 try store.updateStreamingMessage(id: m.id, content: "tok")177 #expect(await msgs.next()?.first?.content == "tok")178 }179180 @Test func exportAndImportRoundTrip() throws {181 let (store, c, m) = try seeded()182 var rich = m[3]183 rich.reasoning = "Let me think"; rich.backend = .mlx184 rich.sources = [SourceReference(documentID: UUID(), documentName: "roast.pdf", chunkIndex: 0, page: 3, excerpt: "200 °C", score: 0.9)]185 rich.attachments = [Attachment(kind: .document, fileName: "notes.txt", mimeType: "text/plain", byteCount: 12, storedName: "abc.txt")]186 rich.metrics = GenerationMetrics(promptTokens: 10, completionTokens: 5, generationTime: 0.25)187 try store.saveMessage(rich)188 let edited = try store.editUserMessage(m[2].id, newContent: "Branch two")189190 let md = try store.exportMarkdown(c.id, scope: .wholeTree)191 #expect(md.contains("# Résumé du café") && md.contains("Assistant — Qwen · MLX") && md.contains("<summary>Reasoning</summary>"))192 #expect(md.contains("roast.pdf, p. 3") && md.contains("notes.txt") && md.contains("Branch 2 of 2") && md.contains("Branch two"))193 let active = try store.exportMarkdown(c.id)194 #expect(active.contains("Branch two") && !active.contains("roasting temperature"))195 #expect(try store.exportPlainText(c.id).contains("User:\nBranch two"))196197 let data = try store.exportJSON(c.id)198 let other = try ConversationStore.inMemory()199 let imported = try other.importJSON(data)200 #expect(imported.id == c.id) // no conflict: ids kept201 let original = try store.messages(in: c.id)202 #expect(try other.messages(in: c.id) == original) // lossless, dates included203 #expect(try other.activeBranch(c.id).last?.id == edited.id)204 #expect(try other.search("ethiopien").count == 2)205206 let again = try other.importJSON(data) // conflict: fresh ids, same shape207 #expect(again.id != c.id)208 let copy = try other.messages(in: again.id)209 #expect(copy.count == original.count && Set(copy.map(\.id)).isDisjoint(with: Set(original.map(\.id))))210 #expect(try other.activeBranch(again.id).map(\.content) == other.activeBranch(c.id).map(\.content))211 #expect(throws: ConversationStoreError.self) { try other.importJSON(Data("{\"x\":1}".utf8)) }212 }213214 @Test func syncLastWriterWinsAndTombstones() async throws {215 let (a, c, m) = try seeded()216 let folder = try a.save(ChatFolder(name: "Synced"))217 let template = try a.save(PromptTemplate(name: "T", kind: .system, body: "b"))218 var prefs = try a.preferences(); prefs.accent = "teal"; try a.savePreferences(prefs)219 let b = try ConversationStore.inMemory()220221 let first = try await a.changes(since: nil)222 #expect(!first.contains { $0.kind == .template && $0.id != template.id.uuidString }) // built-ins are not shipped223 #expect(try await b.apply(first) == first.count)224 #expect(try b.activeBranch(c.id).map(\.content) == m.map(\.content))225 #expect(try b.folders().map(\.id) == [folder.id])226 #expect(try b.preferences().accent == "teal")227 #expect(try b.search("roasting").count == 1)228 #expect(try await b.apply(first) == 0) // idempotent229230 // B renames later than A's last write → B wins on A231 let mark = Date()232 var renamed = try #require(try b.conversation(c.id)); renamed.title = "Renamed on B"233 try b.saveConversation(renamed)234 let delta = try await b.changes(since: mark)235 #expect(delta.map(\.kind) == [.conversation])236 #expect(try await a.apply(delta) == 1)237 #expect(try a.conversation(c.id)?.title == "Renamed on B")238 #expect(try a.search("renamed").first?.kind == .title)239240 // an older remote version never overwrites a newer local one241 var stale = delta[0]; stale.updatedAt = Date(timeIntervalSince1970: 1000)242 #expect(try await a.apply([stale]) == 0)243244 // deletions travel as tombstones245 let beforeDelete = Date()246 try a.deleteMessageSubtree(m[2].id)247 try a.delete(ChatFolder.self, id: folder.id)248 let deletions = try await a.changes(since: beforeDelete)249 #expect(deletions.filter { $0.kind == .message && $0.isDeleted }.count == 2)250 _ = try await b.apply(deletions)251 #expect(try b.messages(in: c.id).count == 2)252 #expect(try b.folders().isEmpty)253 #expect(try b.search("roasting").isEmpty)254255 let beforeConversationDelete = Date()256 try a.deleteConversation(c.id)257 _ = try await b.apply(try await a.changes(since: beforeConversationDelete))258 #expect(try b.conversation(c.id) == nil && b.search("ethiopien").isEmpty)259 #expect(try b.purgeTombstones(olderThan: Date().addingTimeInterval(60)) > 0)260 }261262 @Test func storeOnDiskReopens() throws {263 let dir = FileManager.default.temporaryDirectory.appendingPathComponent("lc-conv-\(UUID().uuidString)")264 defer { try? FileManager.default.removeItem(at: dir) }265 let url = dir.appendingPathComponent("db.sqlite")266 var id: UUID?267 do {268 let store = try ConversationStore(url: url)269 let c = try store.saveConversation(Conversation(title: "Persisted"))270 try store.appendMessage(ChatMessage(conversationID: c.id, parentID: nil, role: .user, content: "kept on disk"))271 id = c.id272 }273 let reopened = try ConversationStore(url: url)274 #expect(try reopened.conversation(#require(id))?.title == "Persisted")275 #expect(try reopened.search("disk").count == 1)276 #expect(try reopened.templates().filter(\.isBuiltIn).count == BuiltInTemplates.all.count) // seeded once277 }278279 @Test func attachmentsAreContentAddressed() throws {280 let dir = FileManager.default.temporaryDirectory.appendingPathComponent("lc-att-\(UUID().uuidString)")281 defer { try? FileManager.default.removeItem(at: dir) }282 let files = AttachmentStore(directory: dir)283 let a = try files.store(data: Data("hello".utf8), fileName: "a.TXT", mimeType: "text/plain", kind: .document, extractedText: "hello")284 let b = try files.store(data: Data("hello".utf8), fileName: "b.txt", mimeType: "text/plain", kind: .document)285 let img = try files.store(data: Data([1, 2, 3]), fileName: "p.png", mimeType: "image/png", kind: .image)286 #expect(a.storedName == b.storedName && a.storedName.hasSuffix(".txt") && a.byteCount == 5)287 #expect(files.data(for: a) == Data("hello".utf8))288289 let store = try ConversationStore.inMemory()290 let c = try store.saveConversation(Conversation())291 try store.appendMessage(ChatMessage(conversationID: c.id, parentID: nil, role: .user, content: "see file", attachments: [a]))292 let referenced = try store.referencedAttachmentNames()293 #expect(referenced == [a.storedName])294 #expect(files.removeOrphans(referenced: referenced) == 1)295 #expect(files.data(for: img) == nil && files.data(for: a) != nil)296 }297}298