// // 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) } }