feat: add persistent Markdown notes
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import AppKit
|
||||
import ConjureKit
|
||||
import Testing
|
||||
|
||||
@Test @MainActor
|
||||
func notesRoundTripIndependentMarkdownUnicodeAndImageContents() throws {
|
||||
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
||||
defer { try? FileManager.default.removeItem(at: directory) }
|
||||
let store = try NotesStore(directory: directory)
|
||||
let firstID = try store.create()
|
||||
let secondID = try store.create()
|
||||
#expect(store.notes.count == 2)
|
||||
#expect(store.notes.allSatisfy { $0.title == "Untitled Note" })
|
||||
|
||||
let bitmap = try #require(NSBitmapImageRep(
|
||||
bitmapDataPlanes: nil, pixelsWide: 1, pixelsHigh: 1,
|
||||
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
|
||||
colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0
|
||||
))
|
||||
bitmap.setColor(.red, atX: 0, y: 0)
|
||||
let png = try #require(bitmap.representation(using: .png, properties: [:]))
|
||||
let imageFile = FileWrapper(regularFileWithContents: png)
|
||||
imageFile.preferredFilename = "pixel.png"
|
||||
let attachment = NSTextAttachment(fileWrapper: imageFile)
|
||||
let first = NSMutableAttributedString(string: "\n # 日本語 🦊 \n**bold** and `code`\n")
|
||||
first.append(NSAttributedString(attachment: attachment))
|
||||
let expectedFirstText = first.string
|
||||
try store.update(id: firstID, contents: first)
|
||||
first.mutableString.setString("Mutated caller buffer")
|
||||
#expect(store.notes.first { $0.id == firstID }?.contents.string == expectedFirstText)
|
||||
#expect(store.notes.first { $0.id == firstID }?.title == "# 日本語 🦊")
|
||||
|
||||
try store.update(id: secondID, contents: NSAttributedString(string: "Second note"))
|
||||
try store.update(id: secondID, contents: NSAttributedString(string: "## Edited\n- café\n- Ελληνικά"))
|
||||
#expect(store.notes.first { $0.id == firstID }?.contents.string == expectedFirstText)
|
||||
|
||||
let reloaded = try NotesStore(directory: directory)
|
||||
#expect(Set(reloaded.notes.map(\.id)) == Set([firstID, secondID]))
|
||||
let restoredFirst = try #require(reloaded.notes.first { $0.id == firstID })
|
||||
#expect(restoredFirst.contents.string == expectedFirstText)
|
||||
#expect(restoredFirst.title == "# 日本語 🦊")
|
||||
let restoredAttachment = try #require(restoredFirst.contents.attribute(
|
||||
.attachment, at: restoredFirst.contents.length - 1, effectiveRange: nil
|
||||
) as? NSTextAttachment)
|
||||
let restoredPNG = try #require(restoredAttachment.fileWrapper?.regularFileContents)
|
||||
#expect(restoredPNG == png)
|
||||
#expect(NSImage(data: restoredPNG) != nil)
|
||||
#expect(reloaded.notes.first { $0.id == secondID }?.contents.string == "## Edited\n- café\n- Ελληνικά")
|
||||
}
|
||||
|
||||
@Test @MainActor
|
||||
func notesWriteFailuresPreserveSavedMemoryAndDiskContents() throws {
|
||||
let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
||||
defer { try? FileManager.default.removeItem(at: root) }
|
||||
let directory = root.appendingPathComponent("Notes")
|
||||
let backup = root.appendingPathComponent("SavedNotes")
|
||||
let store = try NotesStore(directory: directory)
|
||||
let id = try store.create()
|
||||
try store.update(id: id, contents: NSAttributedString(string: "Saved contents"))
|
||||
|
||||
// A regular file in place of the parent directory fails even when tests run as root.
|
||||
try FileManager.default.moveItem(at: directory, to: backup)
|
||||
try Data("not a directory".utf8).write(to: directory)
|
||||
#expect(throws: (any Error).self) {
|
||||
try store.update(id: id, contents: NSAttributedString(string: "Unsaved changes"))
|
||||
}
|
||||
#expect(throws: (any Error).self) { try store.create() }
|
||||
#expect(store.notes.count == 1)
|
||||
#expect(store.notes.first?.contents.string == "Saved contents")
|
||||
#expect(store.notes.first?.title == "Saved contents")
|
||||
|
||||
try FileManager.default.removeItem(at: directory)
|
||||
try FileManager.default.moveItem(at: backup, to: directory)
|
||||
let reloaded = try NotesStore(directory: directory)
|
||||
#expect(reloaded.notes.first?.id == id)
|
||||
#expect(reloaded.notes.first?.contents.string == "Saved contents")
|
||||
}
|
||||
|
||||
@Test @MainActor
|
||||
func corruptPersistedNotesRaiseErrorsWithoutReplacement() throws {
|
||||
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
||||
defer { try? FileManager.default.removeItem(at: directory) }
|
||||
let package = directory.appendingPathComponent(UUID().uuidString + ".rtfd")
|
||||
try FileManager.default.createDirectory(at: package, withIntermediateDirectories: true)
|
||||
let corruptRTF = package.appendingPathComponent("TXT.rtf")
|
||||
let bytes = Data("This is not an RTF document".utf8)
|
||||
try bytes.write(to: corruptRTF)
|
||||
|
||||
#expect(throws: (any Error).self) { try NotesStore(directory: directory) }
|
||||
#expect(try Data(contentsOf: corruptRTF) == bytes)
|
||||
}
|
||||
|
||||
@Test @MainActor
|
||||
func noteTitlesUseFirstNonemptyLineAndCapAtSixtyCharacters() throws {
|
||||
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
||||
defer { try? FileManager.default.removeItem(at: directory) }
|
||||
let store = try NotesStore(directory: directory)
|
||||
let id = try store.create()
|
||||
try store.update(id: id, contents: NSAttributedString(string: "\n \t\n " + String(repeating: "🦊", count: 70) + " \nIgnored"))
|
||||
#expect(store.notes.first?.title == String(repeating: "🦊", count: 60))
|
||||
try store.update(id: id, contents: NSAttributedString(string: " \t\n\n"))
|
||||
#expect(store.notes.first?.title == "Untitled Note")
|
||||
}
|
||||
Reference in New Issue
Block a user