refactor(posts): route every library write through the write rail (#179)
CI / ci (push) Canceled after 0s

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
This commit is contained in:
AlexKucera committed 2026-11-10 03:05:48 +01:00
1 parent 8c5ceffa3c
commit 06283b6287
23 files changed
+364 -363

No files matched your search

+19 -19
View File
@@ -51,7 +51,7 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(fix: "The replaced sentence.", quote: "The anchor sentence.")],
)
model.select(post.id, home: home)
model.select(post.id)
let session = try #require(model.session)
let generation = session.loadGeneration
@@ -59,7 +59,7 @@ struct FixCardApplyTests {
// buffer first (flush-first, like every op that reads the piece).
session.edit("# Carded\n\nThe anchor sentence.\nPlus a typed line.\n")
#expect(model.applyCard(written[0].card, home: home) == .applied)
#expect(model.applyCard(written[0].card) == .applied)
// Disk carries the replacement over the flushed buffer...
#expect(PostStore.readBody(id: post.id, home: home) == "# Carded\n\nThe replaced sentence.\nPlus a typed line.\n")
@@ -80,14 +80,14 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(fix: "The replaced sentence.", quote: "The anchor sentence.")],
)
model.select(post.id, home: home)
model.select(post.id)
// The Post changed after the card was issued — the quote is gone.
model.session?.edit("# Carded\n\nThe sentence changed.\n")
model.session?.flush()
let postURL = folder.appendingPathComponent("post.md")
let before = try Data(contentsOf: postURL)
#expect(model.applyCard(written[0].card, home: home) == .stale)
#expect(model.applyCard(written[0].card) == .stale)
#expect(try Data(contentsOf: postURL) == before) // nothing selected, nothing changed
let diskCard = try #require(FixCardStore.scan(pieceURL: folder).first)
@@ -104,11 +104,11 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(pattern: "Demo", fix: "The replaced sentence.", quote: "The anchor sentence.", lane: .demonstration)],
)
model.select(post.id, home: home)
model.select(post.id)
let postURL = folder.appendingPathComponent("post.md")
let before = try Data(contentsOf: postURL)
let outcome = model.applyCard(written[0].card, home: home)
let outcome = model.applyCard(written[0].card)
guard case let .refused(why) = outcome else {
Issue.record("expected refusal, got \(outcome)")
return
@@ -127,7 +127,7 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(fix: "The replaced sentence.", quote: "The anchor sentence.")],
)
model.select(post.id, home: home)
model.select(post.id)
// A foreign write lands on disk underneath the writer (#67) — the
// reload question is up and unanswered.
let postURL = folder.appendingPathComponent("post.md")
@@ -135,7 +135,7 @@ struct FixCardApplyTests {
model.session?.diskDidChange(diskText: "foreign text on disk")
let before = try Data(contentsOf: postURL)
let outcome = model.applyCard(written[0].card, home: home)
let outcome = model.applyCard(written[0].card)
guard case let .refused(why) = outcome else {
Issue.record("expected refusal, got \(outcome)")
return
@@ -157,15 +157,15 @@ struct FixCardApplyTests {
card(pattern: "Telling", quote: "The anchor sentence."),
],
)
model.select(post.id, home: home)
model.select(post.id)
let postURL = folder.appendingPathComponent("post.md")
let before = try Data(contentsOf: postURL)
model.skipCard(written[0].card, home: home)
model.skipCard(written[0].card)
try await pollUntil { model.openPieceCards.first(where: { $0.id == written[0].card.id })?.status == .skipped }
#expect(try FixCardStore.scan(pieceURL: folder).first(where: { $0.id == written[0].card.id })?.status == .skipped)
model.doneCard(written[1].card, home: home)
model.doneCard(written[1].card)
try await pollUntil { model.openPieceCards.first(where: { $0.id == written[1].card.id })?.status == .done }
#expect(try FixCardStore.scan(pieceURL: folder).first(where: { $0.id == written[1].card.id })?.status == .done)
@@ -241,7 +241,7 @@ struct FixCardApplyTests {
piecePath: folder.path,
cards: [card(fix: "The replaced sentence.", quote: "The anchor sentence.")],
)
model.select(post.id, home: home)
model.select(post.id)
model.session?.edit("# Carded\n\nThe sentence changed.\n") // stale the anchor
model.session?.flush()
let postURL = folder.appendingPathComponent("post.md")
@@ -252,7 +252,7 @@ struct FixCardApplyTests {
try FileManager.default.setAttributes([.posixPermissions: 0o000], ofItemAtPath: cardsDir.path)
defer { try? FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: cardsDir.path) }
#expect(model.applyCard(written[0].card, home: home) == .stale) // the primary contract holds…
#expect(model.applyCard(written[0].card) == .stale) // the primary contract holds…
#expect(model.lastError?.contains("card close failed") == true) // …and the flag failure is loud
#expect(try Data(contentsOf: postURL) == before) // still nothing selected or changed
}
@@ -270,12 +270,12 @@ struct FixCardApplyTests {
)
try FileManager.default.removeItem(at: folder.appendingPathComponent("post.md")) // the read will fail
model.select(post.id, home: home)
model.select(post.id)
#expect(model.session?.loadFailed == true)
// The writer typed the anchor into the empty failed-session buffer.
model.session?.edit("The anchor sentence. typed nowhere durable.")
let outcome = model.applyCard(written[0].card, home: home)
let outcome = model.applyCard(written[0].card)
guard case .refused = outcome else {
Issue.record("expected refusal, got \(outcome)")
return
@@ -295,9 +295,9 @@ struct FixCardApplyTests {
let post = try PostStore.writeNew(body: "# Carded\n\nThe anchor sentence.\n", home: home)
let folder = try #require(PostStore.folder(of: post.id, home: home))
let written = try FixCardStore.submit(piecePath: folder.path, cards: [card()])
model.select(post.id, home: home)
model.select(post.id)
#expect(model.promoteCard(written[0].card, home: home) == .promoted)
#expect(model.promoteCard(written[0].card) == .promoted)
let learnings = home.appendingPathComponent("learnings/posts", isDirectory: true)
#expect(try FileManager.default.contentsOfDirectory(atPath: learnings.path).filter { $0.hasSuffix(".md") }.count == 1)
@@ -306,7 +306,7 @@ struct FixCardApplyTests {
// Already promoted → refused, no second file (one confirm = one entry).
let scanned = try #require(FixCardStore.scan(pieceURL: folder).first)
#expect(model.promoteCard(scanned, home: home) == .refused("card already promoted"))
#expect(model.promoteCard(scanned) == .refused("card already promoted"))
#expect(try FileManager.default.contentsOfDirectory(atPath: learnings.path).filter { $0.hasSuffix(".md") }.count == 1)
}
@@ -316,7 +316,7 @@ struct FixCardApplyTests {
let folder = try #require(PostStore.folder(of: post.id, home: home))
let written = try FixCardStore.submit(piecePath: folder.path, cards: [card()])
#expect(model.promoteCard(written[0].card, home: home) == .refused("open the piece to work its cards"))
#expect(model.promoteCard(written[0].card) == .refused("open the piece to work its cards"))
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("learnings").path))
#expect(model.lastError == nil)
}