fix(editor): honest save indicator — Changed while dirty (#83)
The dot claimed "Saved" for open posts even with unflushed keystrokes; only draft mode told the truth. SaveState now mirrors cleanliness: Changed while text differs from the last flush, Saved when clean. The Saving… state is deleted — the synchronous write never lasts a render pass. Save failed stays sticky until a write actually succeeds, and Keep Local no longer adopts local text as flushed when the post vanished mid-alert (that path silently killed ⌘S with "Saved" showing). Closes #83 [Generated by AI]
This commit is contained in:
1 parent
c1f5d707f8
commit
fc0dde4f7c
5 files changed
+79
-12
No files matched your search
@@ -37,10 +37,10 @@ struct EditorSessionTests {
|
||||
|
||||
#expect(session.saveState == .saved)
|
||||
session.edit("# Alpha\n\nOne. Two.")
|
||||
#expect(session.saveState == .saved) // still inside the debounce window
|
||||
#expect(session.saveState == .changed) // #83: dirty shows while the debounce holds
|
||||
|
||||
try await Task.sleep(for: .milliseconds(200))
|
||||
#expect(session.saveState == .saved) // saved → Saving… → Saved lands here
|
||||
#expect(session.saveState == .saved) // Changed → flush → Saved lands here
|
||||
#expect(PostStore.readBody(id: "alpha", home: home) == "# Alpha\n\nOne. Two.")
|
||||
|
||||
// Sidecar: written on flush, reads back.
|
||||
@@ -101,6 +101,46 @@ struct EditorSessionTests {
|
||||
#expect(PostStore.readBody(id: "gamma", home: home) == "# Gamma\n\nLocal wins.")
|
||||
}
|
||||
|
||||
@Test func `dirty edits show Changed until flushed; reverting to flushed text clears it`() {
|
||||
let home = makeSessionHome()
|
||||
seedPost(home, id: "eta", body: "# Eta\n\nBase.")
|
||||
let session = EditorSession(postID: "eta", home: home)
|
||||
|
||||
// Dirty while the debounce holds (#83) — never a lying "Saved".
|
||||
session.edit("# Eta\n\nDirty.")
|
||||
#expect(session.saveState == .changed)
|
||||
|
||||
// Flush lands → Saved.
|
||||
session.flush()
|
||||
#expect(session.saveState == .saved)
|
||||
|
||||
// Editing back to the flushed text = clean again (undo counts);
|
||||
// anything else still differs from disk → stays Changed.
|
||||
session.edit("# Eta\n\nBase.")
|
||||
#expect(session.saveState == .changed)
|
||||
session.edit("# Eta\n\nDirty.") // back to the flushed content
|
||||
#expect(session.saveState == .saved)
|
||||
|
||||
// Keep Local after a conflict: local ≠ disk → dirty.
|
||||
session.edit("# Eta\n\nLocal mine.")
|
||||
session.flush()
|
||||
try? PostStore.overwrite(id: "eta", body: "# Eta\n\nExternal.", home: home)
|
||||
session.diskDidChange(diskText: "# Eta\n\nExternal.")
|
||||
session.keepLocal()
|
||||
#expect(session.saveState == .changed)
|
||||
|
||||
// #83: Keep Local with the post vanished mid-alert — the session must
|
||||
// stay dirty (never a lying "Saved"), ⌘S still attempts the write.
|
||||
try? FileManager.default.removeItem(
|
||||
at: home.appendingPathComponent("posts/eta", isDirectory: true),
|
||||
)
|
||||
session.diskDidChange(diskText: "# Eta\n\nExternal 2.") // alert raised; disk now unreadable
|
||||
session.keepLocal()
|
||||
#expect(session.saveState == .changed)
|
||||
session.flush() // still dirty → write attempted → folder gone → failed
|
||||
#expect(session.saveState == .failed)
|
||||
}
|
||||
|
||||
@Test func `write failure flips saveState to failed and retries on next flush`() throws {
|
||||
let home = makeSessionHome()
|
||||
seedPost(home, id: "delta", body: "# Delta\n\nBody.")
|
||||
@@ -120,6 +160,16 @@ struct EditorSessionTests {
|
||||
// Next flush retries (still failed until the post returns).
|
||||
session.flush()
|
||||
#expect(session.saveState == .failed)
|
||||
|
||||
// #83: failed is sticky — reverting to the flushed text never shows
|
||||
// "Saved" over a file whose last write threw; only a write clears it.
|
||||
session.edit("# Delta\n\nBody.") // back to on-disk content
|
||||
session.flush()
|
||||
#expect(session.saveState == .failed)
|
||||
|
||||
// …and sticky across further edits: still failed, never "Changed".
|
||||
session.edit("# Delta\n\nDifferent.")
|
||||
#expect(session.saveState == .failed)
|
||||
}
|
||||
|
||||
@Test func `debounce does not clobber while the conflict alert is unanswered`() async throws {
|
||||
@@ -135,7 +185,7 @@ struct EditorSessionTests {
|
||||
|
||||
try await Task.sleep(for: .milliseconds(200)) // debounce window passes
|
||||
#expect(PostStore.readBody(id: "epsilon", home: home) == "# Epsilon\n\nExternal.") // external edit intact
|
||||
#expect(session.saveState == .saved)
|
||||
#expect(session.saveState == .changed) // #83: pending local edits, alert held the write
|
||||
|
||||
// Keep Local → next explicit flush overwrites (the writer's choice).
|
||||
session.keepLocal()
|
||||
|
||||
Reference in new issue
Block a user