Repository Public
1// SP Notes Core2// File: Tests/SPNotesSyncTests/EntityFeedTests.swift3// Description: The entity change feed (API-NEXT §3) in the engine: `kinds`, the cursor rule (a cursor is only valid for4// its set of kinds), soft deletes and tombstones of the new kinds, windowing, reset, and the behaviour5// against a 2.x server, which must be exactly that of 1.0.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesAPI11import SPNotesStore12import Testing13@testable import SPNotesSync14@testable import SPNotesTestSupport1516@Suite("Entity feed (2.0)")17struct EntityFeedTests {18 private func row(_ pairs: [String: Any]) -> String { String(decoding: try! JSONSerialization.data(withJSONObject: pairs, options: [.sortedKeys]), as: UTF8.self) }1920 /// A small workspace with one row of every kind of the feed.21 private func seed(_ fake: FakeBackend) -> (page: String, task: String, canvas: String, db: String, prop: String, view: String, row: String, edge: String, ref: String, concept: String) {22 let page = fake.webCreatePage(title: "Lectures")23 let other = fake.webCreatePage(title: "Données")24 let task = fake.webCreateTask(title: "Relire", pageId: page, priority: "high")25 let canvas = fake.webCreateCanvas(title: "Carte")26 func id() -> String { UUID().uuidString.lowercased() }27 let ids = (db: id(), prop: id(), view: id(), row: id(), edge: id(), ref: id(), concept: id())28 fake.webPutEntity(kind: "database", id: ids.db, json: row(["pageId": page, "name": "Lectures", "isTemplate": false]))29 fake.webPutEntity(kind: "database_property", id: ids.prop, json: row(["databaseId": ids.db, "name": "Name", "type": "title", "config": [:], "position": 0]))30 fake.webPutEntity(kind: "database_view", id: ids.view, json: row(["databaseId": ids.db, "name": "Table", "type": "table", "config": [:], "position": 0, "isDefault": true]))31 fake.webPutEntity(kind: "database_row", id: ids.row, json: row(["databaseId": ids.db, "pageId": other, "values": [ids.prop: "Engle (1982)"], "position": 1]))32 fake.webPutEntity(kind: "edge", id: ids.edge, json: row(["fromId": page, "toId": other, "kind": "relation", "relType": "supports", "origin": "user", "status": "accepted"]))33 fake.webPutEntity(kind: "reference", id: ids.ref, json: row(["pageId": page, "citeKey": "engle1982", "title": "ARCH", "authors": ["Engle, R."], "year": 1982]))34 fake.webPutEntity(kind: "concept", id: ids.concept, json: row(["name": "hétéroscédasticité", "origin": "user", "status": "accepted"]))35 return (page, task, canvas, ids.db, ids.prop, ids.view, ids.row, ids.edge, ids.ref, ids.concept)36 }3738 @Test("a 3.0 server: the feed is asked with every kind and each kind lands in the local store")39 func everyKindArrives() async throws {40 let world = World(apiLevel: 3)41 let fake = world.fake42 try #require(fake.level == 3)43 let ids = seed(fake)44 let a = try world.device("A")45 #expect(await a.sync() == .completed)4647 #expect(fake.count("GET", "^/api/meta$") == 1)48 #expect(fake.count("GET", "^/api/sync\\?") == fake.count("GET", "kinds=task,database,database_property,database_view,database_row,edge,reference,canvas,concept"), "every feed request carries the kinds")49 #expect(try await a.store.meta(SyncEngine.kindsKey) == "task,database,database_property,database_view,database_row,edge,reference,canvas,concept")50 #expect(await a.engine.serverFeatures()?.has(ServerFeature.entities) == true)5152 #expect(try await a.tasks.list().map(\.id) == [ids.task])53 #expect(try await a.store.databases().map(\.id) == [ids.db])54 #expect(try await a.store.databaseProperties(databaseId: ids.db).map(\.id) == [ids.prop])55 #expect(try await a.store.databaseViews(databaseId: ids.db).map(\.id) == [ids.view])56 #expect(try await a.store.databaseRows(databaseId: ids.db).map(\.id) == [ids.row])57 #expect(try await a.store.edges(from: ids.page).map(\.id) == [ids.edge], "edges are indexed on fromId / toId")58 #expect(try await a.store.edges(touching: ids.page).map(\.id) == [ids.edge])59 #expect(try await a.store.references(projectId: ids.page).map(\.id) == [ids.ref], "references are indexed on the page they are attached to")60 #expect(try await a.store.concepts().map(\.id) == [ids.concept])61 let canvas = try #require(try await a.store.canvas(pageId: ids.canvas))62 #expect(canvas.version == 1)63 #expect(!canvas.dirty)64 #expect(try await a.store.entityCursor(kind: .task) == fake.entityHead, "the cursor of each kind follows the feed")65 #expect(try await a.store.syncCursor() == fake.entityHead)6667 // A second cycle asks only for what is new.68 fake.clearLog()69 #expect(await a.sync() == .completed)70 #expect(fake.count("GET", "^/api/sync\\?") == 1)71 #expect(fake.count("GET", "since=\(fake.entityHead)&") == 1 || fake.count("GET", "since=\(fake.entityHead)$") == 1)72 #expect(fake.count("GET", "^/api/meta$") == 0, "the features are trusted for a while")73 await world.closeAll()74 }7576 @Test("rows deleted on the server leave the device: soft deletes (deletedAt) and tombstones of the new kinds")77 func deletionsOfTheNewKinds() async throws {78 let world = World(apiLevel: 3)79 let fake = world.fake80 let ids = seed(fake)81 let a = try world.device("A")82 #expect(await a.sync() == .completed)8384 fake.webDeleteTask(ids.task) // soft: a row with deletedAt85 fake.webDeleteEntity(kind: "database_row", id: ids.row) // soft86 fake.webDeleteEntity(kind: "reference", id: ids.ref) // soft87 fake.webDeleteEntity(kind: "edge", id: ids.edge, permanently: true) // tombstone88 fake.webDeleteEntity(kind: "concept", id: ids.concept, permanently: true) // tombstone89 fake.webDeleteEntity(kind: "database_view", id: ids.view, permanently: true)90 fake.webPurge(ids.canvas) // tombstones page + canvas91 #expect(await a.sync() == .completed)9293 #expect(try await a.tasks.list(.all).isEmpty)94 #expect(try await a.store.databaseRows(databaseId: ids.db).isEmpty)95 #expect(try await a.store.references().isEmpty)96 #expect(try await a.store.edges(touching: ids.page).isEmpty, "the tombstone of an edge is applied (it was skipped before 2.0)")97 #expect(try await a.store.concepts().isEmpty)98 #expect(try await a.store.databaseViews(databaseId: ids.db).isEmpty)99 #expect(try await a.store.canvas(pageId: ids.canvas) == nil)100 #expect(try await a.store.page(id: ids.canvas) == nil)101 #expect(try await a.store.databases().map(\.id) == [ids.db], "what was not deleted stays")102103 // A row that comes back (same id) is taken again.104 fake.webPutEntity(kind: "concept", id: ids.concept, json: row(["name": "revenu", "origin": "user", "status": "accepted"]))105 #expect(await a.sync() == .completed)106 #expect(try await a.store.concepts().map(\.id) == [ids.concept])107 await world.closeAll()108 }109110 @Test("windowing: many rows of several kinds through small windows arrive once each, cut lists included")111 func windowing() async throws {112 let world = World(apiLevel: 3)113 let fake = world.fake114 var options = SyncOptions()115 options.feedPageSize = 3116 var taskIds: [String] = []117 var conceptIds: [String] = []118 for i in 0..<17 {119 taskIds.append(fake.webCreateTask(title: "T\(i)"))120 if i % 2 == 0 {121 let id = UUID().uuidString.lowercased()122 conceptIds.append(id)123 fake.webPutEntity(kind: "concept", id: id, json: row(["name": "C\(i)"]))124 }125 if i % 5 == 0 { fake.webCreatePage(title: "P\(i)") }126 }127 let a = try world.device("A", options: options)128 #expect(await a.sync() == .completed)129 #expect(Set(try await a.tasks.list(.all).map(\.id)) == Set(taskIds))130 #expect(Set(try await a.store.concepts().map(\.id)) == Set(conceptIds))131 #expect(fake.count("GET", "^/api/sync\\?") >= 6, "the copy really went through several windows")132 #expect(try await a.store.syncCursor() == fake.entityHead)133 try await expectMirror(a)134 await world.closeAll()135 }136137 @Test("the cursor rule: a device synced without kinds reads the feed again from 0 when it starts asking for them, and again when the set changes")138 func cursorIsOnlyValidForItsKinds() async throws {139 let world = World(apiLevel: 3)140 let fake = world.fake141 fake.setAPILevel(2)142 let note = fake.webCreatePage(title: "Note")143 var options = SyncOptions()144 options.metaMaxAge = 0145 let a = try world.device("A", options: options)146 #expect(await a.sync() == .completed)147 let cursor = try await a.store.syncCursor()148 #expect(cursor > 0)149 #expect(try await a.store.meta(SyncEngine.kindsKey) == nil)150 #expect(fake.count("GET", "kinds=") == 0)151152 // The server is upgraded to 3.0; a task already exists there with a revision LOWER than the device's cursor would allow.153 fake.setAPILevel(3)154 let task = fake.webCreateTask(title: "Créée sur le web")155 fake.clearLog()156 #expect(await a.sync() == .completed)157 #expect(fake.count("GET", "^/api/sync\\?since=0&.*kinds=task") == 1, "restarted from 0 with the kinds")158 #expect(try await a.tasks.list().map(\.id) == [task])159 #expect(try await a.store.page(id: note) != nil)160 try await expectMirror(a)161162 // Same set: no restart.163 fake.clearLog()164 #expect(await a.sync() == .completed)165 #expect(fake.count("GET", "^/api/sync\\?since=0&") == 0)166167 // Another set of kinds (an app that turns some off): from 0 again, with that set.168 await a.close()169 var fewer = SyncOptions()170 fewer.entityKinds = [.task, .canvas]171 let b = try Device("A2", fake: fake, options: fewer)172 #expect(await b.sync() == .completed)173 #expect(fake.count("GET", "kinds=task,canvas$") >= 1)174 await b.close()175 await world.closeAll()176 }177178 @Test("a 2.x server: the engine behaves exactly as 1.0 — no kinds, no 3.0 route, queued task and canvas work waits untouched")179 func legacyServerBehavesAsOnePointZero() async throws {180 let world = World(apiLevel: 3)181 let fake = world.fake182 fake.setAPILevel(2)183 let a = try world.device("A")184 let page = try await a.create("Note", [("b1", "texte")])185 // Work that only a 3.0 server can take is queued locally (the features are unknown before the first cycle).186 let task = try await a.tasks.create(title: "Hors ligne", pageId: page)187 let canvas = try await a.canvases.create(title: "Carte", document: Canvas.document([Canvas.node("n1", "A")]))188 #expect(await a.sync() == .completed)189190 #expect(fake.count("GET", "^/api/meta$") == 1)191 #expect(await a.engine.serverFeatures() == .legacy)192 let allowed = Set(["/api/meta", "/api/sync", "/api/pages", "/api/pages/\(page)", "/api/pages/\(canvas)"])193 #expect(fake.requests.allSatisfy { allowed.contains($0.path) }, "only 1.0 routes: \(Set(fake.requests.map(\.path)).subtracting(allowed))")194 #expect(fake.count("GET", "kinds=") == 0)195 #expect(fake.count("POST", "^/api/tasks") == 0)196 #expect(fake.count("PUT", "^/api/canvases") == 0)197 #expect(try await a.store.meta(SyncEngine.kindsKey) == nil)198 // Nothing was dropped or marked: the operations wait for a server that knows them.199 #expect(try await a.store.entityOps(state: .pending).map(\.entityId) == [task.id])200 #expect(try await a.store.canvas(pageId: canvas)?.dirty == true)201 // A 2.x server refuses the page type `canvas`: the page itself waits (retried at each cycle), its canvas behind it.202 #expect(try await a.page(canvas).needsCreate)203 let status = await a.engine.currentStatus()204 #expect(status.pending == 3)205 #expect(status.unsupported == 0 && status.rejected == 0)206 // Once the features are known the repository refuses new task writes with a typed error instead of queueing for ever.207 await #expect(throws: RepositoryError.unsupported(feature: ServerFeature.tasksV2)) { try await a.tasks.create(title: "Refusée") }208209 // The day the server is upgraded, the queued work goes.210 fake.setAPILevel(3)211 try await a.restart()212 #expect(await a.sync() == .completed)213 #expect(fake.task(task.id)?.title == "Hors ligne")214 #expect(Canvas.texts(Canvas.parse(fake.canvasData(canvas))) == ["n1": "A"])215 try await expectTaskMirror(a)216 try await expectCanvasMirror(a, canvas)217 await world.closeAll()218 }219220 @Test("a reset feed with kinds: everything is read again, entities included; a task edited offline is kept and pushed")221 func resetWithKinds() async throws {222 let world = World(apiLevel: 3)223 let fake = world.fake224 let ids = seed(fake)225 let a = try world.device("A")226 #expect(await a.sync() == .completed)227 let snapshot = fake.snapshot()228 // After the backup: the server gets more revisions, the device follows them.229 for i in 0..<6 { fake.webCreateTask(title: "Après \(i)") }230 fake.webUpdateTask(ids.task, title: "Relire (web)")231 #expect(await a.sync() == .completed)232 #expect(try await a.tasks.list().count == 7)233 // The database is restored: the device's cursor is ahead of the server.234 fake.restore(snapshot)235 try await a.tasks.update(id: ids.task, TaskEdit(priority: .low))236 #expect(await a.sync() == .completed)237 #expect(fake.count("GET", "^/api/sync\\?since=0&") >= 2)238 // The task of the backup is back at its old title, with the local change on top.239 #expect(fake.task(ids.task)?.title == "Relire")240 #expect(fake.task(ids.task)?.priority == "low")241 #expect(try await a.tasks.task(id: ids.task)?.title == "Relire")242 #expect(try await a.store.concepts().map(\.id) == [ids.concept])243 #expect(try await a.store.entityOpCount() == 0)244 await world.closeAll()245 }246247 @Test("a complete copy from 0 removes the clean entities the server no longer has, and keeps the ones with local work")248 func fullCopyRemovesWhatTheServerLost() async throws {249 let world = World(apiLevel: 3)250 let fake = world.fake251 let ids = seed(fake)252 let kept = fake.webCreateTask(title: "Gardée")253 let a = try world.device("A")254 #expect(await a.sync() == .completed)255 // The rows vanish from the server without a tombstone reaching this device (it was away for longer than the tombstones live).256 fake.lock.withLock {257 fake.entities["concept:" + ids.concept] = nil258 fake.entities["edge:" + ids.edge] = nil259 fake.tasks[ids.task] = nil260 }261 try await a.tasks.update(id: kept, TaskEdit(title: "Gardée et modifiée"))262 try await a.store.setSyncCursor(0)263 #expect(await a.sync() == .completed)264 #expect(try await a.store.concepts().isEmpty)265 #expect(try await a.store.edges(touching: ids.page).isEmpty)266 #expect(try await a.tasks.list(.all).map(\.id) == [kept])267 #expect(fake.task(kept)?.title == "Gardée et modifiée")268 #expect(try await a.store.databases().map(\.id) == [ids.db])269 await world.closeAll()270 }271272 @Test("an entity operation this build cannot execute is kept and surfaced, and does not block the tasks")273 func unsupportedEntityOperation() async throws {274 let world = World(apiLevel: 3)275 let a = try world.device("A")276 let rowId = UUID().uuidString.lowercased()277 try await a.store.saveLocalEntity(kind: .databaseRow, id: rowId, json: Data(#"{"databaseId":"d","values":{}}"#.utf8), op: EntityOpVerb.update, payload: Data("{}".utf8))278 let task = try await a.tasks.create(title: "Passe quand même")279 #expect(await a.sync() == .completed)280 #expect(world.fake.task(task.id) != nil)281 let parked = try await a.store.entityOps(state: .unsupported)282 #expect(parked.map(\.entityId) == [rowId])283 #expect(parked.first?.payload == Data("{}".utf8), "payload intact")284 let status = await a.engine.currentStatus()285 #expect(status.unsupported == 1)286 #expect(status.pending == 0)287 await world.closeAll()288 }289}290