Repository Public
1// SP Notes Core2// File: Tests/SPNotesSyncTests/FaultTests.swift3// Description: Scenario tests of the synchronisation engine, part 2: lost acknowledgements, edits made while a push is in flight,4// sign-out and network loss in the middle of a cycle, failing and misbehaving servers, status stream, re-entrancy of5// sync()/schedule(), stop().6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesAPI11import SPNotesStore12import Testing13@testable import SPNotesSync14@testable import SPNotesTestSupport1516@Suite("Lost acknowledgements")17struct LostAcknowledgementTests {18 @Test("create: the retry neither duplicates the note nor raises a conflict")19 func create() async throws {20 let world = World()21 let fake = world.fake22 let a = try world.device("A")23 let id = try await a.create("New note", [("a", "hello")], tags: ["x"])24 fake.inject(.dropResponse, method: "POST", path: "^/api/pages$")25 #expect(await a.sync() == .offline, "a broken connection reads as offline")26 #expect(fake.page(id)?.text == "hello", "the server applied the creation")27 #expect(try await a.page(id).needsCreate, "the device does not know")2829 #expect(await a.sync() == .completed)30 #expect(fake.allPages.count == 1)31 #expect(fake.page(id)?.version == 1, "nothing was written twice")32 #expect(fake.page(id)?.tags == ["x"])33 #expect(try await a.store.conflicts().isEmpty)34 try await expectMirror(a)35 #expect(fake.requests.filter { $0.method == "POST" && $0.path == "/api/pages" }.allSatisfy { $0.idempotencyKey == nil },36 "creations carry no idempotency key: the client id is the guarantee")37 await world.closeAll()38 }3940 @Test("create, then the note is edited before the retry: the edit reaches the server (a replayed or existing answer is never trusted)")41 func createThenEdit() async throws {42 let world = World()43 let fake = world.fake44 let a = try world.device("A")45 let id = try await a.create("Draft", [("a", "content A")], properties: #"{"favorite":true}"#)46 fake.inject(.dropResponse, method: "POST", path: "^/api/pages$")47 #expect(await a.sync() == .offline)48 try await a.edit(id, block: "a", text: "content B")49 try await a.setTitle(id, "Final")50 #expect(await a.sync() == .completed)51 let page = try #require(fake.page(id))52 #expect(page.text == "content B" && page.title == "Final")53 #expect(page.version == 2)54 #expect(fake.history(id).contains { $0.text == "content A" }, "the first copy is in the server history")55 #expect(try await a.store.conflicts().isEmpty)56 try await expectMirror(a)57 await world.closeAll()58 }5960 @Test("patch: the retry is recognised as already applied")61 func patch() async throws {62 let world = World()63 let fake = world.fake64 let a = try world.device("A")65 let id = try await a.create("Note", [("a", "one")])66 #expect(await a.sync() == .completed)67 try await a.edit(id, block: "a", text: "one two")68 fake.inject(.dropResponse, method: "PATCH", path: "^/api/pages/")69 #expect(await a.sync() == .offline)70 #expect(fake.page(id)?.version == 2)71 #expect(await a.sync() == .completed)72 #expect(fake.page(id)?.version == 2, "not written a second time")73 #expect(try await a.store.conflicts().isEmpty)74 #expect(try await a.texts(id) == ["one two"])75 try await expectMirror(a)76 await world.closeAll()77 }7879 @Test("patch, then typing goes on in the same paragraph: no false conflict with one's own write", arguments: [false, true])80 func patchThenKeepTyping(killed: Bool) async throws {81 let world = World()82 let fake = world.fake83 let a = try world.device("A")84 let id = try await a.create("Note", [("a", "one")])85 #expect(await a.sync() == .completed)86 try await a.edit(id, block: "a", text: "one two")87 fake.inject(.dropResponse, method: "PATCH", path: "^/api/pages/")88 #expect(await a.sync() == .offline)89 try await a.edit(id, block: "a", text: "one two three")90 if killed { try await a.restart() } // the memory of what was sent survives the app91 #expect(await a.sync() == .completed)92 #expect(try await a.store.conflicts().isEmpty)93 #expect(fake.page(id)?.text == "one two three")94 #expect(fake.page(id)?.version == 3)95 try await expectMirror(a)96 await world.closeAll()97 }9899 @Test("patch lost, then another device changes the same paragraph: this one IS a conflict")100 func patchLostThenForeignChange() async throws {101 let world = World()102 let fake = world.fake103 let a = try world.device("A")104 let id = try await a.create("Note", [("a", "one")])105 #expect(await a.sync() == .completed)106 try await a.edit(id, block: "a", text: "one two")107 fake.inject(.dropResponse, method: "PATCH", path: "^/api/pages/")108 #expect(await a.sync() == .offline)109 fake.webEdit(id, content: Doc.data([("a", "rewritten elsewhere")]))110 try await a.edit(id, block: "a", text: "one two three")111 #expect(await a.sync() == .completed)112 #expect(try await a.store.conflict(pageId: id) != nil)113 #expect(try await a.texts(id) == ["one two three"])114 #expect(fake.page(id)?.text == "rewritten elsewhere")115 await world.closeAll()116 }117118 @Test("upload: the retry does not store the file twice")119 func upload() async throws {120 let world = World()121 let fake = world.fake122 let a = try world.device("A")123 let id = try await a.create("With file", [("a", "x")])124 let bytes = Data((0..<5000).map { UInt8($0 % 256) })125 let file = try await a.attach(id, filename: "scan.pdf", mime: "application/pdf", bytes: bytes)126 fake.inject(.dropResponse, method: "POST", path: "^/api/attachments$")127 #expect(await a.sync() == .offline)128 #expect(fake.attachment(file.id)?.bytes == bytes)129 #expect(try await a.store.attachment(id: file.id)?.uploaded == false)130 #expect(try await a.store.attachment(id: file.id)?.state == .pendingUpload, "an interruption is not a failed upload")131 #expect(await a.sync() == .completed)132 #expect(fake.allAttachments.count == 1)133 #expect(try await a.store.attachment(id: file.id)?.uploaded == true)134 #expect(try await a.store.attachment(id: file.id)?.state == .local)135 await world.closeAll()136 }137138 @Test("trash, restore and tags: replays are harmless")139 func queueAndTags() async throws {140 let world = World()141 let fake = world.fake142 let a = try world.device("A")143 let id = try await a.create("Note", [("a", "x")])144 #expect(await a.sync() == .completed)145 _ = try await a.store.trash(id: id)146 fake.inject(.dropResponse, method: "DELETE", path: "^/api/pages/")147 #expect(await a.sync() == .offline)148 #expect(try await a.store.opCount() == 1)149 #expect(await a.sync() == .completed)150 #expect(fake.page(id)?.deletedAt != nil)151 #expect(fake.requests.last { $0.method == "DELETE" }?.replay == true, "same idempotency key: the stored answer was replayed")152 _ = try await a.store.restore(id: id)153 _ = try await a.store.setTags(id: id, names: ["a", "b"])154 fake.inject(.dropResponse, method: "POST", path: "action=restore")155 #expect(await a.sync() == .offline)156 #expect(await a.sync() == .completed)157 #expect(await a.sync() == .completed)158 #expect(fake.page(id)?.deletedAt == nil)159 #expect(fake.page(id)?.tags == ["a", "b"])160 try await expectMirror(a)161 await world.closeAll()162 }163}164165@Suite("Edits during a push, interruptions")166struct InFlightTests {167 @Test("an edit made while the push is in flight is kept and pushed by the next cycle", arguments: ["same block", "other block", "title", "pin"])168 func editDuringPatch(kind: String) async throws {169 let world = World()170 let fake = world.fake171 let a = try world.device("A")172 let id = try await a.create("Note", [("a", "one"), ("b", "two")])173 #expect(await a.sync() == .completed)174 try await a.edit(id, block: "a", text: "one+")175 let gate = Gate()176 fake.inject(.hold(gate), method: "PATCH", path: "^/api/pages/")177 async let outcome = a.sync()178 await gate.reached()179 switch kind {180 case "same block": try await a.edit(id, block: "a", text: "one++")181 case "other block": try await a.edit(id, block: "b", text: "two+")182 case "title": try await a.setTitle(id, "Renamed during the push")183 default: _ = try await a.store.setPinned(id: id, true)184 }185 gate.open()186 // sync() answers for the state of the store when it was called; the later edit belongs to the next cycle187 #expect(await outcome == .completed)188 let local = try await a.page(id)189 #expect(local.isDirty, "the edit made during the push is still waiting")190 #expect(fake.page(id)?.text == "one+\ntwo")191 #expect(await a.sync() == .completed)192 #expect(try await a.store.conflicts().isEmpty)193 switch kind {194 case "same block": #expect(fake.page(id)?.text == "one++\ntwo")195 case "other block": #expect(fake.page(id)?.text == "one+\ntwo+")196 case "title": #expect(fake.page(id)?.title == "Renamed during the push")197 default: #expect(fake.page(id)?.pinned == true)198 }199 try await expectMirror(a)200 await world.closeAll()201 }202203 @Test("an edit made while the creation is in flight is kept and pushed")204 func editDuringCreate() async throws {205 let world = World()206 let fake = world.fake207 let a = try world.device("A")208 let id = try await a.create("Note", [("a", "first words")])209 let gate = Gate()210 fake.inject(.hold(gate), method: "POST", path: "^/api/pages$")211 async let outcome = a.sync()212 await gate.reached()213 try await a.edit(id, block: "a", text: "first words and more")214 gate.open()215 #expect(await outcome == .completed)216 #expect(try await a.texts(id) == ["first words and more"])217 #expect(await a.sync() == .completed)218 #expect(fake.page(id)?.text == "first words and more")219 #expect(try await a.store.conflicts().isEmpty)220 try await expectMirror(a)221 await world.closeAll()222 }223224 @Test("a note purged on the device while its creation is in flight does not come back as a live note")225 func purgeDuringCreate() async throws {226 let world = World()227 let fake = world.fake228 let a = try world.device("A")229 let id = try await a.create("Short-lived", [("a", "x")])230 let gate = Gate()231 fake.inject(.hold(gate), method: "POST", path: "^/api/pages$")232 async let outcome = a.sync()233 await gate.reached()234 _ = try await a.store.purge(id: id)235 gate.open()236 #expect(await outcome == .completed)237 #expect(fake.page(id)?.deletedAt != nil, "trashed on the server at once")238 #expect(await a.engine.currentStatus().needsReauth, "its permanent deletion waits for a passkey confirmation")239 fake.setAllFresh(true)240 #expect(await a.sync() == .completed)241 #expect(await a.sync() == .completed)242 #expect(fake.page(id) == nil)243 #expect(try await a.store.page(id: id) == nil)244 #expect(try await a.store.opCount() == 0)245 await world.closeAll()246 }247248 @Test("401 in the middle of a cycle: signed out, nothing lost, a later cycle with a new session completes")249 func signedOutMidCycle() async throws {250 let world = World()251 let fake = world.fake252 let a = try world.device("A")253 var ids: [String] = []254 for i in 0..<4 { ids.append(try await a.create("Note \(i)", [("a", "v1 \(i)")])) }255 #expect(await a.sync() == .completed)256 for (i, id) in ids.enumerated() { try await a.edit(id, block: "a", text: "v2 \(i)") }257 let created = try await a.create("Created before sign-out", [("a", "new")])258 _ = try await a.store.trash(id: ids[3])259260 // the session is revoked after the second write of the cycle261 let token = try #require(a.token)262 fake.inject(.hold(Gate.opened { fake.revoke(token: token) }), method: "PATCH", path: "^/api/pages/", count: 1)263 #expect(await a.sync() == .signedOut)264 #expect(await a.engine.currentStatus().phase == .signedOut)265 #expect(await a.client.hasSession == false)266 #expect(try await a.store.counts().pending >= 3, "the unsent edits are still waiting")267 for (i, id) in ids.enumerated() { #expect(try await a.texts(id) == ["v2 \(i)"]) }268269 fake.clearLog()270 #expect(await a.sync() == .signedOut)271 #expect(fake.requests.isEmpty, "no request without a session")272273 await a.client.setSessionToken(fake.issueToken())274 #expect(await a.sync() == .completed)275 for (i, id) in ids.enumerated() { #expect(fake.page(id)?.text == "v2 \(i)") }276 #expect(fake.page(created)?.text == "new")277 #expect(fake.page(ids[3])?.deletedAt != nil)278 #expect(try await a.store.conflicts().isEmpty)279 #expect(await a.sync() == .completed)280 try await expectMirror(a)281 await world.closeAll()282 }283284 @Test("the network drops in the middle of a cycle: offline, nothing lost, the next cycle completes")285 func offlineMidCycle() async throws {286 let world = World()287 let fake = world.fake288 let a = try world.device("A")289 var ids: [String] = []290 for i in 0..<5 { ids.append(try await a.create("Note \(i)", [("a", "v1 \(i)")])) }291 #expect(await a.sync() == .completed)292 for (i, id) in ids.enumerated() { try await a.edit(id, block: "a", text: "v2 \(i)") }293 fake.inject(.hold(Gate.opened { a.setOffline(true) }), method: "PATCH", path: "^/api/pages/", count: 1)294 #expect(await a.sync() == .offline)295 #expect(await a.engine.currentStatus().phase == .offline)296 let sent = ids.filter { fake.page($0)?.version == 2 }.count297 #expect(sent >= 1 && sent < 5, "the cycle stopped at the first request that could not leave (\(sent) sent)")298 a.setOffline(false)299 #expect(await a.sync() == .completed)300 for (i, id) in ids.enumerated() { #expect(fake.page(id)?.text == "v2 \(i)" && fake.page(id)?.version == 2) }301 try await expectMirror(a)302 await world.closeAll()303 }304305 @Test("stop() interrupts a cycle; nothing is lost and the next sync resumes")306 func stopMidCycle() async throws {307 let world = World()308 let fake = world.fake309 let a = try world.device("A")310 let id = try await a.create("Note", [("a", "one")])311 #expect(await a.sync() == .completed)312 try await a.edit(id, block: "a", text: "one two")313 let other = try await a.create("Other", [("a", "other")])314 let gate = Gate()315 fake.inject(.hold(gate), method: "POST", path: "^/api/pages$")316 async let outcome = a.sync()317 await gate.reached()318 await a.engine.stop()319 async let after = a.sync() // asked for after the stop: a new request, not a casualty of it320 #expect(await outcome == .offline)321 gate.open()322 #expect(await after == .completed)323 #expect(await a.engine.currentStatus().phase == .idle)324 #expect(await a.sync() == .completed)325 #expect(fake.page(id)?.text == "one two")326 #expect(fake.page(other)?.text == "other")327 #expect(fake.allPages.count == 2)328 #expect(await a.sync() == .completed)329 try await expectMirror(a)330 await world.closeAll()331 }332}333334extension Gate {335 /// A gate that runs `action` when a request reaches it and lets the request through at once.336 static func opened(_ action: @escaping @Sendable () -> Void) -> Gate {337 let gate = Gate()338 Task.detached {339 await gate.reached()340 action()341 gate.open()342 }343 return gate344 }345}346347@Suite("Server failures, status, re-entrancy")348struct RobustnessTests {349 @Test("one note refused by the server does not block the others; it is retried later")350 func oneBadNote() async throws {351 let world = World()352 let fake = world.fake353 let a = try world.device("A")354 var ids: [String] = []355 for i in 0..<4 { ids.append(try await a.create("Note \(i)", [("a", "v1")])) }356 #expect(await a.sync() == .completed)357 for id in ids { try await a.edit(id, block: "a", text: "v2") }358 fake.inject(.status(500), method: "PATCH", path: "^/api/pages/", count: 1)359 #expect(await a.sync() == .completed)360 #expect(ids.filter { fake.page($0)?.text == "v2" }.count == 3)361 let failed = try #require(ids.first { fake.page($0)?.text == "v1" })362 #expect(try await a.page(failed).syncState == .error)363 #expect(try await a.page(failed).syncError != nil)364 #expect(await a.engine.currentStatus().pending == 1)365 #expect(await a.sync() == .completed)366 #expect(fake.page(failed)?.text == "v2")367 #expect(try await a.page(failed).syncState == .synced)368 await world.closeAll()369 }370371 @Test("a server that keeps failing is left alone after a few requests")372 func serverDown() async throws {373 let world = World()374 let fake = world.fake375 let a = try world.device("A")376 for i in 0..<30 { try await a.create("Note \(i)", [("a", "x")]) }377 fake.inject(.status(503), path: ".*", count: 1000)378 let outcome = await a.sync()379 guard case .failed = outcome else { Issue.record("expected a failure, got \(outcome)"); return }380 #expect(fake.requests.count <= SyncEngine.maxServerFailures, "\(fake.requests.count) requests sent to a server that is down")381 if case .failed = await a.engine.currentStatus().phase {} else { Issue.record("phase should be failed") }382 fake.clearFaults()383 fake.inject(.status(429, code: "rate_limited"), path: ".*", count: 1000)384 fake.clearLog()385 guard case .failed = await a.sync() else { Issue.record("expected a failure"); return }386 #expect(fake.requests.count == 1, "a rate limit stops the cycle at once")387 fake.clearFaults()388 #expect(await a.sync() == .completed)389 #expect(fake.allPages.count == 30)390 await world.closeAll()391 }392393 @Test("a connection that times out or breaks reads as offline")394 func timeouts() async throws {395 let world = World()396 let fake = world.fake397 let a = try world.device("A")398 try await a.create("Note", [("a", "x")])399 for code in [URLError.Code.timedOut, .cannotConnectToHost, .networkConnectionLost, .dnsLookupFailed] {400 fake.inject(.network(code), path: ".*")401 fake.clearLog()402 #expect(await a.sync() == .offline)403 #expect(fake.requests.count == 1)404 }405 #expect(await a.sync() == .completed)406 await world.closeAll()407 }408409 @Test("a feed that never advances, never ends or always asks for a reset fails the cycle instead of looping")410 func misbehavingFeed() async throws {411 let world = World()412 let fake = world.fake413 fake.webCreatePage(title: "One")414 let a = try world.device("A")415 #expect(await a.sync() == .completed)416 let cursor = try await a.store.syncCursor()417 func feed(cursor: Int64, more: Bool, reset: Bool = false) -> String {418 #"{"cursor":\#(cursor),"head":999,"more":\#(more),"reset":\#(reset),"serverTime":"2026-11-18T19:07:04.808Z","workspace":{"id":"w","name":"W"},"user":{"displayName":null},"pages":[],"attachments":[],"tombstones":[],"tags":[]}"#419 }420 // more = true with the same cursor, for ever421 fake.inject(.raw(status: 200, body: feed(cursor: cursor, more: true)), method: "GET", path: "^/api/sync", count: 100_000)422 fake.clearLog()423 guard case .failed = await a.sync() else { Issue.record("expected a failure"); return }424 #expect(fake.requests.count <= 2)425 fake.clearFaults()426 // a cursor that goes backwards427 fake.inject(.raw(status: 200, body: feed(cursor: cursor - 1, more: false)), method: "GET", path: "^/api/sync", count: 100_000)428 guard case .failed = await a.sync() else { Issue.record("expected a failure"); return }429 #expect(try await a.store.syncCursor() == cursor, "the stored cursor never moves backwards")430 fake.clearFaults()431 // reset, for ever432 fake.inject(.raw(status: 200, body: feed(cursor: 0, more: true, reset: true)), method: "GET", path: "^/api/sync", count: 100_000)433 fake.clearLog()434 guard case .failed = await a.sync() else { Issue.record("expected a failure"); return }435 #expect(fake.requests.count <= 4)436 fake.clearFaults()437 // garbage438 fake.inject(.raw(status: 200, body: "<html>proxy error</html>"), method: "GET", path: "^/api/sync", count: 1)439 guard case .failed = await a.sync() else { Issue.record("expected a failure"); return }440 // and the engine recovers (the interrupted full copy starts again and ends)441 #expect(await a.sync() == .completed)442 #expect(try await a.store.pageCount() == 1)443 try await expectMirror(a)444 await world.closeAll()445 }446447 @Test("status stream: syncing then idle, pending drops to 0, conflicts and lastSuccess are reported")448 func statusStream() async throws {449 let world = World()450 let a = try world.device("A")451 let collected = Collector<SyncStatus>()452 let stream = await a.engine.statusUpdates()453 let listener = Task { for await status in stream { collected.append(status) } }454 #expect(await a.engine.currentStatus() == SyncStatus())455456 let one = try await a.create("One", [("a", "1")])457 try await a.create("Two", [("a", "2")])458 _ = try await a.attach(one, filename: "a.txt", mime: "text/plain", bytes: Data("a".utf8))459 await a.engine.refreshCounts()460 #expect(await a.engine.currentStatus().pending == 3, "two notes and one file")461 #expect(await a.engine.currentStatus().phase == .idle)462 _ = try await a.store.trash(id: one)463 await a.engine.refreshCounts()464 #expect(await a.engine.currentStatus().pending == 4, "plus one queued action")465466 #expect(await a.sync() == .completed)467 let final = await a.engine.currentStatus()468 #expect(final.phase == .idle && final.pending == 0 && final.conflicts == 0 && final.lastSuccess != nil && !final.needsReauth)469 await eventually("the last status to be delivered") { collected.values.last == final }470 let phases = collected.values.map(\.phase)471 #expect(phases.first == .idle)472 #expect(phases.contains(.syncing))473 #expect(phases.last == .idle)474 let syncingIndex = try #require(phases.firstIndex(of: .syncing))475 #expect(collected.values[syncingIndex].pending == 4)476 #expect(collected.values.dropFirst(syncingIndex).map(\.pending).last == 0)477478 a.setOffline(true)479 try await a.create("Three", [("a", "3")])480 #expect(await a.sync() == .offline)481 #expect(await a.engine.currentStatus().phase == .offline)482 #expect(await a.engine.currentStatus().pending == 1)483 #expect(await a.engine.currentStatus().lastSuccess == final.lastSuccess)484 listener.cancel()485 await world.closeAll()486 }487488 @Test("refreshCounts() racing with a cycle never leaves a stale phase behind")489 func statusRace() async throws {490 let world = World()491 let a = try world.device("A")492 for i in 0..<20 { try await a.create("Note \(i)", [("a", "x")]) }493 for _ in 0..<5 {494 async let outcome = a.sync()495 await withTaskGroup(of: Void.self) { group in496 for _ in 0..<50 { group.addTask { await a.engine.refreshCounts() } }497 }498 #expect(await outcome == .completed)499 #expect(await a.engine.currentStatus().phase == .idle)500 try await a.create("More", [("a", "y")])501 }502 await world.closeAll()503 }504505 @Test("sync() called during a cycle waits for ONE more cycle that covers the caller's edits")506 func reentrantSync() async throws {507 let world = World()508 let fake = world.fake509 let a = try world.device("A")510 let id = try await a.create("Note", [("a", "one")])511 #expect(await a.sync() == .completed)512 fake.clearLog()513 let gate = Gate()514 fake.inject(.hold(gate), method: "GET", path: "^/api/sync")515 async let first = a.sync()516 await gate.reached()517 try await a.edit(id, block: "a", text: "edited during the first cycle")518 let late = Task { await withTaskGroup(of: SyncOutcome.self) { group -> [SyncOutcome] in519 for _ in 0..<6 { group.addTask { await a.sync() } }520 var all: [SyncOutcome] = []521 for await outcome in group { all.append(outcome) }522 return all523 } }524 try await Task.sleep(for: .milliseconds(100)) // the six calls are now waiting525 gate.open()526 #expect(await first == .completed)527 #expect(await late.value == Array(repeating: .completed, count: 6))528 #expect(fake.page(id)?.text == "edited during the first cycle", "when sync() returns, what was in the store when it was called is pushed")529 #expect(fake.count("GET", "^/api/sync") == 2, "six callers shared one extra cycle")530 await world.closeAll()531 }532533 @Test("callers waiting for the next cycle are released when the running one fails")534 func reentrantSyncOffline() async throws {535 let world = World()536 let fake = world.fake537 let a = try world.device("A")538 try await a.create("Note", [("a", "one")])539 let gate = Gate()540 fake.inject(.hold(gate), method: "POST", path: "^/api/pages$")541 async let first = a.sync()542 await gate.reached()543 async let second = a.sync()544 try await Task.sleep(for: .milliseconds(50))545 a.setOffline(true)546 gate.open()547 let outcomes = await [first, second]548 #expect(outcomes[0] == .offline)549 #expect(outcomes[1] == .offline, "the waiting caller is not left hanging and no doomed cycle is started for it")550 await world.closeAll()551 }552553 @Test("schedule() debounces: many calls, one cycle")554 func scheduleDebounce() async throws {555 let world = World()556 let fake = world.fake557 let a = try world.device("A")558 let id = try await a.create("Note", [("a", "x")])559 for _ in 0..<10 {560 await a.engine.schedule(after: .milliseconds(80))561 try await Task.sleep(for: .milliseconds(5))562 }563 #expect(fake.requests.isEmpty, "nothing before the quiet period ends")564 await eventually("the scheduled cycle") { fake.page(id) != nil && fake.count("GET", "^/api/sync") >= 1 }565 try await Task.sleep(for: .milliseconds(200))566 #expect(fake.count("GET", "^/api/sync") == 1)567 await a.engine.schedule(after: .milliseconds(50))568 await a.engine.stop()569 try await Task.sleep(for: .milliseconds(150))570 #expect(fake.count("GET", "^/api/sync") == 1, "stop() cancels the scheduled cycle")571 await world.closeAll()572 }573}574575/// Thread-safe accumulator for values received from a stream.576final class Collector<Value: Sendable>: @unchecked Sendable {577 private let lock = NSLock()578 private var items: [Value] = []579 func append(_ value: Value) { lock.withLock { items.append(value) } }580 var values: [Value] { lock.withLock { items } }581}582583@Suite("Server fidelity and scale")584struct FidelityTests {585 @Test("the fake replays an idempotent POST like the server: stored first answer, no x-existing, whatever the new body")586 func replaySemantics() async throws {587 let world = World()588 let fake = world.fake589 let a = try world.device("A")590 let id = UUID().uuidString.lowercased()591 let key = "spn-test-" + id592 let first = try await a.api.createPage(PageDraft(id: id, title: "A", content: RawJSON(data: Doc.data([("a", "A")]))), idempotencyKey: key)593 #expect(!first.existing && first.value.title == "A")594 let replayed = try await a.api.createPage(PageDraft(id: id, title: "B", content: RawJSON(data: Doc.data([("a", "B")]))), idempotencyKey: key)595 #expect(!replayed.existing, "a replay does not say that the id exists")596 #expect(replayed.value.title == "A", "…and describes the first request")597 #expect(fake.page(id)?.title == "A")598 let honest = try await a.api.createPage(PageDraft(id: id, title: "B"))599 #expect(honest.existing && honest.value.title == "A")600 // the same key on another path is refused601 do {602 _ = try await a.api.trashPage(id: id, idempotencyKey: key)603 Issue.record("expected 422")604 } catch let error as APIError { #expect(error.status == 422) }605 // a purged page is still "created" for a replay: this is why the engine sends creations without a key606 fake.webPurge(id)607 let ghost = try await a.api.createPage(PageDraft(id: id, title: "A"), idempotencyKey: key)608 #expect(ghost.value.id == id && fake.page(id) == nil)609 // mutations without the right Origin, requests without a session610 let stranger = HTTPClient(configuration: APIConfiguration(baseURL: fake.baseURL, userAgent: "X"), tokenStore: InMemoryTokenStore(token: "not-a-session-token-0123456789abcdefghijklm"),611 sessionConfiguration: FakeBackend.sessionConfiguration())612 do { _ = try await NotesAPI(client: stranger).sync(since: 0); Issue.record("expected 401") } catch let error as APIError { #expect(error.isUnauthenticated) }613 await world.closeAll()614 }615616 @Test("notes whose blocks have no ids: the server's ids are adopted, a lost answer followed by typing is still recognised")617 func blocksWithoutIds() async throws {618 let world = World()619 let fake = world.fake620 let a = try world.device("A")621 func bare(_ texts: [String]) -> Data {622 let content = texts.map { ["type": "paragraph", "content": [["type": "text", "text": $0]]] as [String: Any] }623 return try! JSONSerialization.data(withJSONObject: ["type": "doc", "content": content], options: [.sortedKeys])624 }625 func put(_ id: String?, _ texts: [String]) async throws -> String {626 let digest = ContentDigest(content: bare(texts))627 if let id {628 _ = try await a.store.updateContent(id: id, content: bare(texts), plainText: digest.plainText, wordCount: digest.wordCount, preview: digest.preview)629 return id630 }631 return try await a.store.createPage(NewPage(title: "Bare", content: bare(texts), plainText: digest.plainText, wordCount: digest.wordCount, preview: digest.preview)).id632 }633 let first = try await put(nil, ["one"])634 #expect(await a.sync() == .completed)635 #expect(try await a.blocks(first).allSatisfy { !$0.id.isEmpty }, "the device now holds the ids the server assigned")636 #expect(try await a.page(first).isDirty == false)637 #expect(fake.page(first)?.version == 1)638639 let second = try await put(nil, ["one"])640 fake.inject(.dropResponse, method: "POST", path: "^/api/pages$")641 #expect(await a.sync() == .offline)642 _ = try await put(second, ["one two"])643 #expect(await a.sync() == .completed)644 #expect(try await a.store.conflicts().isEmpty)645 #expect(fake.page(second)?.text == "one two")646 #expect(await a.sync() == .completed)647 #expect(try await a.store.counts().pending == 0)648 await world.closeAll()649 }650651 @Test("more pending notes than one batch are all pushed by one call")652 func largeBacklog() async throws {653 let world = World()654 let a = try world.device("A")655 let total = SyncEngine.pushBatch + 30656 let drafts = (0..<total).map { i in NewPage(title: "Note \(i)", content: Doc.data([("b", "text \(i)")]), plainText: "text \(i)", wordCount: 2, preview: "text \(i)") }657 _ = try await a.store.createPages(drafts)658 #expect(await a.sync() == .completed)659 #expect(world.fake.allPages.count == total)660 #expect(await a.engine.currentStatus().pending == 0)661 await world.closeAll()662 }663664 @Test("purging a folder purges the notes inside it on the server too (the server deletes one row per request)")665 func purgeDoesNotCascadeOnTheServer() async throws {666 let world = World()667 let fake = world.fake668 let a = try world.device("A")669 let folder = try await a.create("Folder", properties: #"{"folder":true}"#)670 let inside = try await a.create("Inside", [("a", "x")], parent: folder)671 #expect(await a.sync() == .completed)672 fake.setAllFresh(true)673 _ = try await a.store.purge(id: folder)674 #expect(await a.sync() == .completed)675 #expect(fake.page(folder) == nil)676 #expect(try await a.store.page(id: inside) == nil, "gone from the device")677 #expect(fake.page(inside) == nil, "the store queues one purge per page: nothing of the folder stays on the server")678 await world.closeAll()679 }680}681