Repository Public
1// SP Notes Core2// File: Sources/SPNotesFeaturesCatalog/FeatureWorld.swift3// Description: A complete in-memory "device" for the feature screens: the fake backend, a real store file in a temporary4// directory, the API client, the sync engine and the repositories. Used by `FeatureCatalogView` and by the5// tests of SPNotesFeatures. Never linked in a shipping app.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesAPI11import SPNotesStore12import SPNotesSync13import SPNotesTestSupport1415/// Attachment files of the sample device.16public struct FeatureWorldFiles: AttachmentFileLocating {17 public let root: URL18 public func url(relativePath: String) -> URL { root.appendingPathComponent(relativePath) }19 public func relativePath(id: String, filename: String) -> String { "\(id)/\(filename)" }20 public func remove(relativePath: String) { try? FileManager.default.removeItem(at: url(relativePath: relativePath)) }21}2223public final class FeatureWorld: @unchecked Sendable {24 public let fake: FakeBackend25 public let directory: URL26 public let store: NotesStore27 public let client: HTTPClient28 public let api: NotesAPI29 public let engine: SyncEngine30 public let files: FeatureWorldFiles31 public let repositories: NotesRepositories3233 /// - Parameter samples: installs the sample data of every area (`FeatureSamples.install`) and runs one sync so the34 /// local copy is filled. `false` = an empty workspace (empty-state tests).35 public init(samples: Bool = true, apiLevel: Int = 3) async throws {36 fake = FakeBackend(apiLevel: apiLevel)37 directory = FileManager.default.temporaryDirectory.appendingPathComponent("spn-features-\(UUID().uuidString)")38 try FileManager.default.createDirectory(at: directory.appendingPathComponent("files"), withIntermediateDirectories: true)39 files = FeatureWorldFiles(root: directory.appendingPathComponent("files"))40 store = try NotesStore(url: directory.appendingPathComponent("notes.sqlite"))41 client = HTTPClient(configuration: APIConfiguration(baseURL: fake.baseURL, userAgent: "features"),42 tokenStore: InMemoryTokenStore(token: fake.issueToken()), sessionConfiguration: FakeBackend.sessionConfiguration())43 api = NotesAPI(client: client)44 engine = SyncEngine(store: store, api: api, files: files)45 repositories = NotesRepositories(store: store, api: api, engine: engine)46 if samples {47 FeatureSamples.install(on: self)48 _ = await engine.sync()49 }50 }5152 deinit {53 fake.shutdown()54 try? FileManager.default.removeItem(at: directory)55 }5657 public func setOffline(_ offline: Bool) { fake.setOffline(offline) }5859 @discardableResult60 public func sync() async -> SyncOutcome { await engine.sync() }61}6263/// Sample data of the catalogue and of the tests. Each area owns one `Samples+<Area>.swift` file with its installer;64/// recorded answers of the scratch backend live in `Samples/<group>/<name>.json` (resources of this target).65public enum FeatureSamples {66 /// A fixed "now" for deterministic renders: 2026-11-19 15:00 UTC.67 public static let now = Date(timeIntervalSince1970: 1_795_100_400)6869 public static func install(on world: FeatureWorld) {70 installWork(on: world)71 installKnowledge(on: world)72 installMedia(on: world)73 installAssistant(on: world)74 installBlocks(on: world)75 }7677 /// The text of a bundled sample (`Samples/<group>/<name>.json`), "null" when missing.78 public static func text(_ group: String, _ name: String, ext: String = "json") -> String {79 guard let url = url(group, name, ext: ext), let text = try? String(contentsOf: url, encoding: .utf8) else { return "null" }80 return text81 }8283 public static func data(_ group: String, _ name: String, ext: String) -> Data? {84 url(group, name, ext: ext).flatMap { try? Data(contentsOf: $0) }85 }8687 public static func url(_ group: String, _ name: String, ext: String = "json") -> URL? {88 let url = Bundle.module.resourceURL?.appendingPathComponent("Samples").appendingPathComponent(group).appendingPathComponent(name + "." + ext)89 guard let url, FileManager.default.fileExists(atPath: url.path) else { return nil }90 return url91 }92}93