feat: delete the selected note

This commit is contained in:
2026-10-19 19:43:25 +02:00
parent 0d9f7e0f00
commit 63d0387339
7 changed files with 156 additions and 8 deletions
@@ -101,3 +101,33 @@ func noteTitlesUseFirstNonemptyLineAndCapAtSixtyCharacters() throws {
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)
}