test(posts): pin library write-rail seam (#179)
CI / ci (push) Canceled after 0s

RED: PostsModelRail skeleton (one internal seam pairing a library write
with exactly one rescan + the error strip, three error routings) and the
seam-observability suite. On the base every verb hand-copies the
write-then-rescan-then-strip choreography, so nothing reaches the rail:
executed 3 / failed 3, events [] at every verb. Fixes #179
This commit is contained in:
AlexKucera committed 2026-11-10 02:41:05 +01:00
1 parent 627ecf515e
commit 8c5ceffa3c
3 files changed
+211

No files matched your search

+131
View File
@@ -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)
}
}