Repository Public
1// SP Notes Core2// File: Tests/SPNotesSyncTests/TestSupport.swift3// Description: Plumbing of the sync tests: a "device" (a real NotesStore file in a temporary directory, an HTTP client and an4// engine, restartable like an app that was killed), document builders with explicit block ids, and comparisons5// between devices and the server.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesAPI11import SPNotesStore12import Testing13@testable import SPNotesSync14@testable import SPNotesTestSupport1516/// Attachment files of one device, under its temporary directory.17struct TestFiles: AttachmentFileLocating {18 let root: URL19 func url(relativePath: String) -> URL { root.appendingPathComponent(relativePath) }20 func relativePath(id: String, filename: String) -> String { "\(id)/\(filename)" }21 func remove(relativePath: String) { try? FileManager.default.removeItem(at: url(relativePath: relativePath)) }22 func exists(_ relativePath: String) -> Bool { FileManager.default.fileExists(atPath: url(relativePath: relativePath).path) }23}2425/// One paragraph of a test document: a block id and its text.26typealias Block = (id: String, text: String)2728enum Doc {29 /// A document made of paragraphs with explicit block ids (canonical bytes: sorted keys).30 static func data(_ blocks: [Block]) -> Data {31 let content: [[String: Any]] = blocks.map { block in32 var node: [String: Any] = ["type": "paragraph", "attrs": ["id": block.id]]33 if !block.text.isEmpty { node["content"] = [["type": "text", "text": block.text]] }34 return node35 }36 return try! JSONSerialization.data(withJSONObject: ["type": "doc", "content": content], options: [.sortedKeys])37 }3839 static func blocks(_ data: Data) -> [Block] {40 guard let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return [] }41 return (root["content"] as? [[String: Any]] ?? []).map { node in42 let id = (node["attrs"] as? [String: Any])?["id"] as? String ?? ""43 let text = (node["content"] as? [[String: Any]] ?? []).compactMap { $0["text"] as? String }.joined()44 return (id, text)45 }46 }4748 static func texts(_ data: Data) -> [String] { blocks(data).map(\.text) }49}5051/// A device: store file + HTTP client + engine. `restart()` simulates the app being killed and launched again.52final class Device: @unchecked Sendable {53 let name: String54 let fake: FakeBackend55 let directory: URL56 let tokenStore: InMemoryTokenStore57 let files: TestFiles58 let options: SyncOptions59 private(set) var store: NotesStore60 private(set) var client: HTTPClient61 private(set) var api: NotesAPI62 private(set) var engine: SyncEngine6364 init(_ name: String, fake: FakeBackend, token: String? = nil, options: SyncOptions = SyncOptions()) throws {65 self.name = name66 self.fake = fake67 self.options = options68 directory = FileManager.default.temporaryDirectory.appendingPathComponent("spn-sync-\(name)-\(UUID().uuidString)")69 try FileManager.default.createDirectory(at: directory.appendingPathComponent("files"), withIntermediateDirectories: true)70 tokenStore = InMemoryTokenStore(token: token ?? fake.issueToken())71 files = TestFiles(root: directory.appendingPathComponent("files"))72 store = try NotesStore(url: directory.appendingPathComponent("notes.sqlite"))73 (client, api, engine) = Self.stack(name: name, fake: fake, tokenStore: tokenStore, store: store, files: files, options: options)74 }7576 deinit { try? FileManager.default.removeItem(at: directory) }7778 private static func stack(name: String, fake: FakeBackend, tokenStore: InMemoryTokenStore, store: NotesStore, files: TestFiles, options: SyncOptions) -> (HTTPClient, NotesAPI, SyncEngine) {79 let client = HTTPClient(configuration: APIConfiguration(baseURL: fake.baseURL, userAgent: name), tokenStore: tokenStore, sessionConfiguration: FakeBackend.sessionConfiguration())80 let api = NotesAPI(client: client)81 return (client, api, SyncEngine(store: store, api: api, files: files, options: options))82 }8384 /// Kills the "app": the store is closed, then a new store on the same file, a new client and a new engine are created.85 func restart() async throws {86 await engine.stop()87 await store.close()88 store = try NotesStore(url: directory.appendingPathComponent("notes.sqlite"))89 (client, api, engine) = Self.stack(name: name, fake: fake, tokenStore: tokenStore, store: store, files: files, options: options)90 }9192 func close() async {93 await engine.stop()94 await store.close()95 }9697 var token: String? { tokenStore.load() }98 func setOffline(_ offline: Bool) { fake.setOffline(offline, device: name) }99100 @discardableResult101 func sync() async -> SyncOutcome { await engine.sync() }102103 // MARK: local edits (what the app does)104105 @discardableResult106 func create(_ title: String, _ blocks: [Block] = [("b0", "")], id: String? = nil, parent: String? = nil, tags: [String] = [], properties: String = "{}") async throws -> String {107 let content = Doc.data(blocks)108 let digest = ContentDigest(content: content)109 let page = try await store.createPage(NewPage(id: id, parentId: parent, title: title, content: content, properties: Data(properties.utf8),110 plainText: digest.plainText, wordCount: digest.wordCount, preview: digest.preview, tags: tags))111 return page.id112 }113114 func page(_ id: String) async throws -> PageRecord {115 guard let page = try await store.page(id: id) else { throw StoreError.notFound("page \(id)") }116 return page117 }118119 func blocks(_ id: String) async throws -> [Block] { Doc.blocks(try await page(id).content) }120 func texts(_ id: String) async throws -> [String] { Doc.texts(try await page(id).content) }121122 func setBlocks(_ id: String, _ blocks: [Block], title: String? = nil) async throws {123 let content = Doc.data(blocks)124 let digest = ContentDigest(content: content)125 _ = try await store.updateContent(id: id, title: title, content: content, plainText: digest.plainText, wordCount: digest.wordCount, preview: digest.preview)126 }127128 func setTitle(_ id: String, _ title: String) async throws {129 let page = try await page(id)130 _ = try await store.updateContent(id: id, title: title, content: nil, plainText: page.plainText, wordCount: page.wordCount, preview: page.preview)131 }132133 /// Replaces the text of one block.134 func edit(_ id: String, block: String, text: String) async throws {135 var blocks = try await blocks(id)136 guard let index = blocks.firstIndex(where: { $0.id == block }) else { throw StoreError.notFound("block \(block)") }137 blocks[index].text = text138 try await setBlocks(id, blocks)139 }140141 /// Appends a new paragraph.142 func append(_ id: String, block: String, text: String) async throws {143 try await setBlocks(id, try await blocks(id) + [(block, text)])144 }145146 /// Writes a file in the attachments directory and registers it as waiting for upload.147 @discardableResult148 func attach(_ pageId: String, filename: String, mime: String, bytes: Data, id: String? = nil) async throws -> AttachmentRecord {149 let attachmentId = id ?? UUID().uuidString.lowercased()150 let relative = files.relativePath(id: attachmentId, filename: filename)151 let url = files.url(relativePath: relative)152 try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)153 try bytes.write(to: url)154 return try await store.addLocalAttachment(id: attachmentId, pageId: pageId, filename: filename, mime: mime, size: Int64(bytes.count), sha256: nil, localPath: relative)155 }156157 /// Every page of the store, whatever its state.158 func allPages() async throws -> [PageRecord] {159 var out: [String: PageRecord] = [:]160 for list in [SmartList.all, .archived, .trash, .inbox] {161 var offset = 0162 while true {163 let batch = try await store.summaries(list: list, limit: 500, offset: offset)164 for summary in batch { if let page = try await store.page(id: summary.id) { out[page.id] = page } }165 if batch.count < 500 { break }166 offset += 500167 }168 }169 return Array(out.values)170 }171}172173/// One fake server and its devices; everything is released at the end of the test.174final class World: @unchecked Sendable {175 let fake: FakeBackend176 private(set) var devices: [Device] = []177178 /// `apiLevel` nil = the default of the run (3, or `SPN_FAKE_API_LEVEL`); the 2.0 suites ask for 3 explicitly.179 init(apiLevel: Int? = nil) { fake = FakeBackend(apiLevel: apiLevel) }180181 deinit { fake.shutdown() }182183 func device(_ name: String, options: SyncOptions = SyncOptions()) throws -> Device {184 let device = try Device(name, fake: fake, options: options)185 devices.append(device)186 return device187 }188189 func closeAll() async { for device in devices { await device.close() } }190}191192/// The state of a page that must be identical everywhere once everything is synchronised.193struct PageFingerprint: Equatable, CustomStringConvertible {194 var title: String195 var texts: [String]196 var tags: [String]197 var pinned: Bool198 var archived: Bool199 var parentId: String?200 var trashed: Bool201 var properties: String202203 var description: String { "\(title) \(texts) tags=\(tags) pinned=\(pinned) archived=\(archived) parent=\(parentId ?? "-") trashed=\(trashed) props=\(properties)" }204205 private static func canonical(_ data: Data) -> String {206 guard let object = try? JSONSerialization.jsonObject(with: data), let out = try? JSONSerialization.data(withJSONObject: object, options: [.sortedKeys]) else { return "?" }207 return String(decoding: out, as: UTF8.self)208 }209210 init(_ page: PageRecord) {211 title = page.title; texts = Doc.texts(page.content); tags = page.tags.sorted(); pinned = page.pinned; archived = page.archived212 parentId = page.parentId; trashed = page.deletedAt != nil; properties = Self.canonical(page.properties)213 }214215 init(_ page: FakePage) {216 title = page.title; texts = Doc.texts(page.content); tags = page.tags.sorted(); pinned = page.pinned; archived = page.archived217 parentId = page.parentId; trashed = page.deletedAt != nil; properties = Self.canonical(page.properties)218 }219}220221/// Checks that a device holds exactly the pages of the server, with the same state, and nothing waiting.222func expectMirror(_ device: Device, sourceLocation: SourceLocation = #_sourceLocation) async throws {223 let local = Dictionary(uniqueKeysWithValues: try await device.allPages().map { ($0.id, $0) })224 let remote = Dictionary(uniqueKeysWithValues: device.fake.allPages.map { ($0.id, $0) })225 #expect(Set(local.keys) == Set(remote.keys), "\(device.name): same set of pages as the server", sourceLocation: sourceLocation)226 for (id, page) in local {227 guard let server = remote[id] else { continue }228 #expect(PageFingerprint(page) == PageFingerprint(server), "\(device.name): page \(id) equals the server copy", sourceLocation: sourceLocation)229 #expect(!page.isDirty, "\(device.name): page \(id) has nothing left to push", sourceLocation: sourceLocation)230 #expect(page.serverVersion == server.version, "\(device.name): page \(id) is at the server version", sourceLocation: sourceLocation)231 }232 #expect(try await device.store.opCount() == 0, "\(device.name): empty queue", sourceLocation: sourceLocation)233}234235/// Waits until a condition holds (polling), or fails after the timeout.236func eventually(timeout: TimeInterval = 5, _ what: String = "condition", sourceLocation: SourceLocation = #_sourceLocation, _ condition: () async throws -> Bool) async rethrows {237 let deadline = Date().addingTimeInterval(timeout)238 while Date() < deadline {239 if try await condition() { return }240 try? await Task.sleep(for: .milliseconds(20))241 }242 Issue.record("timed out waiting for \(what)", sourceLocation: sourceLocation)243}244245/// A small deterministic generator (SplitMix64) for reproducible random scenarios.246struct SeededRandom: RandomNumberGenerator {247 private var state: UInt64248 init(seed: UInt64) { state = seed }249 mutating func next() -> UInt64 {250 state &+= 0x9E3779B97F4A7C15251 var z = state252 z = (z ^ (z >> 30)) &* 0xBF58476D1CE4E5B9253 z = (z ^ (z >> 27)) &* 0x94D049BB133111EB254 return z ^ (z >> 31)255 }256}257