Repository Public
1// SP Notes Core2// File: Tests/SPNotesSyncTests/TrashAttachmentQueueTests.swift3// Description: Scenario tests of the synchronisation engine, part 4: trash and purge across devices, attachments (upload order,4// automatic and explicit download, deletions, missing files), queued purge needing re-authentication, reminders5// and tasks, inbox triage.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesAPI11import SPNotesStore12import Testing13@testable import SPNotesSync14@testable import SPNotesTestSupport1516@Suite("Trash and purge")17struct TrashPurgeTests {18 @Test("a note trashed on one device while the other edits it: the edit waits, nothing is lost, a restore lets it through", arguments: ["B restores", "A restores"])19 func trashVersusEdit(who: String) async throws {20 let world = World()21 let fake = world.fake22 let a = try world.device("A")23 let b = try world.device("B")24 let id = try await a.create("Note", [("a", "original")])25 #expect(await a.sync() == .completed)26 #expect(await b.sync() == .completed)2728 _ = try await a.store.trash(id: id)29 #expect(await a.sync() == .completed)30 try await b.edit(id, block: "a", text: "edited on B")31 _ = try await b.store.setPinned(id: id, true)32 #expect(await b.sync() == .completed)33 #expect(await b.sync() == .completed)3435 let waiting = try await b.page(id)36 #expect(waiting.deletedAt != nil, "B sees the note in the trash")37 #expect(waiting.isDirty && waiting.syncError == nil, "its edit waits, without an error")38 #expect(try await b.texts(id) == ["edited on B"])39 #expect(fake.page(id)?.text == "original")40 #expect(fake.page(id)?.deletedAt != nil, "the trash was not undone behind the user's back")41 #expect(await b.engine.currentStatus().pending == 1)42 #expect(try await b.store.conflicts().isEmpty)4344 if who == "B restores" {45 _ = try await b.store.restore(id: id)46 #expect(await b.sync() == .completed) // restore, then the edit, in one call47 } else {48 _ = try await a.store.restore(id: id)49 #expect(await a.sync() == .completed)50 #expect(await b.sync() == .completed)51 }52 #expect(fake.page(id)?.deletedAt == nil)53 #expect(fake.page(id)?.text == "edited on B")54 #expect(fake.page(id)?.pinned == true)55 #expect(await a.sync() == .completed)56 #expect(await b.sync() == .completed)57 try await expectMirror(a)58 try await expectMirror(b)59 await world.closeAll()60 }6162 @Test("a trashed note moved (unsynced) into a trashed folder that is then restored: live here, so restored on the server too")63 func restoreThroughUnsyncedMove() async throws {64 let world = World()65 let fake = world.fake66 let a = try world.device("A")67 let b = try world.device("B")68 let folder = try await a.create("Folder", properties: #"{"folder":true}"#)69 let note = try await a.create("Note", [("a", "x")])70 #expect(await a.sync() == .completed)71 _ = try await a.store.trash(id: note)72 _ = try await a.store.trash(id: folder)73 #expect(await a.sync() == .completed)74 #expect(await b.sync() == .completed)75 // on B: the note is dragged into the folder inside the trash, then the folder is restored — the note comes out with it76 _ = try await b.store.move(id: note, parentId: folder)77 _ = try await b.store.restore(id: folder)78 #expect(try await b.page(note).deletedAt == nil)79 // the server only knows "restore the folder": for it the note is not inside, and stays in the trash80 #expect(await b.sync() == .completed)81 #expect(fake.page(folder)?.deletedAt == nil)82 #expect(fake.page(note)?.deletedAt == nil, "what the user sees wins: the note is restored on the server")83 #expect(fake.page(note)?.parentId == folder)84 #expect(await b.sync() == .completed)85 #expect(await a.sync() == .completed)86 try await expectMirror(a)87 try await expectMirror(b)88 await world.closeAll()89 }9091 @Test("a note with unsynced text that is trashed then purged elsewhere comes back under the same id")92 func purgeVersusEdit() async throws {93 let world = World()94 let fake = world.fake95 let a = try world.device("A")96 let id = fake.webCreatePage(title: "Doomed", content: Doc.data([("a", "server")]), tags: ["t"])97 #expect(await a.sync() == .completed)98 try await a.append(id, block: "mine", text: "only on this device")99 fake.webTrash(id)100 #expect(await a.sync() == .completed)101 #expect(try await a.page(id).isDirty)102 fake.webPurge(id)103 #expect(await a.sync() == .completed) // the tombstone arrives: the row is kept and marked "to create"104 #expect(await a.sync() == .completed)105 let back = try #require(fake.page(id))106 #expect(back.text == "server\nonly on this device")107 #expect(back.tags == ["t"])108 #expect(await a.sync() == .completed)109 try await expectMirror(a)110 await world.closeAll()111 }112113 @Test("trashing and restoring a folder carries its notes on every device")114 func trashFolder() async throws {115 let world = World()116 let a = try world.device("A")117 let b = try world.device("B")118 let folder = try await a.create("Folder", properties: #"{"folder":true}"#)119 let inside = try await a.create("Inside", [("a", "x")], parent: folder)120 let deeper = try await a.create("Deeper", [("a", "y")], parent: inside)121 #expect(await a.sync() == .completed)122 #expect(await b.sync() == .completed)123 _ = try await b.store.trash(id: folder)124 #expect(await b.sync() == .completed)125 #expect(await a.sync() == .completed)126 for id in [folder, inside, deeper] { #expect(try await a.page(id).deletedAt != nil) }127 try await expectMirror(a)128 _ = try await a.store.restore(id: folder)129 #expect(await a.sync() == .completed)130 #expect(await b.sync() == .completed)131 for id in [folder, inside, deeper] { #expect(try await b.page(id).deletedAt == nil) }132 try await expectMirror(a)133 try await expectMirror(b)134 await world.closeAll()135 }136137 @Test("a queued purge that needs a passkey confirmation sets needsReauth and stays queued, then goes through at once")138 func purgeNeedsReauth() async throws {139 let world = World()140 let fake = world.fake141 let a = try world.device("A")142 let b = try world.device("B")143 let id = try await a.create("To purge", [("a", "x")])144 let other = try await a.create("Other", [("a", "y")])145 #expect(await a.sync() == .completed)146 #expect(await b.sync() == .completed)147148 _ = try await a.store.purge(id: id)149 try await a.edit(other, block: "a", text: "y edited")150 for _ in 0..<3 {151 #expect(await a.sync() == .completed, "the rest of the cycle is not blocked")152 #expect(await a.engine.currentStatus().needsReauth, "the flag does not flicker between cycles")153 #expect(try await a.store.opCount() == 1)154 }155 #expect(fake.page(id) != nil)156 #expect(fake.page(other)?.text == "y edited")157 #expect(try await a.store.queuedOps().first?.attempts == 0, "waiting for the user is not a failed attempt")158 #expect(await a.engine.currentStatus().pending == 1)159160 fake.setFresh(true, token: try #require(a.token)) // the user confirmed with the passkey161 #expect(await a.sync() == .completed)162 #expect(fake.page(id) == nil)163 #expect(await a.engine.currentStatus().needsReauth == false)164 #expect(try await a.store.opCount() == 0)165 #expect(await b.sync() == .completed)166 #expect(try await b.store.page(id: id) == nil)167 try await expectMirror(a)168 try await expectMirror(b)169 await world.closeAll()170 }171172 @Test("an action whose target is already gone on the server is dropped, not retried for ever")173 func opsOnMissingTargets() async throws {174 let world = World()175 let fake = world.fake176 let a = try world.device("A")177 let inbox = fake.webCreatePage(title: "Capture", content: Doc.data([("a", "x")]), inbox: true)178 #expect(await a.sync() == .completed)179 #expect(try await a.page(inbox).inboxAt != nil)180 _ = try await a.store.setInbox(id: inbox, at: nil)181 _ = try await a.store.enqueueOp(kind: OpKind.triage, targetId: inbox, payload: try JSONEncoder().encode(QueuedTriage(action: "keep", section: "projects")))182 #expect(await a.sync() == .completed)183 #expect(fake.page(inbox)?.inboxAt == nil && fake.page(inbox)?.section == "projects")184 #expect(try await a.store.opCount() == 0)185 // a second triage of the same page: 409 not_in_inbox → dropped186 _ = try await a.store.enqueueOp(kind: OpKind.triage, targetId: inbox, payload: try JSONEncoder().encode(QueuedTriage(action: "archive")))187 #expect(await a.sync() == .completed)188 #expect(try await a.store.opCount() == 0)189 #expect(await a.sync() == .completed)190 try await expectMirror(a)191 await world.closeAll()192 }193194 @Test("an action this build does not know is kept and surfaced, never dropped, and does not block the queue (2.0)")195 func unknownOpsAreParked() async throws {196 let world = World()197 let fake = world.fake198 let a = try world.device("A")199 let id = try await a.create("Note", [("a", "x")])200 #expect(await a.sync() == .completed)201 let future = try await a.store.enqueueOp(kind: "somethingFromTheFuture", targetId: id, payload: Data(#"{"keep":"me"}"#.utf8))202 let legacyTask = try await a.store.enqueueOp(kind: OpKind.createTask, targetId: "task-1") // declared in 1.0 but never dispatched203 _ = try await a.store.trash(id: id) // a known action on the SAME target, queued after the unknown one204 #expect(await a.sync() == .completed)205 // The known action went through; the two unknown ones are still there, intact.206 #expect(fake.page(id)?.deletedAt != nil)207 #expect(try await a.store.opCount() == 2)208 let parked = try await a.store.parkedOps()209 #expect(parked.map(\.id) == [future.id, legacyTask.id])210 #expect(parked.first?.op.payload == Data(#"{"keep":"me"}"#.utf8))211 #expect(parked.first?.op.idempotencyKey == future.idempotencyKey)212 #expect(parked.first?.reason == "unsupported op kind: somethingFromTheFuture")213 let status = await a.engine.currentStatus()214 #expect(status.unsupported == 2)215 #expect(status.pending == 0)216 // Later cycles neither drop nor retry them, and the device still mirrors the server.217 #expect(await a.sync() == .completed)218 #expect(await a.sync() == .completed)219 #expect(try await a.store.parkedOps().map(\.op.attempts) == [0, 0])220 #expect(try await a.page(id).deletedAt != nil)221 #expect(try await a.store.pendingPushCount() == 0)222 // They survive a restart of the app.223 try await a.restart()224 #expect(await a.sync() == .completed)225 #expect(try await a.store.parkedOps().map(\.op.kind) == ["somethingFromTheFuture", OpKind.createTask])226 // A kind this build supports that an older build had parked goes back in line at the next cycle.227 let known = try await a.store.enqueueOp(kind: OpKind.deleteAttachment, targetId: "00000000-0000-4000-8000-00000000dead")228 try await a.store.parkOp(id: known.id, reason: "parked by an older build")229 #expect(try await a.store.parkedOpCount() == 3)230 #expect(await a.sync() == .completed) // released, sent, 404 (already gone) → done231 #expect(try await a.store.opCount() == 2)232 #expect(try await a.store.parkedOpCount() == 2)233 await world.closeAll()234 }235}236237@Suite("Attachments")238struct AttachmentTests {239 @Test("pictures and drawing sources are downloaded automatically; other files on demand")240 func downloads() async throws {241 let world = World()242 let fake = world.fake243 let page = fake.webCreatePage(title: "With files")244 let trashed = fake.webCreatePage(title: "Trashed")245 let png = Data((0..<2000).map { UInt8($0 % 200) })246 let drawing = Data((0..<900).map { UInt8($0 % 100) })247 let pdf = Data((0..<3000).map { UInt8($0 % 50) })248 let image = fake.webAddAttachment(pageId: page, filename: "photo.png", mime: "image/png", bytes: png)249 let source = fake.webAddAttachment(pageId: page, filename: "drawing-1.pkdrawing", mime: "application/octet-stream", bytes: drawing)250 let document = fake.webAddAttachment(pageId: page, filename: "paper.pdf", mime: "application/pdf", bytes: pdf)251 let big = fake.webAddAttachment(pageId: page, filename: "huge.png", mime: "image/png", bytes: Data(count: 4000))252 let inTrash = fake.webAddAttachment(pageId: trashed, filename: "old.png", mime: "image/png", bytes: png)253 fake.webTrash(trashed)254255 var options = SyncOptions()256 options.autoDownloadLimit = 3000257 let a = try world.device("A", options: options)258 #expect(await a.sync() == .completed)259 func state(_ id: String) async throws -> AttachmentRecord { try #require(try await a.store.attachment(id: id)) }260 #expect(try await state(image).state == .local)261 #expect(try Data(contentsOf: a.files.url(relativePath: try #require(try await state(image).localPath))) == png)262 #expect(try Data(contentsOf: a.files.url(relativePath: try #require(try await state(source).localPath))) == drawing)263 #expect(try await state(document).state == .remote, "a PDF is not downloaded on its own")264 #expect(try await state(big).state == .remote, "over the automatic limit")265 #expect(try await state(inTrash).state == .remote, "nothing for notes in the trash")266267 let url = try await a.engine.download(attachmentId: document)268 #expect(try Data(contentsOf: url) == pdf)269 #expect(try await state(document).state == .local)270 fake.clearLog()271 #expect(try await a.engine.download(attachmentId: document) == url)272 #expect(fake.requests.isEmpty, "already on the device")273274 // a failing download does not block the cycle and is tried again later275 let late = fake.webAddAttachment(pageId: page, filename: "late.png", mime: "image/png", bytes: png)276 fake.inject(.status(500), method: "GET", path: "^/api/files/")277 #expect(await a.sync() == .completed)278 #expect(try await state(late).state == .remote)279 #expect(await a.sync() == .completed)280 #expect(try await state(late).state == .local)281 await world.closeAll()282 }283284 @Test("a file deleted on the server leaves the device; a file deleted on the device leaves the server and the other device")285 func deletions() async throws {286 let world = World()287 let fake = world.fake288 let a = try world.device("A")289 let b = try world.device("B")290 let page = try await a.create("Note", [("a", "x")])291 let bytes = Data((0..<1000).map { UInt8($0 % 256) })292 let one = try await a.attach(page, filename: "one.png", mime: "image/png", bytes: bytes)293 let two = try await a.attach(page, filename: "two.png", mime: "image/png", bytes: bytes)294 #expect(await a.sync() == .completed)295 #expect(await b.sync() == .completed)296 let pathOnB = try #require(try await b.store.attachment(id: one.id)?.localPath)297 #expect(b.files.exists(pathOnB))298299 // deleted on the web300 fake.webDeleteAttachment(one.id)301 #expect(await a.sync() == .completed)302 #expect(await b.sync() == .completed)303 #expect(!a.files.exists(try #require(one.localPath)), "the local file is removed")304 #expect(!b.files.exists(pathOnB))305 #expect(try await a.store.attachment(id: one.id)?.deletedAt != nil)306 #expect(try await a.store.attachments(pageId: page).map(\.id) == [two.id])307308 // deleted on device A309 if let path = try await a.store.markAttachmentDeleted(id: two.id) { a.files.remove(relativePath: path) }310 #expect(await a.sync() == .completed)311 #expect(fake.attachment(two.id)?.deletedAt != nil)312 #expect(await b.sync() == .completed)313 #expect(try await b.store.attachments(pageId: page).isEmpty)314 #expect(!b.files.exists(b.files.relativePath(id: two.id, filename: "two.png")))315 #expect(try await a.store.opCount() == 0)316 await world.closeAll()317 }318319 @Test("a missing local file does not block the queue; it is uploaded when the file is back")320 func missingFile() async throws {321 let world = World()322 let fake = world.fake323 let a = try world.device("A")324 let page = try await a.create("Note", [("a", "x")])325 let lost = try await a.attach(page, filename: "lost.png", mime: "image/png", bytes: Data("lost".utf8))326 let fine = try await a.attach(page, filename: "fine.png", mime: "image/png", bytes: Data("fine".utf8))327 try FileManager.default.removeItem(at: a.files.url(relativePath: try #require(lost.localPath)))328 #expect(await a.sync() == .completed)329 #expect(fake.attachment(fine.id) != nil)330 #expect(fake.attachment(lost.id) == nil)331 #expect(try await a.store.attachment(id: lost.id)?.lastError == SyncEngine.missingFile)332 #expect(await a.engine.currentStatus().pending == 0, "a file that cannot be sent does not keep the counter up")333 let attempts = try await a.store.attachment(id: lost.id)?.uploadAttempts334 #expect(await a.sync() == .completed)335 #expect(try await a.store.attachment(id: lost.id)?.uploadAttempts == attempts, "recorded once, not at every cycle")336337 try Data("lost".utf8).write(to: a.files.url(relativePath: try #require(lost.localPath)))338 #expect(await a.sync() == .completed)339 #expect(fake.attachment(lost.id)?.bytes == Data("lost".utf8))340 await world.closeAll()341 }342343 @Test("a file refused for its type is not sent again at every cycle; a server hiccup is retried at the next one")344 func refusedAndRetried() async throws {345 let world = World()346 let fake = world.fake347 let a = try world.device("A")348 let page = try await a.create("Note", [("a", "x")])349 let refused = try await a.attach(page, filename: "page.html", mime: "text/html", bytes: Data("<p>".utf8))350 let fine = try await a.attach(page, filename: "fine.txt", mime: "text/plain", bytes: Data("fine".utf8))351 fake.inject(.status(502), method: "POST", path: "^/api/attachments$", count: 1)352 #expect(await a.sync() == .completed)353 #expect(await a.sync() == .completed)354 #expect(await a.sync() == .completed)355 #expect(fake.attachment(fine.id) != nil)356 #expect(try await a.store.attachment(id: refused.id)?.state == .failed)357 #expect(fake.requests.filter { $0.method == "POST" && $0.path == "/api/attachments" && $0.status == 415 }.count == 1)358 await world.closeAll()359 }360361 @Test("the multipart body of an upload is parsed: bytes, name, type, page and id arrive intact")362 func multipart() async throws {363 let world = World()364 let fake = world.fake365 let a = try world.device("A")366 let page = try await a.create("Note", [("a", "x")])367 var bytes = Data("--spnotes-fake-boundary\r\n\r\n".utf8) // content that looks like a delimiter368 bytes.append(contentsOf: (0..<70_000).map { UInt8(truncatingIfNeeded: $0 &* 31) })369 let file = try await a.attach(page, filename: "relevé \"final\".bin", mime: "application/octet-stream", bytes: bytes)370 #expect(await a.sync() == .completed)371 let stored = try #require(fake.attachment(file.id))372 #expect(stored.bytes == bytes)373 #expect(stored.pageId == page)374 #expect(stored.mime == "application/octet-stream")375 await world.closeAll()376 }377}378379@Suite("Reminders")380struct ReminderTests {381 @Test("reminders become tasks: create, update, done, delete")382 func lifecycle() async throws {383 let world = World()384 let fake = world.fake385 let a = try world.device("A")386 let page = try await a.create("Note", [("a", "x")])387 let due = Date(timeIntervalSince1970: 1_800_000_000)388 let reminder = try await a.store.createReminder(pageId: page, title: "Call the notary", dueAt: due)389 let done = try await a.store.createReminder(title: "Already done", done: true)390 #expect(await a.sync() == .completed)391 #expect(fake.allTasks.count == 2)392 let taskId = try #require(try await a.store.reminder(id: reminder.id)?.serverTaskId)393 #expect(fake.task(taskId)?.title == "Call the notary")394 #expect(fake.task(taskId)?.pageId == page)395 #expect(abs((fake.task(taskId)?.dueAt ?? .distantPast).timeIntervalSince(due)) < 0.01)396 #expect(fake.task(try #require(try await a.store.reminder(id: done.id)?.serverTaskId))?.status == "done")397 #expect(try await a.store.dirtyReminders().isEmpty)398399 _ = try await a.store.updateReminder(id: reminder.id, title: "Call the notary today", dueAt: .some(nil), done: true)400 #expect(await a.sync() == .completed)401 #expect(fake.task(taskId)?.title == "Call the notary today")402 #expect(fake.task(taskId)?.dueAt == nil)403 #expect(fake.task(taskId)?.status == "done")404405 try await a.store.deleteReminder(id: reminder.id)406 #expect(await a.sync() == .completed)407 #expect(fake.task(taskId)?.deletedAt != nil)408 #expect(try await a.store.reminder(id: reminder.id) == nil)409 fake.clearLog()410 #expect(await a.sync() == .completed)411 #expect(fake.requests.allSatisfy { $0.method == "GET" })412 await world.closeAll()413 }414415 @Test("a lost answer on the creation of a task neither duplicates it nor hides a later edit")416 func lostAnswer() async throws {417 let world = World()418 let fake = world.fake419 let a = try world.device("A")420 let reminder = try await a.store.createReminder(title: "First wording")421 fake.inject(.dropResponse, method: "POST", path: "^/api/tasks$")422 #expect(await a.sync() == .offline)423 #expect(fake.allTasks.count == 1)424 _ = try await a.store.updateReminder(id: reminder.id, title: "Second wording", done: true)425 #expect(await a.sync() == .completed)426 #expect(fake.allTasks.count == 1, "the replayed answer did not create a second task")427 #expect(fake.allTasks.first?.title == "Second wording", "…and did not pass for the current state")428 #expect(fake.allTasks.first?.status == "done")429 #expect(try await a.store.dirtyReminders().isEmpty)430 await world.closeAll()431 }432433 @Test("a reminder edited after its task was deleted on the server is created again")434 func taskDeletedOnServer() async throws {435 let world = World()436 let fake = world.fake437 let a = try world.device("A")438 let reminder = try await a.store.createReminder(title: "Keep me")439 #expect(await a.sync() == .completed)440 let first = try #require(try await a.store.reminder(id: reminder.id)?.serverTaskId)441 // deleted from the web442 let other = try world.device("Web")443 _ = try await other.api.deleteTask(id: first)444 _ = try await a.store.updateReminder(id: reminder.id, title: "Keep me, edited")445 #expect(await a.sync() == .completed)446 let second = try #require(try await a.store.reminder(id: reminder.id)?.serverTaskId)447 #expect(second != first)448 #expect(fake.allTasks.map(\.title) == ["Keep me, edited"])449 // deleting a reminder whose task is already gone just settles450 _ = try await other.api.deleteTask(id: second)451 try await a.store.deleteReminder(id: reminder.id)452 #expect(await a.sync() == .completed)453 #expect(try await a.store.dirtyReminders().isEmpty)454 await world.closeAll()455 }456}457