Repository Public
1// SP Notes iOS2// File: Tests/ShellTestSupport.swift3// Description: Shared pieces of the shell tests: a device (store file + client + engine + repositories) on the fake backend of4// the Core, with a restart that simulates the app being killed, and an application model whose local operations5// share the store of that device.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesCore11@testable import SPNotes1213/// A device talking to `FakeBackend`: what `AppModel` builds, with the fake's session configuration.14final class FakeDevice: @unchecked Sendable {15 let fake: FakeBackend16 let directory: URL17 let tokenStore: InMemoryTokenStore18 let files: AttachmentFiles19 private(set) var store: NotesStore20 private(set) var api: NotesAPI21 private(set) var engine: SyncEngine2223 init(fake: FakeBackend, store existing: NotesStore? = nil) throws {24 self.fake = fake25 directory = FileManager.default.temporaryDirectory.appendingPathComponent("spn-shell-\(UUID().uuidString)", isDirectory: true)26 try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)27 tokenStore = InMemoryTokenStore(token: fake.issueToken())28 files = AttachmentFiles(directory: directory.appendingPathComponent("Attachments", isDirectory: true))29 store = try existing ?? NotesStore(url: directory.appendingPathComponent("notes.sqlite"))30 (api, engine) = Self.stack(fake: fake, tokenStore: tokenStore, store: store, files: files)31 }3233 deinit { try? FileManager.default.removeItem(at: directory) }3435 private static func stack(fake: FakeBackend, tokenStore: InMemoryTokenStore, store: NotesStore, files: AttachmentFiles) -> (NotesAPI, SyncEngine) {36 let client = HTTPClient(configuration: APIConfiguration(baseURL: fake.baseURL, userAgent: "shell-tests"), tokenStore: tokenStore,37 sessionConfiguration: FakeBackend.sessionConfiguration())38 let api = NotesAPI(client: client)39 return (api, SyncEngine(store: store, api: api, files: files))40 }4142 var repositories: NotesRepositories { NotesRepositories(store: store, api: api, engine: engine) }4344 /// Kills the "app": the store is closed, then a new store on the same file, a new client and a new engine are created.45 func restart() async throws {46 await engine.stop()47 await store.close()48 store = try NotesStore(url: directory.appendingPathComponent("notes.sqlite"))49 (api, engine) = Self.stack(fake: fake, tokenStore: tokenStore, store: store, files: files)50 }5152 @discardableResult func sync() async -> SyncOutcome { await engine.sync() }53 func close() async { await engine.stop(); await store.close() }54}5556@MainActor57enum ShellFixtures {58 /// An application model on a throw-away in-memory store (no session: nothing ever reaches a server).59 static func app() throws -> AppModel {60 let directory = FileManager.default.temporaryDirectory.appendingPathComponent("spn-shell-app-\(UUID().uuidString)", isDirectory: true)61 return try AppModel(configuration: AppConfiguration(api: .production(appVersion: "tests"), dataDirectory: directory, inMemoryStore: true))62 }6364 static func summary(_ id: String, title: String = "Note", type: String = "page", folder: Bool = false) -> PageSummary {65 PageSummary(id: id, title: title, preview: "", updatedAt: Date(), section: "notes", isFolder: folder, syncState: .synced, type: type, position: 0, wordCount: 0)66 }67}68