diff --git a/Museion/PostsModel.swift b/Museion/PostsModel.swift index ec74ac5..0b8fb76 100644 --- a/Museion/PostsModel.swift +++ b/Museion/PostsModel.swift @@ -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() diff --git a/Museion/PostsModelWatcher.swift b/Museion/PostsModelWatcher.swift index 3cd1791..2ec4c3c 100644 --- a/Museion/PostsModelWatcher.swift +++ b/Museion/PostsModelWatcher.swift @@ -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 diff --git a/MuseionTests/PostsModelRepointTests.swift b/MuseionTests/PostsModelRepointTests.swift index b561819..73940d3 100644 --- a/MuseionTests/PostsModelRepointTests.swift +++ b/MuseionTests/PostsModelRepointTests.swift @@ -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) + } }