CI / ci (push) Canceled after 0s
#179: every library-reflecting store write (draft save, session-born post, rename, move to project, archive/restore, trash, convert, project creation, import batch, pass advance) now rides one internal seam — performWrite pairs the write with exactly one rescan and owns the error strip (cleared BEFORE the write so importFiles' per-file failure messages survive the success tail; a nil-Home refusal writes nothing). The model owns the Home: watchedHome is private(set), set only by bind (construction + the Settings repoint); no public method takes a Home parameter — 14 view call sites and ~115 test call sites swept, dead home plumbing cut. The editor's debounced flush, the card closes, the pass substrate and the micropub publish stay off the rail by design (own error semantics, no library regroup). A converged review finding is closed with a guard: the pass advance re-resolves watchedHome after the engine's minutes-long turn, so a mid-pass Settings repoint now refuses the advance with an honest strip message instead of silently stamping pass state into the repointed library (wrong-piece write on a slug collision). createProject failures now surface in the sheet AND the strip — the rail-owns-the- strip posture supersedes the #108 sheet-only rule. Five rail pins (PostsModelWriteRailTests, suite members): write → one library refresh + strip cleared, failed write owns the strip, nil-Home refusal, import batch keeps per-file messages, mid-pass repoint refuses the advance — the last two flip-verified against their guards. ScriptedEngine gains endHeldPrompt (release a held turn as ended). Verification: suite 657/657 the evening this landed; the next-morning re-run could not start — both test runners hung before establishing connection (#134 testmanagerd wedge; an untouched control suite hangs identically — machine state, not this diff; reboot pending). Rail pins 5/5 isolated after the last test-only edit; swiftlint at baseline parity; swiftformat clean. Closes #179 [Generated by AI]
652 lines
27 KiB
Swift
652 lines
27 KiB
Swift
//
|
|
// PostsModelTests.swift
|
|
// MuseionTests
|
|
//
|
|
// #108 audit: the model-layer lifecycle the store tests can't see —
|
|
// archive clearing the open session, move reopening it at the new parent,
|
|
// restore keeping the writer in the piece, New Project's draft targeting.
|
|
// #179: the write-rail pins — one seam pairs every library store write
|
|
// with exactly one rescan and owns the error strip; the model owns the
|
|
// Home (no method takes one).
|
|
//
|
|
|
|
import Foundation
|
|
@testable import Museion
|
|
import Testing
|
|
|
|
/// Throwaway Home + model (EditorSessionTests convention: direct @MainActor
|
|
/// instantiation, no app gate). Internal — PassRunTests builds its rig on
|
|
/// it (review: a hand-copy drifted).
|
|
@MainActor
|
|
func makeModelHome() throws -> (PostsModel, URL) {
|
|
let home = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("postsmodel-\(UUID().uuidString)", isDirectory: true)
|
|
try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true)
|
|
let model = PostsModel()
|
|
model.bind(home: home)
|
|
return (model, home)
|
|
}
|
|
|
|
@Test @MainActor func `archiving the open piece clears selection and session`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Gone Soon\n", home: home)
|
|
model.select(post.id)
|
|
#expect(model.session?.postID == post.id)
|
|
|
|
model.archivePost(post)
|
|
|
|
#expect(model.selectedPostID == nil)
|
|
#expect(model.session == nil)
|
|
#expect(model.lastError == nil)
|
|
#expect(Post.scan(home: home).first?.isArchived == true)
|
|
}
|
|
|
|
@Test @MainActor func `moving the open piece reopens its session at the new parent`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Traveler\n", project: "essays", home: home)
|
|
model.select(post.id)
|
|
#expect(model.session?.folderURL.path.contains("projects/essays") == true)
|
|
|
|
model.moveToProject(post, project: "posts")
|
|
|
|
#expect(model.session?.postID == post.id) // same piece, fresh session
|
|
#expect(model.session?.folderURL.path.contains("projects/posts/\(post.id)") == true)
|
|
#expect(model.lastError == nil)
|
|
}
|
|
|
|
@Test @MainActor func `restore reopens the session off the archive path`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Revived\n", home: home)
|
|
try PostStore.archive(post, home: home)
|
|
model.select(post.id) // opened from the Archive pane
|
|
#expect(model.session?.folderURL.path.contains("archive/") == true)
|
|
|
|
model.restorePost(post)
|
|
|
|
#expect(model.selectedPostID == post.id) // the writer stays in it
|
|
#expect(model.session?.folderURL.path.contains("projects/posts/\(post.id)") == true)
|
|
#expect(model.lastError == nil)
|
|
}
|
|
|
|
@Test @MainActor func `new project draft lands in the named project`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
try PostStore.createProject(named: "essays", home: home)
|
|
|
|
model.newProjectPending(named: "essays")
|
|
#expect(model.draft?.project == "essays")
|
|
|
|
model.updateDraft("# Fresh\n")
|
|
model.saveDraft()
|
|
|
|
let scanned = Post.scan(home: home)
|
|
#expect(scanned.first?.project == "essays")
|
|
#expect(scanned.first?.id == "fresh")
|
|
}
|
|
|
|
@Test @MainActor func `abandoning the draft for an external file drops the pending target`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
try PostStore.createProject(named: "essays", home: home)
|
|
model.newProjectPending(named: "essays")
|
|
#expect(model.draft?.project == "essays")
|
|
|
|
// The external guest takes the column; the draft dies — its target must too.
|
|
let external = home.appendingPathComponent("outside.md")
|
|
try "# foreign\n".write(to: external, atomically: true, encoding: .utf8)
|
|
model.openExternal(external)
|
|
|
|
#expect(model.draft == nil)
|
|
model.newPost()
|
|
#expect(model.draft?.project == Post.defaultProject) // not the abandoned essays
|
|
}
|
|
|
|
@Test @MainActor func `unarchive flushes the dirty open piece before moving`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Keeper\n", home: home)
|
|
try PostStore.archive(post, home: home)
|
|
model.select(post.id)
|
|
model.session?.edit("# Keeper\n\nDirty.") // inside the debounce window
|
|
|
|
model.unarchiveProject("posts")
|
|
|
|
// The edits landed on disk (readBody resolves by id — through the move).
|
|
#expect(PostStore.readBody(id: post.id, home: home) == "# Keeper\n\nDirty.")
|
|
#expect(model.lastError == nil)
|
|
// The reopen swapped in a fresh session over the same piece.
|
|
#expect(model.session?.postID == post.id)
|
|
#expect(model.session?.folderURL.path.contains("projects/posts/\(post.id)") == true)
|
|
}
|
|
|
|
@Test @MainActor func `newOutline stages an outline draft in the target project`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Seed\n", project: "essays", home: home)
|
|
// targetProject reads `posts` — wait for the bind rescan to surface the
|
|
// piece (the watcher path; saveDraft would trigger it too, too late here).
|
|
for _ in 0 ..< 250 where !model.posts.contains(where: { $0.id == post.id }) {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
model.select(post.id)
|
|
|
|
model.newOutline()
|
|
|
|
#expect(model.draft?.text == "- ")
|
|
#expect(model.draft?.type == .outline)
|
|
#expect(model.draft?.project == "essays") // selected non-archived post's project
|
|
#expect(model.selectedPostID == nil)
|
|
#expect(model.session == nil)
|
|
}
|
|
|
|
@Test @MainActor func `newOutline draft falls back to the default project`() throws {
|
|
let (model, _) = try makeModelHome()
|
|
model.newOutline()
|
|
#expect(model.draft?.project == Post.defaultProject)
|
|
}
|
|
|
|
@Test @MainActor func `saveDraft materializes an outline and opens the session on it`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
|
|
model.newOutline()
|
|
model.saveDraft()
|
|
|
|
#expect(model.draft == nil)
|
|
#expect(model.lastError == nil)
|
|
#expect(model.session?.postID == "untitled") // "- " slugs to untitled
|
|
#expect(model.selectedPostID == "untitled")
|
|
let scanned = try #require(Post.scan(home: home).first)
|
|
#expect(scanned.type == .outline)
|
|
#expect(scanned.title == "-") // prose semantics: first line, trimmed — "- "
|
|
}
|
|
|
|
@Test @MainActor func `moveTargets lists empty projects from disk`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
try PostStore.createProject(named: "essays", home: home) // empty — no pieces
|
|
|
|
#expect(model.moveTargets() == ["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)
|
|
|
|
#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)
|
|
model.openGuardedOutlineInProse()
|
|
|
|
#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)
|
|
model.openGuardedOutlineAsOutline()
|
|
|
|
#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)
|
|
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)
|
|
#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)
|
|
model.openGuardedOutlineAsOutline() // consent; disk still mixed
|
|
let session = try #require(model.session)
|
|
session.edit("- one\n- two\nedited") // dirty; debounce armed
|
|
|
|
model.select(post.id) // 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) // 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()
|
|
model.openGuardedOutlineAsOutline()
|
|
#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)
|
|
#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)
|
|
#expect(model.outlineGuardPieceID == nil)
|
|
#expect(model.session?.postID == clean.id)
|
|
|
|
model.select(prose.id)
|
|
#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)
|
|
|
|
model.stageConversion(post)
|
|
#expect(model.pendingConversion?.id == post.id)
|
|
#expect(model.pendingConversion?.lossy == false) // clean outline
|
|
model.convertToPost(headingsLadder: true)
|
|
|
|
#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)
|
|
|
|
model.stageConversion(outline)
|
|
model.convertToPost(headingsLadder: false)
|
|
|
|
#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)
|
|
model.stageConversion(post)
|
|
model.convertToPost(headingsLadder: false) // 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)
|
|
|
|
model.session?.edit("# Chapter\n- one\n - typed child") // unflushed edit
|
|
model.stageConversion(post)
|
|
model.convertToPost(headingsLadder: false)
|
|
|
|
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)
|
|
|
|
// 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)
|
|
|
|
#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)
|
|
|
|
#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) // 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)
|
|
|
|
#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) // now stages lossy from disk
|
|
model.convertToPost(headingsLadder: true)
|
|
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)
|
|
|
|
// #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)
|
|
model.convertToPost(headingsLadder: false)
|
|
|
|
#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)
|
|
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)
|
|
model.convertToPost(headingsLadder: true)
|
|
|
|
#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) // guard fires, open held
|
|
#expect(model.outlineGuardPieceID == post.id)
|
|
|
|
model.stageConversion(post)
|
|
model.convertToPost(headingsLadder: false)
|
|
|
|
#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
|
|
}
|
|
|
|
// #23 (M2, slice 3): the open piece's stored fix cards surface live.
|
|
// The card Input helper is MCPToolsTests' internal card() — one shape,
|
|
// no hand-copy (review).
|
|
|
|
/// refreshCards hops off MainActor (scan runs in Task.detached) — poll for
|
|
/// the hop-back instead of asserting synchronously.
|
|
@MainActor
|
|
private func waitForCards(_ model: PostsModel, _ count: Int) async throws {
|
|
for _ in 0 ..< 250 where model.openPieceCards.count < count {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
}
|
|
|
|
// #179: the rail's rescan lands via Task — bounded poll (FixCardApplyTests'
|
|
// pollUntil, made internal; the post-loop expect makes a silent timeout a
|
|
// failure, not a pass).
|
|
|
|
/// Scenario: one write rail for every library verb (from #179).
|
|
/// Given a model bound to a Home, when a verb's store write runs, then it
|
|
/// lands through the ONE seam — the library reflects it via exactly one
|
|
/// rescan and a stale error strip is cleared on success.
|
|
/// Suite member (not free function): suite-name filters must reach the
|
|
/// rail pins (MuseionTests/AGENTS.md rule).
|
|
@Suite("write rail #179")
|
|
struct PostsModelWriteRailTests {
|
|
@Test @MainActor func `pairs the write with the library refresh and clears the strip`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Rail Pin\n", home: home)
|
|
try await pollUntil { model.posts.contains { $0.id == post.id } }
|
|
|
|
model.lastError = "stale strip" // the rail owns the strip from here
|
|
model.rename(post, to: "Rail Pin Two")
|
|
|
|
#expect(model.lastError == nil)
|
|
try await pollUntil { model.posts.contains { $0.id == "rail-pin-two" } }
|
|
#expect(model.posts.contains { $0.id == post.id } == false)
|
|
}
|
|
|
|
/// Scenario: the rail owns the error strip on a failed write (from #179).
|
|
/// Given a bound model and a store write that throws, the verb's failure
|
|
/// reason lands in the strip AND rides the return for the sheet.
|
|
@Test @MainActor func `owns the strip when the write throws`() throws {
|
|
let (model, home) = try makeModelHome()
|
|
try PostStore.createProject(named: "dup", home: home) // the store already has it
|
|
|
|
let reason = model.createProject(named: "dup")
|
|
|
|
#expect(reason != nil)
|
|
#expect(model.lastError == reason)
|
|
}
|
|
|
|
/// Scenario: the rail refuses cleanly with no Home bound (from #179).
|
|
/// Given a model that was never bound, a write verb refuses with a reason
|
|
/// and nothing reaches any disk.
|
|
@Test @MainActor func `refuses cleanly before a home is bound`() {
|
|
let model = PostsModel()
|
|
|
|
let reason = model.createProject(named: "nowhere")
|
|
|
|
#expect(reason != nil)
|
|
#expect(model.lastError == reason)
|
|
#expect(model.posts.isEmpty)
|
|
}
|
|
|
|
/// Scenario: a mid-batch import failure keeps its strip message while
|
|
/// the good files land (from #179 — the rail clears the strip BEFORE
|
|
/// the write exactly so the per-file failures set inside the closure
|
|
/// survive the success tail).
|
|
@Test @MainActor func `import batch keeps per-file failure messages on the strip`() async throws {
|
|
let (model, _) = try makeModelHome()
|
|
let scratch = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("import-batch-\(UUID().uuidString)", isDirectory: true)
|
|
try FileManager.default.createDirectory(at: scratch, withIntermediateDirectories: true)
|
|
let good = scratch.appendingPathComponent("good.md")
|
|
try "# Good Piece\n".write(to: good, atomically: true, encoding: .utf8)
|
|
let missing = scratch.appendingPathComponent("missing.md") // never written — the read throws
|
|
|
|
model.stageImport([good, missing])
|
|
model.importFiles(origin: .writer)
|
|
|
|
#expect(model.lastError?.contains("Import failed — missing.md") == true)
|
|
// the library refresh rides the rail's rescan Task — poll for it
|
|
try await pollUntil { model.posts.contains { $0.id == "good-piece" } }
|
|
#expect(model.selectedPostID == "good-piece") // …and opened (synchronous select)
|
|
}
|
|
|
|
/// Scenario: a Home repoint while a pass runs refuses the pass advance
|
|
/// (from #179 audit — the rail resolves watchedHome at call time, and
|
|
/// the engine's turn spans minutes). Given a pass that ends normally
|
|
/// after its library was repointed, the advance is refused with the
|
|
/// honest strip message, both libraries keep their pass states, and
|
|
/// nothing was written into the new library.
|
|
@Test @MainActor func `home repoint mid-pass refuses the advance`() async throws {
|
|
let rig = try makeScriptedRig(mode: "hangOnPrompt")
|
|
defer { rig.mentor.shutdown() }
|
|
await rig.mentor.openSession()
|
|
let post = try PostStore.writeNew(body: "# Repoint\n", home: rig.home)
|
|
rig.model.select(post.id)
|
|
|
|
let run = Task { await rig.model.runPass(.pass1, mentor: rig.mentor) }
|
|
try await pollUntil { rig.model.runningPass == .pass1 } // the engine holds the turn
|
|
let homeB = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("repoint-\(UUID().uuidString)", isDirectory: true)
|
|
try FileManager.default.createDirectory(at: homeB, withIntermediateDirectories: true)
|
|
rig.model.bind(home: homeB) // the Settings repoint, mid-pass
|
|
// Release the held turn as a NORMALLY ended one — .ended + no refusal.
|
|
// #require: nothing held means the parked prompt already timed out
|
|
// under machine load — fail loudly, not on a confusing nil strip.
|
|
let engine = try #require(rig.mentor.host.oneShot as? ScriptedEngine)
|
|
try #require(engine.endHeldPrompt())
|
|
await run.value
|
|
|
|
#expect(rig.model.lastError == "The library changed while the pass ran — its Pass State was not advanced.")
|
|
#expect(Post.scan(home: rig.home).first?.passState == .firstDraft) // the pass's own library: not advanced
|
|
#expect(Post.scan(home: homeB).isEmpty) // the new library: nothing stamped
|
|
}
|
|
}
|
|
|
|
@Test @MainActor func `opening a piece surfaces its stored fix cards`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
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))
|
|
try FixCardStore.submit(piecePath: folder.path, cards: [card(pattern: "Hedging", quote: "The anchor sentence.")])
|
|
|
|
model.select(post.id)
|
|
try await waitForCards(model, 1)
|
|
|
|
#expect(model.openPieceCards.map(\.pattern) == ["Hedging"])
|
|
#expect(model.openPieceCards.first?.anchorStatus == .valid)
|
|
}
|
|
|
|
@Test @MainActor func `a card landing after open joins the list without reselect`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
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))
|
|
try FixCardStore.submit(piecePath: folder.path, cards: [card(pattern: "Hedging", quote: "The anchor sentence.")])
|
|
model.select(post.id)
|
|
try await waitForCards(model, 1)
|
|
|
|
// An MCP client submits another card while the piece stays open —
|
|
// the piece-roots watcher fires on <piece>/cards/ and the rescan
|
|
// tail refreshes (poll-loop convention).
|
|
try FixCardStore.submit(piecePath: folder.path, cards: [card(pattern: "Telling", quote: "The anchor sentence.")])
|
|
|
|
for _ in 0 ..< 250 where model.openPieceCards.count < 2 {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
#expect(model.openPieceCards.count == 2) // grew live, no reselect
|
|
}
|
|
|
|
@Test @MainActor func `closing the piece clears the card list`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
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))
|
|
try FixCardStore.submit(piecePath: folder.path, cards: [card(pattern: "Hedging", quote: "The anchor sentence.")])
|
|
model.select(post.id)
|
|
try await waitForCards(model, 1)
|
|
|
|
model.newPost()
|
|
|
|
#expect(model.openPieceCards == []) // the nil-session rule
|
|
}
|
|
|
|
@Test @MainActor func `an archived piece opened shows its cards too`() async throws {
|
|
let (model, home) = try makeModelHome()
|
|
let post = try PostStore.writeNew(body: "# Filed\n\nThe anchor sentence.\n", home: home)
|
|
try PostStore.archive(post, home: home)
|
|
let folder = try #require(PostStore.folder(of: post.id, home: home))
|
|
#expect(folder.path.contains("/archive/"))
|
|
try FixCardStore.submit(piecePath: folder.path, cards: [card(pattern: "Hedging", quote: "The anchor sentence.")])
|
|
|
|
model.select(post.id) // opened from the Archive pane
|
|
try await waitForCards(model, 1)
|
|
|
|
#expect(model.openPieceCards.count == 1)
|
|
}
|