216 lines
13 KiB
Swift
216 lines
13 KiB
Swift
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(attributedString: NoteDocument.importMarkdown(NSAttributedString(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: NoteDocument.importMarkdown(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\ncafé\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")
|
|
}
|
|
|
|
@Test @MainActor
|
|
func deletingNotesPersistsWithoutChangingOtherNotesAndPreservesMemoryOnFailure() throws {
|
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
let backup = directory.appendingPathExtension("backup")
|
|
defer {
|
|
try? FileManager.default.removeItem(at: directory)
|
|
try? FileManager.default.removeItem(at: backup)
|
|
}
|
|
let store = try NotesStore(directory: directory)
|
|
let deleted = try store.create()
|
|
let kept = try store.create()
|
|
try store.update(id: kept, contents: NSAttributedString(string: "Keep this note"))
|
|
try store.delete(id: deleted)
|
|
#expect(store.notes.map(\.id) == [kept])
|
|
#expect(try NotesStore(directory: directory).notes.map(\.id) == [kept])
|
|
#expect(throws: (any Error).self) { try store.delete(id: deleted) }
|
|
|
|
try FileManager.default.moveItem(at: directory, to: backup)
|
|
try Data("Blocked storage".utf8).write(to: directory)
|
|
#expect(throws: (any Error).self) { try store.delete(id: kept) }
|
|
#expect(store.notes.map(\.id) == [kept])
|
|
#expect(store.notes.first?.contents.string == "Keep this note")
|
|
try FileManager.default.removeItem(at: directory)
|
|
try FileManager.default.moveItem(at: backup, to: directory)
|
|
#expect(try NotesStore(directory: directory).notes.first?.contents.string == "Keep this note")
|
|
try store.delete(id: kept)
|
|
#expect(store.notes.isEmpty)
|
|
#expect(try NotesStore(directory: directory).notes.isEmpty)
|
|
}
|
|
|
|
@Test @MainActor
|
|
func legacyNotesConvertWithoutWritingAndBackUpOriginalPackageOnlyOnFirstSave() throws {
|
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
defer { try? FileManager.default.removeItem(at: directory) }
|
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
let id = UUID()
|
|
let url = directory.appendingPathComponent(id.uuidString + ".rtfd")
|
|
let legacy = NSMutableAttributedString(string: "# Heading\n- [ ] task\n**bold** and `code`\n")
|
|
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(.blue, atX: 0, y: 0)
|
|
let png = try #require(bitmap.representation(using: .png, properties: [:]))
|
|
let image = FileWrapper(regularFileWithContents: png)
|
|
image.preferredFilename = "legacy.png"
|
|
legacy.append(NSAttributedString(attachment: NSTextAttachment(fileWrapper: image)))
|
|
let wrapper = try legacy.fileWrapper(from: NSRange(location: 0, length: legacy.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtfd])
|
|
try wrapper.write(to: url, options: .atomic, originalContentsURL: nil)
|
|
let originalRTF = try Data(contentsOf: url.appendingPathComponent("TXT.rtf"))
|
|
let store = try NotesStore(directory: directory)
|
|
let note = try #require(store.notes.first)
|
|
#expect(note.contents.string == "Heading\ntask\nbold and code\n\u{fffc}")
|
|
#expect(note.title == "Heading")
|
|
#expect(try Data(contentsOf: url.appendingPathComponent("TXT.rtf")) == originalRTF)
|
|
#expect(!FileManager.default.fileExists(atPath: url.appendingPathComponent("ConjureNote.json").path))
|
|
#expect(!FileManager.default.fileExists(atPath: directory.appendingPathComponent("LegacyBackups").path))
|
|
|
|
try store.update(id: id, contents: note.contents)
|
|
let backup = directory.appendingPathComponent("LegacyBackups").appendingPathComponent(id.uuidString + ".rtfd")
|
|
#expect(try Data(contentsOf: backup.appendingPathComponent("TXT.rtf")) == originalRTF)
|
|
#expect(try Data(contentsOf: backup.appendingPathComponent("legacy.png")) == png)
|
|
#expect(!FileManager.default.fileExists(atPath: backup.appendingPathComponent("ConjureNote.json").path))
|
|
#expect(FileManager.default.fileExists(atPath: url.appendingPathComponent("ConjureNote.json").path))
|
|
let reloaded = try NotesStore(directory: directory)
|
|
#expect(reloaded.notes.first?.contents.string == note.contents.string)
|
|
let restored = try #require(reloaded.notes.first?.contents)
|
|
let attachment = try #require(restored.attribute(.attachment, at: restored.length - 1, effectiveRange: nil) as? NSTextAttachment)
|
|
#expect(attachment.fileWrapper?.regularFileContents == png)
|
|
try reloaded.update(id: id, contents: NSAttributedString(string: "Changed"))
|
|
#expect(try Data(contentsOf: backup.appendingPathComponent("TXT.rtf")) == originalRTF)
|
|
#expect(try FileManager.default.contentsOfDirectory(atPath: directory.appendingPathComponent("LegacyBackups").path) == [id.uuidString + ".rtfd"])
|
|
}
|
|
|
|
@Test @MainActor
|
|
func failedLegacyBackupBlocksMigrationAndPreservesMemoryAndOriginalSource() throws {
|
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
defer { try? FileManager.default.removeItem(at: directory) }
|
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
let id = UUID()
|
|
let url = directory.appendingPathComponent(id.uuidString + ".rtfd")
|
|
let source = NSAttributedString(string: "## Original")
|
|
let wrapper = try source.fileWrapper(from: NSRange(location: 0, length: source.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtfd])
|
|
try wrapper.write(to: url, options: .atomic, originalContentsURL: nil)
|
|
let original = try Data(contentsOf: url.appendingPathComponent("TXT.rtf"))
|
|
let store = try NotesStore(directory: directory)
|
|
let blocker = directory.appendingPathComponent("LegacyBackups")
|
|
try Data("not a directory".utf8).write(to: blocker)
|
|
#expect(throws: (any Error).self) { try store.update(id: id, contents: NSAttributedString(string: "Unsaved")) }
|
|
#expect(store.notes.first?.contents.string == "Original")
|
|
#expect(try Data(contentsOf: url.appendingPathComponent("TXT.rtf")) == original)
|
|
#expect(!FileManager.default.fileExists(atPath: url.appendingPathComponent("ConjureNote.json").path))
|
|
try FileManager.default.removeItem(at: blocker)
|
|
try store.update(id: id, contents: NSAttributedString(string: "Saved"))
|
|
#expect(try NotesStore(directory: directory).notes.first?.contents.string == "Saved")
|
|
}
|
|
|
|
@Test @MainActor
|
|
func corruptStructuredMetadataIsNeverReinterpretedAsLegacyMarkdown() 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: "# literal"))
|
|
let package = directory.appendingPathComponent(id.uuidString + ".rtfd")
|
|
let metadata = package.appendingPathComponent("ConjureNote.json")
|
|
let corrupt = Data("invalid json".utf8)
|
|
try corrupt.write(to: metadata)
|
|
let originalRTF = try Data(contentsOf: package.appendingPathComponent("TXT.rtf"))
|
|
#expect(throws: NoteDocument.DocumentError.self) { try NotesStore(directory: directory) }
|
|
#expect(try Data(contentsOf: metadata) == corrupt)
|
|
#expect(try Data(contentsOf: package.appendingPathComponent("TXT.rtf")) == originalRTF)
|
|
#expect(!FileManager.default.fileExists(atPath: directory.appendingPathComponent("LegacyBackups").path))
|
|
}
|