fix(search): move scope→item-type taxonomy into Rust (UR-049, DR-063)
Stage 1 of scoped-search-boundary-implementation.md — the query side.
scoped-search-boundary.md diagnosed this leak, specified the fix in
detail, and became the justification for the boundary rule in CLAUDE.md,
the check:boundary tripwire, and the spec-review checklist. The fix was
never built: SCOPE_ITEM_TYPES was still live in searchScope.ts, called by
library.ts, and no SearchScope existed anywhere in src-tauri/. The rule's
own founding violation was still shipping.
Rust now owns the taxonomy:
pub enum SearchScope { All, Music, Movies, Tv }
impl SearchScope { pub fn item_types(self) -> Option<Vec<String>> }
- SearchOptions gains `scope`, resolved by resolve_scope(). Scope wins
over include_item_types, which stays for the non-search get_items
callers that legitimately request one concrete type.
- repository_search resolves the scope ONCE, before the cache/server
paths diverge, so online and offline filter identically — the failure
mode most likely to go unnoticed.
- All expands to None (no filter), not the union of the other scopes:
an explicit includeItemTypes list would silently drop People, folders,
and any type nobody enumerated.
- searchScope.ts re-exports SearchScope from generated bindings instead
of a hand-written union, and no longer names an item type for search.
- library.ts sends { scope }.
8 Rust tests written first, confirmed failing on "use of undeclared type
SearchScope" before the implementation existed.
The frontend tests that asserted includeItemTypes contents were rewritten
to assert the opaque scope is sent and includeItemTypes is absent —
keeping the old assertions would require the frontend to know the
taxonomy again, defeating the fix. The expansion is now asserted in Rust.
Verified the spec's headline criterion by hashing every src/ file, adding
"AudioBook" to the Music scope in Rust, and re-hashing: zero frontend
files change. That criterion failed before this commit.
Stage 2 (result-side grouping: GROUP_ITEM_TYPES, GroupedSearchResult on
both search payloads) remains open.
This commit is contained in:
@@ -294,6 +294,53 @@ pub struct GetItemsOptions {
|
||||
pub genres: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
/// An opaque search scope the frontend selects; Rust owns what it *means*.
|
||||
///
|
||||
/// The expansion table below is Jellyfin domain vocabulary: it changes when
|
||||
/// Jellyfin adds or renames an item type, never when the UI is redesigned. It
|
||||
/// previously lived in the frontend (`searchScope.ts`), which is the boundary
|
||||
/// leak documented in docs/specs/scoped-search-boundary.md. The frontend now
|
||||
/// sends the enum and never names an item type in connection with search.
|
||||
///
|
||||
/// TRACES: UR-049 | DR-063
|
||||
#[derive(specta::Type, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum SearchScope {
|
||||
All,
|
||||
Music,
|
||||
Movies,
|
||||
Tv,
|
||||
}
|
||||
|
||||
impl SearchScope {
|
||||
/// The Jellyfin item types this scope requests, or `None` for `All`.
|
||||
///
|
||||
/// `All` returns `None` rather than the union of every listed type on
|
||||
/// purpose: an explicit `includeItemTypes` list filters out anything not
|
||||
/// named in it, so a union would silently drop People, folders and any type
|
||||
/// nobody enumerated. Callers must omit the filter entirely on `None`.
|
||||
///
|
||||
/// TRACES: UR-049 | DR-063
|
||||
pub fn item_types(self) -> Option<Vec<String>> {
|
||||
match self {
|
||||
SearchScope::All => None,
|
||||
SearchScope::Music => Some(
|
||||
["MusicAlbum", "MusicArtist", "Audio", "Playlist"]
|
||||
.into_iter()
|
||||
.map(String::from)
|
||||
.collect(),
|
||||
),
|
||||
SearchScope::Movies => Some(vec!["Movie".to_string()]),
|
||||
SearchScope::Tv => Some(
|
||||
["Series", "Episode"]
|
||||
.into_iter()
|
||||
.map(String::from)
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Options for search queries
|
||||
#[derive(specta::Type, Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -304,6 +351,28 @@ pub struct SearchOptions {
|
||||
pub include_item_types: Option<Vec<String>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub search_term: Option<String>,
|
||||
/// Opaque scope selected by the UI. When set it **wins** over
|
||||
/// `include_item_types`, which remains for the non-search `get_items`
|
||||
/// callers that legitimately request a single concrete type.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub scope: Option<SearchScope>,
|
||||
}
|
||||
|
||||
impl SearchOptions {
|
||||
/// Expand `scope` into `include_item_types` in place.
|
||||
///
|
||||
/// Call this once, in the search command, *before* dispatching to the
|
||||
/// cache and server paths — both already honour `include_item_types`, and
|
||||
/// resolving in one place keeps online and offline results identical.
|
||||
///
|
||||
/// TRACES: UR-049 | DR-063
|
||||
pub fn resolve_scope(&mut self) {
|
||||
if let Some(scope) = self.scope {
|
||||
// `All` yields None, which clears the filter — the correct
|
||||
// behaviour, not an omission.
|
||||
self.include_item_types = scope.item_types();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Playback information
|
||||
@@ -455,6 +524,131 @@ impl MeaningfulContent for PlaylistCreatedResult {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod search_scope_tests {
|
||||
use super::*;
|
||||
|
||||
/// Music expands to the four Jellyfin types that make up the category.
|
||||
///
|
||||
/// This table is the domain vocabulary that used to live in the frontend
|
||||
/// (`searchScope.ts`'s `SCOPE_ITEM_TYPES`) — the boundary leak that
|
||||
/// docs/specs/scoped-search-boundary.md was written about.
|
||||
///
|
||||
/// @req-test: UT-089 - SearchScope expands to Jellyfin item types
|
||||
#[test]
|
||||
fn music_scope_expands_to_music_item_types() {
|
||||
assert_eq!(
|
||||
SearchScope::Music.item_types(),
|
||||
Some(vec![
|
||||
"MusicAlbum".to_string(),
|
||||
"MusicArtist".to_string(),
|
||||
"Audio".to_string(),
|
||||
"Playlist".to_string(),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/// @req-test: UT-089 - SearchScope expands to Jellyfin item types
|
||||
#[test]
|
||||
fn movies_scope_expands_to_movie_only() {
|
||||
assert_eq!(
|
||||
SearchScope::Movies.item_types(),
|
||||
Some(vec!["Movie".to_string()])
|
||||
);
|
||||
}
|
||||
|
||||
/// @req-test: UT-089 - SearchScope expands to Jellyfin item types
|
||||
#[test]
|
||||
fn tv_scope_expands_to_series_and_episode() {
|
||||
assert_eq!(
|
||||
SearchScope::Tv.item_types(),
|
||||
Some(vec!["Series".to_string(), "Episode".to_string()])
|
||||
);
|
||||
}
|
||||
|
||||
/// `All` must send NO filter — not the union of the other scopes.
|
||||
///
|
||||
/// Sending a union would silently drop every type nobody enumerated
|
||||
/// (Person, folders, …), which an explicit `includeItemTypes` list filters
|
||||
/// out. This is why `item_types()` returns Option rather than Vec.
|
||||
///
|
||||
/// @req-test: UT-090 - All scope sends no item-type filter
|
||||
#[test]
|
||||
fn all_scope_sends_no_filter() {
|
||||
assert_eq!(SearchScope::All.item_types(), None);
|
||||
}
|
||||
|
||||
/// Scope wins over an explicitly supplied include_item_types.
|
||||
///
|
||||
/// @req-test: UT-091 - Scope takes precedence over include_item_types
|
||||
#[test]
|
||||
fn resolve_scope_overrides_include_item_types() {
|
||||
let mut options = SearchOptions {
|
||||
include_item_types: Some(vec!["Movie".to_string()]),
|
||||
scope: Some(SearchScope::Music),
|
||||
..Default::default()
|
||||
};
|
||||
options.resolve_scope();
|
||||
|
||||
assert_eq!(
|
||||
options.include_item_types,
|
||||
Some(vec![
|
||||
"MusicAlbum".to_string(),
|
||||
"MusicArtist".to_string(),
|
||||
"Audio".to_string(),
|
||||
"Playlist".to_string(),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/// `All` clears any include_item_types so no filter reaches the query.
|
||||
///
|
||||
/// @req-test: UT-090 - All scope sends no item-type filter
|
||||
#[test]
|
||||
fn resolve_all_scope_clears_include_item_types() {
|
||||
let mut options = SearchOptions {
|
||||
include_item_types: Some(vec!["Movie".to_string()]),
|
||||
scope: Some(SearchScope::All),
|
||||
..Default::default()
|
||||
};
|
||||
options.resolve_scope();
|
||||
|
||||
assert_eq!(options.include_item_types, None);
|
||||
}
|
||||
|
||||
/// With no scope set, include_item_types passes through untouched — the
|
||||
/// non-search `getItems` callers rely on this.
|
||||
///
|
||||
/// @req-test: UT-091 - Scope takes precedence over include_item_types
|
||||
#[test]
|
||||
fn resolve_without_scope_preserves_include_item_types() {
|
||||
let mut options = SearchOptions {
|
||||
include_item_types: Some(vec!["MusicAlbum".to_string()]),
|
||||
scope: None,
|
||||
..Default::default()
|
||||
};
|
||||
options.resolve_scope();
|
||||
|
||||
assert_eq!(
|
||||
options.include_item_types,
|
||||
Some(vec!["MusicAlbum".to_string()])
|
||||
);
|
||||
}
|
||||
|
||||
/// The frontend sends the enum as camelCase over IPC.
|
||||
///
|
||||
/// @req-test: UT-089 - SearchScope expands to Jellyfin item types
|
||||
#[test]
|
||||
fn scope_deserializes_from_camel_case() {
|
||||
let options: SearchOptions =
|
||||
serde_json::from_str(r#"{"scope": "music", "limit": 10}"#).unwrap();
|
||||
assert!(matches!(options.scope, Some(SearchScope::Music)));
|
||||
|
||||
let all: SearchOptions = serde_json::from_str(r#"{"scope": "all"}"#).unwrap();
|
||||
assert!(matches!(all.scope, Some(SearchScope::All)));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user