From 8d86f1904311f31d827366c6b5ec0cfdb089197f Mon Sep 17 00:00:00 2001 From: Sumit Datta Date: Tue, 8 Jul 2025 18:11:14 +0530 Subject: [PATCH] fix: prevent duplicate root URL loading (#58) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix URL normalization inconsistency that caused root URLs to be loaded twice when users provided URLs with trailing slashes. - Normalize constructed root URLs using url::Url::parse().to_string() to match CLI normalization - Add test case to verify construct_root_url() matches CLI normalization behavior - Ensure consistent URL comparison in duplicate detection logic Fixes issue where https://example.com/ and https://example.com were treated as different URLs. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/utils.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 55e4ad6..abb9048 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,7 +20,13 @@ pub fn extract_domain_from_url(url: &str) -> Option { } pub fn construct_root_url(domain: &str) -> String { - format!("https://{domain}") + let url_string = format!("https://{domain}"); + // Normalize the URL the same way CLI does to ensure consistency + if let Ok(parsed) = url::Url::parse(&url_string) { + parsed.to_string() + } else { + url_string + } } pub fn is_root_url(url: &str) -> bool { @@ -66,13 +72,28 @@ mod tests { #[test] fn test_construct_root_url() { - assert_eq!(construct_root_url("example.com"), "https://example.com"); + assert_eq!(construct_root_url("example.com"), "https://example.com/"); assert_eq!( construct_root_url("subdomain.example.com"), - "https://subdomain.example.com" + "https://subdomain.example.com/" ); } + #[test] + fn test_construct_root_url_matches_cli_normalization() { + // Test that construct_root_url produces URLs that match CLI normalization + // This prevents duplicate loading when user provides URLs with trailing slashes + let domain = "news.ycombinator.com"; + let constructed_root = construct_root_url(domain); + + // Simulate what CLI normalization does for user input with trailing slash + let user_input = "https://news.ycombinator.com/"; + let cli_normalized = url::Url::parse(user_input).unwrap().to_string(); + + assert_eq!(constructed_root, cli_normalized); + assert_eq!(constructed_root, "https://news.ycombinator.com/"); + } + #[test] fn test_is_root_url() { assert!(is_root_url("https://example.com"));