Repository Public
1// SP Notes Core2// File: Tests/SPNotesSyncTests/EntitySupport.swift3// Description: Plumbing of the 2.0 sync tests: repositories of a test device, canvas builders, and comparisons of the4// tasks and canvases of a device with the fake server.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SPNotesAPI10import SPNotesDocument11import SPNotesStore12import Testing13@testable import SPNotesSync14@testable import SPNotesTestSupport1516extension Device {17 /// The tests drive every cycle themselves: no cycle is scheduled behind their back after a repository edit (a18 /// background cycle 400 ms after `canvases.create` pulled the other device's copy early and made19 /// `feedMeetsLocalChange` fail about one run in four under CPU load).20 var context: RepositoryContext {21 var context = RepositoryContext(store: store, api: api, engine: engine)22 context.schedulesSyncAfterEdits = false23 return context24 }25 var tasks: TasksRepository { TasksRepository(context: context) }26 var canvases: CanvasRepository { CanvasRepository(context: context) }2728 func canvasDocument(_ pageId: String) async throws -> CanvasDocument {29 guard let state = try await canvases.state(pageId: pageId) else { throw StoreError.notFound("canvas \(pageId)") }30 return state.document31 }3233 /// Edits the local canvas through a closure, like the app after a gesture.34 func editCanvas(_ pageId: String, _ change: (inout CanvasDocument) -> Void) async throws {35 let loaded = try await canvasDocument(pageId)36 var document = loaded37 change(&document)38 try await canvases.save(pageId: pageId, document, editedFrom: loaded)39 }40}4142enum Canvas {43 static func node(_ id: String, _ text: String, x: Double = 0, y: Double = 0) -> CanvasNode {44 var node = CanvasNode(id: id, kind: .text, x: x, y: y, w: 200, h: 80)45 node.text = text46 return node47 }4849 static func document(_ nodes: [CanvasNode], edges: [CanvasEdge] = []) -> CanvasDocument { CanvasDocument(nodes: nodes, edges: edges) }5051 static func parse(_ data: Data?) -> CanvasDocument? { data.flatMap { try? CanvasDocument(data: $0) } }5253 /// id → text of every node (what must be identical everywhere).54 static func texts(_ document: CanvasDocument?) -> [String: String] {55 Dictionary((document?.nodes ?? []).map { ($0.id, $0.text ?? "") }, uniquingKeysWith: { a, _ in a })56 }57}5859/// What must be identical on a device and on the server for a task.60struct TaskFingerprint: Equatable, CustomStringConvertible {61 var title: String62 var status: String63 var priority: String64 var due: String65 var pageId: String6667 var description: String { "\(title) [\(status) \(priority) due=\(due) page=\(pageId.prefix(6))]" }6869 init(_ task: TaskItem) {70 title = task.title; status = task.status; priority = task.priority ?? "medium"71 due = task.dueAt.map(APIDate.string(from:)) ?? "-"; pageId = task.pageId ?? "-"72 }7374 init(_ task: FakeTask) {75 title = task.title; status = task.status; priority = task.priority76 due = task.dueAt.map(APIDate.string(from:)) ?? "-"; pageId = task.pageId ?? "-"77 }78}7980/// The device holds exactly the live tasks of the server, in the same state, with an empty outbox.81func expectTaskMirror(_ device: Device, sourceLocation: SourceLocation = #_sourceLocation) async throws {82 let local = Dictionary(uniqueKeysWithValues: try await device.tasks.list(.all).map { ($0.id, TaskFingerprint($0)) })83 let remote = Dictionary(uniqueKeysWithValues: device.fake.allTasks.map { ($0.id, TaskFingerprint($0)) })84 #expect(Set(local.keys) == Set(remote.keys), "\(device.name): same tasks as the server", sourceLocation: sourceLocation)85 for (id, task) in local { if let server = remote[id] { #expect(task == server, "\(device.name): task \(id.prefix(6))", sourceLocation: sourceLocation) } }86 #expect(try await device.store.entityOpCount() == 0, "\(device.name): empty entity outbox", sourceLocation: sourceLocation)87 #expect(try await device.store.dirtyEntities(kind: .task).isEmpty, "\(device.name): no dirty task", sourceLocation: sourceLocation)88}8990/// The device holds the canvas of the server, clean, at the server version.91func expectCanvasMirror(_ device: Device, _ pageId: String, sourceLocation: SourceLocation = #_sourceLocation) async throws {92 let record = try #require(try await device.store.canvas(pageId: pageId), "\(device.name): canvas \(pageId.prefix(6)) exists", sourceLocation: sourceLocation)93 let server = try #require(device.fake.canvas(pageId), sourceLocation: sourceLocation)94 #expect(Canvas.parse(record.json) == Canvas.parse(server.data), "\(device.name): canvas equals the server copy", sourceLocation: sourceLocation)95 #expect(!record.dirty, "\(device.name): canvas has nothing left to push", sourceLocation: sourceLocation)96 #expect(!record.hasConflict, "\(device.name): no canvas conflict", sourceLocation: sourceLocation)97 #expect(record.version == server.version, "\(device.name): canvas at the server version", sourceLocation: sourceLocation)98}99