Repository Public
1// SP Notes Core2// File: Tests/SPNotesStoreTests/EngineTests.swift3// Description: Engine-level tests: pragmas, migrations, rollback, persistence across reopen, read-only second4// connection, change notification, meta, wipe.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Testing10@testable import SPNotesStore1112@Suite struct EngineTests {13 @Test func durabilityPragmas() async throws {14 let temp = TempDatabase()15 let store = try NotesStore(url: temp.url)16 #expect(try await store.debugValue("PRAGMA journal_mode") == "wal")17 #expect(try await store.debugValue("PRAGMA synchronous") == "2") // FULL18 #expect(try await store.debugValue("PRAGMA foreign_keys") == "1")19 #expect(Int(try await store.debugValue("PRAGMA busy_timeout") ?? "0")! >= 5000)20 #expect(try await store.debugValue("PRAGMA user_version") == String(NotesStore.schemaVersion))21 #expect(try await store.integrityCheck().isEmpty)22 try await store.checkpoint()23 }2425 @Test func migrationFromUserVersionZero() async throws {26 let temp = TempDatabase()27 do {28 // A file that exists but was never migrated.29 let raw = try Database(path: temp.url.path, readOnly: false)30 #expect(try raw.scalar("PRAGMA user_version") == 0)31 raw.close()32 }33 let store = try NotesStore(url: temp.url)34 #expect(try await store.debugValue("PRAGMA user_version") == String(NotesStore.schemaVersion))35 let page = try await store.createPage(draft("After migration", "works"))36 #expect(try await store.page(id: page.id)?.title == "After migration")37 #expect(try await store.integrityCheck().isEmpty)38 await store.close()39 // Reopening an up-to-date file migrates nothing and keeps the data.40 let again = try NotesStore(url: temp.url)41 #expect(try await again.pageCount() == 1)42 }4344 @Test func failedMigrationStepLeavesNoPartialState() throws {45 let temp = TempDatabase()46 let raw = try Database(path: temp.url.path, readOnly: false)47 struct Boom: Error {}48 let steps: [Schema.Migration] = [49 { try $0.exec("CREATE TABLE a(x)") },50 { try $0.exec("CREATE TABLE b(x)"); try $0.run("INSERT INTO a VALUES(1)"); throw Boom() },51 ]52 #expect(throws: Boom.self) { try Schema.migrate(raw, steps: steps) }53 #expect(try raw.scalar("PRAGMA user_version") == 1)54 #expect(try raw.scalar("SELECT count(*) FROM sqlite_master WHERE name='b'") == 0)55 #expect(try raw.scalar("SELECT count(*) FROM a") == 0)56 // A later run resumes at the failed step.57 let fixed: [Schema.Migration] = [steps[0], { try $0.exec("CREATE TABLE b(x)") }]58 try Schema.migrate(raw, steps: fixed)59 #expect(try raw.scalar("PRAGMA user_version") == 2)60 }6162 @Test func newerSchemaIsRefused() throws {63 let temp = TempDatabase()64 let raw = try Database(path: temp.url.path, readOnly: false)65 try raw.exec("PRAGMA user_version=999")66 raw.close()67 #expect(throws: StoreError.self) { _ = try NotesStore(url: temp.url) }68 }6970 @Test func nestedTransactionRollsBackOnlyItsLevel() throws {71 let raw = try Database(path: ":memory:", readOnly: false)72 try raw.exec("CREATE TABLE t(x)")73 struct Boom: Error {}74 try raw.transaction {75 try raw.run("INSERT INTO t VALUES(1)")76 try? raw.transaction {77 try raw.run("INSERT INTO t VALUES(2)")78 throw Boom()79 }80 try raw.run("INSERT INTO t VALUES(3)")81 }82 #expect(try raw.query("SELECT x FROM t ORDER BY x") { $0.int(0) } == [1, 3])83 #expect(throws: Boom.self) {84 try raw.transaction {85 try raw.run("INSERT INTO t VALUES(4)")86 throw Boom()87 }88 }89 #expect(try raw.scalar("SELECT count(*) FROM t") == 2)90 // Typed round trip, including an empty blob and NULLs.91 try raw.exec("CREATE TABLE v(i, d, s, b, n)")92 try raw.run("INSERT INTO v VALUES(?, ?, ?, ?, ?)", [Int64.max, 1.5, "été ✓", Data(), SQLValue.null])93 let row = try raw.first("SELECT i, d, s, b, n, typeof(b) FROM v") { ($0.int(0), $0.double(1), $0.string(2), $0.optData(3), $0.optString(4), $0.string(5)) }94 #expect(row?.0 == Int64.max && row?.1 == 1.5 && row?.2 == "été ✓" && row?.3 == Data() && row?.4 == nil && row?.5 == "blob")95 #expect(throws: StoreError.self) { try raw.run("INSERT INTO v VALUES(?, ?)", [1]) }96 #expect(throws: StoreError.self) { try raw.exec("THIS IS NOT SQL") }97 }9899 @Test func uncommittedWorkOfADeadProcessIsNotVisible() async throws {100 let temp = TempDatabase()101 let store = try NotesStore(url: temp.url)102 try await store.setMeta("committed", "yes")103 do {104 // Another connection dies in the middle of a transaction (no COMMIT).105 let dying = try Database(path: temp.url.path, readOnly: false)106 try dying.exec("BEGIN IMMEDIATE")107 try dying.run("INSERT INTO meta(key, value) VALUES('half', 'written')")108 try dying.run("DELETE FROM meta WHERE key='committed'")109 dying.close()110 }111 #expect(try await store.allMeta() == ["committed": "yes"])112 try await store.setMeta("after", "ok") // the write lock was released113 await store.close()114 let reopened = try NotesStore(url: temp.url)115 #expect(try await reopened.allMeta() == ["committed": "yes", "after": "ok"])116 #expect(try await reopened.integrityCheck().isEmpty)117 }118119 @Test func throwingBatchLeavesNoPartialState() async throws {120 let store = try NotesStore.inMemory()121 var events: [StoreChange] = []122 let stream = await store.changes()123 // The third draft reuses the first id: the whole batch must vanish, index included.124 await #expect(throws: StoreError.self) {125 try await store.createPages([draft("One", "alpha", id: "a"), draft("Two", "alpha", tags: ["x"]), draft("Dup", "alpha", id: "a")])126 }127 #expect(try await store.pageCount() == 0)128 #expect(try await store.search(query: "alpha").isEmpty)129 #expect(try await store.tags().isEmpty)130 #expect(try await store.debugValue("SELECT count(*) FROM pages_fts") == "0")131 // A failing single mutation: invalid properties on an existing page.132 let page = try await store.createPage(draft("Keep", "body"))133 await #expect(throws: StoreError.self) { try await store.setProperties(id: page.id, properties: Data("[1,2]".utf8)) }134 let after = try await store.must(page.id)135 #expect(after == page)136 // Only the successful creation was published.137 await store.close()138 for await event in stream { events.append(event) }139 #expect(events == [.pages([page.id])])140 }141142 @Test func everythingSurvivesCloseAndReopen() async throws {143 let temp = TempDatabase()144 var store = try NotesStore(url: temp.url)145 try await store.applyRemote(pages: [remote("synced", "Synced note", "server text", version: 4, tags: ["work"])],146 tags: [RemoteTag(name: "work", color: "#f00")], cursor: 77)147 try await store.edit("synced", text: "local edit not pushed")148 try await store.setPinned(id: "synced", true)149 try await store.setTags(id: "synced", names: ["Work", "#Urgent"])150 let offline = try await store.createPage(draft("Offline note", "written without network", tags: ["idea"]))151 let child = try await store.createPage(draft("Child", "c", parent: offline.id))152 try await store.trash(id: child.id)153 let op = try await store.enqueueOp(kind: OpKind.triage, targetId: "synced", payload: Data(#"{"a":1}"#.utf8))154 try await store.failOp(id: op.id, error: "offline", now: epoch)155 try await store.recordConflict(pageId: "synced", server: ServerCopy(version: 5, title: "Server", content: doc("srv"), properties: Data("{}".utf8), updatedAt: epoch))156 try await store.snapshot(pageId: "synced", reason: "manual")157 let attachment = try await store.addLocalAttachment(pageId: "synced", filename: "a.pdf", mime: "application/pdf", size: 10, sha256: "abc", localPath: "aa/a.pdf")158 let reminder = try await store.createReminder(pageId: "synced", title: "Call", dueAt: epoch)159 try await store.setMeta("account.email", "x@example.org")160 let before = try await store.must("synced")161 let beforeOffline = try await store.must(offline.id)162 await store.close()163 await #expect(throws: StoreError.closed) { try await store.pageCount() }164165 store = try NotesStore(url: temp.url)166 let after = try await store.must("synced")167 #expect(after == before)168 #expect(after.dirtyContent && after.dirtyMeta == [.pinned] && after.tagsDirty && after.syncState == .conflict)169 #expect(after.tags == ["urgent", "work"] && after.serverVersion == 4 && after.plainText == "local edit not pushed")170 #expect(try await store.pageBase(id: "synced") == PageBase(title: "Synced note", content: doc("server text"), properties: Data("{}".utf8), version: 4))171 #expect(try await store.must(offline.id) == beforeOffline)172 #expect(try await store.must(offline.id).needsCreate)173 #expect(try await store.must(child.id).deletedAt != nil)174 let ops = try await store.queuedOps()175 #expect(ops.map(\.kind) == [OpKind.trash, OpKind.triage])176 #expect(ops[1].attempts == 1 && ops[1].lastError == "offline" && ops[1].payload == Data(#"{"a":1}"#.utf8))177 #expect(try await store.conflict(pageId: "synced")?.server.version == 5)178 #expect(try await store.localVersions(pageId: "synced").count == 1)179 #expect(try await store.attachment(id: attachment.id)?.state == .pendingUpload)180 #expect(try await store.reminder(id: reminder.id)?.dirty == true)181 #expect(try await store.meta("account.email") == "x@example.org")182 #expect(try await store.syncCursor() == 77)183 #expect(try await store.search(query: "pushed").map(\.id) == ["synced"])184 #expect(try await store.pagesNeedingPush(includeConflicts: true).map(\.page.id).contains("synced"))185 #expect(try await store.integrityCheck().isEmpty)186 }187188 @Test func readOnlyConnectionSeesCommittedDataWhileWriterWrites() async throws {189 let temp = TempDatabase()190 // A read-only open never creates the file.191 #expect(throws: StoreError.self) { _ = try NotesStore(url: temp.url, readOnly: true) }192 let writer = try NotesStore(url: temp.url)193 let first = try await writer.createPage(draft("First", "one"))194 let reader = try NotesStore(url: temp.url, readOnly: true)195 #expect(try await reader.page(id: first.id)?.title == "First")196197 // The writer keeps committing while the reader keeps reading: every read sees a consistent, growing set.198 let writing = Task {199 for index in 0..<200 { try await writer.createPage(draft("Note \(index)", "body \(index)")) }200 }201 var seen = 1202 for _ in 0..<50 {203 let count = try await reader.pageCount()204 #expect(count >= seen)205 seen = count206 _ = try await reader.summaries(list: .all, limit: 20)207 _ = try await reader.search(query: "body")208 }209 try await writing.value210 #expect(try await reader.pageCount() == 201)211 #expect(try await reader.search(query: "body 199", limit: 5).count >= 1)212213 // The read-only connection refuses every write.214 await #expect(throws: StoreError.readOnly) { try await reader.createPage(draft("No")) }215 await #expect(throws: StoreError.readOnly) { try await reader.setMeta("k", "v") }216 await #expect(throws: StoreError.readOnly) { try await reader.nextOps() }217 #expect(try await reader.pageCount() == 201)218219 // A second writer (share extension) can write to the same file.220 let extensionStore = try NotesStore(url: temp.url)221 let shared = try await extensionStore.createPage(draft("Shared", "from the share extension"))222 #expect(try await writer.page(id: shared.id)?.title == "Shared")223 #expect(try await reader.page(id: shared.id)?.needsCreate == true)224 }225226 @Test func changeStreamHasManySubscribers() async throws {227 let store = try NotesStore.inMemory()228 let one = await store.changes()229 let two = await store.changes()230 let page = try await store.createPage(draft("A", "a", tags: ["t"]))231 try await store.trash(id: page.id)232 try await store.recordConflict(pageId: page.id, server: ServerCopy(version: 1, title: "", content: Data(), properties: Data("{}".utf8), updatedAt: epoch))233 let attachment = try await store.addLocalAttachment(pageId: page.id, filename: "f", mime: "m", size: 1, sha256: nil, localPath: "p")234 try await store.createReminder(title: "r")235 try await store.setMeta("k", "v")236 await store.noteExternalChange()237 await store.close()238 var first: [StoreChange] = []239 for await event in one { first.append(event) }240 var second: [StoreChange] = []241 for await event in two { second.append(event) }242 let expected: [StoreChange] = [243 .pages([page.id]), .tags, .pages([page.id]), .queue, .pages([page.id]), .conflicts,244 .attachments([attachment.id]), .reminders, .meta("k"), .reset,245 ]246 #expect(first == expected)247 #expect(second == expected)248 }249250 @Test func metaAndWipe() async throws {251 let store = try NotesStore.inMemory()252 #expect(try await store.syncCursor() == 0)253 try await store.setSyncCursor(42)254 try await store.setMeta("a", "1")255 try await store.setMeta("a", "2")256 #expect(try await store.allMeta() == ["a": "2", NotesStore.syncCursorKey: "42"])257 try await store.setMeta("a", nil)258 #expect(try await store.meta("a") == nil)259 try await store.createPage(draft("X", "findme", tags: ["t"]))260 try await store.enqueueOp(kind: "k")261 try await store.wipeAll()262 #expect(try await store.pageCount() == 0)263 #expect(try await store.opCount() == 0)264 #expect(try await store.tags().isEmpty)265 #expect(try await store.allMeta().isEmpty)266 #expect(try await store.search(query: "findme").isEmpty)267 }268}269