Files
museion/Museion/LibrarySearch.swift
AlexKucera c0cd60b798
CI / ci (push) Canceled after 0s
test(search): red pin for snapshot invalidation
The mounted search snapshot is keyed only to the trimmed query, so
library mutations never restart the sweep: archived/trashed rows
linger in the results and edited bodies never enter them until the
query changes or the view remounts.

Pin the invalidation contract at controller level, the way the pure
seam is pinned: LibrarySearch.SearchKey (query + Home + the model's
content revision) and the LibrarySearch.Snapshot isFresh gate decide
whether a landed sweep may render. The rail pins drive the real
rescan path and prove archive, trash, and a same-query body edit
each move PostsModel.contentRevision. The property is a stub here —
never bumped — so those three pins fail red on this commit.
2026-11-11 22:19:01 +01:00

63 lines
2.3 KiB
Swift

//
// LibrarySearch.swift
// Museion
//
// #144 (FEAT-004): the library search's pure core — extracted from the
// landed scan so ranking/partitioning is testable
// (MuseionTests/LibrarySearchTests.swift) while PostsLibraryColumn's
// structured .task stays the runtime path.
//
import Foundation
/// #144: the library search's pure core — title matches first, then body
/// matches, both halves preserving the scan's title order (#108: Finder
/// order, never re-sorted). nonisolated + the injected `@Sendable` reader
/// keep the seam pure: `bodyOf` is `PostStore.readBody` at the call site
/// (frontmatter never searched — stripping is readBody's contract).
nonisolated enum LibrarySearch {
/// The query is trimmed here; empty (or whitespace-only) matches
/// nothing — the caller renders the sectioned library instead.
static func results(
for query: String,
in candidates: [Post],
bodyOf: @Sendable (Post) -> String?,
) -> [Post] {
let needle = query.trimmingCharacters(in: .whitespacesAndNewlines)
guard !needle.isEmpty else { return [] }
let titleHits = candidates.filter { $0.title.localizedCaseInsensitiveContains(needle) }
let bodyHits = candidates.filter {
!$0.title.localizedCaseInsensitiveContains(needle)
&& (bodyOf($0) ?? "").localizedCaseInsensitiveContains(needle)
}
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
}
}
}