test(editor): pin the rescan stale-Home verdict and the draft-survives repoint (review r1)
CI / ci (push) Canceled after 0s

MF-1: the hoisted `guard watchedHome == home` in rescan was live logic
with no pin — the only new guard still observable after the gate (a
scan started under A can sit in its detached hop while the gate passes
and bind flips to B; landing after B's own scan it would hold A's list
under the B binding until the next B-side event, which with nothing
open is never). `scanLibrary` is the per-model injectable library
scanner beside `scanPieceCards` (captured before the hop); the pin
parks an A scan past the repoint, lets B's scan land, releases, and
asserts the list is still B's and `contentRevision` did not move.

Mutation check: hoist removed → rail-filtered `9 executed / 8 passed /
1 failed`, ONLY that pin, `posts.map(\.id) → ["alpha"]` and
`contentRevision → 3` vs `revision → 2`; restored byte-exact (sha256
prefix d86ce09d6b7e6bd2 before and after).

N-1: the draft-survives-repoint decision pinned — newPost targets the
selected A piece's project, the draft rides the repoint intact, and
`saveDraft()` lands under B only, in the draft's OWN project even
though B does not have it (materialize creates with intermediates).

Filtered GREEN: PostsModelRepointTests + PostsModelDepartureTests
19/19. swiftformat 0/152, swiftlint 0.
This commit is contained in:
AlexKucera committed 2026-11-23 22:34:23 +01:00
1 parent 97712fbacb
commit 92344e224c
3 files changed
+75 -1

No files matched your search

+4
View File
@@ -159,6 +159,10 @@ final class PostsModel {
/// detached disk read is otherwise faster than any test can arrange).
/// Per-model, never static (the hosted suite runs tests in parallel).
var scanPieceCards: @Sendable (URL) -> [FixCardStore.StoredFixCard] = FixCardStore.scan
/// brief-28 r1 (MF-1): the library scanner behind `rescan` — the same
/// injection shape, so the stale-Home pin can HOLD an old library's scan
/// past a repoint and watch its continuation land under the new binding.
var scanLibrary: @Sendable (URL, PostScanCache?) -> [Post] = Post.scan
/// #93: per-file watcher on the open external file — the #67 conflict
/// check reused verbatim for foreign files.
var externalWatcher = PostsWatcher()
+2 -1
View File
@@ -151,8 +151,9 @@ extension PostsModel {
// the counter before the scan so the delta after it says "this
// scan saw disk change" (#144's mounted half).
let missesBefore = scanMemo.misses
let scan = scanLibrary // detached capture, not a property read
let (scanned, openDiskText) = await Task.detached(priority: .userInitiated) {
let posts = Post.scan(home: home, cache: scanMemo)
let posts = scan(home, scanMemo)
let disk = openPostID.flatMap { PostStore.readBody(id: $0, home: home) }
return (posts, disk)
}.value
+69
View File
@@ -25,6 +25,10 @@
// read is observable. The Settings path in these pins is the model's two
// steps — `repoint(to:)` (the gate) then `bind` (the commit) — exactly as
// WorkspaceView's binding and root `.task(id:)` drive them.
// r1 (athena MF-1/N-1): the hoisted stale-Home verdict in `rescan` (the
// library-scan sibling of the card hop-back — the per-model `scanLibrary`
// seam holds it past the repoint) and the draft-survives decision are
// pinned too.
//
import Foundation
@@ -279,4 +283,69 @@ struct PostsModelRepointTests {
#expect(onDiskA.contains("pass_state: First Draft"))
#expect(!FileManager.default.fileExists(atPath: homeA.appendingPathComponent("projects/posts/shared/passes/pass-1.md").path))
}
/// r1 (MF-1): the hoisted stale-Home verdict in `rescan` — the sibling
/// continuation of the card hop-back, and the one guard still observable
/// in production after the gate. A scan started under A sits in its
/// detached hop while the gate passes and `bind` flips to B; landing
/// after B's own scan it would put A's list under the B binding until
/// the next B-side event — which, with nothing open, can be never.
@Test func `a library scan from the old Home lands nothing under the new binding`() async throws {
let (model, homeA, homeB) = try makeTwoHomes(bodyA: "# Alpha\n\nA body\n", bodyB: "# Bravo\n\nB body\n")
// The rig's first rescan lands first: posts is A's list, steady state.
try await pollUntil { model.posts.map(\.id) == ["alpha"] }
// HOLD the next A scan past the repoint (the card pin's shape).
let parked = ParkedFlag()
let release = DispatchSemaphore(value: 0)
model.scanLibrary = { home, cache in
if home == homeA {
parked.set()
_ = release.wait(timeout: .now() + 30) // bounded: a failed test must not leak a parked thread
}
return Post.scan(home: home, cache: cache)
}
Task { await model.rescan() } // parks inside the detached scan
try await pollUntil { parked.isSet }
// The Settings repoint: gate, then binding; B's own scan lands.
#expect(model.repoint(to: homeB))
model.bind(home: homeB)
try await pollUntil { model.posts.map(\.id) == ["bravo"] }
let revision = model.contentRevision
// Release the stale A scan: its continuation completes AFTER B's scan
// — the hoisted verdict applies nothing it found (list, revision).
release.signal()
release.signal() // a second A scan, if one ever queued, must not park the teardown
try await Task.sleep(nanoseconds: 200_000_000) // let the continuation land
#expect(model.posts.map(\.id) == ["bravo"]) // never A's list under the B binding
#expect(model.contentRevision == revision) // and no revision bump for it
}
/// r1 (N-1): the draft-survives decision, pinned — a draft has no Home
/// until first save (#62), so the repoint leaves its buffer alone and
/// the lazy folder lands under B, in the draft's OWN project even when
/// B does not have it yet (materialize creates with intermediates).
@Test func `an in-memory draft survives a repoint and first-saves into the new library only`() async throws {
let (model, homeA, homeB) = try makeTwoHomes(bodyA: "# Piece\n\nA body\n", bodyB: "# Bravo\n\nB body\n")
// A holds `projects/alpha/in-alpha` — a project B does not have.
let alphaPiece = try PostStore.writeNew(body: "# In Alpha\n\nA body\n", project: "alpha", home: homeA)
try await pollUntil { model.posts.contains { $0.id == alphaPiece.id } } // the list must know the piece (targetProject reads it)
model.select(alphaPiece.id)
model.newPost() // targets the selected piece's project
#expect(model.draft?.project == "alpha")
model.updateDraft("# Draft Words\n\nTyped in A, saved in B")
// The Settings repoint: gate, then binding — the draft rides along.
#expect(model.repoint(to: homeB))
model.bind(home: homeB)
#expect(model.draft?.text == "# Draft Words\n\nTyped in A, saved in B") // intact
model.saveDraft()
#expect(PostStore.readBody(id: "draft-words", home: homeB) == "# Draft Words\n\nTyped in A, saved in B")
#expect(PostStore.readBody(id: "draft-words", home: homeA) == nil) // never A
#expect(Post.scan(home: homeB).contains { $0.id == "draft-words" && $0.project == "alpha" }) // B-absent project created
#expect(model.session?.targets(postID: "draft-words", home: homeB) == true) // and it opens there
#expect(model.draft == nil)
}
}