test(editor): pin document departure aborting on failed flushes
CI / ci (push) Canceled after 0s

RED at pinned base 11b196d: all six PostsModelDepartureTests pins fail.
Document departure (newPost, newOutline, select, openExternal,
dismissExternal, openUnguarded) tears the outgoing session down with
teardown(flushFirst: true), discards the flush result, and replaces the
only retained copy of unsaved prose when the write fails. Both
navigation families (saved-post session, external guest) pinned with
injected write failures (read-only piece folder / guest directory), plus
the unanswered #67 conflict hold. Retry after writability is restored
proves the edits land and the switch then completes.

brief-25 [failed-flush-discarded-on-document-switch]
This commit is contained in:
AlexKucera committed 2026-11-16 20:14:32 +01:00
1 parent 11b196d68a
commit ecd14d1714
1 file changed
+182
+182
View File
@@ -0,0 +1,182 @@
//
// PostsModelDepartureTests.swift
// MuseionTests
//
// brief-25 (failed-flush-discarded-on-document-switch): document
// departure is guarded. A navigation whose outgoing flush fails (write
// error, or an unanswered #67 conflict) aborts and retains the dirty
// session — teardown used to discard the flush result and drop the only
// retained copy of unsaved prose. Both navigation families (saved-post
// session, external guest) pinned with injected write failures: a
// read-only piece folder / read-only guest directory (atomic writes
// stage their temp file beside the destination), the permission-
// injection convention of FixCardApplyTests.
//
import Foundation
@testable import Museion
import Testing
@Suite("PostsModelDepartureTests")
@MainActor
struct PostsModelDepartureTests {
private func denyWrites(to dir: URL) throws {
try FileManager.default.setAttributes([.posixPermissions: 0o555], ofItemAtPath: dir.path)
}
private func allowWrites(to dir: URL) throws {
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: dir.path)
}
@Test func `new post aborts when the dirty session flush fails and keeps its text`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Keeper\n", home: home)
model.select(post.id)
model.session?.edit("# Keeper\n\nunique failed-flush paragraph")
let pieceDir = try #require(PostStore.folder(of: post.id, home: home))
try denyWrites(to: pieceDir)
defer { try? allowWrites(to: pieceDir) }
model.newPost()
// The switch aborted: the dirty session is retained, not replaced
// by the draft — its text is the only copy of the edits.
#expect(model.session?.postID == post.id)
#expect(model.session?.text == "# Keeper\n\nunique failed-flush paragraph")
#expect(model.session?.saveState == .failed)
#expect(model.draft == nil)
#expect(model.selectedPostID == post.id)
#expect(model.lastError != nil)
// Writability restored → the retry lands the edits, then switches.
try allowWrites(to: pieceDir)
model.newPost()
#expect(model.draft != nil)
#expect(model.session == nil)
#expect(model.lastError == nil)
#expect(PostStore.readBody(id: post.id, home: home) == "# Keeper\n\nunique failed-flush paragraph")
}
@Test func `new outline aborts when the dirty session flush fails`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# A\n", home: home)
model.select(post.id)
model.session?.edit("# A\n\noutgoing paragraph")
let pieceDir = try #require(PostStore.folder(of: post.id, home: home))
try denyWrites(to: pieceDir)
defer { try? allowWrites(to: pieceDir) }
model.newOutline()
#expect(model.session?.postID == post.id)
#expect(model.draft == nil)
#expect(model.lastError != nil)
try allowWrites(to: pieceDir)
model.newOutline()
#expect(model.draft?.type == .outline)
#expect(PostStore.readBody(id: post.id, home: home) == "# A\n\noutgoing paragraph")
}
@Test func `switching pieces aborts when the outgoing session flush fails`() throws {
let (model, home) = try makeModelHome()
let a = try PostStore.writeNew(body: "# A\n", home: home)
let b = try PostStore.writeNew(body: "# B\n", home: home)
model.select(a.id)
model.session?.edit("# A\n\noutgoing paragraph")
let dirA = try #require(PostStore.folder(of: a.id, home: home))
try denyWrites(to: dirA)
defer { try? allowWrites(to: dirA) }
model.select(b.id)
// The switch aborted before replacing the session (select's own
// teardown AND the second one through openUnguarded both gated).
#expect(model.session?.postID == a.id)
#expect(model.session?.text == "# A\n\noutgoing paragraph")
#expect(model.selectedPostID == a.id)
#expect(model.lastError != nil)
#expect(PostStore.readBody(id: a.id, home: home) == "# A\n")
try allowWrites(to: dirA)
model.select(b.id)
#expect(model.session?.postID == b.id)
#expect(PostStore.readBody(id: a.id, home: home) == "# A\n\noutgoing paragraph")
}
@Test func `open external aborts when the dirty session flush fails`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Host\n", home: home)
model.select(post.id)
model.session?.edit("# Host\n\noutgoing paragraph")
let pieceDir = try #require(PostStore.folder(of: post.id, home: home))
try denyWrites(to: pieceDir)
defer { try? allowWrites(to: pieceDir) }
let extDir = FileManager.default.temporaryDirectory
.appendingPathComponent("museion-departure-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: extDir, withIntermediateDirectories: true)
let extFile = extDir.appendingPathComponent("notes.md")
try "# Foreign\n".write(to: extFile, atomically: true, encoding: .utf8)
model.openExternal(extFile)
// The guest did not take the column: the switch aborted with the
// dirty session retained.
#expect(model.session?.postID == post.id)
#expect(model.externalSession == nil)
#expect(model.lastError != nil)
try allowWrites(to: pieceDir)
model.openExternal(extFile)
#expect(model.externalSession != nil)
#expect(model.session == nil)
#expect(PostStore.readBody(id: post.id, home: home) == "# Host\n\noutgoing paragraph")
}
@Test func `select aborts when the dirty guest flush fails and keeps the guest`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Host\n", home: home)
let extDir = FileManager.default.temporaryDirectory
.appendingPathComponent("museion-departure-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: extDir, withIntermediateDirectories: true)
defer { try? allowWrites(to: extDir) }
let extFile = extDir.appendingPathComponent("notes.md")
try "# Foreign\n".write(to: extFile, atomically: true, encoding: .utf8)
model.openExternal(extFile)
model.externalSession?.edit("# Foreign\n\nguest paragraph that must survive")
try denyWrites(to: extDir)
model.select(post.id)
// The guest stays: its buffer is the only copy of the edits.
#expect(model.externalSession != nil)
#expect(model.externalSession?.text == "# Foreign\n\nguest paragraph that must survive")
#expect(model.session == nil) // the switch aborted before opening the post
#expect(model.selectedPostID == nil)
#expect(model.lastError != nil)
try allowWrites(to: extDir)
model.select(post.id)
#expect(model.externalSession == nil)
#expect(model.session?.postID == post.id)
#expect(try String(contentsOf: extFile, encoding: .utf8) == "# Foreign\n\nguest paragraph that must survive")
}
@Test func `new post aborts while the conflict alert is unanswered`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# A\n", home: home)
model.select(post.id)
model.session?.edit("# A\n\nlocal paragraph")
// #67: a foreign disk body arrived — the alert is up, unanswered.
model.session?.diskDidChange(diskText: "foreign body from disk")
model.newPost()
// Departure mirrors flushOrAbort: the unanswered conflict holds the
// navigation (and never overwrites the foreign disk body).
#expect(model.session?.postID == post.id)
#expect(model.draft == nil)
#expect(model.lastError != nil)
#expect(PostStore.readBody(id: post.id, home: home) == "# A\n")
}
}