Repository Public
1// SP Drive iOS2// File: KitTests/SyncTests.swift3// Description: Integration tests of the sync engine against the real backend: tree operations, uploads, downloads, resumption,4// offline work and restart, conflicts, offline pins, trash.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Testing10@testable import SPDriveKit1112@Suite(.enabled(if: TestEnv.available, "disposable backend not running: Scripts/test-server.sh up"), .serialized)13struct SyncTests {14 @Test func signedInAndRootKnown() async throws {15 let d = try await Device()16 let state = try await d.api.accountState()17 #expect(state.authenticated)18 #expect((state.quota?.limit ?? 0) > 0)19 await d.engine.sync()20 #expect(!d.rootID.isEmpty)21 #expect(d.store.item(d.rootID)?.isFolder == true)22 }2324 @Test func treeOperationsReachTheServerAndAnotherDevice() async throws {25 let a = try await Device(), b = try await Device(host: "127.0.0.1")26 let ws = try await a.workspace("tree")27 let docs = try await a.engine.createFolder(name: "Documents", in: ws.id)28 _ = try await a.engine.createFolder(name: "Archives", in: ws.id)29 #expect(docs.isLocalOnly) // visible at once, before the server knows30 await a.settle()31 let docsReal = try #require(a.child("Documents", of: ws.id))32 let archives = try #require(a.child("Archives", of: ws.id))33 #expect(!docsReal.isLocalOnly && !docsReal.pending)3435 try await a.engine.rename(docsReal.id, to: "Dossiers")36 try await a.engine.move([archives.id], to: docsReal.id)37 try await a.engine.setStarred([docsReal.id], true)38 await a.settle()3940 await b.engine.sync()41 let seen = try #require(b.child("Dossiers", of: ws.id))42 #expect(seen.starred)43 #expect(b.child("Archives", of: seen.id) != nil)44 #expect(b.store.path(to: archives.id).map(\.name).suffix(2) == ["Dossiers", "Archives"])4546 // trash from B, restore from A47 try await b.engine.trash([seen.id])48 #expect(b.child("Dossiers", of: ws.id) == nil)49 await b.settle()50 await a.engine.sync()51 #expect(a.child("Dossiers", of: ws.id) == nil)52 #expect(a.store.trashRoots().contains { $0.id == seen.id })53 #expect(a.store.item(archives.id)?.isTrashed == true)54 try await a.engine.restore([seen.id])55 await a.settle()56 await b.engine.sync()57 #expect(b.child("Dossiers", of: ws.id) != nil)58 #expect(b.store.item(archives.id)?.isTrashed == false)59 }6061 @Test func duplicateNamesAndInvalidNamesAreRefusedLocally() async throws {62 let a = try await Device()63 let ws = try await a.workspace("names")64 let x = try await a.engine.createFolder(name: "A", in: ws.id)65 let y = try await a.engine.createFolder(name: "B", in: ws.id)66 await #expect(throws: RemoteError.nameConflict(name: "A")) { try await a.engine.rename(y.id, to: "A") }67 await #expect(throws: EngineError.invalidName) { try await a.engine.rename(x.id, to: "../evil") }68 await #expect(throws: EngineError.invalidName) { try await a.engine.createFolder(name: " ", in: ws.id) }69 // creating the same name twice picks a free name instead of failing70 let again = try await a.engine.createFolder(name: "A", in: ws.id)71 #expect(again.name == "A (2)")72 await a.settle()73 #expect(a.store.children(of: ws.id).map(\.name).sorted() == ["A", "A (2)", "B"])74 }7576 @Test func uploadThenDownloadOnAnotherDeviceWithIntegrity() async throws {77 let a = try await Device(), b = try await Device(host: "127.0.0.1")78 let ws = try await a.workspace("updown")79 let data = randomData(300_000)80 let item = try await a.engine.importFile(at: a.tempFile("notes.txt", data), into: ws.id)81 #expect(item.isLocalOnly)82 #expect(a.engine.badge(for: item) != .none)83 await a.settle()84 let up = try #require(a.child("notes.txt", of: ws.id))85 #expect(up.sha256 == IntegrityVerifier.sha256(data))86 #expect(up.size == 300_000 && !up.pending)87 #expect(a.store.ops().isEmpty)8889 await b.engine.sync()90 let remote = try #require(b.child("notes.txt", of: ws.id))91 #expect(b.engine.badge(for: remote) == .none)92 let url = try await b.engine.localCopy(of: remote.id)93 #expect(try Data(contentsOf: url) == data)94 #expect(b.engine.badge(for: remote) == .available)95 #expect(b.store.localState(remote.id)?.baseVersionID == remote.versionID)96 // a second open uses the verified copy: no new transfer97 let n = b.store.transfers().count98 _ = try await b.engine.localCopy(of: remote.id)99 #expect(b.store.transfers().count == n)100 }101102 @Test func largeFileTravelsInBlocksAndResumesAfterInterruptions() async throws {103 let a = try await Device(), b = try await Device(host: "127.0.0.1")104 let ws = try await a.workspace("large")105 let data = randomData(20 * 1024 * 1024 + 12_345) // three blocks106 let src = try a.tempFile("film.bin", data)107 // the second block fails twice in transit; the upload must continue from the server's offset, not from zero108 NetworkGate.failNext(2, pathContaining: "offset=8388608")109 _ = try await a.engine.importFile(at: src, into: ws.id)110 await a.settle(timeout: 120)111 let up = try #require(a.child("film.bin", of: ws.id))112 #expect(up.sha256 == IntegrityVerifier.sha256(data))113 let t = try #require(a.store.transfers().first { $0.itemID == up.id && $0.kind == .upload })114 #expect(t.state == .done)115116 // download on B, cut twice: the partial file is kept and completed with a Range request117 await b.engine.sync()118 let remote = try #require(b.child("film.bin", of: ws.id))119 NetworkGate.failNext(1, pathContaining: "files/\(remote.id)/content")120 let url = try await b.engine.localCopy(of: remote.id)121 #expect(IntegrityVerifier.verify(url, sha256: up.sha256))122123 // explicit Range resumption: half of the file is already on disk when the transfer starts124 let c = try await Device(host: "127.0.0.1")125 await c.engine.sync()126 let item = try #require(c.store.item(up.id))127 let tr = try #require(await c.engine.enqueueDownload(item, priority: .user))128 try data.prefix(11_000_000).write(to: c.files.partial(tr.id))129 await c.engine.pump()130 try await c.engine.wait(for: tr.id)131 let ls = try #require(c.store.localState(up.id))132 #expect(IntegrityVerifier.verify(try #require(c.files.url(for: ls)), sha256: up.sha256))133 }134135 @Test func corruptedDownloadIsNeverInstalled() async throws {136 let a = try await Device()137 let ws = try await a.workspace("corrupt")138 let data = randomData(200_000)139 _ = try await a.engine.importFile(at: a.tempFile("doc.bin", data), into: ws.id)140 await a.settle()141 let b = try await Device(host: "127.0.0.1")142 await b.engine.sync()143 let item = try #require(b.child("doc.bin", of: ws.id))144 let tr = try #require(await b.engine.enqueueDownload(item, priority: .user))145 // a partial file with the right length but wrong bytes: verification must reject it and fetch the real content146 try randomData(200_000).write(to: b.files.partial(tr.id))147 await b.engine.pump()148 try await b.engine.wait(for: tr.id)149 let url = try #require(b.store.localState(item.id).flatMap(b.files.url(for:)))150 #expect(try Data(contentsOf: url) == data)151 }152153 @Test func offlineWorkSurvivesRestartAndSyncsOnReconnection() async throws {154 var a = try await Device()155 let ws = try await a.workspace("offline")156 let data1 = randomData(50_000)157 _ = try await a.engine.importFile(at: a.tempFile("avant.txt", data1), into: ws.id)158 await a.settle()159 let avant = try #require(a.child("avant.txt", of: ws.id))160 try await a.engine.pin(avant.id)161 await a.settle()162 #expect(a.engine.badge(for: avant) == .pinned)163164 a.setOffline(true)165 defer { NetworkGate.set("localhost", offline: false) }166 await a.engine.sync()167 #expect(await a.engine.status == .offline)168 // offline: open the pinned file, edit it, create a folder, add a file to it, rename, trash something169 let url = try await a.engine.localCopy(of: avant.id)170 let edited = Data("modifié hors ligne".utf8)171 try edited.write(to: url)172 try await a.engine.noteLocalEdit(avant.id)173 let folder = try await a.engine.createFolder(name: "Créé hors ligne", in: ws.id)174 let data2 = randomData(70_000)175 let added = try await a.engine.importFile(at: a.tempFile("nouveau.bin", data2), into: folder.id)176 try await a.engine.rename(folder.id, to: "Renommé hors ligne")177 await a.engine.sync()178 #expect(a.store.ops().count >= 4)179 #expect(a.child("Renommé hors ligne", of: ws.id) != nil)180181 // the phone restarts while still offline: nothing is lost182 a = try await a.relaunched()183 #expect(a.store.ops().count >= 4)184 #expect(try Data(contentsOf: try #require(a.store.localState(avant.id).flatMap(a.files.url(for:)))) == edited)185 #expect(a.store.localState(avant.id)?.dirty == true)186 #expect(a.store.item(added.id) != nil)187 await a.engine.sync()188 #expect(await a.engine.status == .offline)189190 // back online191 a.setOffline(false)192 await a.settle()193 #expect(a.store.ops().isEmpty)194 let b = try await Device(host: "127.0.0.1")195 await b.engine.sync()196 let renamed = try #require(b.child("Renommé hors ligne", of: ws.id))197 let nouveau = try #require(b.child("nouveau.bin", of: renamed.id))198 #expect(nouveau.sha256 == IntegrityVerifier.sha256(data2))199 let avantB = try #require(b.child("avant.txt", of: ws.id))200 #expect(avantB.sha256 == IntegrityVerifier.sha256(edited))201 #expect(avantB.versionCount == 2) // the previous content is still in the history202 #expect(a.store.localState(avant.id)?.dirty == false)203 #expect(a.engine.badge(for: try #require(a.store.item(avant.id))) == .pinned)204 }205206 @Test func concurrentEditsRaiseAConflictAndNothingIsOverwritten() async throws {207 let a = try await Device(), b = try await Device(host: "127.0.0.1")208 let ws = try await a.workspace("conflict")209 for name in ["both.txt", "mine.txt", "server.txt"] { _ = try await a.engine.importFile(at: a.tempFile(name, Data("v1 \(name)".utf8)), into: ws.id) }210 await a.settle()211 await b.engine.sync()212 var collected: [String: String] = [:]213 for name in ["both.txt", "mine.txt", "server.txt"] {214 let item = try #require(a.child(name, of: ws.id))215 collected[name] = item.id216 try await a.engine.pin(item.id)217 }218 let ids = collected219 await a.settle()220221 // A goes offline and edits; meanwhile B edits the same files and syncs222 a.setOffline(true)223 defer { NetworkGate.set("localhost", offline: false) }224 for (name, id) in ids {225 let url = try await a.engine.localCopy(of: id)226 try Data("A \(name)".utf8).write(to: url)227 try await a.engine.noteLocalEdit(id)228 }229 for (name, id) in ids { try await b.engine.replaceContent(of: id, with: b.tempFile(name, Data("B \(name)".utf8))) }230 await b.settle()231232 a.setOffline(false)233 let events = await a.engine.events()234 await a.engine.sync()235 #expect(await eventually { a.store.conflicts().count == 3 })236 _ = events237 // nothing was overwritten on the server, and A still holds its edits238 await b.engine.sync()239 for (name, id) in ids {240 #expect(b.store.item(id)?.sha256 == IntegrityVerifier.sha256(Data("B \(name)".utf8)))241 #expect(a.engine.badge(for: try #require(a.store.item(id))) == .conflict)242 #expect(try Data(contentsOf: try #require(a.store.localState(id).flatMap(a.files.url(for:)))) == Data("A \(name)".utf8))243 }244245 func conflict(_ name: String) throws -> ConflictRecord { try #require(a.store.conflicts().first { $0.itemID == ids[name] }) }246 // keep both: A's content becomes a separate file, the original takes B's content247 try await a.engine.resolve(conflict("both.txt").id, choice: .keepBoth, deviceName: "iPhone de test")248 await a.settle()249 await b.engine.sync()250 let names = b.store.children(of: ws.id).map(\.name)251 let copy = try #require(names.first { $0.hasPrefix("both (conflit iPhone de test") && $0.hasSuffix(".txt") })252 #expect(b.child(copy, of: ws.id)?.sha256 == IntegrityVerifier.sha256(Data("A both.txt".utf8)))253 #expect(b.store.item(ids["both.txt"]!)?.sha256 == IntegrityVerifier.sha256(Data("B both.txt".utf8)))254 #expect(await eventually { (try? Data(contentsOf: a.store.localState(ids["both.txt"]!).flatMap(a.files.url(for:)) ?? URL(fileURLWithPath: "/nonexistent"))) == Data("B both.txt".utf8) }) // pinned: refreshed255256 // keep mine: A's content becomes the newest version; B's stays in the history257 try await a.engine.resolve(conflict("mine.txt").id, choice: .keepMine, deviceName: "iPhone de test")258 await a.settle()259 let detail = try await a.api.fileDetail(id: ids["mine.txt"]!)260 #expect(detail.item.sha256 == IntegrityVerifier.sha256(Data("A mine.txt".utf8)))261 #expect(detail.versions.count == 3)262 #expect(detail.versions.contains { $0.sha256 == IntegrityVerifier.sha256(Data("B mine.txt".utf8)) })263264 // keep the server's: A's edit is dropped on purpose265 try await a.engine.resolve(conflict("server.txt").id, choice: .keepServer, deviceName: "iPhone de test")266 await a.settle()267 #expect(a.store.item(ids["server.txt"]!)?.sha256 == IntegrityVerifier.sha256(Data("B server.txt".utf8)))268 #expect(a.store.conflicts().isEmpty)269 #expect(a.store.ops().isEmpty)270 }271272 @Test func editHereDeletedThereKeepsTheEdit() async throws {273 let a = try await Device(), b = try await Device(host: "127.0.0.1")274 let ws = try await a.workspace("gone")275 _ = try await a.engine.importFile(at: a.tempFile("rapport.txt", Data("v1".utf8)), into: ws.id)276 await a.settle()277 let id = try #require(a.child("rapport.txt", of: ws.id)).id278 try await a.engine.pin(id)279 await a.settle()280 a.setOffline(true)281 defer { NetworkGate.set("localhost", offline: false) }282 try Data("travail précieux".utf8).write(to: try await a.engine.localCopy(of: id))283 try await a.engine.noteLocalEdit(id)284 await b.engine.sync()285 try await b.engine.trash([id])286 await b.settle()287 try await b.engine.emptyTrash()288289 a.setOffline(false)290 await a.engine.sync()291 #expect(await eventually { a.store.conflicts().contains { $0.itemID == id && $0.reason == .remoteDeleted } })292 #expect(try Data(contentsOf: try #require(a.store.localState(id).flatMap(a.files.url(for:)))) == Data("travail précieux".utf8))293 let c = try #require(a.store.conflicts().first)294 try await a.engine.resolve(c.id, choice: .keepMine, deviceName: "iPhone")295 await a.settle()296 await b.engine.sync()297 #expect(b.child("rapport.txt", of: ws.id)?.sha256 == IntegrityVerifier.sha256(Data("travail précieux".utf8)))298 #expect(a.store.conflicts().isEmpty && a.store.ops().isEmpty)299 }300301 @Test func pinnedFolderFollowsRemoteChangesAndFreeingSpaceNeverDeletesRemotely() async throws {302 let a = try await Device(), b = try await Device(host: "127.0.0.1")303 let ws = try await a.workspace("pin")304 _ = try await a.engine.importFile(at: a.tempFile("un.txt", Data("un".utf8)), into: ws.id)305 await a.settle()306 await b.engine.sync()307 try await b.engine.pin(ws.id)308 await b.settle()309 let un = try #require(b.child("un.txt", of: ws.id))310 #expect(b.engine.badge(for: un) == .pinned)311 #expect(b.store.localState(un.id)?.location == .offline)312313 // new file and new version arrive from A: B fetches them without being asked314 _ = try await a.engine.importFile(at: a.tempFile("deux.txt", Data("deux".utf8)), into: ws.id)315 try await a.engine.replaceContent(of: un.id, with: a.tempFile("un.txt", Data("un, version 2".utf8)))316 await a.settle()317 await b.settle()318 let deux = try #require(b.child("deux.txt", of: ws.id))319 #expect(try Data(contentsOf: try #require(b.store.localState(deux.id).flatMap(b.files.url(for:)))) == Data("deux".utf8))320 #expect(try Data(contentsOf: try #require(b.store.localState(un.id).flatMap(b.files.url(for:)))) == Data("un, version 2".utf8))321322 // B works offline on pinned content323 b.setOffline(true)324 #expect(try Data(contentsOf: try await b.engine.localCopy(of: deux.id)) == Data("deux".utf8))325 b.setOffline(false)326327 // freeing space on B removes copies only328 await b.engine.unpin(ws.id)329 #expect(b.store.localState(un.id)?.hasCopy != true)330 #expect(b.engine.badge(for: un) == .none)331 await b.settle()332 await a.engine.sync()333 #expect(a.child("un.txt", of: ws.id) != nil && a.child("deux.txt", of: ws.id) != nil)334 #expect(a.store.item(un.id)?.isTrashed == false)335 }336337 @Test func cancelPauseResumeAndCacheCleaning() async throws {338 let a = try await Device()339 let ws = try await a.workspace("queue")340 // cancel an upload that never left: the provisional item disappears, nothing reaches the server341 a.setOffline(true)342 defer { NetworkGate.set("localhost", offline: false) }343 let ghost = try await a.engine.importFile(at: a.tempFile("abandon.bin", randomData(10_000)), into: ws.id)344 await a.engine.sync()345 let t = try #require(a.store.activeTransfer(itemID: ghost.id, kind: .upload))346 await a.engine.cancel(t.id)347 #expect(a.store.item(ghost.id) == nil)348 #expect(a.store.ops().isEmpty)349 a.setOffline(false)350351 let data = randomData(120_000)352 _ = try await a.engine.importFile(at: a.tempFile("garde.bin", data), into: ws.id)353 await a.settle()354 #expect(a.child("abandon.bin", of: ws.id) == nil)355 let kept = try #require(a.child("garde.bin", of: ws.id))356 // an uploaded, unpinned file stays as a cached copy; cleaning the cache frees it without touching the Drive357 #expect(a.store.localState(kept.id)?.location == .cache)358 let freed = await a.engine.clearCache()359 #expect(freed == 120_000)360 #expect(a.store.localState(kept.id) == nil)361 await a.settle()362 #expect(a.child("garde.bin", of: ws.id) != nil)363364 // pause then resume a download365 a.setOffline(true)366 let tr = try #require(await a.engine.enqueueDownload(kept, priority: .user))367 await a.engine.pause(tr.id)368 #expect(a.store.transfer(tr.id)?.state == .paused)369 a.setOffline(false)370 await a.engine.resume(tr.id)371 try await a.engine.wait(for: tr.id)372 #expect(a.engine.badge(for: kept) == .available)373 await a.engine.clearFinished()374 #expect(a.store.transfers(states: [.done, .cancelled]).isEmpty)375 }376377 @Test func localSearchWorksOfflineAndServerSearchOnline() async throws {378 let a = try await Device()379 let ws = try await a.workspace("search")380 for (name, size) in [("Été 2026 budget.xlsx", 2000), ("photo-plage.jpg", 9000), ("notes de réunion.md", 300)] { _ = try await a.engine.importFile(at: a.tempFile(name, randomData(size)), into: ws.id) }381 await a.settle()382 a.setOffline(true)383 defer { NetworkGate.set("localhost", offline: false) }384 #expect(a.store.search(text: "ete budget", within: ws.id).map(\.name) == ["Été 2026 budget.xlsx"]) // accents and case ignored385 #expect(a.store.search(text: "", kinds: [.image], within: ws.id).map(\.name) == ["photo-plage.jpg"])386 #expect(a.store.search(text: "", ext: "md", within: ws.id).count == 1)387 #expect(a.store.search(text: "", within: ws.id, minSize: 5000).map(\.name) == ["photo-plage.jpg"])388 #expect(a.store.search(text: "100%_", within: ws.id).isEmpty) // wildcard characters are literal389 a.setOffline(false)390 var q = SearchQuery()391 q.text = "réunion"392 #expect(try await a.api.search(q).contains { $0.name == "notes de réunion.md" })393 }394}395