Add bookmark metadata updates

This commit is contained in:
2026-05-08 06:03:43 -04:00
parent a236e6a53b
commit 26d0e4346c
4 changed files with 160 additions and 3 deletions
+6 -1
View File
@@ -1,4 +1,6 @@
use ely_domain::{DomainError, DownloadId, PluginId, ProfileId, SpaceId, SplitId, TabId};
use ely_domain::{
BookmarkId, DomainError, DownloadId, PluginId, ProfileId, SpaceId, SplitId, TabId,
};
use thiserror::Error;
#[derive(Clone, Debug, Error, Eq, PartialEq)]
@@ -9,6 +11,9 @@ pub enum CoreError {
#[error("tab not found: {id}")]
TabNotFound { id: TabId },
#[error("bookmark not found: {id}")]
BookmarkNotFound { id: BookmarkId },
#[error("space not found: {id}")]
SpaceNotFound { id: SpaceId },
@@ -31,6 +31,38 @@ impl BrowserCore {
Ok(bookmark_id)
}
pub fn set_bookmark_collection_name(
&mut self,
bookmark_id: &BookmarkId,
collection_name: impl Into<String>,
) -> Result<(), CoreError> {
self.bookmark_mut(bookmark_id)?.set_collection_name(collection_name)?;
Ok(())
}
pub fn set_bookmark_tags(
&mut self,
bookmark_id: &BookmarkId,
tags: Vec<String>,
) -> Result<(), CoreError> {
self.bookmark_mut(bookmark_id)?.set_tags(tags)?;
Ok(())
}
pub fn set_bookmark_note(
&mut self,
bookmark_id: &BookmarkId,
note: impl Into<String>,
) -> Result<(), CoreError> {
self.bookmark_mut(bookmark_id)?.set_note(note)?;
Ok(())
}
pub fn clear_bookmark_note(&mut self, bookmark_id: &BookmarkId) -> Result<(), CoreError> {
self.bookmark_mut(bookmark_id)?.clear_note();
Ok(())
}
pub(super) fn find_bookmark_match(&self, query: &str) -> Option<UrlText> {
let normalized_query = query.trim().to_lowercase();
if normalized_query.is_empty() {
@@ -52,6 +84,13 @@ impl BrowserCore {
.cloned()
.collect()
}
fn bookmark_mut(&mut self, bookmark_id: &BookmarkId) -> Result<&mut BookmarkEntry, CoreError> {
self.bookmarks
.iter_mut()
.find(|bookmark| bookmark.id() == bookmark_id)
.ok_or_else(|| CoreError::BookmarkNotFound { id: bookmark_id.clone() })
}
}
fn bookmark_matches_query(bookmark: &BookmarkEntry, normalized_query: &str) -> bool {
+89 -2
View File
@@ -1,7 +1,7 @@
use std::error::Error;
use ely_browser_core::{BrowserCore, InitialBrowserConfig};
use ely_domain::{CommandIntent, CommandScope, ProfileKind, UrlText};
use ely_browser_core::{BrowserCore, CoreError, InitialBrowserConfig};
use ely_domain::{BookmarkId, CommandIntent, CommandScope, DomainError, ProfileKind, UrlText};
#[test]
fn bookmark_active_tab_records_current_context() -> Result<(), Box<dyn Error>> {
@@ -41,6 +41,68 @@ fn bookmark_active_tab_reuses_existing_bookmark() -> Result<(), Box<dyn Error>>
Ok(())
}
#[test]
fn bookmark_metadata_updates_collection_tags_and_note() -> Result<(), Box<dyn Error>> {
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
core.open_tab(UrlText::parse("https://example.com/research")?);
let bookmark_id = core.bookmark_active_tab()?;
core.set_bookmark_collection_name(&bookmark_id, "Research")?;
core.set_bookmark_tags(&bookmark_id, vec![" rust ".to_string(), "gpui".to_string()])?;
core.set_bookmark_note(&bookmark_id, " Read with Servo notes ")?;
let snapshot = core.snapshot()?;
let [bookmark] = snapshot.bookmarks.as_slice() else {
return Err(format!("expected 1 bookmark, got {}", snapshot.bookmarks.len()).into());
};
assert_eq!(bookmark.collection_name(), "Research");
assert_eq!(bookmark.tags(), &["rust".to_string(), "gpui".to_string()]);
assert_eq!(bookmark.note(), Some("Read with Servo notes"));
core.clear_bookmark_note(&bookmark_id)?;
let snapshot = core.snapshot()?;
assert_eq!(snapshot.bookmarks[0].note(), None);
Ok(())
}
#[test]
fn bookmark_metadata_rejects_empty_fields() -> Result<(), Box<dyn Error>> {
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
core.open_tab(UrlText::parse("https://example.com/research")?);
let bookmark_id = core.bookmark_active_tab()?;
let Err(collection_error) = core.set_bookmark_collection_name(&bookmark_id, " ") else {
return Err("expected empty bookmark collection error".into());
};
assert_eq!(
collection_error,
CoreError::Domain(DomainError::EmptyField { field: "bookmark collection" })
);
let Err(tag_error) =
core.set_bookmark_tags(&bookmark_id, vec!["rust".to_string(), " ".to_string()])
else {
return Err("expected empty bookmark tag error".into());
};
assert_eq!(tag_error, CoreError::Domain(DomainError::EmptyField { field: "bookmark tag" }));
let Err(note_error) = core.set_bookmark_note(&bookmark_id, " ") else {
return Err("expected empty bookmark note error".into());
};
assert_eq!(note_error, CoreError::Domain(DomainError::EmptyField { field: "bookmark note" }));
Ok(())
}
#[test]
fn bookmark_metadata_requires_known_bookmark() -> Result<(), Box<dyn Error>> {
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
let unknown_bookmark_id = BookmarkId::new();
let Err(bookmark_error) = core.set_bookmark_note(&unknown_bookmark_id, "Read later") else {
return Err("expected unknown bookmark error".into());
};
assert_eq!(bookmark_error, CoreError::BookmarkNotFound { id: unknown_bookmark_id });
Ok(())
}
#[test]
fn bookmarks_scoped_search_opens_matching_bookmark() -> Result<(), Box<dyn Error>> {
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
@@ -64,6 +126,31 @@ fn bookmarks_scoped_search_opens_matching_bookmark() -> Result<(), Box<dyn Error
Ok(())
}
#[test]
fn bookmarks_scoped_search_matches_metadata() -> Result<(), Box<dyn Error>> {
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;
core.open_tab(UrlText::parse("https://example.com/research")?);
let bookmark_id = core.bookmark_active_tab()?;
core.set_bookmark_collection_name(&bookmark_id, "Research")?;
core.set_bookmark_tags(&bookmark_id, vec!["gpui".to_string()])?;
core.set_bookmark_note(&bookmark_id, "Servo embed reference")?;
core.set_command_query("@bookmarks gpui");
let intent = core.submit_command()?;
let active_tab = core.active_tab()?;
assert_eq!(
intent,
Some(CommandIntent::ScopedSearch {
scope: CommandScope::Bookmarks,
query: "gpui".to_string()
})
);
assert_eq!(active_tab.url().as_str(), "https://example.com/research");
Ok(())
}
#[test]
fn bookmarks_stay_with_active_profile() -> Result<(), Box<dyn Error>> {
let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;