diff --git a/Museion/LibrarySearch.swift b/Museion/LibrarySearch.swift index 4ab1e3a..6db7338 100644 --- a/Museion/LibrarySearch.swift +++ b/Museion/LibrarySearch.swift @@ -32,4 +32,31 @@ nonisolated enum LibrarySearch { } return titleHits + bodyHits } + + /// #144 (mounted half): the identity of one search sweep — the + /// trimmed query, the Home it ran under, and the library content + /// revision it saw (`PostsModel.contentRevision`). The column's + /// `.task(id:)` re-keys on ANY difference, so a keystroke, a Home + /// repoint, or a library write that landed after the last sweep + /// restarts it instead of serving the stale snapshot. + nonisolated struct SearchKey: Equatable { + var query: String + var homePath: String + var revision: Int + } + + /// #144 (mounted half): one landed sweep — the results plus the key + /// that produced them. `isFresh` is the column's stale gate: results + /// render only under the key that computed them, so an archived, + /// trashed, renamed, or since-edited library drops to the recompute + /// state, never stale rows. + nonisolated struct Snapshot: Equatable { + var key: SearchKey + var results: [Post] + + /// A snapshot renders only under its own key. + func isFresh(for key: SearchKey) -> Bool { + self.key == key + } + } } diff --git a/Museion/PostsModel.swift b/Museion/PostsModel.swift index 562ba8b..142dd15 100644 --- a/Museion/PostsModel.swift +++ b/Museion/PostsModel.swift @@ -96,6 +96,15 @@ final class PostsModel { /// all times (#107). private(set) var posts: [Post] = [] + /// #144 (mounted half): the library content revision — the search + /// snapshot's invalidation signal. The rescan rail bumps it whenever + /// a scan sees the library change: a Post-list change OR any re-read + /// post.md (a body-only edit compares equal as a Post — #143's skip + /// keeps the list still — yet the search must re-sweep). The `posts` + /// assignment skip stays untouched: panes never regroup on bumps, + /// only the mounted search re-keys. + private(set) var contentRevision = 0 + /// The selected Post's id; nil = nothing selected (empty editor). /// #146: setter internal — the split-out domain extensions set it. var selectedPostID: String? diff --git a/MuseionTests/LibrarySearchTests.swift b/MuseionTests/LibrarySearchTests.swift index 2368676..5130730 100644 --- a/MuseionTests/LibrarySearchTests.swift +++ b/MuseionTests/LibrarySearchTests.swift @@ -89,3 +89,113 @@ struct LibrarySearchTests { #expect(byBody.map(\.id) == ["b-morning-pages"]) } } + +/// #144 (mounted half): the snapshot invalidation the pure seam can't +/// see — PostsLibraryColumn keys its `.task(id:)` to query + Home + +/// `PostsModel.contentRevision` and renders a landed snapshot only +/// while it `isFresh` for the CURRENT key. The controller pins drive +/// the real rescan rail (write verbs + the rail's own rescan Task) and +/// prove archive, trash, and a same-query body edit each move the +/// revision — without that signal a standing snapshot outlives the +/// library it searched (archived rows linger in the results; edited +/// bodies never enter or leave them until the query changes). +@Suite("library search snapshot invalidation") +struct LibrarySearchSnapshotTests { + @Test func `a snapshot renders only under the key that computed it`() { + let swept = LibrarySearch.Snapshot( + key: LibrarySearch.SearchKey(query: "quartz", homePath: "/users/a/museion", revision: 4), + results: [piece("a", title: "Quartz Harbor")], + ) + #expect(swept.isFresh(for: LibrarySearch.SearchKey(query: "quartz", homePath: "/users/a/museion", revision: 4))) + #expect(!swept.isFresh(for: LibrarySearch.SearchKey(query: "harbor", homePath: "/users/a/museion", revision: 4))) + #expect(!swept.isFresh(for: LibrarySearch.SearchKey(query: "quartz", homePath: "/users/b/museion", revision: 4))) + #expect(!swept.isFresh(for: LibrarySearch.SearchKey(query: "quartz", homePath: "/users/a/museion", revision: 5))) + } + + /// The sweep a mounted column would have landed before the archive: + /// key = (query, home, revision at sweep time), results from the + /// real seam + readBody rail. + @MainActor + private func standingSweep(_ model: PostsModel, home: URL, query: String) -> LibrarySearch.Snapshot { + LibrarySearch.Snapshot( + key: LibrarySearch.SearchKey(query: query, homePath: home.path, revision: model.contentRevision), + results: LibrarySearch.results(for: query, in: model.posts.filter { !$0.isArchived }) { + PostStore.readBody(id: $0.id, home: home) + }, + ) + } + + @Test @MainActor func `archiving a result invalidates the standing snapshot`() async throws { + let (model, home) = try makeModelHome() + let post = try PostStore.writeNew(body: "# Quartz Harbor\n\nthe quartz lamp", home: home) + try await pollUntil { model.posts.contains { $0.id == post.id } } + let standing = standingSweep(model, home: home, query: "quartz") + #expect(standing.results.map(\.id) == [post.id]) // the sweep found it + + model.archivePost(post) + try await pollUntil { model.posts.contains { $0.id == post.id && $0.isArchived } } + + // Same query, same Home — the library moved under the sweep. + #expect(!standing.isFresh(for: LibrarySearch.SearchKey( + query: "quartz", + homePath: home.path, + revision: model.contentRevision, + ))) + } + + @Test @MainActor func `trashing a result invalidates the standing snapshot`() async throws { + let (model, home) = try makeModelHome() + let post = try PostStore.writeNew(body: "# Quartz Harbor\n\nthe quartz lamp", home: home) + try await pollUntil { model.posts.contains { $0.id == post.id } } + let standing = standingSweep(model, home: home, query: "quartz") + #expect(standing.results.map(\.id) == [post.id]) + + model.trash(post) + try await pollUntil { !model.posts.contains { $0.id == post.id } } + + #expect(!standing.isFresh(for: LibrarySearch.SearchKey( + query: "quartz", + homePath: home.path, + revision: model.contentRevision, + ))) + } + + @Test @MainActor func `a same-query body edit invalidates the standing snapshot`() async throws { + let (model, home) = try makeModelHome() + let post = try PostStore.writeNew(body: "# Quiet Study\n\nplain body", home: home) + try await pollUntil { model.posts.contains { $0.id == post.id } } + let standing = standingSweep(model, home: home, query: "quartz") + #expect(standing.results.isEmpty) // nothing matches yet + + // The body gains the needle with NO Post metadata change — the + // #143 equality skip's blind spot: the list itself never moves. + let file = home.appendingPathComponent("projects/posts/\(post.id)/post.md", isDirectory: false) + try PostFrontmatter.stamped( + body: "# Quiet Study\n\nthe quartz lamp", passState: .firstDraft, origin: .writer, type: .post, + ).write(to: file, atomically: true, encoding: .utf8) + let before = model.posts + await model.rescan() + + #expect(model.posts == before) // metadata equal — the scan's skip held + let reswept = LibrarySearch.results(for: "quartz", in: model.posts.filter { !$0.isArchived }) { + PostStore.readBody(id: $0.id, home: home) + } + #expect(reswept.map(\.id) == [post.id]) // a fresh sweep WOULD find it now + #expect(!standing.isFresh(for: LibrarySearch.SearchKey( + query: "quartz", + homePath: home.path, + revision: model.contentRevision, + ))) + } + + @Test @MainActor func `an unchanged rescan keeps the revision`() async throws { + let (model, home) = try makeModelHome() + let post = try PostStore.writeNew(body: "# Settled\n", home: home) + try await pollUntil { model.posts.contains { $0.id == post.id } } + let settled = model.contentRevision + + await model.rescan() // nothing changed since the last scan — all cache hits + + #expect(model.contentRevision == settled) + } +}