feat(library): title-first match core for library search (#144)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
LibrarySearch — the pure seam behind the search field: title matches rank ahead of body matches, both halves keep the scan's title order (#108 Finder order), query trimmed with empty matching nothing, body text injected so PostStore.readBody (frontmatter-stripped) stays the call-site reader. 7 pins incl. the real readBody rail; the ranking pin is flip-verified.
This commit is contained in:
1 parent
de1154dacd
commit
488da126d3
2 files changed
+115
No files matched your search
@@ -169,6 +169,30 @@ struct PostsPaneView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// #144 (FEAT-004): 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 view runs it inside its
|
||||||
|
/// detached scan task; `bodyOf` is `PostStore.readBody` at the call site,
|
||||||
|
/// injected so the seam stays testable.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// #21: the library column, shared by the unfocused layout and the focus-mode
|
/// #21: the library column, shared by the unfocused layout and the focus-mode
|
||||||
/// left reveal overlay — one implementation, both surfaces. Owns the rename +
|
/// left reveal overlay — one implementation, both surfaces. Owns the rename +
|
||||||
/// move sheets and the context menu (they travel with the post rows).
|
/// move sheets and the context menu (they travel with the post rows).
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
//
|
||||||
|
// LibrarySearchTests.swift
|
||||||
|
// MuseionTests
|
||||||
|
//
|
||||||
|
// #144 (FEAT-004): the library search's pure core — title-first ranking,
|
||||||
|
// body scan through the injected reader (PostStore.readBody at the call
|
||||||
|
// site), frontmatter never searched. The field itself is view-local.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
@testable import Museion
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
private func piece(_ id: String, title: String) -> Post {
|
||||||
|
Post(id: id, title: title, passState: .firstDraft, origin: .writer, type: .post, project: "posts", isArchived: false)
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LibrarySearchTests {
|
||||||
|
@Test func `title matches rank ahead of body matches, scan order kept in both halves`() {
|
||||||
|
let titleHit1 = piece("a", title: "Alpha Quartz")
|
||||||
|
let bodyHit1 = piece("b", title: "Bravo")
|
||||||
|
let titleHit2 = piece("c", title: "Charlie Quartz")
|
||||||
|
let bodyHit2 = piece("d", title: "Delta")
|
||||||
|
let noHit = piece("e", title: "Echo")
|
||||||
|
let bodies = ["b": "mentions quartz once", "d": "QUARTZ again", "e": "nothing here"]
|
||||||
|
let found = LibrarySearch.results(for: "quartz", in: [titleHit1, bodyHit1, titleHit2, bodyHit2, noHit]) { bodies[$0.id] }
|
||||||
|
#expect(found.map(\.id) == ["a", "c", "b", "d"])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `body-only match reaches pieces whose titles stay silent`() {
|
||||||
|
let quiet = piece("quiet", title: "A Quiet Study")
|
||||||
|
let found = LibrarySearch.results(for: "quartz", in: [quiet]) { _ in "the quartz lamp flickered" }
|
||||||
|
#expect(found.map(\.id) == ["quiet"])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `a piece matching title and body appears once, in the title half`() {
|
||||||
|
let both = piece("both", title: "Quartz Notes")
|
||||||
|
let other = piece("other", title: "Other")
|
||||||
|
let found = LibrarySearch.results(for: "quartz", in: [other, both]) { $0.id == "both" ? "quartz body" : nil }
|
||||||
|
#expect(found.map(\.id) == ["both"])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `matching is case-insensitive on both fields`() {
|
||||||
|
let titled = piece("titled", title: "quartz harbor")
|
||||||
|
let found = LibrarySearch.results(for: "QUARTZ", in: [titled]) { _ in nil }
|
||||||
|
#expect(found.map(\.id) == ["titled"])
|
||||||
|
|
||||||
|
let bodied = piece("bodied", title: "Untitled")
|
||||||
|
let byBody = LibrarySearch.results(for: "HARBOR", in: [bodied]) { _ in "the harbor at dawn" }
|
||||||
|
#expect(byBody.map(\.id) == ["bodied"])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `empty or whitespace-only query matches nothing`() {
|
||||||
|
let anyPiece = piece("a", title: "Anything")
|
||||||
|
#expect(LibrarySearch.results(for: "", in: [anyPiece]) { _ in "Anything" }.isEmpty)
|
||||||
|
#expect(LibrarySearch.results(for: " \n ", in: [anyPiece]) { _ in "Anything" }.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `unreadable body never matches`() {
|
||||||
|
let anyPiece = piece("a", title: "Untitled")
|
||||||
|
#expect(LibrarySearch.results(for: "vanished", in: [anyPiece]) { _ in nil }.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `real readBody rail searches bodies, never frontmatter`() throws {
|
||||||
|
// Piece A carries "Pass-2 Done" ONLY in frontmatter (every piece's
|
||||||
|
// pass_state would match a raw-text scan); piece B carries the
|
||||||
|
// needle only in its body. Stripping is readBody's contract.
|
||||||
|
let home = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent("libsearch-\(UUID().uuidString)", isDirectory: true)
|
||||||
|
for (id, text) in [
|
||||||
|
"a-quiet-study": "---\npass_state: Pass-2 Done\n---\n\n# A Quiet Study\n\nProse only.",
|
||||||
|
"b-morning-pages": "---\n---\n\n# Morning Pages\n\nThe espresso machine hissed.",
|
||||||
|
] {
|
||||||
|
let dir = home.appendingPathComponent("projects/posts/\(id)", isDirectory: true)
|
||||||
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||||||
|
try text.write(to: dir.appendingPathComponent("post.md"), atomically: true, encoding: .utf8)
|
||||||
|
}
|
||||||
|
let posts = Post.scan(home: home)
|
||||||
|
#expect(posts.count == 2)
|
||||||
|
|
||||||
|
let frontmatterOnly = LibrarySearch.results(for: "Pass-2 Done", in: posts) {
|
||||||
|
PostStore.readBody(id: $0.id, home: home)
|
||||||
|
}
|
||||||
|
#expect(frontmatterOnly.isEmpty)
|
||||||
|
|
||||||
|
let byBody = LibrarySearch.results(for: "espresso", in: posts) {
|
||||||
|
PostStore.readBody(id: $0.id, home: home)
|
||||||
|
}
|
||||||
|
#expect(byBody.map(\.id) == ["b-morning-pages"])
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user