shutdown() flushed both sessions best-effort and ignored the results — quitting with an unanswered #67 conflict wrote local text over the foreign disk body (flush() holds for conflicts only in the debounce), and a failed quit-time flush silently dropped the only copy of the edits. Quit now routes through the same departure gate as navigation: documentsMayDepart runs both sessions through sessionMayDepart, and the failed-flush-at-quit posture is BLOCK QUIT — applicationShouldTerminate (quitReply(posts:), before the Cold Turkey ask) refuses termination with one spoken reason via a hosted-guarded alert. PostsModel.active (weak, registered by AppGateState.init — the WritingLockState.active precedent) is the delegate's reachability. Finish-audit fixes folded in: sessionMayDepart refuses a never-loaded session's retyped words (its flush is a vacuous success — the same gap closed for navigation); shutdown stays void (no production reader of a Bool). +5 pins (conflict hold, clean flush, guest flush, never-loaded refusal, delegate refusal incl. chmod-injected flush failure), all flip-verified. Rail 708/708; format+lint clean. Closes #200
265 lines
11 KiB
Swift
265 lines
11 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.
|
|
//
|
|
// #200 (gate quit-time flushes through the departure gate): quitting is
|
|
// the one departure that used to bypass the gate — shutdown() flushed
|
|
// both sessions best-effort. Pins: a quit-time shutdown with an
|
|
// unanswered #67 conflict leaves the foreign disk body untouched (the
|
|
// #114 harm); the open external guest's edits ride the shutdown flush;
|
|
// a never-loaded session with retyped words refuses (its flush would
|
|
// report a vacuous success and drop the only copy — finish-audit LOW).
|
|
// The block-quit posture itself (applicationShouldTerminate's refusal)
|
|
// is pinned in WritingLockTests at the delegate seam.
|
|
//
|
|
|
|
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")
|
|
}
|
|
|
|
@Test func `quit-time shutdown leaves the foreign disk body untouched 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, for real this time: the foreign body is ON disk (quit's
|
|
// flush() never held for a conflict — only the debounce did, so
|
|
// shutdown used to write the local text over it, #114's harm).
|
|
try PostStore.overwrite(id: post.id, body: "foreign body from disk", home: home)
|
|
model.session?.diskDidChange(diskText: "foreign body from disk")
|
|
|
|
model.shutdown()
|
|
|
|
// AC #200: the foreign disk body survives the quit-time flush and
|
|
// the strip says why instead of pretending it saved.
|
|
#expect(PostStore.readBody(id: post.id, home: home) == "foreign body from disk")
|
|
#expect(model.lastError != nil)
|
|
}
|
|
|
|
@Test func `quit-time shutdown flushes a clean dirty session`() 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\nlast words before quitting")
|
|
|
|
model.shutdown()
|
|
|
|
// No conflict, no failure: the edits land and quit proceeds clean.
|
|
#expect(PostStore.readBody(id: post.id, home: home) == "# A\n\nlast words before quitting")
|
|
#expect(model.lastError == nil)
|
|
}
|
|
|
|
@Test func `quit-time shutdown flushes the open external guest`() throws {
|
|
let (model, _) = try makeModelHome()
|
|
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 words that must land before quitting")
|
|
|
|
model.shutdown()
|
|
|
|
// The guest leg of the quit gate: its edits ride the shutdown
|
|
// flush, exactly as the old best-effort shutdown did.
|
|
#expect(try String(contentsOf: extFile, encoding: .utf8) == "# Foreign\n\nguest words that must land before quitting")
|
|
#expect(model.lastError == nil)
|
|
}
|
|
|
|
@Test func `quit-time shutdown refuses over a never-loaded session with retyped text`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# A\n", home: home)
|
|
// The file turns unreadable between scan and open: the session
|
|
// opens empty (loadFailed), the writer retypes into it.
|
|
let file = try #require(PostStore.folder(of: post.id, home: home))
|
|
.appendingPathComponent("post.md")
|
|
try FileManager.default.setAttributes([.posixPermissions: 0o000], ofItemAtPath: file.path)
|
|
defer { try? FileManager.default.setAttributes([.posixPermissions: 0o644], ofItemAtPath: file.path) }
|
|
model.select(post.id)
|
|
#expect(model.session?.loadFailed == true)
|
|
model.session?.edit("# Retyped words over a file that never loaded")
|
|
|
|
model.shutdown()
|
|
|
|
// flush() reports a vacuous success for a never-loaded session —
|
|
// the gate must refuse instead of letting the retype die quietly.
|
|
#expect(model.lastError != nil)
|
|
#expect(model.session?.text == "# Retyped words over a file that never loaded")
|
|
}
|
|
}
|