Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/LocalChatServiceTests/SyncTests.swift3// Description: Tests of LocalChatSync: two devices converging through a folder and through the relay, conflicts, tombstones, wrong passphrase, tampering, echo, resume.4import Foundation5import LocalChatCore6import Testing7@testable import LocalChatSync89/// In-memory SyncableStore with last-writer-wins, as the conversation store implements it.10final class MemoryStore: SyncableStore, @unchecked Sendable {11 private let lock = NSLock()12 private var records: [String: SyncRecord] = [:]13 private var _applyCalls = 014 var applyCalls: Int { lock.withLock { _applyCalls } }15 private func key(_ r: SyncRecord) -> String { r.kind.rawValue + ":" + r.id }1617 func put(_ kind: SyncRecord.Kind, _ id: String, _ text: String, at date: Date = Date(), deleted: Bool = false) {18 let r = SyncRecord(kind: kind, id: id, updatedAt: date, isDeleted: deleted, payload: Data(text.utf8))19 lock.withLock { records[key(r)] = r }20 }21 func text(_ kind: SyncRecord.Kind, _ id: String) -> String? {22 lock.withLock { records[kind.rawValue + ":" + id].flatMap { $0.isDeleted ? nil : String(data: $0.payload, encoding: .utf8) } }23 }24 func isDeleted(_ kind: SyncRecord.Kind, _ id: String) -> Bool { lock.withLock { records[kind.rawValue + ":" + id]?.isDeleted == true } }25 var count: Int { lock.withLock { records.count } }2627 func changes(since: Date?) async throws -> [SyncRecord] {28 lock.withLock { records.values.filter { since == nil || $0.updatedAt > since! }.sorted { $0.updatedAt < $1.updatedAt } }29 }30 func apply(_ incoming: [SyncRecord]) async throws -> Int {31 lock.withLock {32 _applyCalls += 1; var applied = 033 for r in incoming where (records[key(r)]?.updatedAt ?? .distantPast) < r.updatedAt { records[key(r)] = r; applied += 1 }34 return applied35 }36 }37}3839/// Talks to a SyncRelay through its HTTP handler, as NodeSyncTransport does over the network.40struct RelayTransport: SyncTransport {41 let relay: SyncRelay42 func send(_ method: String, _ path: String, query: [String: String] = [:], body: Data = Data()) async throws -> HTTPResponseMessage {43 guard let response = await relay.handle(HTTPRequestMessage(method: method, path: "/lc/v1/sync/" + path, query: query, body: body)) else { throw SyncError.transport("no route") }44 return response45 }46 func fetchMetadata() async throws -> Data? { let r = try await send("GET", "meta"); return r.status == 200 ? r.body : nil }47 func storeMetadata(_ data: Data) async throws {48 let r = try await send("PUT", "meta", body: data)49 if r.status == 409 { throw SyncError.metadataConflict }50 }51 func listBatches(after cursor: String?) async throws -> [SyncBatchRef] {52 let r = try await send("GET", "batches", query: cursor.map { ["after": $0] } ?? [:])53 struct Listing: Decodable { struct E: Decodable { var id: String; var cursor: Int64 }; var batches: [E] }54 return try JSONDecoder().decode(Listing.self, from: r.body).batches.map { SyncBatchRef(id: $0.id, cursor: String($0.cursor)) }55 }56 func fetchBatch(_ id: String) async throws -> Data {57 let r = try await send("GET", "batches/\(id)")58 guard r.status == 200 else { throw SyncError.transport("HTTP \(r.status)") }59 return r.body60 }61 func uploadBatch(id: String, data: Data) async throws {62 let r = try await send("PUT", "batches/\(id)", body: data)63 guard (200..<300).contains(r.status) else { throw SyncError.transport("HTTP \(r.status)") }64 }65}6667/// Wraps a transport to count uploads and to fail on demand.68final class FlakyTransport: SyncTransport, @unchecked Sendable {69 let inner: any SyncTransport70 private let lock = NSLock()71 private var _uploads = 0, _fetches = 072 var failUploadsAfter: Int?73 var failFetches = false74 init(_ inner: any SyncTransport) { self.inner = inner }75 var uploads: Int { lock.withLock { _uploads } }76 func fetchMetadata() async throws -> Data? { try await inner.fetchMetadata() }77 func storeMetadata(_ data: Data) async throws { try await inner.storeMetadata(data) }78 func listBatches(after cursor: String?) async throws -> [SyncBatchRef] { try await inner.listBatches(after: cursor) }79 func fetchBatch(_ id: String) async throws -> Data {80 if lock.withLock({ failFetches }) { throw SyncError.transport("offline") }81 return try await inner.fetchBatch(id)82 }83 func uploadBatch(id: String, data: Data) async throws {84 let n = lock.withLock { _uploads += 1; return _uploads }85 if let limit = lock.withLock({ failUploadsAfter }), n > limit { throw SyncError.transport("offline") }86 try await inner.uploadBatch(id: id, data: data)87 }88}8990private func temp(_ name: String = "") throws -> URL {91 let url = FileManager.default.temporaryDirectory.appendingPathComponent("lc-sync-\(UUID().uuidString)\(name)")92 try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)93 return url94}95private func engine(_ store: MemoryStore, _ dir: URL, _ name: String) -> SyncEngine {96 SyncEngine(store: store, stateURL: dir.appendingPathComponent("\(name)-state.json"), pbkdfIterations: 1000)97}98private let t0 = Date(timeIntervalSinceNow: -3600)99100@Suite struct SyncEngineTests {101 /// Runs the same scenario over any transport pair (both ends see the same storage).102 func converge(_ transportA: any SyncTransport, _ transportB: any SyncTransport) async throws {103 let dir = try temp(); let a = MemoryStore(), b = MemoryStore()104 let ea = engine(a, dir, "a"), eb = engine(b, dir, "b")105 try await ea.configure(transport: transportA, passphrase: "shared secret")106 try await eb.configure(transport: transportB, passphrase: "shared secret")107108 a.put(.conversation, "c1", "from A", at: t0)109 a.put(.message, "m1", "hello", at: t0)110 b.put(.template, "t1", "from B", at: t0)111 let first = try await ea.sync()112 #expect(first.pushedRecords == 2 && first.pushedBatches == 1 && first.appliedRecords == 0)113 let second = try await eb.sync()114 #expect(second.appliedRecords == 2 && second.pushedRecords == 1)115 let third = try await ea.sync()116 #expect(third.appliedRecords == 1 && third.pushedRecords == 0) // nothing echoes back117 #expect(a.count == 3 && b.count == 3)118 #expect(b.text(.conversation, "c1") == "from A" && a.text(.template, "t1") == "from B")119120 // quiet rounds move nothing121 let quietA = try await ea.sync(), quietB = try await eb.sync()122 #expect(quietA.pushedBatches == 0 && quietA.pulledBatches == 0 && quietB.pushedBatches == 0 && quietB.pulledBatches == 0)123124 // conflict: the later edit wins on both sides, whatever the sync order125 a.put(.conversation, "c1", "A older edit", at: Date())126 b.put(.conversation, "c1", "B newer edit", at: Date().addingTimeInterval(1))127 _ = try await eb.sync(); _ = try await ea.sync(); _ = try await eb.sync()128 #expect(a.text(.conversation, "c1") == "B newer edit" && b.text(.conversation, "c1") == "B newer edit")129130 // tombstone: a deletion propagates and beats the older version131 a.put(.message, "m1", "", at: Date().addingTimeInterval(2), deleted: true)132 _ = try await ea.sync(); _ = try await eb.sync()133 #expect(b.isDeleted(.message, "m1") && b.text(.message, "m1") == nil)134135 // a third device joining later gets everything136 let c = MemoryStore(); let ec = engine(c, dir, "c")137 try await ec.configure(transport: transportB, passphrase: "shared secret")138 _ = try await ec.sync()139 #expect(c.count == 3 && c.text(.conversation, "c1") == "B newer edit" && c.isDeleted(.message, "m1"))140 let final = try await ea.sync()141 #expect(final.appliedRecords == 0 && final.pushedRecords == 0)142 }143144 @Test func convergeThroughFolder() async throws {145 let folder = try temp("-shared")146 try await converge(FolderSyncTransport(folder: folder), FolderSyncTransport(folder: folder))147 // only ciphertext reaches the folder148 for file in try FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil) {149 let text = String(decoding: try Data(contentsOf: file), as: UTF8.self)150 #expect(!text.contains("from A") && !text.contains("hello") && !text.contains("B newer edit"), "\(file.lastPathComponent)")151 }152 }153154 @Test func convergeThroughRelay() async throws {155 let relay = SyncRelay(directory: try temp("-relay"))156 try await converge(RelayTransport(relay: relay), RelayTransport(relay: relay))157 #expect(await relay.batchCount >= 4)158 }159160 @Test func wrongPassphraseAppliesNothing() async throws {161 let folder = try temp("-shared"), dir = try temp()162 let a = MemoryStore(), b = MemoryStore()163 let ea = engine(a, dir, "a"), eb = engine(b, dir, "b")164 try await ea.configure(transport: FolderSyncTransport(folder: folder), passphrase: "right")165 a.put(.conversation, "c1", "private", at: t0)166 _ = try await ea.sync()167 await #expect(throws: SyncError.wrongPassphrase) { try await eb.configure(transport: FolderSyncTransport(folder: folder), passphrase: "wrong") }168 #expect(await eb.isConfigured == false)169 await #expect(throws: SyncError.notConfigured) { try await eb.sync() }170 #expect(b.count == 0 && b.applyCalls == 0)171 // a stored key that does not match the group is refused too172 await #expect(throws: SyncError.wrongPassphrase) { try await eb.configure(transport: FolderSyncTransport(folder: folder), masterKey: Data(repeating: 1, count: 32)) }173 // the right passphrase returns a key that can be reused at next launch174 let key = try await eb.configure(transport: FolderSyncTransport(folder: folder), passphrase: "right")175 #expect(key.count == 32)176 let again = engine(MemoryStore(), dir, "again")177 try await again.configure(transport: FolderSyncTransport(folder: folder), masterKey: key)178 #expect(try await again.sync().appliedRecords == 1)179 }180181 @Test func tamperedBatchesAreRejected() async throws {182 let folder = try temp("-shared"), dir = try temp()183 let a = MemoryStore(), b = MemoryStore()184 let ea = engine(a, dir, "a"), eb = engine(b, dir, "b")185 let transport = FolderSyncTransport(folder: folder)186 try await ea.configure(transport: transport, passphrase: "pw"); try await eb.configure(transport: transport, passphrase: "pw")187 a.put(.conversation, "c1", "one", at: t0)188 _ = try await ea.sync()189 a.put(.conversation, "c2", "two", at: Date())190 _ = try await ea.sync()191 let files = try FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil).filter { $0.pathExtension == "lcbatch" }.sorted { $0.lastPathComponent < $1.lastPathComponent }192 try #require(files.count == 2)193 // flip one ciphertext byte of the first batch194 var object = try JSONSerialization.jsonObject(with: Data(contentsOf: files[0])) as! [String: Any]195 var cipher = Data(base64Encoded: object["ciphertext"] as! String)!196 cipher[cipher.count / 2] ^= 0x40197 object["ciphertext"] = cipher.base64EncodedString()198 try JSONSerialization.data(withJSONObject: object).write(to: files[0])199 // replay the second batch under a new name (header id no longer matches)200 try FileManager.default.copyItem(at: files[1], to: folder.appendingPathComponent("FFFFFFFF-replayed-00000001.lcbatch"))201 // garbage202 try Data("not a batch".utf8).write(to: folder.appendingPathComponent("garbage.lcbatch"))203204 let summary = try await eb.sync()205 #expect(summary.skippedBatches.count == 3 && summary.appliedRecords == 1)206 #expect(b.text(.conversation, "c1") == nil && b.text(.conversation, "c2") == "two")207 // header tampering is caught as well (the header is authenticated)208 let good = try SyncEngineInternals.sealForTest(header: ("x-00000001", "x"), text: "v")209 #expect(throws: SyncError.self) { try SyncEngineInternals.openWithAlteredHeader(good) }210 }211212 @Test func resumesAfterInterruptedPushAndPull() async throws {213 let folder = try temp("-shared"), dir = try temp()214 let a = MemoryStore(), b = MemoryStore()215 let flakyA = FlakyTransport(FolderSyncTransport(folder: folder))216 let ea = engine(a, dir, "a")217 try await ea.configure(transport: flakyA, passphrase: "pw")218 for i in 0..<900 { a.put(.message, "m\(i)", "text \(i)", at: t0.addingTimeInterval(Double(i))) } // 3 batches of ≤ 400219 flakyA.failUploadsAfter = 1220 await #expect(throws: SyncError.self) { try await ea.sync() }221 if case .failed = await ea.currentStatus {} else { Issue.record("expected a failed status") }222 flakyA.failUploadsAfter = nil223 // a new engine instance (app relaunch) continues from the persisted state: the first batch is not uploaded again224 let relaunched = engine(a, dir, "a")225 try await relaunched.configure(transport: flakyA, passphrase: "pw")226 let resumed = try await relaunched.sync()227 #expect(resumed.pushedRecords == 500 && resumed.pushedBatches == 2)228 #expect(try FileManager.default.contentsOfDirectory(atPath: folder.path).filter { $0.hasSuffix(".lcbatch") }.count == 3)229230 let flakyB = FlakyTransport(FolderSyncTransport(folder: folder))231 let eb = engine(b, dir, "b")232 try await eb.configure(transport: flakyB, passphrase: "pw")233 flakyB.failFetches = true234 await #expect(throws: SyncError.self) { try await eb.sync() }235 #expect(b.count == 0)236 flakyB.failFetches = false237 #expect(try await eb.sync().appliedRecords == 900)238 #expect(b.count == 900 && b.text(.message, "m899") == "text 899")239 #expect(try await eb.sync().pushedRecords == 0) // 900 received records are not echoed240 #expect(flakyB.uploads == 0)241 }242243 @Test func localOnlyModeSendsNothing() async throws {244 let dir = try temp(); let store = MemoryStore()245 let e = engine(store, dir, "solo")246 store.put(.conversation, "c1", "private", at: t0)247 #expect(await e.currentStatus == .localOnly)248 #expect(await e.isConfigured == false)249 await #expect(throws: SyncError.notConfigured) { try await e.sync() }250 let folder = try temp("-shared")251 try await e.configure(transport: FolderSyncTransport(folder: folder), passphrase: "pw")252 _ = try await e.sync()253 await e.disable()254 #expect(await e.currentStatus == .localOnly)255 store.put(.conversation, "c2", "after disabling", at: Date())256 await #expect(throws: SyncError.notConfigured) { try await e.sync() }257 #expect(try FileManager.default.contentsOfDirectory(atPath: folder.path).filter { $0.hasSuffix(".lcbatch") }.count == 1)258 #expect(SyncRecord.Kind.carriesModelWeights == false)259 #expect(!SyncRecord.Kind.allCases.map(\.rawValue).contains { $0.contains("model") || $0.contains("weight") || $0.contains("download") })260 }261262 @Test func statusStream() async throws {263 let dir = try temp(); let store = MemoryStore(); let e = engine(store, dir, "s")264 let stream = await e.statusUpdates()265 let collector = Task { () -> [SyncStatus] in266 var seen: [SyncStatus] = []267 for await s in stream { seen.append(s); if case .idle(let last) = s, last != nil { break } }268 return seen269 }270 try await e.configure(transport: FolderSyncTransport(folder: try temp("-shared")), passphrase: "pw")271 store.put(.folder, "f1", "x", at: t0)272 _ = try await e.sync()273 let seen = await collector.value274 #expect(seen.first == .localOnly && seen.contains(.syncing))275 if case .idle(let last) = seen.last { #expect(last?.pushedRecords == 1) } else { Issue.record("expected idle") }276 }277}278279@Suite struct SyncRelayTests {280 @Test func routesLimitsAndPersistence() async throws {281 let dir = try temp("-relay")282 var limits = SyncRelay.Limits(); limits.maxBatchBytes = 64; limits.maxBatches = 3283 let relay = SyncRelay(directory: dir, limits: limits)284 func send(_ m: String, _ p: String, _ q: [String: String] = [:], _ body: Data = Data()) async -> HTTPResponseMessage? {285 await relay.handle(HTTPRequestMessage(method: m, path: p, query: q, body: body))286 }287 #expect(await send("GET", "/v1/models") == nil) // not ours288 #expect(await send("GET", "/lc/v1/sync/meta")?.status == 404)289 #expect(await send("PUT", "/lc/v1/sync/meta", [:], Data("meta-1".utf8))?.status == 201)290 #expect(await send("PUT", "/lc/v1/sync/meta", [:], Data("meta-1".utf8))?.status == 200) // same content: idempotent291 #expect(await send("PUT", "/lc/v1/sync/meta", [:], Data("meta-2".utf8))?.status == 409)292 #expect(await send("GET", "/lc/v1/sync/meta")?.body == Data("meta-1".utf8))293 #expect(await send("PUT", "/lc/v1/sync/batches/a-1", [:], Data("one".utf8))?.status == 201)294 #expect(await send("PUT", "/lc/v1/sync/batches/a-1", [:], Data("one again".utf8))?.status == 200) // retry keeps its position295 #expect(await send("GET", "/lc/v1/sync/batches/a-1")?.body == Data("one again".utf8))296 #expect(await send("PUT", "/lc/v1/sync/batches/big", [:], Data(repeating: 1, count: 65))?.status == 413)297 #expect(await send("PUT", "/lc/v1/sync/batches/empty")?.status == 413)298 for bad in ["../meta.json", "a/b", ".hidden", "a b", String(repeating: "x", count: 129)] {299 let status = await send("PUT", "/lc/v1/sync/batches/" + bad, [:], Data("x".utf8))?.status300 #expect(status == 400 || status == 404, "\(bad)")301 }302 #expect(await send("GET", "/lc/v1/sync/batches/unknown")?.status == 404)303 #expect(await send("DELETE", "/lc/v1/sync/batches/a-1")?.status == 405)304 for id in ["a-2", "a-3", "a-4"] { _ = await send("PUT", "/lc/v1/sync/batches/\(id)", [:], Data(id.utf8)) }305 struct Listing: Decodable { struct E: Decodable { var id: String; var cursor: Int64 }; var batches: [E]; var cursor: Int64 }306 var listing = try JSONDecoder().decode(Listing.self, from: (await send("GET", "/lc/v1/sync/batches"))!.body)307 #expect(listing.batches.map(\.id) == ["a-2", "a-3", "a-4"] && listing.batches.map(\.cursor) == [2, 3, 4]) // a-1 pruned (max 3), cursors stay monotonic308 listing = try JSONDecoder().decode(Listing.self, from: (await send("GET", "/lc/v1/sync/batches", ["after": "3"]))!.body)309 #expect(listing.batches.map(\.id) == ["a-4"] && listing.cursor == 4)310 #expect(!FileManager.default.fileExists(atPath: dir.deletingLastPathComponent().appendingPathComponent("meta.json").path))311 // a restarted relay keeps batches and never reuses a cursor312 let restarted = SyncRelay(directory: dir, limits: limits)313 #expect(await restarted.batchCount == 3)314 let response = await restarted.handle(HTTPRequestMessage(method: "PUT", path: "/lc/v1/sync/batches/a-5", body: Data("5".utf8)))315 #expect(String(decoding: response!.body, as: UTF8.self).contains("5"))316 }317318 @Test func nodeTransportRefusesInsecurePublicHTTP() throws {319 #expect(throws: SyncError.insecureURL) { try NodeSyncTransport(baseURL: URL(string: "http://example.com:8765")!, apiKey: "k") }320 #expect(throws: SyncError.insecureURL) { try NodeSyncTransport(baseURL: URL(string: "ftp://studio.local")!, apiKey: "k") }321 #expect(try NodeSyncTransport(baseURL: URL(string: "http://studio.local:8765/v1")!, apiKey: "k").baseURL.absoluteString == "http://studio.local:8765/")322 _ = try NodeSyncTransport(baseURL: URL(string: "http://192.168.2.103:8765")!, apiKey: "k")323 _ = try NodeSyncTransport(baseURL: URL(string: "http://100.125.225.117:8765")!, apiKey: "k")324 _ = try NodeSyncTransport(baseURL: URL(string: "https://example.com")!, apiKey: "k")325 _ = try NodeSyncTransport(baseURL: URL(string: "http://example.com")!, apiKey: "k", allowInsecureHTTP: true)326 #expect(NodeSyncTransport.isPrivateHost("10.0.0.1") && NodeSyncTransport.isPrivateHost("localhost") && !NodeSyncTransport.isPrivateHost("8.8.8.8") && !NodeSyncTransport.isPrivateHost("172.32.0.1"))327 }328}329