SPB Git

Repository Public

Support.swift 2.9 KB 59 lines swift

1// SP Notes Core2// File: Tests/SPNotesStoreTests/Support.swift3// Description: Shared helpers of the store tests: temporary database files, document and remote page builders.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8@testable import SPNotesStore910/// A unique temporary directory holding one database file; removed when the value is released.11final class TempDatabase: Sendable {12    let directory: URL13    var url: URL { directory.appendingPathComponent("notes.sqlite") }1415    init() {16        directory = FileManager.default.temporaryDirectory.appendingPathComponent("spnotes-store-tests-\(UUID().uuidString)")17        try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)18    }1920    deinit { try? FileManager.default.removeItem(at: directory) }21}2223/// An opaque "document" whose bytes depend on the text.24func doc(_ text: String) -> Data {25    let object: [String: Any] = ["type": "doc", "content": [["type": "paragraph", "content": [["type": "text", "text": text]]]]]26    return try! JSONSerialization.data(withJSONObject: object, options: [.sortedKeys])27}2829func words(_ text: String) -> Int { text.split(separator: " ").count }3031func draft(_ title: String, _ text: String = "", id: String? = nil, parent: String? = nil, section: String? = nil,32           tags: [String] = [], properties: String = "{}") -> NewPage {33    NewPage(id: id, parentId: parent, section: section, title: title, content: doc(text), properties: Data(properties.utf8),34            plainText: text, wordCount: words(text), preview: String(text.prefix(80)), tags: tags)35}3637let epoch = Date(timeIntervalSince1970: 1_780_000_000)3839func remote(_ id: String, _ title: String, _ text: String = "", version: Int = 1, parent: String? = nil, section: String = "notes",40            tags: [String] = [], properties: String = "{}", pinned: Bool = false, archived: Bool = false, icon: String? = nil,41            position: Double = 0, updatedAt: Date = epoch, deletedAt: Date? = nil, rev: Int64 = 1) -> RemotePage {42    RemotePage(id: id, parentId: parent, section: section, title: title, icon: icon, content: doc(text), properties: Data(properties.utf8),43               position: position, pinned: pinned, archived: archived, version: version, wordCount: words(text), createdAt: epoch,44               updatedAt: updatedAt, deletedAt: deletedAt, rev: rev, tags: tags, plainText: text, preview: String(text.prefix(80)))45}4647extension NotesStore {48    /// Edits the body of a page, deriving plain text, word count and preview from the text.49    @discardableResult50    func edit(_ id: String, title: String? = nil, text: String) throws -> Bool {51        try updateContent(id: id, title: title, content: doc(text), plainText: text, wordCount: words(text), preview: String(text.prefix(80)))52    }5354    func must(_ id: String) throws -> PageRecord {55        guard let page = try page(id: id) else { throw StoreError.notFound(id) }56        return page57    }58}59
Files main
Menu

Theme