museion/postsmodel-write-rail: one seam pairs every library write with the rescan (#179) #185
No files matched your search
@@ -171,6 +171,11 @@ final class PostsModel {
|
||||
/// Last failed-op message surfaced in the editor (nil = clean).
|
||||
/// #146: setter internal — the split-out domain extensions set it.
|
||||
var lastError: String?
|
||||
/// #179: rail observability — the write-rail suite drives every
|
||||
/// store-write verb and asserts one `.write` + one `.pairedRescan`
|
||||
/// each. Nil in production (no observer, no cost).
|
||||
@ObservationIgnored
|
||||
var onRailEvent: (@MainActor (PostsModelRailEvent) -> Void)?
|
||||
/// Home the watcher is bound to; also the rescan target.
|
||||
private var watchedHome: URL?
|
||||
private var watcher = PostsWatcher()
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
//
|
||||
// PostsModelRail.swift
|
||||
// Museion
|
||||
//
|
||||
// #179: the library write rail — ONE internal seam every library verb's
|
||||
// store write rides. The rail pairs the write with exactly ONE library
|
||||
// rescan and routes the failure; a verb that bypasses it is a silent
|
||||
// stale-library bug (the hand-copied write-then-rescan choreography this
|
||||
// file replaces). The editor's flush keeps its own posture outside the
|
||||
// rail (different error semantics, editor-owned — EditorSession), as do
|
||||
// the pass substrate's snapshot/stats writes (they never regroup the
|
||||
// library) and the publish stamp (wire-owned, deliberately quiet).
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// #179: rail observability — what the write-rail suite reads off the
|
||||
/// seam. `.write` = a verb's body rode the rail; `.pairedRescan` = the
|
||||
/// rail scheduled the verb's ONE rescan.
|
||||
enum PostsModelRailEvent: Equatable {
|
||||
case write
|
||||
case pairedRescan
|
||||
}
|
||||
|
||||
// MARK: Write rail (#179)
|
||||
|
||||
extension PostsModel {
|
||||
/// #179: the rail's default entry — strip-routed. The op runs the
|
||||
/// verb's store write FIRST, then its state updates (a throw leaves
|
||||
/// nothing half-updated); success clears the strip and pairs exactly
|
||||
/// one rescan, a throw routes the failure into the strip.
|
||||
@discardableResult
|
||||
func performLibraryWrite<T>(_ op: () throws -> T) -> T? {
|
||||
onRailEvent?(.write)
|
||||
do {
|
||||
let result = try op()
|
||||
lastError = nil
|
||||
pairRailRescan()
|
||||
return result
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// #179: the reason-returning entry (project creation) — the sheet
|
||||
/// owns its inline reason and the strip behind the sheet must not
|
||||
/// double-message one error. Same ONE rescan on success.
|
||||
func performLibraryWrite(returningReason op: () throws -> Void) -> String? {
|
||||
onRailEvent?(.write)
|
||||
do {
|
||||
try op()
|
||||
pairRailRescan()
|
||||
return nil
|
||||
} catch {
|
||||
return error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// #179: the strip-keeping entry (the import batch) — per-file
|
||||
/// failures continue inside the op and the strip keeps the verb's own
|
||||
/// message; the rail only pairs the ONE rescan.
|
||||
func performLibraryWrite(keepingStrip op: () -> Void) {
|
||||
onRailEvent?(.write)
|
||||
op()
|
||||
pairRailRescan()
|
||||
}
|
||||
|
||||
/// #179: the ONE pairing point — a write through any verb is followed
|
||||
/// by exactly one rescan, pinned here and nowhere else.
|
||||
private func pairRailRescan() {
|
||||
onRailEvent?(.pairedRescan)
|
||||
Task { await rescan() }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
//
|
||||
// PostsModelWriteRailTests.swift
|
||||
// MuseionTests
|
||||
//
|
||||
// #179: the write rail's seam observability — every store-write verb
|
||||
// rides the rail and pairs exactly ONE rescan (AC 2). RED on the
|
||||
// unrail base: the verbs hand-copy the write-then-rescan choreography,
|
||||
// so nothing reaches the rail's hook and every expectation below fails.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
@testable import Museion
|
||||
import Testing
|
||||
|
||||
@Suite(.serialized)
|
||||
@MainActor
|
||||
struct PostsModelWriteRailTests {
|
||||
/// #179 (AC 1+2): drive every library write verb; each records
|
||||
/// exactly one `.write` + one `.pairedRescan` — the pairing pinned
|
||||
/// once, at the rail.
|
||||
@Test func `every write verb rides the rail with exactly one paired rescan`() throws {
|
||||
let (model, home) = try makeModelHome()
|
||||
var events: [PostsModelRailEvent] = []
|
||||
model.onRailEvent = { events.append($0) }
|
||||
|
||||
func rail(_ verb: String) {
|
||||
#expect(events == [.write, .pairedRescan], "verb did not ride the rail exactly once with one paired rescan: \(verb)")
|
||||
events.removeAll()
|
||||
}
|
||||
|
||||
// create post — the draft's first save is the write
|
||||
model.newPost()
|
||||
model.updateDraft("# Rail One\n")
|
||||
model.saveDraft(home: home)
|
||||
rail("saveDraft")
|
||||
|
||||
// session-born post
|
||||
_ = model.newPostFromSession(body: "# Rail Two\n", home: home)
|
||||
rail("newPostFromSession")
|
||||
|
||||
// project creation (reason returns to the sheet; still railed)
|
||||
#expect(model.createProject(named: "essays", home: home) == nil)
|
||||
rail("createProject")
|
||||
|
||||
// move to project
|
||||
let movable = try PostStore.writeNew(body: "# Movables\n", home: home)
|
||||
model.moveToProject(movable, project: "essays", home: home)
|
||||
rail("moveToProject")
|
||||
|
||||
// rename
|
||||
let toRename = try PostStore.writeNew(body: "# Rename Me\n", home: home)
|
||||
model.rename(toRename, to: "Renamed Title", home: home)
|
||||
rail("rename")
|
||||
|
||||
// archive → restore (id survives both — one Post value drives them)
|
||||
let toArchive = try PostStore.writeNew(body: "# Archive Me\n", home: home)
|
||||
model.archivePost(toArchive, home: home)
|
||||
rail("archivePost")
|
||||
model.restorePost(toArchive, home: home)
|
||||
rail("restorePost")
|
||||
|
||||
// whole-project unarchive: an archived piece in "essays" restores
|
||||
// the project's other child (moved earlier) too
|
||||
let inProject = try PostStore.writeNew(body: "# In Project\n", project: "essays", home: home)
|
||||
model.archivePost(inProject, home: home)
|
||||
rail("archivePost (project member)")
|
||||
model.unarchiveProject("essays", home: home)
|
||||
rail("unarchiveProject")
|
||||
|
||||
// convert
|
||||
let outline = try PostStore.writeNew(body: "- one\n- two\n", type: .outline, home: home)
|
||||
model.stageConversion(outline, home: home) // staging asks, never writes
|
||||
model.convertToPost(headingsLadder: false, home: home)
|
||||
rail("convertToPost")
|
||||
|
||||
// import batch — ONE paired rescan for the whole batch
|
||||
let ext = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent("rail-import-\(UUID().uuidString).md")
|
||||
try "# Imported\n".write(to: ext, atomically: true, encoding: .utf8)
|
||||
model.stageImport([ext])
|
||||
model.importFiles(origin: .writer, home: home)
|
||||
rail("importFiles")
|
||||
|
||||
// trash
|
||||
let toTrash = try PostStore.writeNew(body: "# Trash Me\n", home: home)
|
||||
model.trash(toTrash, home: home)
|
||||
rail("trash")
|
||||
|
||||
#expect(model.lastError == nil)
|
||||
}
|
||||
|
||||
/// #179 (AC 2): the pass chain's library-visible write — Pass State
|
||||
/// advance — rides the rail too; the snapshot/stats writes never do
|
||||
/// (they don't regroup the library).
|
||||
@Test func `a completed pass rides the rail for its pass-state advance`() async throws {
|
||||
let (model, home, mentor) = try makePassRig()
|
||||
defer { mentor.shutdown() } // #124: the engine dies with the test
|
||||
await mentor.openSession()
|
||||
let post = try PostStore.writeNew(body: "# Pass Piece\n", home: home)
|
||||
model.select(post.id, home: home)
|
||||
// Wait for the bind rescan to surface the piece (PassRunTests poll).
|
||||
for _ in 0 ..< 250 where !model.posts.contains(where: { $0.id == post.id }) {
|
||||
try await Task.sleep(nanoseconds: 20_000_000)
|
||||
}
|
||||
|
||||
var events: [PostsModelRailEvent] = []
|
||||
model.onRailEvent = { events.append($0) }
|
||||
await model.runPass(.pass1, home: home, mentor: mentor)
|
||||
|
||||
#expect(events == [.write, .pairedRescan])
|
||||
#expect(model.lastError == nil)
|
||||
}
|
||||
|
||||
/// #179: the failure pairing — a write that throws rides the rail
|
||||
/// once, pairs NO rescan (nothing changed on disk), and the strip owns
|
||||
/// the reason.
|
||||
@Test func `a failed write rides the rail once and pairs no rescan`() throws {
|
||||
let (model, home) = try makeModelHome()
|
||||
var events: [PostsModelRailEvent] = []
|
||||
model.onRailEvent = { events.append($0) }
|
||||
let post = try PostStore.writeNew(body: "# Trashed Twice\n", home: home)
|
||||
|
||||
model.trash(post, home: home) // first: rides + pairs
|
||||
#expect(events == [.write, .pairedRescan])
|
||||
events.removeAll()
|
||||
|
||||
model.trash(post, home: home) // folder already gone → the write throws
|
||||
#expect(events == [.write])
|
||||
#expect(model.lastError != nil)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user