feat(outline): mixed-content open guard — S7 (#111)
CI / ci (push) Canceled after 0s

Opening a type: outline piece whose body parses with dropped prose
(paragraph lines between bullets) now asks before the outliner mounts:
Open in Prose (default, nothing drops), Open as Outline (explicit
consent to save normalization), or Cancel. The detector shares the
parser's item definition, the ask presents over an empty editor so
nothing can normalize on save before the choice, and the prose answer
rides a routing override that dies with the next open.

Audit round pinned the lifecycle: reselect of the open piece is a
no-op (an armed debounce must never flush mid-ask), and a draft or
guest staged during the ask displaces it without being destroyed.

Closes #111

[Generated by AI]
This commit is contained in:
AlexKucera committed 2026-10-15 18:31:26 +02:00
1 parent c23ee0f62f
commit 0b172f73ef
12 files changed
+440 -17

No files matched your search

+136
View File
@@ -159,3 +159,139 @@ private func makeModelHome() throws -> (PostsModel, URL) {
#expect(model.moveTargets(home: home) == ["essays", "posts"])
// "posts" exists too (bind created it); a dot-project never shows.
}
// #111 (S7): the mixed-content outline guard — no silent prose loss.
/// A `type: outline` piece whose body parses with dropped prose.
@discardableResult
private func writeMixedOutline(home: URL) throws -> Post {
try PostStore.writeNew(
body: "# Mixed\n\n- one\nA paragraph between bullets.\n- two\n",
type: .outline,
home: home,
)
}
@Test @MainActor func `mixed outline fires the guard and holds the open`() throws {
let (model, home) = try makeModelHome()
let post = try writeMixedOutline(home: home)
model.select(post.id, home: home)
#expect(model.outlineGuardPieceID == post.id) // the ask is up
#expect(model.session == nil) // nothing editable while it's up
#expect(model.selectedPostID == post.id)
// The file is untouched — the choice precedes any save.
#expect(try String(contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8)
.contains("A paragraph between bullets."))
}
@Test @MainActor func `guard prose answer opens prose and never drops`() throws {
let (model, home) = try makeModelHome()
let post = try writeMixedOutline(home: home)
model.select(post.id, home: home)
model.openGuardedOutlineInProse(home: home)
#expect(model.outlineGuardPieceID == nil)
#expect(model.session?.postID == post.id)
#expect(model.proseOverridePostID == post.id) // routes prose despite type
// A full edit+flush cycle in prose mode: nothing drops.
model.session?.edit("- one\nA paragraph between bullets.\n- two\nedited")
_ = model.session?.flush()
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8)
#expect(onDisk.contains("edited"))
#expect(onDisk.contains("A paragraph between bullets."))
}
@Test @MainActor func `guard outline answer is explicit consent`() throws {
let (model, home) = try makeModelHome()
let post = try writeMixedOutline(home: home)
model.select(post.id, home: home)
model.openGuardedOutlineAsOutline(home: home)
#expect(model.outlineGuardPieceID == nil)
#expect(model.session?.postID == post.id)
#expect(model.proseOverridePostID == nil) // outline routing stands
// The consented save drops the prose — as ruled, but only now.
model.session?.edit("- one\n- two")
_ = model.session?.flush()
let onDisk = try String(contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8)
#expect(!onDisk.contains("A paragraph between bullets."))
}
@Test @MainActor func `guard cancel backs out and stays guarded`() throws {
let (model, home) = try makeModelHome()
let post = try writeMixedOutline(home: home)
model.select(post.id, home: home)
model.cancelOutlineGuard()
#expect(model.outlineGuardPieceID == nil)
#expect(model.session == nil)
// Still guarded: the next open re-fires the ask, file untouched.
model.select(post.id, home: home)
#expect(model.outlineGuardPieceID == post.id)
}
// Audit pins: the guard's lifecycle holes (reselect / displacement).
@Test @MainActor func `reselecting the open guarded piece is a no-op`() throws {
let (model, home) = try makeModelHome()
let post = try writeMixedOutline(home: home)
model.select(post.id, home: home)
model.openGuardedOutlineAsOutline(home: home) // consent; disk still mixed
let session = try #require(model.session)
session.edit("- one\n- two\nedited") // dirty; debounce armed
model.select(post.id, home: home) // muscle-memory re-click on the open row
#expect(model.outlineGuardPieceID == nil) // no re-ask over a prior answer
#expect(model.session === session) // the open session survives, dirty state intact
}
@Test @MainActor func `new draft during the ask displaces it and survives`() throws {
let (model, home) = try makeModelHome()
let post = try writeMixedOutline(home: home)
model.select(post.id, home: home) // ask up
model.newPost() // ⌘N fires while the sheet is up
#expect(model.outlineGuardPieceID == nil) // the ask died with the displacement
#expect(model.draft != nil)
model.updateDraft("# Keep me\n")
// Answering the (now-gone) ask is inert — the draft survives.
model.openGuardedOutlineInProse(home: home)
model.openGuardedOutlineAsOutline(home: home)
#expect(model.draft != nil)
#expect(model.selectedPostID == nil) // the draft still owns the editor
}
@Test @MainActor func `guard fires from the scan-known type too`() async throws {
let (model, home) = try makeModelHome()
let post = try writeMixedOutline(home: home)
// Wait for the bind rescan so `posts` knows the piece (poll-loop convention).
for _ in 0 ..< 250 where !model.posts.contains(where: { $0.id == post.id }) {
try await Task.sleep(nanoseconds: 20_000_000)
}
#expect(model.posts.contains(where: { $0.id == post.id }))
model.select(post.id, home: home)
#expect(model.outlineGuardPieceID == post.id) // the scan path guards too
}
@Test @MainActor func `clean outline and prose pieces open without the guard`() throws {
let (model, home) = try makeModelHome()
let clean = try PostStore.writeNew(body: "# Clean\n\n- one\n- two\n", type: .outline, home: home)
let prose = try PostStore.writeNew(body: "# Plain\n\nA paragraph.\n", home: home)
model.select(clean.id, home: home)
#expect(model.outlineGuardPieceID == nil)
#expect(model.session?.postID == clean.id)
model.select(prose.id, home: home)
#expect(model.outlineGuardPieceID == nil)
#expect(model.session?.postID == prose.id)
}