Files
museion/MuseionTests/PostsModelWriteRailTests.swift
T
AlexKucera 06283b6287
CI / ci (push) Canceled after 0s
refactor(posts): route every library write through the write rail (#179)
One internal seam (PostsModelRail.swift) now pairs every library
verb store write with exactly one library rescan and owns the error
strip: saveDraft, newPostFromSession, rename, trash, createProject,
moveToProject, archivePost, restorePost, unarchiveProject,
importFiles, convertToPost, and runPass pass-state advance (12 sites
on this base; the pass snapshot/stats writes, the publish stamp, and
the editor flush stay outside by design).

The model owns the Home: boundHome is set by bind() alone (app
construction + Settings repoint); every public verb lost its home:
parameter (create post/outline/session-born, rename, move, archive,
restore, trash, convert, project creation, import, select, cards,
passes, blind read, publish).

The three rail entries are railWrite/railWriteReturningReason/
railWriteKeepingStrip — deliberately NOT overloads: a shared base
name let unlabeled trailing-closure calls resolve into the labeled
non-generic entry (Swift binds trailing closures to labeled first
parameters), silently bypassing the strip and the pairing.

Views + 11 test files swept to the homeless signatures (160 sites).
RED suite green: 3/3. Full unit suite: 654/654. Fixes #179
2026-11-10 03:05:48 +01:00

132 lines
4.6 KiB
Swift

//
// 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()
rail("saveDraft")
// session-born post
_ = model.newPostFromSession(body: "# Rail Two\n")
rail("newPostFromSession")
// project creation (reason returns to the sheet; still railed)
#expect(model.createProject(named: "essays") == nil)
rail("createProject")
// move to project
let movable = try PostStore.writeNew(body: "# Movables\n", home: home)
model.moveToProject(movable, project: "essays")
rail("moveToProject")
// rename
let toRename = try PostStore.writeNew(body: "# Rename Me\n", home: home)
model.rename(toRename, to: "Renamed Title")
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)
rail("archivePost")
model.restorePost(toArchive)
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)
rail("archivePost (project member)")
model.unarchiveProject("essays")
rail("unarchiveProject")
// convert
let outline = try PostStore.writeNew(body: "- one\n- two\n", type: .outline, home: home)
model.stageConversion(outline) // staging asks, never writes
model.convertToPost(headingsLadder: false)
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)
rail("importFiles")
// trash
let toTrash = try PostStore.writeNew(body: "# Trash Me\n", home: home)
model.trash(toTrash)
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)
// 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, mentor: mentor)
#expect(events == [.write, .pairedRescan])
#expect(model.lastError == nil)
}
/// #179: the failure pairing — a write that throws rides the rail
/// once (`.write` + `.writeFailed`), 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) // first: rides + pairs
#expect(events == [.write, .pairedRescan])
events.removeAll()
model.trash(post) // folder already gone → the write throws
#expect(events == [.write, .writeFailed])
#expect(model.lastError != nil)
}
}