Files
helder/Tests/HelderUITests/Overlays/NoteStoreTests.swift
T

100 lines
3.1 KiB
Swift

import HelderUI
import Testing
@MainActor
@Suite struct NoteStoreTests {
@Test func loadsTheNoteThatIsAlreadyOnDisk() async {
let store = NoteStore(notes: FakeNotes("earlier thoughts\n"))
await store.load()
#expect(store.text == "earlier thoughts\n")
#expect(!store.isDirty)
}
@Test func nothingIsWrittenWhileTyping() async {
let notes = FakeNotes()
let store = NoteStore(notes: notes)
await store.load()
store.text = "buy milk"
#expect(notes.writes.isEmpty)
#expect(store.isDirty)
}
@Test func savingOnFocusLossWritesTheEditedText() async {
let notes = FakeNotes()
let store = NoteStore(notes: notes)
await store.load()
store.text = "buy milk"
await store.save()
#expect(notes.writes == ["buy milk"])
#expect(notes.onDisk == "buy milk")
#expect(!store.isDirty)
}
@Test func anUnchangedNoteIsNeverRewritten() async {
let notes = FakeNotes("unchanged")
let store = NoteStore(notes: notes)
await store.load()
await store.save()
await store.save()
#expect(notes.writes.isEmpty)
}
@Test func aFailedWriteLeavesTheNoteDirtyForTheNextTry() async {
let notes = FakeNotes()
notes.failWrites = true
let store = NoteStore(notes: notes)
store.text = "keep me"
await store.save()
#expect(store.isDirty)
}
@Test func passableTextIsTheTrimmedNote() {
let store = NoteStore(notes: FakeNotes())
store.text = " refactor the policy\n\n"
#expect(store.passableText == "refactor the policy")
}
@Test func anEmptyNoteHasNothingToPass() {
let store = NoteStore(notes: FakeNotes())
store.text = " \n "
#expect(store.passableText == nil)
}
@Test func aFailedWriteRaisesAToast() async {
let notes = FakeNotes()
notes.failWrites = true
let store = NoteStore(notes: notes)
var toasts: [String?] = []
store.notify = { title, detail in toasts.append(title + " " + (detail ?? "")) }
store.text = "keep me"
#expect(await store.save() == false)
#expect(toasts == ["Save failed .notes.txt"])
}
@Test func anOutsideEditReplacesACleanNote() async {
let notes = FakeNotes("old")
let store = NoteStore(notes: notes)
await store.load()
notes.setOnDisk("new from the agent")
await store.reloadIfClean()
#expect(store.text == "new from the agent")
#expect(!store.isDirty)
}
@Test func anOutsideEditNeverReplacesUnsavedTyping() async {
let notes = FakeNotes("old")
let store = NoteStore(notes: notes)
await store.load()
store.text = "typing"
notes.setOnDisk("new from the agent")
await store.reloadIfClean()
#expect(store.text == "typing")
}
@Test func theFooterCountsCharacters() {
let store = NoteStore(notes: FakeNotes())
store.text = "héllo"
#expect(store.characterCount == 5)
}
}