Repository Public
1// SP Notes Core2// File: Tests/SPNotesSyncTests/SyncEngineTests.swift3// Description: Scenario tests of the synchronisation engine, part 1: first and incremental pulls, server reset, pages purged while a4// device was away, a whole offline session survived by a killed app, replays without duplicates.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SPNotesAPI10import SPNotesStore11import Testing12@testable import SPNotesSync13@testable import SPNotesTestSupport1415@Suite("Pull, offline session, replay")16struct PullAndOfflineTests {17 @Test("the first sync pulls everything through a paged feed; the next one pulls only the changes")18 func firstAndIncrementalSync() async throws {19 let world = World()20 let fake = world.fake21 var ids: [String] = []22 for i in 0..<23 { ids.append(fake.webCreatePage(title: "Note \(i)", content: Doc.data([("b\(i)", "text \(i)")]), tags: i % 3 == 0 ? ["work"] : [])) }23 fake.webTrash(ids[4])24 fake.webPatchMeta(ids[5], ["archived": true, "pinned": true])25 let child = fake.webCreatePage(title: "Child", content: Doc.data([("c", "child")]), parentId: ids[0])26 let row = fake.webCreatePage(title: "Database row", databaseId: UUID().uuidString.lowercased())2728 var options = SyncOptions()29 options.feedPageSize = 730 let a = try world.device("A", options: options)31 #expect(await a.sync() == .completed)32 #expect(fake.count("GET", "^/api/sync", device: "A") >= 4, "the feed was read page by page")33 #expect(try await a.store.pageCount() == 24, "every page except the database row")34 #expect(try await a.store.page(id: row) == nil)35 #expect(try await a.page(child).parentId == ids[0])36 #expect(try await a.page(ids[4]).deletedAt != nil)37 #expect(try await a.page(ids[5]).archived)38 #expect(try await a.page(ids[3]).tags == ["work"])39 #expect(try await a.store.tags().map(\.name) == ["work"])40 #expect(try await a.store.syncCursor() == fake.head)41 #expect(try await a.store.counts().pending == 0)4243 // incremental: one change on the server → one feed request, one page in it44 fake.clearLog()45 fake.webEdit(ids[2], title: "Renamed", content: Doc.data([("b2", "text 2"), ("n", "added on the web")]))46 #expect(await a.sync() == .completed)47 let feeds = fake.requests.filter { $0.path == "/api/sync" }48 #expect(feeds.count == 1)49 #expect(feeds.first?.query.contains("since=0") == false)50 #expect(try await a.page(ids[2]).title == "Renamed")51 #expect(try await a.texts(ids[2]) == ["text 2", "added on the web"])52 #expect(fake.requests.allSatisfy { $0.method == "GET" }, "nothing to push: no write")53 await world.closeAll()54 }5556 @Test("a reset feed downloads everything again; dirty notes are kept and pushed, notes the restored server lost are given back")57 func serverReset() async throws {58 let world = World()59 let fake = world.fake60 let p1 = fake.webCreatePage(title: "One", content: Doc.data([("a", "one")]))61 let p2 = fake.webCreatePage(title: "Two", content: Doc.data([("a", "two")]))62 let p3 = fake.webCreatePage(title: "Three", content: Doc.data([("a", "three")]))63 let backup = fake.snapshot()64 // after the backup: an edit, a new page, a purge65 fake.webEdit(p2, content: Doc.data([("a", "two"), ("b", "written after the backup")]))66 let p4 = fake.webCreatePage(title: "Four (created after the backup)", content: Doc.data([("a", "four")]), tags: ["late"])67 fake.webPurge(p3)6869 let a = try world.device("A")70 #expect(await a.sync() == .completed)71 #expect(try await a.store.page(id: p3) == nil)72 #expect(try await a.store.page(id: p4) != nil)73 // local work not synced yet74 try await a.append(p1, block: "local", text: "typed before the restore")75 let fresh = try await a.create("Created offline", [("x", "offline text")])7677 fake.restore(backup) // the database goes back in time: the cursor of A is now ahead78 #expect(await a.sync() == .completed)7980 // nothing local was lost, and the server has it81 #expect(fake.page(p1)?.text.contains("typed before the restore") == true)82 #expect(fake.page(fresh)?.text == "offline text")83 // the edit made after the backup existed only on the device: it is back on the server84 #expect(fake.page(p2)?.text.contains("written after the backup") == true)85 // the page created after the backup existed only on the device: created again under the same id86 #expect(fake.page(p4)?.title == "Four (created after the backup)")87 #expect(fake.page(p4)?.tags == ["late"])88 // the page purged after the backup is back on the server (restored) and therefore on the device89 #expect(try await a.store.page(id: p3) != nil)90 #expect(await a.sync() == .completed)91 try await expectMirror(a)92 #expect(try await a.store.meta(SyncEngine.fullCopyKey) == nil)9394 // a second device starting from zero sees the same thing95 let b = try world.device("B")96 #expect(await b.sync() == .completed)97 try await expectMirror(b)98 await world.closeAll()99 }100101 @Test("pages purged on the server while a device was away disappear from it, unless they hold unsynced work")102 func purgedWhileAway() async throws {103 let world = World()104 let fake = world.fake105 let clean = fake.webCreatePage(title: "Clean", content: Doc.data([("a", "clean")]))106 let edited = fake.webCreatePage(title: "Edited locally", content: Doc.data([("a", "server text")]))107 let pinned = fake.webCreatePage(title: "Only pinned locally", content: Doc.data([("a", "pin")]))108 let a = try world.device("A")109 #expect(await a.sync() == .completed)110111 a.setOffline(true)112 try await a.append(edited, block: "mine", text: "my unsynced paragraph")113 _ = try await a.store.setPinned(id: pinned, true)114 fake.webPurge(clean)115 fake.webPurge(edited)116 fake.webPurge(pinned)117 #expect(await a.sync() == .offline)118 a.setOffline(false)119 #expect(await a.sync() == .completed)120 #expect(await a.sync() == .completed)121122 #expect(try await a.store.page(id: clean) == nil, "a clean page follows the purge")123 // the note with unsynced text is created again under the same id: nothing is lost124 let again = try #require(fake.page(edited))125 #expect(again.text == "server text\nmy unsynced paragraph")126 #expect(again.title == "Edited locally")127 #expect(try await a.page(edited).isDirty == false)128 // a flag alone does not bring a permanently deleted note back129 #expect(fake.page(pinned) == nil)130 #expect(try await a.store.page(id: pinned) == nil)131 try await expectMirror(a)132 await world.closeAll()133 }134135 @Test("a complete copy from cursor 0 removes synced pages the server no longer has (tombstones missed)")136 func fullCopyPrunes() async throws {137 let world = World()138 let fake = world.fake139 let keep = fake.webCreatePage(title: "Keep")140 let gone = fake.webCreatePage(title: "Gone")141 let a = try world.device("A")142 #expect(await a.sync() == .completed)143 let local = try await a.create("Never pushed", [("a", "local")])144 a.setOffline(true)145 fake.webPurge(gone)146 try await a.store.setSyncCursor(0) // the app asks for a complete refresh147 a.setOffline(false)148 // the creation is pushed first, then the complete copy runs149 #expect(await a.sync() == .completed)150 #expect(try await a.store.page(id: gone) == nil)151 #expect(try await a.store.page(id: keep) != nil)152 #expect(try await a.store.page(id: local) != nil)153 try await expectMirror(a)154 await world.closeAll()155 }156157 @Test("a whole offline session survives a killed app and reaches the server and the other device exactly; replays duplicate nothing")158 func offlineSessionThenKill() async throws {159 let world = World()160 let fake = world.fake161 let existing = fake.webCreatePage(title: "Existing", content: Doc.data([("e", "existing")]))162 let toTrash = fake.webCreatePage(title: "To trash", content: Doc.data([("t", "trash me")]))163 let a = try world.device("A")164 let b = try world.device("B")165 #expect(await a.sync() == .completed)166 #expect(await b.sync() == .completed)167168 a.setOffline(true)169 // nested creations170 let folder = try await a.create("Folder", [("f", "")], properties: #"{"folder":true}"#)171 let note = try await a.create("Note in folder", [("n1", "first"), ("n2", "second")], parent: folder, tags: ["Work", "#Idea"])172 let deep = try await a.create("Deep note", [("d", "deep")], parent: note)173 let loose = try await a.create(" ", [("l", "untitled note")])174 // edits, tags, pin, move, trash, restore175 try await a.edit(note, block: "n2", text: "second, edited")176 try await a.append(note, block: "n3", text: "third")177 _ = try await a.store.setTags(id: existing, names: ["urgent"])178 _ = try await a.store.setPinned(id: existing, true)179 _ = try await a.store.setFavorite(id: existing, true)180 _ = try await a.store.move(id: existing, parentId: folder)181 try await a.append(existing, block: "e2", text: "edited offline")182 _ = try await a.store.trash(id: toTrash)183 let trashedNew = try await a.create("Created then trashed", [("x", "x")])184 _ = try await a.store.trash(id: trashedNew)185 let restored = try await a.create("Trashed then restored", [("r", "r")])186 _ = try await a.store.trash(id: restored)187 _ = try await a.store.restore(id: restored)188 let png = Data([0x89, 0x50, 0x4E, 0x47] + (0..<300).map { UInt8($0 % 251) })189 let file = try await a.attach(note, filename: "picture.png", mime: "image/png", bytes: png)190 #expect(await a.sync() == .offline)191 #expect(fake.allPages.count == 2, "nothing reached the server")192193 try await a.restart() // the app is killed before any sync194 #expect(try await a.store.counts().pending > 0)195 a.setOffline(false)196 #expect(await a.sync() == .completed)197198 // the server has exactly the local state199 #expect(fake.page(folder)?.title == "Folder")200 #expect(fake.page(note)?.parentId == folder)201 #expect(fake.page(deep)?.parentId == note)202 #expect(fake.page(note)?.text == "first\nsecond, edited\nthird")203 #expect(fake.page(note)?.tags == ["idea", "work"])204 #expect(fake.page(loose)?.title == "Untitled")205 let e = try #require(fake.page(existing))206 #expect(e.tags == ["urgent"] && e.pinned && e.parentId == folder)207 #expect(e.text == "existing\nedited offline")208 #expect(FakeBackend.jsonEqual(e.properties, Data(#"{"favorite":true}"#.utf8)))209 #expect(fake.page(toTrash)?.deletedAt != nil)210 #expect(fake.page(trashedNew)?.deletedAt != nil)211 #expect(fake.page(restored)?.deletedAt == nil)212 #expect(fake.attachment(file.id)?.bytes == png)213 #expect(fake.attachment(file.id)?.pageId == note)214 #expect(fake.allPages.count == 8)215 #expect(await a.sync() == .completed)216 try await expectMirror(a)217 #expect(try await a.store.attachment(id: file.id)?.uploaded == true)218 #expect(await a.engine.currentStatus().pending == 0)219220 // the other device receives it, picture included221 #expect(await b.sync() == .completed)222 try await expectMirror(b)223 let received = try #require(try await b.store.attachment(id: file.id))224 #expect(received.state == .local)225 #expect(try Data(contentsOf: b.files.url(relativePath: try #require(received.localPath))) == png)226227 // replaying whole cycles changes nothing and writes nothing228 fake.clearLog()229 let head = fake.head230 #expect(await a.sync() == .completed)231 try await a.restart()232 #expect(await a.sync() == .completed)233 #expect(await b.sync() == .completed)234 #expect(fake.requests.allSatisfy { $0.method == "GET" })235 #expect(fake.head == head)236 #expect(fake.allPages.count == 8 && fake.allAttachments.count == 1)237 await world.closeAll()238 }239240 @Test("a parent whose creation fails keeps its children waiting; nothing is filed at the top level by accident")241 func childrenWaitForTheirParent() async throws {242 let world = World()243 let fake = world.fake244 let a = try world.device("A")245 let parent = try await a.create("Parent", [("p", "parent")])246 let child = try await a.create("Child", [("c", "child")], parent: parent)247 let grandchild = try await a.create("Grandchild", [("g", "grandchild")], parent: child)248 let other = try await a.create("Unrelated", [("o", "other")])249 let reminder = try await a.store.createReminder(pageId: child, title: "Call back")250 let file = try await a.attach(child, filename: "doc.txt", mime: "text/plain", bytes: Data("hello".utf8))251 _ = try await a.store.trash(id: grandchild)252253 fake.inject(.status(400), method: "POST", path: "^/api/pages$", count: 1) // the parent is refused once254 #expect(await a.sync() == .completed)255 #expect(fake.page(parent) == nil && fake.page(child) == nil && fake.page(grandchild) == nil)256 #expect(fake.page(other) != nil, "an unrelated note is not held back")257 #expect(try await a.page(parent).syncState == .error)258 #expect(try await a.page(child).needsCreate)259 #expect(fake.allAttachments.isEmpty && fake.allTasks.isEmpty, "the file and the reminder wait for their page")260 #expect(try await a.store.opCount() == 1, "the trash of a page not created yet stays queued")261 #expect(fake.count("DELETE", "^/api/pages/") == 0)262263 #expect(await a.sync() == .completed)264 #expect(fake.page(child)?.parentId == parent)265 #expect(fake.page(grandchild)?.parentId == child)266 #expect(fake.page(grandchild)?.deletedAt != nil)267 #expect(fake.attachment(file.id)?.pageId == child)268 #expect(fake.allTasks.map(\.pageId) == [child])269 #expect(try await a.store.reminder(id: reminder.id)?.dirty == false)270 #expect(await a.sync() == .completed)271 try await expectMirror(a)272 await world.closeAll()273 }274275 @Test("local order of notes created offline is kept on the server")276 func positionsOfOfflineCreations() async throws {277 let world = World()278 let a = try world.device("A")279 let first = try await a.create("First", [("a", "1")])280 let second = try await a.create("Second", [("a", "2")])281 _ = try await a.store.move(id: second, parentId: nil, position: 0.5) // dragged above the first one282 let before = try await a.page(second).position283 #expect(await a.sync() == .completed)284 #expect(await a.sync() == .completed)285 #expect(try await a.page(second).position == before)286 #expect((world.fake.page(second)?.position ?? 9) < (world.fake.page(first)?.position ?? 0))287 try await expectMirror(a)288 await world.closeAll()289 }290}291