diff --git a/crates/ely_browser_core/src/state/downloads.rs b/crates/ely_browser_core/src/state/downloads.rs index 8023370..b71cf6d 100644 --- a/crates/ely_browser_core/src/state/downloads.rs +++ b/crates/ely_browser_core/src/state/downloads.rs @@ -81,6 +81,13 @@ impl BrowserCore { .ok_or_else(|| CoreError::DownloadTargetPathUnavailable { id: download_id.clone() }) } + pub fn clear_downloads_for_active_profile(&mut self) -> usize { + let active_profile_id = self.active_profile_id.clone(); + let before_count = self.download_entries.len(); + self.download_entries.retain(|entry| entry.profile_id() != &active_profile_id); + before_count - self.download_entries.len() + } + pub(super) fn visible_downloads(&self) -> Vec { self.download_entries .iter() diff --git a/crates/ely_browser_core/tests/downloads.rs b/crates/ely_browser_core/tests/downloads.rs index 15c727e..fff89ee 100644 --- a/crates/ely_browser_core/tests/downloads.rs +++ b/crates/ely_browser_core/tests/downloads.rs @@ -110,6 +110,36 @@ fn returns_visible_download_target_file_path() -> Result<(), Box> { Ok(()) } +#[test] +fn clears_downloads_for_active_profile() -> Result<(), Box> { + let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?; + let default_profile_id = core.active_tab()?.profile_id().clone(); + core.record_download_started( + UrlText::parse("https://example.com/report.pdf")?, + "report.pdf", + Some(2048), + )?; + + core.create_profile("Personal", 0xf54e00, ProfileKind::Standard)?; + core.record_download_started( + UrlText::parse("https://example.com/archive.zip")?, + "archive.zip", + Some(4096), + )?; + + assert_eq!(core.clear_downloads_for_active_profile(), 1); + assert!(core.snapshot()?.download_entries.is_empty()); + + core.select_profile(&default_profile_id)?; + let default_snapshot = core.snapshot()?; + assert_eq!(default_snapshot.download_entries.len(), 1); + assert_eq!(default_snapshot.download_entries[0].file_name(), "report.pdf"); + assert_eq!(core.clear_downloads_for_active_profile(), 1); + assert!(core.snapshot()?.download_entries.is_empty()); + assert_eq!(core.clear_downloads_for_active_profile(), 0); + Ok(()) +} + #[test] fn rejects_target_path_for_ask_every_time_download() -> Result<(), Box> { let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;