From 08005d322fd708d44c95672a43c23c96fba828fb Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Tue, 3 Nov 2026 22:17:40 +0100 Subject: [PATCH] refactor(library): extract search seam (#144) Mount the pure LibrarySearch.results(for:in:bodyOf:) core inside the landed structured scan: the nonisolated async search() keeps its shape (off-main, same task, .task(id: trimmedQuery) untouched) and now delegates to the seam with PostStore.readBody as the injected reader; the reader's per-piece Task.isCancelled guard preserves the sweep's between-files cancellation of body reads. No behavior change vs 53c314d; the seam exists so the ranking logic is testable. --- Museion/LibrarySearch.swift | 35 +++++++++++++++++++++++++++++++++++ Museion/PostsPaneView.swift | 29 ++++++++++------------------- 2 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 Museion/LibrarySearch.swift diff --git a/Museion/LibrarySearch.swift b/Museion/LibrarySearch.swift new file mode 100644 index 0000000..4ab1e3a --- /dev/null +++ b/Museion/LibrarySearch.swift @@ -0,0 +1,35 @@ +// +// 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 + } +} diff --git a/Museion/PostsPaneView.swift b/Museion/PostsPaneView.swift index 6838bb2..a14d3e1 100644 --- a/Museion/PostsPaneView.swift +++ b/Museion/PostsPaneView.swift @@ -315,10 +315,13 @@ struct PostsLibraryColumn: View { } } - /// #144: the title+body scan — title matches first (stable partition, - /// both halves keep the scan's Finder order), body via readBody - /// (frontmatter never searched); the per-piece cancellation check stops - /// a superseded sweep between files. + /// #144: the title+body scan — a thin mount for the pure + /// `LibrarySearch` seam (title matches first, stable partition, both + /// halves keep the scan's Finder order; body via readBody — frontmatter + /// never searched), pinned by MuseionTests/LibrarySearchTests.swift. + /// The reader's per-piece cancellation check stops a superseded sweep's + /// body reads between files; the in-memory title walk itself completes + /// cheaply and the caller's guard discards its result. /// /// ponytail: full-body re-read per sweep (folder(of:) re-walked per /// piece — same unbounded-read ceiling as Post.scan, LearningsIndex @@ -328,22 +331,10 @@ struct PostsLibraryColumn: View { matching trimmed: String, home: URL, ) async -> [Post] { - var byTitle: [Post] = [] - var byBody: [Post] = [] - for post in candidates { - if Task.isCancelled { - break - } - if post.title.localizedCaseInsensitiveContains(trimmed) { - byTitle.append(post) - continue - } - let body = PostStore.readBody(id: post.id, home: home) ?? "" - if body.localizedCaseInsensitiveContains(trimmed) { - byBody.append(post) - } + LibrarySearch.results(for: trimmed, in: candidates) { post in + guard !Task.isCancelled else { return nil } + return PostStore.readBody(id: post.id, home: home) } - return byTitle + byBody } /// One piece row + its context menu — sections and search results