teardown(flushFirst:) discarded flush()'s result, so document
navigation (newPost, newOutline, select, openExternal, dismissExternal,
openUnguarded) dropped the outgoing dirty session after a failed write
and replaced the only retained copy of the unsaved prose.
One shared departure gate now covers every site, placed before any
document-specific state clears:
- sessionMayDepart(_:): flushOrAbort's two gates (unanswered #67
conflict holds; failed flush holds) lifted to document departure —
flushOrAbort routes through it unchanged.
- dismissExternal() -> Bool: the guest departs only when its flush
lands; on failure the guest, its buffer, and its watcher stay live.
- departOpenDocuments(): the full-switch composition (guest + post
session) behind newPost/newOutline/openExternal.
- select/openUnguarded gate their same-id-conditional session teardowns
through sessionMayDepart directly.
Departure now aborts with the strip error set and the dirty session
retained; retrying after writability returns lands the edits and
completes the switch. All six PostsModelDepartureTests pins RED at
11b196d, GREEN with this change.
brief-25 [failed-flush-discarded-on-document-switch]
183 lines
7.3 KiB
Swift
183 lines
7.3 KiB
Swift
//
|
|
// 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 postA = try PostStore.writeNew(body: "# A\n", home: home)
|
|
let postB = try PostStore.writeNew(body: "# B\n", home: home)
|
|
model.select(postA.id)
|
|
model.session?.edit("# A\n\noutgoing paragraph")
|
|
let dirA = try #require(PostStore.folder(of: postA.id, home: home))
|
|
try denyWrites(to: dirA)
|
|
defer { try? allowWrites(to: dirA) }
|
|
|
|
model.select(postB.id)
|
|
|
|
// The switch aborted before replacing the session (select's own
|
|
// teardown AND the second one through openUnguarded both gated).
|
|
#expect(model.session?.postID == postA.id)
|
|
#expect(model.session?.text == "# A\n\noutgoing paragraph")
|
|
#expect(model.selectedPostID == postA.id)
|
|
#expect(model.lastError != nil)
|
|
#expect(PostStore.readBody(id: postA.id, home: home) == "# A\n")
|
|
|
|
try allowWrites(to: dirA)
|
|
model.select(postB.id)
|
|
#expect(model.session?.postID == postB.id)
|
|
#expect(PostStore.readBody(id: postA.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")
|
|
}
|
|
}
|