test(editor): pin Home repoint as a document departure — RED at ca265d3
CI / ci (push) Canceled after 0s

PostsModelRepointTests (6 pins over a two-Home same-slug rig,
projects/posts/shared/post.md in both libraries): the gate runs
before anything changes (failed flush, unanswered #67, never-loaded
retype — sessionMayDepart's three legs), the same-slug piece opens
from B and saves there (flush AND quit), a no-collision repoint lands
the dirty buffer before tearing down, and a pass started after the
repoint runs against the new library.

RED at ca265d3bd3 on the rail
(-only-testing:MuseionTests/PostsModelRepointTests): 6 executed /
0 passed / 6 failed, zero compile errors — each on the finding's
signature: the retained A session under the B binding
(session.text = "A body, typed in A", targets(home: B) false), B's
edits landing on A's disk, watchedHome = B despite a refused flush,
and the no-collision vanish path dropping the dirty buffer.

The tests speak the Settings path as it exists at base (bind), which
has no gate to refuse through; the fix commit moves each repoint call
to the gated entry. EditorSession gains the pure identity predicates
the assertions speak in (targets(postID:home:) / targets(home:)) —
read-only, no behavior change.
This commit is contained in:
AlexKucera committed 2026-11-23 20:16:02 +01:00
1 parent ca265d3bd3
commit bce6abb38b
2 files changed
+227

No files matched your search

+18
View File
@@ -63,6 +63,24 @@ final class EditorSession {
return nil
}
/// brief-28: document identity — a post session is (Home, ID), never the
/// bare id: two libraries can hold the same slug, and only the pair says
/// which piece the buffer belongs to. The target is immutable, so the
/// Home here is the one the session was BORN under — a model whose
/// `watchedHome` moved past it must treat this session as a foreign
/// document, not as its own open piece.
func targets(postID id: String, home: URL) -> Bool {
guard case let .post(mine, myHome) = target else { return false }
return mine == id && myHome == home
}
/// brief-28: the Home half alone — does this post session live under
/// `home`? External guests never do.
func targets(home: URL) -> Bool {
guard case let .post(_, myHome) = target else { return false }
return myHome == home
}
/// The document's folder — the piece's resolved folder under projects/
/// or archive/ for posts (image resolution #71; resolved once at init,
/// ADR-0012 — a vanished piece still needs some path, so an unresolvable
+209
View File
@@ -0,0 +1,209 @@
//
// PostsModelRepointTests.swift
// MuseionTests
//
// brief-28 (home-repoint-retains-old-editor-target): a Home repoint is a
// document departure. Settings used to commit the new Home and flip
// `watchedHome` (bind → startWatching) without departing the open
// session: the session's immutable target kept the OLD Home, a same-slug
// piece in the new library made the ID-only reselect return it, and
// autosave/Save/quit wrote the old library while structural operations
// resolved against the new one (#198's departure gate never saw this
// seam). Pins: the gate runs BEFORE anything changes (a refused repoint
// leaves Home A, the binding, the session's (Home, ID) target and its
// buffer untouched — all three `sessionMayDepart` legs), an accepted
// repoint lands the edits on A first and leaves no live session targeting
// an A piece under B, the same-slug piece opens from B and saves there
// (flush AND quit), a no-collision repoint tears down cleanly (the old
// vanish path dropped the dirty buffer), a card scan from the old library
// never lands on the new library's piece (identity re-check at
// COMPLETION), and a pass started after the repoint runs against the new
// library.
//
// Two-Home rig: `projects/posts/shared/post.md` in BOTH libraries — the
// collision shape the finding names; differing bodies so a wrong-library
// read is observable.
//
import Foundation
@testable import Museion
import Testing
@Suite("PostsModelRepointTests")
@MainActor
struct PostsModelRepointTests {
private func denyWrites(to dir: URL) throws {
try FileManager.default.setAttributes([.posixPermissions: 0o555], ofItemAtPath: dir.path)
}
private func allowWrites(to dir: URL) throws {
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: dir.path)
}
/// Two Homes holding the SAME slug — `writeNew` derives `shared` from
/// the `# Shared` title in both (a non-Shared bodyA makes a unique
/// slug instead: the no-collision shape). The model is bound to A.
// swiftlint:disable:next large_tuple
private func makeTwoHomes(bodyA: String, bodyB: String) throws -> (model: PostsModel, homeA: URL, homeB: URL) {
let base = FileManager.default.temporaryDirectory
.appendingPathComponent("repoint-\(UUID().uuidString)", isDirectory: true)
let homeA = base.appendingPathComponent("A", isDirectory: true)
let homeB = base.appendingPathComponent("B", isDirectory: true)
for home in [homeA, homeB] {
try FileManager.default.createDirectory(at: home, withIntermediateDirectories: true)
}
_ = try PostStore.writeNew(body: bodyA, home: homeA)
_ = try PostStore.writeNew(body: bodyB, home: homeB)
let model = PostsModel()
model.bind(home: homeA)
return (model, homeA, homeB)
}
@Test func `a same-slug piece opens from the new library after a repoint and saves there, never the old one`() throws {
let (model, homeA, homeB) = try makeTwoHomes(bodyA: "# Shared\n\nA body\n", bodyB: "# Shared\n\nB body\n")
model.select("shared")
#expect(model.session?.text == "# Shared\n\nA body\n")
model.session?.edit("# Shared\n\nA body, typed in A\n")
model.bind(home: homeB) // the Settings repoint
model.select("shared")
// B's piece opened from B — not the retained A session (whose
// ID-only reselect used to return it under the new UI).
#expect(model.session?.text == "# Shared\n\nB body\n")
#expect(model.session?.targets(postID: "shared", home: homeB) == true)
model.session?.edit("# Shared\n\nB body, typed in B\n")
model.session?.flush() // ⌘S — the debounce's own sink
#expect(PostStore.readBody(id: "shared", home: homeB) == "# Shared\n\nB body, typed in B\n")
// A holds exactly what the departure flushed — never B's edits.
#expect(PostStore.readBody(id: "shared", home: homeA) == "# Shared\n\nA body, typed in A\n")
model.session?.edit("# Shared\n\nB body, typed in B again\n")
model.shutdown() // quit rides the departure gate (#200)
#expect(PostStore.readBody(id: "shared", home: homeB) == "# Shared\n\nB body, typed in B again\n")
#expect(PostStore.readBody(id: "shared", home: homeA) == "# Shared\n\nA body, typed in A\n")
}
@Test func `repoint refuses when the open piece's flush fails and changes nothing`() throws {
let (model, homeA, homeB) = try makeTwoHomes(bodyA: "# Shared\n\nA body\n", bodyB: "# Shared\n\nB body\n")
model.select("shared")
model.session?.edit("# Shared\n\nA body, typed in A\n")
let pieceDir = try #require(PostStore.folder(of: "shared", home: homeA))
try denyWrites(to: pieceDir)
defer { try? allowWrites(to: pieceDir) }
model.bind(home: homeB) // the Settings repoint
// ORDERING: the gate fired before ANY state changed — the binding,
// the selection, the session's (Home, ID) target and its buffer.
#expect(model.watchedHome == homeA)
#expect(model.selectedPostID == "shared")
#expect(model.session?.postID == "shared")
#expect(model.session?.targets(postID: "shared", home: homeA) == true)
#expect(model.session?.text == "# Shared\n\nA body, typed in A\n")
#expect(model.session?.saveState == .failed)
#expect(model.lastError != nil)
#expect(PostStore.readBody(id: "shared", home: homeB) == "# Shared\n\nB body\n")
// Writability restored → the edits land on A first, then the
// repoint goes through and departs the session.
try allowWrites(to: pieceDir)
model.bind(home: homeB)
#expect(PostStore.readBody(id: "shared", home: homeA) == "# Shared\n\nA body, typed in A\n")
#expect(model.session == nil)
#expect(model.selectedPostID == nil)
#expect(model.watchedHome == homeB)
}
@Test func `repoint refuses while the conflict alert is unanswered`() throws {
let (model, homeA, homeB) = try makeTwoHomes(bodyA: "# Shared\n\nA body\n", bodyB: "# Shared\n\nB body\n")
model.select("shared")
model.session?.edit("# Shared\n\nA body, typed in A\n")
// #67: a foreign disk body arrived — the alert is up, unanswered.
model.session?.diskDidChange(diskText: "foreign body from disk")
model.bind(home: homeB) // the Settings repoint
#expect(model.watchedHome == homeA)
#expect(model.selectedPostID == "shared")
#expect(model.session?.postID == "shared")
#expect(model.session?.targets(postID: "shared", home: homeA) == true)
#expect(model.session?.conflictAlert == true)
#expect(model.session?.text == "# Shared\n\nA body, typed in A\n")
#expect(model.lastError != nil)
// Never over the foreign body, never into the new library.
#expect(PostStore.readBody(id: "shared", home: homeA) == "# Shared\n\nA body\n")
#expect(PostStore.readBody(id: "shared", home: homeB) == "# Shared\n\nB body\n")
}
@Test func `repoint refuses over a never-loaded session with retyped text`() throws {
let (model, homeA, homeB) = try makeTwoHomes(bodyA: "# Shared\n\nA body\n", bodyB: "# Shared\n\nB body\n")
// The file turns unreadable between scan and open: the session
// opens empty (loadFailed), the writer retypes into it (#200's leg).
let file = try #require(PostStore.folder(of: "shared", home: homeA))
.appendingPathComponent("post.md")
try FileManager.default.setAttributes([.posixPermissions: 0o000], ofItemAtPath: file.path)
defer { try? FileManager.default.setAttributes([.posixPermissions: 0o644], ofItemAtPath: file.path) }
model.select("shared")
#expect(model.session?.loadFailed == true)
model.session?.edit("# Retyped words over a file that never loaded")
model.bind(home: homeB) // the Settings repoint
#expect(model.watchedHome == homeA)
#expect(model.selectedPostID == "shared")
#expect(model.session?.targets(postID: "shared", home: homeA) == true)
#expect(model.session?.text == "# Retyped words over a file that never loaded")
#expect(model.lastError != nil)
}
@Test func `a no-collision repoint lands the open piece's edits first and tears it down cleanly`() async throws {
let (model, homeA, homeB) = try makeTwoHomes(bodyA: "# Unique\n\nA body\n", bodyB: "# Shared\n\nB body\n")
model.select("unique")
model.session?.edit("# Unique\n\nA body, typed in A\n")
model.bind(home: homeB) // the Settings repoint
// The old session's edits landed on A BEFORE anything moved — the
// vanish path used to tear it down without a flush.
#expect(PostStore.readBody(id: "unique", home: homeA) == "# Unique\n\nA body, typed in A\n")
#expect(model.session == nil)
#expect(model.selectedPostID == nil)
// No ghost: the new library's rescan leaves nothing selected, nothing open.
try await pollUntil { model.posts.contains { $0.id == "shared" } }
#expect(model.session == nil)
#expect(model.selectedPostID == nil)
#expect(model.watchedHome == homeB)
}
@Test func `a pass started after the repoint runs against the new library`() async throws {
let (model, homeA, mentor) = try makePassRig()
defer { mentor.shutdown() }
await mentor.openSession()
_ = try PostStore.writeNew(body: "# Shared\n\nA body\n", home: homeA)
let homeB = FileManager.default.temporaryDirectory
.appendingPathComponent("repoint-pass-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: homeB, withIntermediateDirectories: true)
_ = try PostStore.writeNew(body: "# Shared\n\nB body\n", home: homeB)
model.select("shared")
#expect(model.session?.text == "# Shared\n\nA body\n")
model.bind(home: homeB) // the Settings repoint
model.select("shared")
#expect(model.session?.text == "# Shared\n\nB body\n")
await model.runPass(.pass1, mentor: mentor)
// Snapshot and the advance landed on B's piece with B's body — the
// retained A session used to hand its text to the new library.
let snapshot = try String(
contentsOf: homeB.appendingPathComponent("projects/posts/shared/passes/pass-1.md"),
encoding: .utf8,
)
#expect(snapshot.contains("B body"))
let onDiskB = try String(contentsOf: homeB.appendingPathComponent("projects/posts/shared/post.md"), encoding: .utf8)
#expect(onDiskB.contains("pass_state: Pass-1 Done"))
let onDiskA = try String(contentsOf: homeA.appendingPathComponent("projects/posts/shared/post.md"), encoding: .utf8)
#expect(onDiskA.contains("pass_state: First Draft"))
#expect(!FileManager.default.fileExists(atPath: homeA.appendingPathComponent("projects/posts/shared/passes/pass-1.md").path))
}
}