Repository Public
1// SP Notes Core2// File: Tests/SPNotesStoreTests/QueueHistoryTests.swift3// Description: Ops queue (order, leases, backoff), attachments lifecycle and eviction, local versions4// (dedup, pruning, restore) and reminders.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Testing10@testable import SPNotesStore1112@Suite struct QueueHistoryTests {13 @Test func opsAreFifoWithLeasesAndBackoff() async throws {14 let store = try NotesStore.inMemory()15 let t0 = Date(timeIntervalSince1970: 2_000_000_000)16 let a = try await store.enqueueOp(kind: OpKind.createTask, targetId: "t1", payload: Data(#"{"title":"x"}"#.utf8))17 let b = try await store.enqueueOp(kind: OpKind.updateTask, targetId: "t1")18 let c = try await store.enqueueOp(kind: OpKind.triage, targetId: "p9")19 let d = try await store.enqueueOp(kind: "custom")20 #expect(try await store.opCount() == 4)21 #expect([a, b, c, d].map(\.id) == [a.id, a.id + 1, a.id + 2, a.id + 3])22 let keys = [a, b, c, d].map(\.idempotencyKey)23 #expect(Set(keys).count == 4)24 for key in keys { #expect(key.wholeMatch(of: /[A-Za-z0-9_-]{8,100}/) != nil) }25 #expect(a.payload == Data(#"{"title":"x"}"#.utf8) && a.attempts == 0 && a.lastError == nil && d.targetId == nil)2627 // Due ops come out oldest first and are leased: a second call does not hand them out again.28 #expect(try await store.nextOps(limit: 3, now: t0).map(\.id) == [a.id, b.id, c.id])29 #expect(try await store.nextOps(now: t0).map(\.id) == [d.id])30 #expect(try await store.nextOps(now: t0).isEmpty)31 // A lease expires (the engine died): the ops are handed out again.32 #expect(try await store.nextOps(now: t0.addingTimeInterval(121)).count == 4)33 try await store.releaseAllOpLeases()3435 // Failure: exponential backoff, capped at 15 minutes; later ops of the SAME target wait behind it.36 let failed = try #require(try await store.failOp(id: a.id, error: "HTTP 503", now: t0))37 #expect(failed.attempts == 1 && failed.lastError == "HTTP 503" && failed.nextAttemptAt == t0.addingTimeInterval(5))38 #expect(try await store.nextOps(now: t0).map(\.id) == [c.id, d.id]) // b is held behind a39 try await store.releaseOp(id: c.id)40 try await store.releaseOp(id: d.id)41 #expect(try await store.nextOps(now: t0.addingTimeInterval(5)).map(\.id) == [a.id, b.id, c.id, d.id])42 var delays: [TimeInterval] = []43 for attempt in 2...12 {44 let op = try #require(try await store.failOp(id: a.id, error: "again", now: t0))45 #expect(op.attempts == attempt)46 delays.append(op.nextAttemptAt.timeIntervalSince(t0))47 }48 #expect(delays == [10, 20, 40, 80, 160, 320, 640, 900, 900, 900, 900])49 #expect((1...40).map { NotesStore.backoffDelay(attempts: $0) }.max() == 900)5051 // Completion removes; unknown ids are ignored.52 try await store.completeOp(id: a.id)53 try await store.completeOp(id: 9_999)54 #expect(try await store.failOp(id: 9_999, error: "x") == nil)55 #expect(try await store.queuedOps().map(\.id) == [b.id, c.id, d.id])56 try await store.releaseAllOpLeases()57 #expect(try await store.nextOps(now: t0).first?.id == b.id)58 await #expect(throws: StoreError.self) { try await store.enqueueOp(kind: "") }59 }6061 @Test func attachmentsLifecycleAndEviction() async throws {62 let store = try NotesStore.inMemory()63 let page = try await store.createPage(draft("With files", "b"))64 let one = try await store.addLocalAttachment(pageId: page.id, filename: "one.pdf", mime: "application/pdf", size: 1_000, sha256: "s1", localPath: "one/one.pdf", pageCount: 2)65 let two = try await store.addLocalAttachment(id: "TWO", pageId: page.id, filename: "two.png", mime: "image/png", size: 2_000, sha256: nil, localPath: "two/two.png")66 #expect(one.state == .pendingUpload && !one.uploaded && one.localPath == "one/one.pdf" && one.pageCount == 2 && two.id == "two")67 #expect(try await store.attachments(pageId: page.id).map(\.id).sorted() == [one.id, "two"].sorted())68 #expect(try await store.pendingUploads().count == 2)69 await #expect(throws: StoreError.self) {70 try await store.addLocalAttachment(id: "two", pageId: page.id, filename: "x", mime: "m", size: 1, sha256: nil, localPath: "x")71 }7273 // Upload: in flight → failed (retried) → uploaded.74 try await store.markAttachmentUploading(id: one.id)75 #expect(try await store.pendingUploads().map(\.id) == ["two"])76 try await store.markAttachmentFailed(id: one.id, error: "timeout")77 var record = try #require(try await store.attachment(id: one.id))78 #expect(record.state == .failed && record.uploadAttempts == 1 && record.lastError == "timeout")79 #expect(try await store.pendingUploads().count == 2)80 try await store.markAttachmentUploading(id: one.id)81 try await store.resetTransientAttachmentStates() // crash during the upload82 #expect(try await store.attachment(id: one.id)?.state == .pendingUpload)83 try await store.markAttachmentUploaded(id: one.id, sha256: "server-hash")84 record = try #require(try await store.attachment(id: one.id))85 #expect(record.state == .local && record.uploaded && record.lastError == nil && record.sha256 == "server-hash" && record.size == 1_000)86 #expect(try await store.pendingUploads().map(\.id) == ["two"])8788 // Deleting a file that was never uploaded leaves no op; deleting an uploaded one queues deleteAttachment.89 #expect(try await store.markAttachmentDeleted(id: "two") == "two/two.png")90 #expect(try await store.attachment(id: "two") == nil)91 #expect(try await store.opCount() == 0)92 #expect(try await store.markAttachmentDeleted(id: one.id) == "one/one.pdf")93 #expect(try await store.attachment(id: one.id)?.deletedAt != nil)94 #expect(try await store.attachments(pageId: page.id).isEmpty)95 #expect(try await store.queuedOps().map { "\($0.kind):\($0.targetId ?? "")" } == ["deleteAttachment:\(one.id)"])96 #expect(try await store.markAttachmentDeleted(id: one.id) == nil)97 #expect(try await store.opCount() == 1)98 await #expect(throws: StoreError.notFound("attachment nope")) { try await store.markAttachmentUploading(id: "nope") }99100 // Downloads and eviction: least recently used first, never keepOffline, never a pending upload.101 let t = { (s: Double) in epoch.addingTimeInterval(s) }102 try await store.applyRemote(attachments: ["r1", "r2", "r3", "r4"].map {103 RemoteAttachment(id: $0, pageId: page.id, filename: "\($0).bin", mime: "application/octet-stream", size: 10, createdAt: epoch)104 })105 #expect(try await store.evictionCandidates().isEmpty) // nothing on the device yet106 try await store.markAttachmentDownloading(id: "r1")107 #expect(try await store.attachment(id: "r1")?.state == .downloading)108 try await store.markAttachmentFailed(id: "r1", error: "404")109 #expect(try await store.attachment(id: "r1")?.state == .remote)110 #expect(try await store.attachment(id: "r1")?.uploadAttempts == 0)111 try await store.markAttachmentDownloaded(id: "r1", localPath: "r1/f", at: t(30))112 try await store.markAttachmentDownloaded(id: "r2", localPath: "r2/f", at: t(10))113 try await store.markAttachmentDownloaded(id: "r3", localPath: "r3/f", at: t(20))114 try await store.markAttachmentDownloaded(id: "r4", localPath: "r4/f", at: t(5))115 try await store.setAttachmentKeepOffline(id: "r4", true)116 try await store.addLocalAttachment(id: "pend", pageId: page.id, filename: "p", mime: "m", size: 1, sha256: nil, localPath: "pend/p")117 #expect(try await store.evictionCandidates().map(\.id) == ["r2", "r3", "r1"])118 try await store.touchAttachment(id: "r2", at: t(40))119 #expect(try await store.evictionCandidates(limit: 2).map(\.id) == ["r3", "r1"])120 try await store.markAttachmentEvicted(id: "r3")121 record = try #require(try await store.attachment(id: "r3"))122 #expect(record.state == .remote && record.localPath == nil)123 #expect(try await store.evictionCandidates().map(\.id) == ["r1", "r2"])124 }125126 @Test func attachmentsWantedOffline() async throws {127 let store = try NotesStore.inMemory()128 try await store.applyRemote(pages: [remote("p", "P", "b"), remote("t", "Trashed", "b", deletedAt: epoch)])129 let t = { (s: Double) in epoch.addingTimeInterval(s) }130 func att(_ id: String, page: String = "p", name: String, mime: String, size: Int64, at: Double, deleted: Bool = false) -> RemoteAttachment {131 RemoteAttachment(id: id, pageId: page, filename: name, mime: mime, size: size, createdAt: t(at), deletedAt: deleted ? t(at) : nil)132 }133 try await store.applyRemote(attachments: [134 att("img-small", name: "a.png", mime: "image/png", size: 100, at: 1),135 att("img-big", name: "b.jpg", mime: "image/jpeg", size: 5_000, at: 2),136 att("drawing", name: "sketch.pkdrawing", mime: "application/octet-stream", size: 200, at: 3),137 att("pdf", name: "c.pdf", mime: "application/pdf", size: 50, at: 4),138 att("pdf-keep", name: "d.pdf", mime: "application/pdf", size: 9_000, at: 5),139 att("img-deleted", name: "e.png", mime: "image/png", size: 10, at: 6, deleted: true),140 att("img-trashed-page", page: "t", name: "f.png", mime: "image/png", size: 10, at: 7),141 att("img-local", name: "g.png", mime: "image/png", size: 10, at: 8),142 att("img-failed", name: "h.png", mime: "image/png", size: 10, at: 9),143 ])144 try await store.setAttachmentKeepOffline(id: "pdf-keep", true)145 try await store.markAttachmentDownloaded(id: "img-local", localPath: "g/g.png")146 try await store.markAttachmentDownloading(id: "img-failed")147 try await store.markAttachmentFailed(id: "img-failed", error: "timeout") // a failed download is wanted again148 try await store.addLocalAttachment(id: "mine", pageId: "p", filename: "new.png", mime: "image/png", size: 10, sha256: nil, localPath: "m/new.png")149 try await store.markAttachmentFailed(id: "mine", error: "upload failed") // a failed UPLOAD is not a download150 #expect(try await store.attachmentsWantedOffline(maxSize: 1_000).map(\.id) == ["img-failed", "pdf-keep", "drawing", "img-small"])151 #expect(try await store.attachmentsWantedOffline(maxSize: 10_000).map(\.id) == ["img-failed", "pdf-keep", "drawing", "img-big", "img-small"])152 #expect(try await store.attachmentsWantedOffline(maxSize: 0).map(\.id) == ["pdf-keep"])153 #expect(try await store.attachmentsWantedOffline(maxSize: 1_000, limit: 2).map(\.id) == ["img-failed", "pdf-keep"])154 try await store.markAttachmentDownloading(id: "drawing") // in flight: not listed155 try await store.trash(id: "p")156 #expect(try await store.attachmentsWantedOffline(maxSize: 10_000).isEmpty)157 }158159 @Test func localVersionsDedupPruneAndRestore() async throws {160 let store = try NotesStore.inMemory()161 try await store.applyRemote(pages: [remote("p", "Title", "v0")])162 let first = try #require(try await store.snapshot(pageId: "p", reason: "open"))163 #expect(first.title == "Title" && first.reason == "open" && first.wordCount == 1)164 #expect(try await store.snapshot(pageId: "p", reason: "again") == nil) // identical to the latest165 try await store.edit("p", title: "Title 2", text: "v1 text")166 let second = try #require(try await store.snapshot(pageId: "p", reason: "autosave"))167 #expect(try await store.localVersions(pageId: "p").map(\.id) == [second.id, first.id]) // newest first168 let full = try #require(try await store.localVersion(id: first.id))169 #expect(full.content == doc("v0") && full.title == "Title" && full.plainText == "v0")170 #expect(try await store.localVersion(id: "nope") == nil)171172 // Restore: the current state is snapshotted first, then the old one comes back as a local edit.173 try await store.edit("p", text: "v2 unsaved in history")174 let before = try await store.must("p")175 let restored = try await store.restoreVersion(id: first.id)176 #expect(restored.title == "Title" && restored.content == doc("v0") && restored.plainText == "v0" && restored.wordCount == 1)177 #expect(restored.dirtyContent && restored.editSeq == before.editSeq + 1 && restored.syncState == .pending)178 let versions = try await store.localVersions(pageId: "p")179 #expect(versions.map(\.reason) == ["before-restore", "autosave", "open"])180 #expect(try await store.localVersion(id: versions[0].id)?.content == doc("v2 unsaved in history"))181 #expect(try await store.search(query: "v0").map(\.id) == ["p"])182 #expect(try await store.search(query: "unsaved").isEmpty)183 #expect(try await store.pageBase(id: "p")?.content == doc("v0")) // base = last synced copy184 await #expect(throws: StoreError.self) { try await store.restoreVersion(id: "nope") }185186 // Pruning keeps the newest 100 per page, and only touches that page.187 try await store.applyRemote(pages: [remote("other", "Other", "o")])188 try await store.snapshot(pageId: "other", reason: "keep")189 for index in 0..<120 {190 try await store.edit("p", text: "revision \(index)")191 try await store.snapshot(pageId: "p", reason: "r\(index)")192 }193 let kept = try await store.localVersions(pageId: "p")194 #expect(kept.count == NotesStore.maxLocalVersionsPerPage)195 #expect(kept.first?.reason == "r119" && kept.last?.reason == "r20")196 #expect(try await store.localVersions(pageId: "other").count == 1)197 }198199 @Test func remindersCrud() async throws {200 let store = try NotesStore.inMemory()201 let page = try await store.createPage(draft("P"))202 let t = { (s: Double) in epoch.addingTimeInterval(s) }203 let r1 = try await store.createReminder(pageId: page.id, title: "Later", dueAt: t(200))204 let r2 = try await store.createReminder(pageId: page.id, title: "Sooner", dueAt: t(100))205 let r3 = try await store.createReminder(title: "No date")206 let mirrored = try await store.createReminder(id: "srv", title: "From server", dueAt: t(50), serverTaskId: "task-1")207 #expect(r1.dirty && !r1.done && r1.serverTaskId == nil && r1.editSeq == 1 && !mirrored.dirty)208 #expect(try await store.reminders(pageId: page.id).map(\.id) == [r2.id, r1.id])209 #expect(try await store.upcomingReminders().map(\.id) == ["srv", r2.id, r1.id])210 #expect(try await store.upcomingReminders(from: t(100), limit: 1).map(\.id) == [r2.id])211 #expect(Set(try await store.dirtyReminders().map(\.id)) == [r1.id, r2.id, r3.id])212213 // Pushed, then edited during the acknowledgement of an older state.214 try await store.markReminderSynced(id: r1.id, serverTaskId: "task-9", pushedEditSeq: 1)215 #expect(try await store.reminder(id: r1.id)?.dirty == false)216 #expect(try await store.reminder(id: r1.id)?.serverTaskId == "task-9")217 let done = try await store.updateReminder(id: r1.id, done: true)218 #expect(done.done && done.dirty && done.editSeq == 2)219 #expect(try await store.updateReminder(id: r1.id, done: true).editSeq == 2) // no-op220 let cleared = try await store.updateReminder(id: r1.id, title: "Renamed", dueAt: .some(nil))221 #expect(cleared.dueAt == nil && cleared.title == "Renamed" && cleared.editSeq == 3)222 try await store.markReminderSynced(id: r1.id, serverTaskId: nil, pushedEditSeq: 2)223 #expect(try await store.reminder(id: r1.id)?.dirty == true)224 #expect(try await store.reminder(id: r1.id)?.serverTaskId == "task-9")225 #expect(try await store.upcomingReminders().map(\.id) == ["srv", r2.id])226227 // Deletion: local-only rows vanish; rows known to the server wait as deleted + dirty.228 try await store.deleteReminder(id: r3.id)229 #expect(try await store.reminder(id: r3.id) == nil)230 try await store.deleteReminder(id: "srv")231 let tombstone = try #require(try await store.reminder(id: "srv"))232 #expect(tombstone.deleted && tombstone.dirty)233 #expect(try await store.upcomingReminders().map(\.id) == [r2.id])234 try await store.markReminderSynced(id: "srv", serverTaskId: nil, pushedEditSeq: tombstone.editSeq)235 #expect(try await store.reminder(id: "srv") == nil)236 await #expect(throws: StoreError.self) { try await store.deleteReminder(id: "nope") }237238 // Purging the page removes its local reminders and flags the server-known ones for deletion.239 try await store.purge(id: page.id)240 #expect(try await store.reminder(id: r2.id) == nil)241 let orphan = try #require(try await store.reminder(id: r1.id))242 #expect(orphan.deleted && orphan.dirty && orphan.pageId == nil)243 }244}245