feat(outline): convert to post — S4 (#114)
CI / ci (push) Canceled after 0s

In-place outline→post conversion on the piece: id, folder, Pass
State, origin kept, type frontmatter flips to post, body transforms
once. The ask picks the transform — Verbatim Bullets (default,
lossless) or Headings ladder (roots ##, stepping to the ###### floor,
deeper stays bullets), with a warning when the ladder would drop
paragraph lines between bullets.

The open session flushes first, then reopens routed to prose via
proseOverridePostID (lastDraftWasOutline's machinery reversed).
An outline that went lossy on disk behind an unwarned ask aborts
the convert; an unanswered #67 conflict aborts every flush-first
op (shared flushOrAbort seam); rescan no longer hands a same-id
reopened session its predecessor's stale disk text; a held #111
guard ask dies with the outline it pointed at.

Audit round (finish-audit, three reviewers) included: 13 new pins,
206 tests green, pin discrimination verified. Record:
docs/devlog/20261016-issue114-convert-to-post_log.md
This commit is contained in:
AlexKucera committed 2026-10-16 18:03:41 +02:00
1 parent 21e2f27450
commit c0513aae85
14 files changed
+547 -19

No files matched your search

+173
View File
@@ -295,3 +295,176 @@ private func writeMixedOutline(home: URL) throws -> Post {
#expect(model.outlineGuardPieceID == nil)
#expect(model.session?.postID == prose.id)
}
// #114 (S4): Convert to Post — the session re-route.
@Test @MainActor func `convert reopens the open piece routed to prose`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Chapter\n- one\n - nested", type: .outline, home: home)
model.select(post.id, home: home)
model.stageConversion(post, home: home)
#expect(model.pendingConversion?.id == post.id)
#expect(model.pendingConversion?.lossy == false) // clean outline
model.convertToPost(headingsLadder: true, home: home)
#expect(model.pendingConversion == nil)
#expect(model.session?.postID == post.id) // reopened on the converted body
#expect(model.proseOverridePostID == post.id) // prose over the lagging rescan
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8)
#expect(onDisk.contains("type: post"))
#expect(onDisk.contains("## one"))
}
@Test @MainActor func `convert of an unopened piece leaves the session alone`() throws {
let (model, home) = try makeModelHome()
let open = try PostStore.writeNew(body: "# Stay\n", home: home)
let outline = try PostStore.writeNew(body: "- a\n - b", type: .outline, home: home)
model.select(open.id, home: home)
model.stageConversion(outline, home: home)
model.convertToPost(headingsLadder: false, home: home)
#expect(model.session?.postID == open.id) // untouched
#expect(model.selectedPostID == open.id)
#expect(model.proseOverridePostID == nil) // no reopen, no override
#expect(try PostStore.readBody(id: outline.id, home: home) == "- a\n - b")
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/\(outline.id)/post.md"), encoding: .utf8)
#expect(onDisk.hasPrefix("---\ntype: post\n"))
}
@Test @MainActor func `verbatim convert flips the posts list after the rescan`() async throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Chapter\n- one\n - nested", type: .outline, home: home)
// bind rescan first so posts knows the piece — then the pin is the
// live-report contract: the sidebar type flips once the rescan lands
for _ in 0 ..< 250 where !model.posts.contains(where: { $0.id == post.id }) {
try await Task.sleep(nanoseconds: 20_000_000)
}
#expect(model.posts.first(where: { $0.id == post.id })?.type == .outline)
model.select(post.id, home: home)
model.stageConversion(post, home: home)
model.convertToPost(headingsLadder: false, home: home) // VERBATIM
#expect(model.proseOverridePostID == post.id)
#expect(model.session?.postID == post.id)
// poll for the rescan to land (the same Task { await rescan() } the app runs)
for _ in 0 ..< 250 where model.posts.first(where: { $0.id == post.id })?.type != .post {
try await Task.sleep(nanoseconds: 20_000_000)
}
#expect(model.posts.first(where: { $0.id == post.id })?.type == .post)
}
// #114 audit pins: the hardening round (finish-audit).
@Test @MainActor func `dirty open piece flushes before the convert`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Chapter\n- one", type: .outline, home: home)
model.select(post.id, home: home)
model.session?.edit("# Chapter\n- one\n - typed child") // unflushed edit
model.stageConversion(post, home: home)
model.convertToPost(headingsLadder: false, home: home)
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8)
#expect(onDisk.contains("type: post"))
#expect(onDisk.contains("- typed child")) // the flush landed BEFORE the transform
}
@Test @MainActor func `staging warns from the session text when the piece is dirty`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Clean\n- one\n- two", type: .outline, home: home)
model.select(post.id, home: home)
// prose typed since the last flush — disk still clean, session is not
model.session?.edit("# Clean\n- one\nA paragraph between bullets.\n- two")
model.stageConversion(post, home: home)
#expect(model.pendingConversion?.lossy == true) // from session.text, not stale disk
}
@Test @MainActor func `mixed body stages a lossy warning from disk`() throws {
let (model, home) = try makeModelHome()
let post = try writeMixedOutline(home: home) // closed piece — disk is the truth
model.stageConversion(post, home: home)
#expect(model.pendingConversion?.lossy == true)
}
@Test @MainActor func `headings aborts when the body went lossy behind the ask`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Clean\n- one\n- two", type: .outline, home: home)
model.stageConversion(post, home: home) // staged clean, no warning shown
// a foreign write makes the body mixed while the sheet was up
let file = home.appendingPathComponent("projects/posts/\(post.id)/post.md", isDirectory: false)
try PostStore.stamped(body: "# Clean\n- one\nA paragraph between bullets.\n- two", passState: .firstDraft, origin: .writer, type: .outline)
.write(to: file, atomically: true, encoding: .utf8)
model.convertToPost(headingsLadder: true, home: home)
#expect(model.lastError?.contains("nothing was converted") == true)
#expect(model.session?.postID == nil || model.session?.postID != post.id)
let onDisk = try String(contentsOf: file, encoding: .utf8)
#expect(onDisk.contains("type: outline")) // untouched — no unconsented drop
#expect(onDisk.contains("A paragraph between bullets."))
// the consented path still converts: a WARNED mixed piece + Headings proceeds
model.stageConversion(post, home: home) // now stages lossy from disk
model.convertToPost(headingsLadder: true, home: home)
let converted = try String(contentsOf: file, encoding: .utf8)
#expect(converted.contains("type: post"))
#expect(!converted.contains("A paragraph between bullets.")) // dropped, as consented
}
@Test @MainActor func `unanswered conflict aborts the convert`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Clean\n- one", type: .outline, home: home)
model.select(post.id, home: home)
// #67 conflict up: disk holds foreign text, the editor's version unwritten
model.session?.diskDidChange(diskText: "# Foreign rewrite\nbody")
#expect(model.session?.conflictAlert == true)
model.stageConversion(post, home: home)
model.convertToPost(headingsLadder: false, home: home)
#expect(model.lastError?.contains("reload question") == true)
#expect(model.session?.conflictAlert == true) // still answerable, not trampled
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8)
#expect(onDisk.contains("type: outline")) // nothing converted underneath the alert
}
@Test @MainActor func `failed flush aborts the convert`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# Chapter\n- one", type: .outline, home: home)
model.select(post.id, home: home)
model.session?.edit("# Chapter\n- one\n - child") // dirty
// vanish the piece: the flush throws (never resurrects), convert must abort
try FileManager.default.removeItem(at: home.appendingPathComponent("projects/posts/\(post.id)", isDirectory: true))
model.stageConversion(post, home: home)
model.convertToPost(headingsLadder: true, home: home)
#expect(model.lastError?.contains("nothing was changed") == true)
#expect(model.session?.postID == post.id) // session stays up, dirty and honest
}
@Test @MainActor func `convert dismisses a held guard ask on the same piece`() throws {
let (model, home) = try makeModelHome()
let post = try writeMixedOutline(home: home)
model.select(post.id, home: home) // guard fires, open held
#expect(model.outlineGuardPieceID == post.id)
model.stageConversion(post, home: home)
model.convertToPost(headingsLadder: false, home: home)
#expect(model.outlineGuardPieceID == nil) // the stale ask died with the outline
#expect(model.selectedPostID == nil) // cancel teardown: nothing was open
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8)
#expect(onDisk.contains("type: post"))
#expect(onDisk.contains("A paragraph between bullets.")) // verbatim kept everything
}