SPB Git

Repository Public

EntityTests.swift 32.4 KB 447 lines swift

1// SP Notes Core2// File: Tests/SPNotesStoreTests/EntityTests.swift3// Description: The 2.0 tables: generic entities (feed application, typed queries, cursors, local edits), the ordered4//              entity outbox (nothing unsupported or refused is ever dropped), parked ops of the 1.x queue, canvases,5//              the events cache and the read-model cache.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import Testing11@testable import SPNotesStore1213private func j(_ text: String) -> Data { Data(text.utf8) }1415/// Collects the changes a store publishes.16private actor Recorder {17    var seen: [StoreChange] = []18    func add(_ change: StoreChange) { seen.append(change) }19}2021private func record(_ store: NotesStore) async -> (Recorder, Task<Void, Never>) {22    let recorder = Recorder()23    let stream = await store.changes()24    let task = Task { for await change in stream { await recorder.add(change) } }25    return (recorder, task)26}2728extension NotesStore {29    func rawRun(_ sql: String) throws { try write { _ = try db.run(sql) } }3031    func applyThenFail(_ rows: [RemoteEntity], cursors: [EntityKind: Int64]) throws {32        try write {33            _ = try applyRemoteEntities(rows, cursors: cursors)34            throw StoreError.invalid("boom")35        }36    }37}3839@Suite struct EntityTests {40    @Test func kindsAndFeedParameter() {41        #expect(EntityKind.all.count == 9)42        #expect(EntityKind.feedParameter() == "task,database,database_property,database_view,database_row,edge,reference,canvas,concept")43        #expect(EntityKind.feedParameter([.task, .edge]) == "task,edge")44        #expect(EntityKind("future_kind").rawValue == "future_kind")45        let decoded = try? JSONDecoder().decode([EntityKind].self, from: j(#"["task","whatever"]"#))46        #expect(decoded == [.task, EntityKind("whatever")])47    }4849    @Test func indexExtraction() {50        #expect(EntityIndex.extract(kind: .task, json: j(#"{"pageId":"p1","blockId":"b","due":"2026-12-01"}"#)) == EntityIndex(scope: "p1", ref: "b", sort: .text("2026-12-01")))51        #expect(EntityIndex.extract(kind: .task, json: j(#"{"page_id":"p1","due_at":null}"#)) == EntityIndex(scope: "p1"))52        #expect(EntityIndex.extract(kind: .databaseRow, json: j(#"{"database_id":"d","position":2.5}"#)) == EntityIndex(scope: "d", sort: .number(2.5)))53        #expect(EntityIndex.extract(kind: .edge, json: j(#"{"sourceId":"a","targetId":"b"}"#)) == EntityIndex(scope: "a", ref: "b"))54        #expect(EntityIndex.extract(kind: .concept, json: j(#"{"name":"Liquidité"}"#)) == EntityIndex(sort: .text("Liquidité")))55        #expect(EntityIndex.extract(kind: .databaseView, json: j(#"{"databaseId":"d","position":true}"#)) == EntityIndex(scope: "d"))   // a boolean is not a position56        #expect(EntityIndex.extract(kind: EntityKind("unknown"), json: j(#"{"pageId":"p"}"#)) == EntityIndex())57        #expect(EntityIndex.extract(kind: .task, json: j("not json")) == EntityIndex())58        #expect(EntityIndex.extract(kind: .task, json: j("[1]")) == EntityIndex())59    }6061    @Test func applyFeedUpsertsOrdersAndIsIdempotent() async throws {62        let store = try NotesStore.inMemory()63        let rows = [64            RemoteEntity(kind: .databaseProperty, id: "p2", rev: 5, json: j(#"{"databaseId":"d1","name":"Statut","position":2}"#)),65            RemoteEntity(kind: .databaseProperty, id: "p1", rev: 4, json: j(#"{"databaseId":"d1","name":"Nom","position":1}"#)),66            RemoteEntity(kind: .databaseProperty, id: "p3", rev: 6, json: j(#"{"databaseId":"d2","name":"Autre","position":1}"#)),67            RemoteEntity(kind: .database, id: "d1", rev: 3, json: j(#"{"title":"Projets"}"#), updatedAt: epoch),68            RemoteEntity(kind: EntityKind("future_kind"), id: "f1", rev: 7, json: j(#"{"anything":[1,2,{"x":null}]}"#)),69        ]70        let summary = try await store.applyRemoteEntities(rows, cursors: [.databaseProperty: 6, .database: 3])71        #expect(summary.applied == 5 && summary.stale == 0 && summary.removed == 0 && summary.keptLocal.isEmpty)72        #expect(try await store.databaseProperties(databaseId: "d1").map(\.id) == ["p1", "p2"])73        #expect(try await store.databaseProperties(databaseId: "d2").map(\.id) == ["p3"])74        #expect(try await store.databases().first?.updatedAt == epoch)75        #expect(try await store.entity(kind: EntityKind("future_kind"), id: "f1")?.json == j(#"{"anything":[1,2,{"x":null}]}"#))   // bytes untouched76        #expect(try await store.entityCursor(kind: .databaseProperty) == 6)77        #expect(try await store.entityCursor(kind: .task) == 0)78        #expect(try await store.entityCursors() == [.databaseProperty: 6, .database: 3])79        #expect(try await store.entityCounts() == [.databaseProperty: 3, .database: 1, EntityKind("future_kind"): 1])80        #expect(try await store.entityCount(kind: .databaseProperty, scope: "d1") == 2)81        // The same window again changes nothing.82        let again = try await store.applyRemoteEntities(rows, cursors: [.databaseProperty: 6])83        #expect(again.applied == 0 && again.stale == 5)84        // A newer revision replaces the row and its lookup columns.85        _ = try await store.applyRemoteEntities([RemoteEntity(kind: .databaseProperty, id: "p1", rev: 9, json: j(#"{"databaseId":"d2","name":"Nom","position":0}"#))])86        #expect(try await store.databaseProperties(databaseId: "d1").map(\.id) == ["p2"])87        #expect(try await store.databaseProperties(databaseId: "d2").map(\.id) == ["p1", "p3"])88        #expect(try await store.entity(kind: .databaseProperty, id: "p1")?.rev == 9)89        struct Property: Decodable, Equatable { var name: String; var position: Int }90        #expect(try await store.entity(kind: .databaseProperty, id: "p1")?.decode(Property.self) == Property(name: "Nom", position: 0))91    }9293    @Test func typedQueriesUseTheIndexes() async throws {94        let store = try NotesStore.inMemory()95        _ = try await store.applyRemoteEntities([96            RemoteEntity(kind: .task, id: "t1", rev: 1, json: j(#"{"pageId":"pg","title":"b","due":"2026-12-05"}"#)),97            RemoteEntity(kind: .task, id: "t2", rev: 2, json: j(#"{"pageId":"pg","title":"a","due":"2026-11-30"}"#)),98            RemoteEntity(kind: .task, id: "t3", rev: 3, json: j(#"{"pageId":"pg","title":"sans date"}"#)),99            RemoteEntity(kind: .task, id: "t4", rev: 4, json: j(#"{"title":"sans page","due":"2026-01-01"}"#)),100            RemoteEntity(kind: .edge, id: "e1", rev: 5, json: j(#"{"sourceId":"a","targetId":"b","relType":"supports"}"#)),101            RemoteEntity(kind: .edge, id: "e2", rev: 6, json: j(#"{"sourceId":"c","targetId":"a"}"#)),102            RemoteEntity(kind: .edge, id: "e3", rev: 7, json: j(#"{"sourceId":"a","targetId":"a"}"#)),103            RemoteEntity(kind: .databaseRow, id: "r1", rev: 8, json: j(#"{"databaseId":"d","pageId":"row-page","position":1}"#)),104            RemoteEntity(kind: .reference, id: "ref1", rev: 9, json: j(#"{"title":"Black-Scholes 1973","projectId":"prj"}"#)),105            RemoteEntity(kind: .concept, id: "c2", rev: 10, json: j(#"{"name":"Volatilité"}"#)),106            RemoteEntity(kind: .concept, id: "c1", rev: 11, json: j(#"{"name":"Arbitrage"}"#)),107            RemoteEntity(kind: .databaseView, id: "v1", rev: 12, json: j(#"{"databaseId":"d","position":0}"#)),108        ])109        #expect(try await store.tasks(pageId: "pg").map(\.id) == ["t2", "t1", "t3"])       // by due date, undated last110        #expect(try await store.tasks().map(\.id) == ["t4", "t2", "t1", "t3"])111        #expect(try await store.edges(from: "a").map(\.id) == ["e1", "e3"])112        #expect(try await store.edges(to: "a").map(\.id) == ["e2", "e3"])113        #expect(try await store.edges(touching: "a").map(\.id) == ["e1", "e3", "e2"])114        #expect(try await store.edges(to: "zzz").isEmpty)115        #expect(try await store.databaseRows(databaseId: "d").map(\.id) == ["r1"])116        #expect(try await store.databaseViews(databaseId: "d").map(\.id) == ["v1"])117        #expect(try await store.references(projectId: "prj").map(\.id) == ["ref1"])118        #expect(try await store.references().count == 1)119        #expect(try await store.concepts().map(\.id) == ["c1", "c2"])120        #expect(try await store.entities(kind: .task, limit: 2, offset: 1).map(\.id) == ["t2", "t1"])121        let plan = try await store.queryPlan("SELECT id FROM entities WHERE kind='task' AND scope='pg' AND deleted=0 ORDER BY sort_key IS NULL, sort_key, id")122        #expect(plan.contains("entities_scope") || plan.contains("sqlite_autoindex_entities"))123        let backlinks = try await store.queryPlan("SELECT id FROM entities WHERE kind='edge' AND ref='a' AND deleted=0")124        #expect(backlinks.contains("entities_ref"))125    }126127    @Test func tombstonesAndCursorCommitWithTheRows() async throws {128        let store = try NotesStore.inMemory()129        _ = try await store.applyRemoteEntities([RemoteEntity(kind: .concept, id: "c", rev: 1, json: j("{}")), RemoteEntity(kind: .concept, id: "d", rev: 2, json: j("{}"))], cursors: [.concept: 2])130        let summary = try await store.applyRemoteEntities([], tombstones: [EntityTombstone(kind: .concept, id: "c"), EntityTombstone(kind: .concept, id: "absent"), EntityTombstone(kind: .task, id: "c")],131                                                         cursors: [.concept: 5])132        #expect(summary.removed == 1)133        #expect(try await store.concepts().map(\.id) == ["d"])134        #expect(try await store.entityCursor(kind: .concept) == 5)135        // After a reset the cursors restart and the rows stay.136        try await store.resetEntitySync(kinds: [.concept])137        #expect(try await store.entityCursor(kind: .concept) == 0)138        #expect(try await store.entity(kind: .concept, id: "d")?.rev == 0)139        #expect(try await store.applyRemoteEntities([RemoteEntity(kind: .concept, id: "d", rev: 1, json: j(#"{"name":"x"}"#))]).applied == 1)140        // After a full re-download, clean rows the server no longer has leave; dirty ones stay.141        _ = try await store.saveLocalEntity(kind: .concept, id: "local", json: j(#"{"name":"brouillon"}"#), op: EntityOpVerb.create)142        _ = try await store.applyRemoteEntities([RemoteEntity(kind: .concept, id: "gone", rev: 2, json: j("{}"))])143        #expect(try await store.removeSyncedEntities(kind: .concept, notIn: ["d"]) == ["gone"])144        #expect(Set(try await store.concepts().map(\.id)) == ["d", "local"])145        try await store.setEntityCursor(kind: .task, 3)146        try await store.resetEntitySync()147        #expect(try await store.entityCursors().isEmpty)148    }149150    @Test func localEditsWinUntilTheirOutboxIsEmptyAndNothingRemoteIsLost() async throws {151        let store = try NotesStore.inMemory()152        _ = try await store.applyRemoteEntities([RemoteEntity(kind: .task, id: "t", rev: 3, json: j(#"{"title":"serveur v1","pageId":"p"}"#))])153        let op = try await store.saveLocalEntity(kind: .task, id: "t", json: j(#"{"title":"local","pageId":"p"}"#), op: EntityOpVerb.update,154                                                 payload: j(#"{"title":"local"}"#), baseVersion: 3)155        #expect(op.state == .pending && op.baseVersion == 3 && op.payload == j(#"{"title":"local"}"#))156        #expect(op.idempotencyKey.range(of: "^[0-9a-f]{32}$", options: .regularExpression) != nil)157        #expect(try await store.dirtyEntities().map(\.id) == ["t"])158        // The feed delivers a newer server copy while the edit waits: the local JSON stays, the server copy is kept aside.159        let summary = try await store.applyRemoteEntities([RemoteEntity(kind: .task, id: "t", rev: 8, json: j(#"{"title":"serveur v2","pageId":"p"}"#))])160        #expect(summary.keptLocal == [EntityTombstone(kind: .task, id: "t")])161        var row = try #require(try await store.entity(kind: .task, id: "t"))162        #expect(row.json == j(#"{"title":"local","pageId":"p"}"#))163        #expect(row.remoteJSON == j(#"{"title":"serveur v2","pageId":"p"}"#))164        #expect(row.dirty && row.rev == 8)165        // Not clean while the outbox still holds an entry.166        #expect(try await store.markEntitySynced(kind: .task, id: "t") == false)167        try await store.completeEntityOp(id: op.id)168        #expect(try await store.markEntitySynced(kind: .task, id: "t", json: j(#"{"title":"local","pageId":"p","version":4}"#), rev: 9))169        row = try #require(try await store.entity(kind: .task, id: "t"))170        #expect(!row.dirty && row.remoteJSON == nil && row.rev == 9)171        #expect(row.json == j(#"{"title":"local","pageId":"p","version":4}"#))172        // A local delete keeps the row (flagged) until acknowledged, then removes it.173        let del = try await store.saveLocalEntity(kind: .task, id: "t", json: nil, op: EntityOpVerb.delete)174        #expect(try await store.tasks().isEmpty)175        #expect(try await store.entities(kind: .task, includeDeleted: true).count == 1)176        try await store.completeEntityOp(id: del.id)177        #expect(try await store.markEntitySynced(kind: .task, id: "t"))178        #expect(try await store.entity(kind: .task, id: "t") == nil)179        // A tombstone on a dirty row never destroys local work.180        _ = try await store.applyRemoteEntities([RemoteEntity(kind: .task, id: "u", rev: 1, json: j(#"{"title":"u"}"#))])181        _ = try await store.saveLocalEntity(kind: .task, id: "u", json: j(#"{"title":"u modifié"}"#), op: EntityOpVerb.update)182        let stoned = try await store.applyRemoteEntities([], tombstones: [EntityTombstone(kind: .task, id: "u")])183        #expect(stoned.removed == 0 && stoned.keptLocal.count == 1)184        #expect(try await store.entity(kind: .task, id: "u")?.json == j(#"{"title":"u modifié"}"#))185        #expect(try await store.entityOpCount() == 1)186        // Arguments are checked; an action on a row that does not exist needs its JSON.187        await #expect(throws: StoreError.self) { _ = try await store.saveLocalEntity(kind: .task, id: "", json: j("{}"), op: "create") }188        await #expect(throws: StoreError.self) { _ = try await store.saveLocalEntity(kind: .task, id: "nope", json: nil, op: "update") }189    }190191    @Test func outboxOrderBackoffAndBlocking() async throws {192        let store = try NotesStore.inMemory()193        let a1 = try await store.enqueueEntityOp(kind: .task, entityId: "a", op: EntityOpVerb.create, payload: j("{}"))194        let a2 = try await store.enqueueEntityOp(kind: .task, entityId: "a", op: EntityOpVerb.update)195        let b1 = try await store.enqueueEntityOp(kind: .task, entityId: "b", op: EntityOpVerb.create)196        let c1 = try await store.enqueueEntityOp(kind: .edge, entityId: "a", op: EntityOpVerb.create)   // same id, other kind: independent197        #expect(try await store.nextEntityOps().map(\.id) == [a1.id, a2.id, b1.id, c1.id])198        #expect(try await store.nextEntityOps(limit: 2).map(\.id) == [a1.id, a2.id])199        // A failed attempt backs off and holds back the later ops of the same entity only.200        let now = Date()201        let failed = try #require(try await store.failEntityOp(id: a1.id, error: "http 503", now: now))202        #expect(failed.attempts == 1 && failed.lastError == "http 503" && failed.state == .pending)203        #expect(failed.nextAttemptAt.timeIntervalSince(now) > 4.9 && failed.nextAttemptAt.timeIntervalSince(now) < 5.1)204        #expect(try await store.nextEntityOps(now: now).map(\.id) == [b1.id, c1.id])205        #expect(try await store.nextEntityOps(now: now.addingTimeInterval(6)).map(\.id) == [a1.id, a2.id, b1.id, c1.id])206        _ = try await store.failEntityOp(id: a1.id, error: "http 503", now: now)207        #expect(try await store.entityOps(kind: .task, entityId: "a").first?.attempts == 2)208        #expect(try await store.nextEntityOps(now: now.addingTimeInterval(6)).map(\.id) == [b1.id, c1.id])   // 10 s the second time209        try await store.completeEntityOp(id: a1.id)210        try await store.completeEntityOp(id: 999_999)   // unknown: ignored211        #expect(try await store.nextEntityOps(now: now).map(\.id) == [a2.id, b1.id, c1.id])212        #expect(try await store.failEntityOp(id: 999_999, error: "x") == nil)213        await #expect(throws: StoreError.self) { _ = try await store.enqueueEntityOp(kind: .task, entityId: "", op: "x") }214    }215216    @Test func unsupportedAndRejectedOpsAreKeptSurfacedAndNeverDropped() async throws {217        let temp = TempDatabase()218        var store = try NotesStore(url: temp.url)219        let future = try await store.enqueueEntityOp(kind: EntityKind("hologram"), entityId: "h", op: "materialise", payload: j(#"{"v":1}"#))220        let later = try await store.enqueueEntityOp(kind: EntityKind("hologram"), entityId: "h", op: EntityOpVerb.update)221        let other = try await store.enqueueEntityOp(kind: .task, entityId: "t", op: EntityOpVerb.create)222        let refused = try await store.enqueueEntityOp(kind: .task, entityId: "r", op: EntityOpVerb.update)223        try await store.markEntityOpUnsupported(id: future.id, reason: "no handler for hologram/materialise")224        try await store.rejectEntityOp(id: refused.id, error: "http 422 validation")225        // Kept, with payload and key; later ops of the same entity wait behind it; other entities flow.226        #expect(try await store.nextEntityOps().map(\.id) == [other.id])227        #expect(try await store.entityOpCount() == 4)228        #expect(try await store.entityOps(state: .unsupported).map(\.id) == [future.id])229        #expect(try await store.entityOps(state: .rejected).map(\.lastError) == ["http 422 validation"])230        #expect(try await store.entityOpCount(state: .pending) == 2)231        // Survives a restart, byte for byte.232        await store.close()233        store = try NotesStore(url: temp.url)234        let kept = try #require(try await store.entityOps(state: .unsupported).first)235        #expect(kept.payload == j(#"{"v":1}"#) && kept.idempotencyKey == future.idempotencyKey && kept.op == "materialise" && kept.lastError == "no handler for hologram/materialise")236        // A build that now supports it releases it; order is preserved.237        #expect(try await store.retryEntityOps(where: { kind, op in kind.rawValue == "hologram" && op == "materialise" }) == 1)238        #expect(try await store.nextEntityOps().map(\.id) == [future.id, later.id, other.id])239        #expect(try await store.entityOps(state: .rejected).count == 1)240        // Only an explicit decision removes an unsent op, and it hands the op back.241        #expect(try await store.retryEntityOps(id: refused.id) == 1)242        #expect(try await store.entityOpCount(state: .rejected) == 0)243        try await store.rejectEntityOp(id: refused.id, error: "again")244        #expect(try await store.discardEntityOp(id: refused.id)?.entityId == "r")245        #expect(try await store.discardEntityOp(id: refused.id) == nil)246        #expect(try await store.entityOpCount() == 3)247        // An unknown state string written by a newer build reads as unsupported (never as pending).248        try await store.rawRun("UPDATE entity_ops SET state='quarantined' WHERE id=\(other.id)")249        #expect(try await store.entityOps().first { $0.id == other.id }?.state == .unsupported)250        #expect(try await store.nextEntityOps().map(\.id) == [future.id, later.id])251    }252253    @Test func parkedOpsOfTheLegacyQueue() async throws {254        let temp = TempDatabase()255        var store = try NotesStore(url: temp.url)256        let unknown = try await store.enqueueOp(kind: "futureKind", targetId: "page-1", payload: j(#"{"a":1}"#))257        let trash = try await store.enqueueOp(kind: OpKind.trash, targetId: "page-1")258        _ = try await store.nextOps()259        try await store.parkOp(id: unknown.id, reason: "unsupported op kind: futureKind")260        try await store.parkOp(id: 424242, reason: "ignored")261        try await store.releaseAllOpLeases()262        // Never handed out again, never removed, and it does not hold back the later op of the same target.263        #expect(try await store.nextOps().map(\.id) == [trash.id])264        #expect(try await store.opCount() == 2)265        #expect(try await store.parkedOpCount() == 1)266        let parked = try #require(try await store.parkedOps().first)267        #expect(parked.op.kind == "futureKind" && parked.op.payload == j(#"{"a":1}"#) && parked.op.idempotencyKey == unknown.idempotencyKey)268        #expect(parked.reason == "unsupported op kind: futureKind" && parked.id == unknown.id)269        await store.close()270        store = try NotesStore(url: temp.url)271        #expect(try await store.parkedOps().map(\.id) == [unknown.id])272        // Released only for the kinds asked.273        #expect(try await store.unparkOps(kinds: ["somethingElse"]) == 0)274        #expect(try await store.unparkOps(kinds: ["futureKind"]) == 1)275        #expect(try await store.parkedOpCount() == 0)276        try await store.releaseAllOpLeases()277        #expect(try await store.nextOps().map(\.id) == [unknown.id, trash.id])278        // Completing a parked op clears its marker.279        try await store.parkOp(id: unknown.id, reason: "r")280        try await store.completeOp(id: unknown.id)281        #expect(try await store.parkedOpCount() == 0)282        #expect(try await store.debugValue("SELECT count(*) FROM ops_parked") == "0")283        #expect(try await store.unparkOps() == 0)284    }285286    @Test func canvasLifecycle() async throws {287        let store = try NotesStore.inMemory()288        #expect(try await store.canvas(pageId: "c") == nil)289        // Created offline.290        #expect(try await store.saveCanvas(pageId: "c", json: j(#"{"v":1}"#)))291        #expect(try await store.saveCanvas(pageId: "c", json: j(#"{"v":1}"#)) == false)292        var c = try #require(try await store.canvas(pageId: "c"))293        #expect(c.dirty && c.version == 0 && c.baseJSON == nil && c.editSeq == 1)294        #expect(try await store.canvasesNeedingPush().map(\.pageId) == ["c"])295        // Pushed while a new edit happened: the pushed copy becomes the base, the canvas stays dirty.296        _ = try await store.saveCanvas(pageId: "c", json: j(#"{"v":2}"#))297        try await store.markCanvasPushed(pageId: "c", version: 1, pushedEditSeq: 1, pushed: j(#"{"v":1}"#))298        c = try #require(try await store.canvas(pageId: "c"))299        #expect(c.dirty && c.version == 1 && c.baseJSON == j(#"{"v":1}"#))300        try await store.markCanvasPushed(pageId: "c", version: 2, pushedEditSeq: c.editSeq, pushed: c.json)301        c = try #require(try await store.canvas(pageId: "c"))302        #expect(!c.dirty && c.version == 2 && c.baseJSON == nil)303        #expect(try await store.canvasesNeedingPush().isEmpty)304        // A clean canvas takes the server copy; an older or identical one changes nothing.305        #expect(try await store.applyRemoteCanvas(pageId: "c", json: j(#"{"v":3}"#), version: 3, rev: 40) == .applied)306        #expect(try await store.applyRemoteCanvas(pageId: "c", json: j(#"{"v":3}"#), version: 3, rev: 41) == .unchanged)307        #expect(try await store.applyRemoteCanvas(pageId: "c", json: j(#"{"v":0}"#), version: 1) == .unchanged)308        #expect(try await store.canvas(pageId: "c")?.json == j(#"{"v":3}"#))309        #expect(try await store.canvas(pageId: "c")?.rev == 41)310        // A local edit keeps the synced copy as base; a newer server copy is NOT applied: the three copies come back.311        _ = try await store.saveCanvas(pageId: "c", json: j(#"{"v":"local"}"#))312        _ = try await store.saveCanvas(pageId: "c", json: j(#"{"v":"local2"}"#))313        #expect(try await store.canvas(pageId: "c")?.baseJSON == j(#"{"v":3}"#))314        let result = try await store.applyRemoteCanvas(pageId: "c", json: j(#"{"v":"remote"}"#), version: 4)315        #expect(result == .diverged(base: j(#"{"v":3}"#), local: j(#"{"v":"local2"}"#), remote: j(#"{"v":"remote"}"#), remoteVersion: 4))316        #expect(try await store.canvas(pageId: "c")?.json == j(#"{"v":"local2"}"#))317        // Clean merge: stored, rebased on the server copy, still to push.318        try await store.resolveCanvas(pageId: "c", merged: j(#"{"v":"merged"}"#), remote: j(#"{"v":"remote"}"#), remoteVersion: 4)319        c = try #require(try await store.canvas(pageId: "c"))320        #expect(c.json == j(#"{"v":"merged"}"#) && c.baseJSON == j(#"{"v":"remote"}"#) && c.version == 4 && c.dirty)321        // Unmergeable: the server copy is kept next to the untouched local canvas, which waits for a decision.322        #expect(try await store.applyRemoteCanvas(pageId: "c", json: j(#"{"v":"remote5"}"#), version: 5) != .applied)323        try await store.recordCanvasConflict(pageId: "c", remote: j(#"{"v":"remote5"}"#), remoteVersion: 5)324        c = try #require(try await store.canvas(pageId: "c"))325        #expect(c.hasConflict && c.json == j(#"{"v":"merged"}"#) && c.conflictVersion == 5)326        #expect(try await store.canvasesNeedingPush().isEmpty)327        #expect(try await store.canvasesNeedingPush(includeConflicts: true).count == 1)328        #expect(try await store.canvasConflicts().map(\.pageId) == ["c"])329        try await store.discardCanvasConflict(pageId: "c")330        c = try #require(try await store.canvas(pageId: "c"))331        #expect(!c.hasConflict && c.version == 5 && c.baseJSON == j(#"{"v":"remote5"}"#) && c.json == j(#"{"v":"merged"}"#) && c.dirty)332        // A merge that equals the server copy leaves nothing to push.333        try await store.resolveCanvas(pageId: "c", merged: j(#"{"v":"same"}"#), remote: j(#"{"v":"same"}"#), remoteVersion: 6)334        #expect(try await store.canvas(pageId: "c")?.dirty == false)335        try await store.markCanvasPushFailed(pageId: "c", error: "http 500")336        #expect(try await store.canvas(pageId: "c")?.syncError == "http 500")337        await #expect(throws: StoreError.self) { try await store.resolveCanvas(pageId: "absent", merged: j("{}"), remote: j("{}"), remoteVersion: 1) }338        await #expect(throws: StoreError.self) { _ = try await store.saveCanvas(pageId: "", json: j("{}")) }339        try await store.deleteCanvas(pageId: "c")340        #expect(try await store.canvas(pageId: "c") == nil)341    }342343    @Test func purgingACanvasPageRemovesItsCanvas() async throws {344        let store = try NotesStore.inMemory()345        let page = try await store.createPage(NewPage(type: "canvas", title: "Tableau blanc", content: doc(""), properties: j("{}"), plainText: "", wordCount: 0, preview: ""))346        #expect(try await store.page(id: page.id)?.type == "canvas")347        _ = try await store.saveCanvas(pageId: page.id, json: j(#"{"format":"spn-canvas-1"}"#))348        _ = try await store.trash(id: page.id)349        #expect(try await store.canvas(pageId: page.id) != nil)   // the trash keeps everything350        _ = try await store.purge(id: page.id)351        #expect(try await store.canvas(pageId: page.id) == nil)352    }353354    @Test func eventsCache() async throws {355        let store = try NotesStore.inMemory()356        func at(_ hours: Double) -> Date { epoch.addingTimeInterval(hours * 3600) }357        try await store.cacheEvents([358            EventRecord(id: "e1", at: at(1), kind: "page.created", pageId: "p1", summary: "Création"),359            EventRecord(id: "e2", at: at(2), kind: "page.edited", pageId: "p1", actor: "user", device: "Mac"),360            EventRecord(id: "e3", at: at(3), kind: "task.completed", pageId: "p2", entityKind: "task", entityId: "t1"),361            EventRecord(id: "e4", at: at(4), kind: "ai.applied", pageId: "p1", actor: "ai", data: j(#"{"model":"x"}"#)),362        ], fetchedAt: at(10))363        try await store.cacheEvents([])364        #expect(try await store.eventCount() == 4)365        #expect(try await store.events().map(\.id) == ["e4", "e3", "e2", "e1"])366        #expect(try await store.events(EventQuery(pageId: "p1")).map(\.id) == ["e4", "e2", "e1"])367        #expect(try await store.events(EventQuery(kinds: ["page.created", "task.completed"])).map(\.id) == ["e3", "e1"])368        #expect(try await store.events(EventQuery(kindPrefix: "page.")).map(\.id) == ["e2", "e1"])369        #expect(try await store.events(EventQuery(from: at(2), to: at(3))).map(\.id) == ["e3", "e2"])370        #expect(try await store.events(EventQuery(actor: "ai")).first?.data == j(#"{"model":"x"}"#))371        #expect(try await store.events(EventQuery(limit: 1, offset: 1)).map(\.id) == ["e3"])372        #expect(try await store.latestEventDate() == at(4))373        let e3 = try #require(try await store.events(EventQuery(kinds: ["task.completed"])).first)374        #expect(e3.entityKind == "task" && e3.entityId == "t1" && e3.fetchedAt == at(10) && e3.actor == "user")375        // The same id again replaces (the server coalesces page.edited).376        try await store.cacheEvents([EventRecord(id: "e2", at: at(2.5), kind: "page.edited", pageId: "p1", summary: "révisé")], fetchedAt: at(11))377        #expect(try await store.eventCount() == 4)378        #expect(try await store.events(EventQuery(kinds: ["page.edited"])).first?.summary == "révisé")379        #expect(try await store.clearEvents(before: at(2.4)) == 1)380        #expect(try await store.clearEvents() == 3)381        #expect(try await store.latestEventDate() == nil)382    }383384    @Test func readModelCache() async throws {385        let store = try NotesStore.inMemory()386        let fetched = epoch387        try await store.setCached(CacheKey.health, json: j(#"{"orphans":3}"#), fetchedAt: fetched, etag: "W/1")388        try await store.setCached(CacheKey.graph(view: "local", scope: "p1"), json: j("{}"), fetchedAt: fetched)389        try await store.setCached(CacheKey.graph(view: "global"), json: j("{}"), fetchedAt: fetched)390        struct Brief: Codable, Equatable, Sendable { var due: Int }391        try await store.setCached(CacheKey.brief, value: Brief(due: 2), fetchedAt: fetched)392        let health = try #require(try await store.cached(CacheKey.health))393        #expect(health.json == j(#"{"orphans":3}"#) && health.fetchedAt == fetched && health.etag == "W/1")394        #expect(health.age(now: fetched.addingTimeInterval(90)) == 90)395        #expect(health.isFresh(maxAge: 120, now: fetched.addingTimeInterval(90)))396        #expect(!health.isFresh(maxAge: 60, now: fetched.addingTimeInterval(90)))397        #expect(try await store.cached(CacheKey.brief)?.decode(Brief.self) == Brief(due: 2))398        #expect(try await store.cached("absent") == nil)399        #expect(try await store.cachedKeys(prefix: "graph.").keys.sorted() == ["graph.global", "graph.local.p1"])400        #expect(try await store.cachedKeys().count == 4)401        try await store.setCached(CacheKey.health, json: j(#"{"orphans":0}"#), fetchedAt: fetched.addingTimeInterval(5))402        #expect(try await store.cached(CacheKey.health)?.json == j(#"{"orphans":0}"#))403        #expect(try await store.cached(CacheKey.health)?.etag == nil)404        #expect(try await store.removeCached(prefix: "graph.") == 2)405        #expect(try await store.removeCached(key: CacheKey.brief) == 1)406        #expect(try await store.removeCached(key: "absent") == 0)407        #expect(try await store.cachedKeys().keys.sorted() == [CacheKey.health])408        #expect(CacheKey.zoom(pageId: "p", level: 2) == "zoom.p.2")409        #expect(CacheKey.query("abc") == "query.abc")410        await #expect(throws: StoreError.self) { try await store.setCached("", json: j("{}")) }411    }412413    @Test func changesArePublishedPerKind() async throws {414        let store = try NotesStore.inMemory()415        let (recorder, task) = await record(store)416        _ = try await store.applyRemoteEntities([RemoteEntity(kind: .task, id: "t", rev: 1, json: j("{}")), RemoteEntity(kind: .edge, id: "e", rev: 2, json: j("{}"))])417        _ = try await store.saveLocalEntity(kind: .task, id: "t2", json: j("{}"), op: EntityOpVerb.create)418        _ = try await store.saveCanvas(pageId: "c", json: j("{}"))419        try await store.cacheEvents([EventRecord(id: "e", at: epoch, kind: "page.created")])420        try await store.setCached(CacheKey.brief, json: j("{}"))421        let op = try await store.enqueueOp(kind: "x")422        try await store.parkOp(id: op.id, reason: "r")423        try await Task.sleep(for: .milliseconds(100))424        task.cancel()425        let seen = await recorder.seen426        #expect(seen.contains(.entities(kind: "task", ids: ["t"])))427        #expect(seen.contains(.entities(kind: "edge", ids: ["e"])))428        #expect(seen.contains(.entities(kind: "task", ids: ["t2"])))429        #expect(seen.contains(.entityQueue))430        #expect(seen.contains(.canvases(["c"])))431        #expect(seen.contains(.events))432        #expect(seen.contains(.cache("brief")))433        #expect(seen.filter { $0 == .queue }.count == 2)434    }435436    @Test func aFailedFeedWindowWritesNothing() async throws {437        let store = try NotesStore.inMemory()438        _ = try await store.applyRemoteEntities([RemoteEntity(kind: .task, id: "t", rev: 1, json: j("{}"))], cursors: [.task: 1])439        // A statement fails after the window was applied inside the same transaction: everything rolls back.440        await #expect(throws: StoreError.self) {441            try await store.applyThenFail([RemoteEntity(kind: .task, id: "u", rev: 2, json: j("{}"))], cursors: [.task: 2])442        }443        #expect(try await store.tasks().map(\.id) == ["t"])444        #expect(try await store.entityCursor(kind: .task) == 1)445    }446}447
Files main
Menu

Theme