chore: linting for both js and rs

This commit is contained in:
zhom
2025-05-29 23:57:54 +04:00
parent cf57b2a043
commit 5c02b59af4
33 changed files with 836 additions and 608 deletions
+15 -16
View File
@@ -54,7 +54,7 @@ impl VersionComponent {
.filter_map(|part| part.parse().ok())
.collect();
let major = parts.get(0).copied().unwrap_or(0);
let major = parts.first().copied().unwrap_or(0);
let minor = parts.get(1).copied().unwrap_or(0);
let patch = parts.get(2).copied().unwrap_or(0);
@@ -103,31 +103,31 @@ impl VersionComponent {
}
// Extract kind and number
let (kind, number) = if pre_release.starts_with("alpha") {
let (kind, number) = if let Some(stripped) = pre_release.strip_prefix("alpha") {
(
PreReleaseKind::Alpha,
Self::extract_number(&pre_release[5..]),
Self::extract_number(stripped),
)
} else if pre_release.starts_with("beta") {
} else if let Some(stripped) = pre_release.strip_prefix("beta") {
(
PreReleaseKind::Beta,
Self::extract_number(&pre_release[4..]),
Self::extract_number(stripped),
)
} else if pre_release.starts_with("rc") {
(PreReleaseKind::RC, Self::extract_number(&pre_release[2..]))
} else if pre_release.starts_with("dev") {
(PreReleaseKind::Dev, Self::extract_number(&pre_release[3..]))
} else if pre_release.starts_with("pre") {
(PreReleaseKind::Pre, Self::extract_number(&pre_release[3..]))
} else if pre_release.starts_with('a') {
} else if let Some(stripped) = pre_release.strip_prefix("rc") {
(PreReleaseKind::RC, Self::extract_number(stripped))
} else if let Some(stripped) = pre_release.strip_prefix("dev") {
(PreReleaseKind::Dev, Self::extract_number(stripped))
} else if let Some(stripped) = pre_release.strip_prefix("pre") {
(PreReleaseKind::Pre, Self::extract_number(stripped))
} else if let Some(stripped) = pre_release.strip_prefix('a') {
(
PreReleaseKind::Alpha,
Self::extract_number(&pre_release[1..]),
Self::extract_number(stripped),
)
} else if pre_release.starts_with('b') {
} else if let Some(stripped) = pre_release.strip_prefix('b') {
(
PreReleaseKind::Beta,
Self::extract_number(&pre_release[1..]),
Self::extract_number(stripped),
)
} else {
return None;
@@ -903,7 +903,6 @@ impl ApiClient {
#[cfg(test)]
mod tests {
use super::*;
use tokio;
#[test]
fn test_version_parsing() {
+3 -15
View File
@@ -17,7 +17,7 @@ pub struct UpdateNotification {
pub timestamp: u64,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct AutoUpdateState {
pub pending_updates: Vec<UpdateNotification>,
pub disabled_browsers: HashSet<String>, // browsers disabled during update
@@ -26,17 +26,6 @@ pub struct AutoUpdateState {
pub last_check_timestamp: u64,
}
impl Default for AutoUpdateState {
fn default() -> Self {
Self {
pending_updates: Vec::new(),
disabled_browsers: HashSet::new(),
auto_update_downloads: HashSet::new(),
last_check_timestamp: 0,
}
}
}
pub struct AutoUpdater {
version_service: BrowserVersionService,
browser_runner: BrowserRunner,
@@ -77,7 +66,7 @@ impl AutoUpdater {
for profile in profiles {
browser_profiles
.entry(profile.browser.clone())
.or_insert_with(Vec::new)
.or_default()
.push(profile);
}
@@ -132,7 +121,7 @@ impl AutoUpdater {
// Only consider versions newer than current
self.is_version_newer(&v.version, current_version) &&
// Respect version type preference
(is_current_stable == !v.is_prerelease || !is_current_stable)
is_current_stable != v.is_prerelease
})
.max_by(|a, b| self.compare_versions(&a.version, &b.version));
@@ -843,7 +832,6 @@ mod tests {
std::fs::write(&state_file, json).unwrap();
// Dismiss notification (remove from pending updates)
let mut state = state;
state
.pending_updates
.retain(|n| n.id != "test_notification");
+18 -23
View File
@@ -80,7 +80,7 @@ impl Browser for FirefoxBrowser {
// Find the .app directory
let app_path = std::fs::read_dir(install_dir)?
.filter_map(Result::ok)
.find(|entry| entry.path().extension().map_or(false, |ext| ext == "app"))
.find(|entry| entry.path().extension().is_some_and(|ext| ext == "app"))
.ok_or("Browser app not found")?;
// Construct the browser executable path
@@ -148,13 +148,11 @@ impl Browser for FirefoxBrowser {
if browser_dir.exists() {
println!("Directory exists, checking for .app files...");
if let Ok(entries) = std::fs::read_dir(&browser_dir) {
for entry in entries {
if let Ok(entry) = entry {
println!(" Found entry: {:?}", entry.path());
if entry.path().extension().map_or(false, |ext| ext == "app") {
println!(" Found .app file: {:?}", entry.path());
return true;
}
for entry in entries.flatten() {
println!(" Found entry: {:?}", entry.path());
if entry.path().extension().is_some_and(|ext| ext == "app") {
println!(" Found .app file: {:?}", entry.path());
return true;
}
}
}
@@ -186,7 +184,7 @@ impl Browser for ChromiumBrowser {
// Find the .app directory
let app_path = std::fs::read_dir(install_dir)?
.filter_map(Result::ok)
.find(|entry| entry.path().extension().map_or(false, |ext| ext == "app"))
.find(|entry| entry.path().extension().is_some_and(|ext| ext == "app"))
.ok_or("Browser app not found")?;
// Construct the browser executable path
@@ -226,7 +224,6 @@ impl Browser for ChromiumBrowser {
// Add proxy configuration if provided
if let Some(proxy) = proxy_settings {
if proxy.enabled {
// Read PAC file and encode it as base64
let pac_path = Path::new(profile_path).join("proxy.pac");
if pac_path.exists() {
let pac_content = fs::read(&pac_path)?;
@@ -260,18 +257,16 @@ impl Browser for ChromiumBrowser {
if browser_dir.exists() {
println!("Directory exists, checking for .app files...");
if let Ok(entries) = std::fs::read_dir(&browser_dir) {
for entry in entries {
if let Ok(entry) = entry {
println!(" Found entry: {:?}", entry.path());
if entry.path().extension().map_or(false, |ext| ext == "app") {
println!(" Found .app file: {:?}", entry.path());
// Try to get the executable path as a final verification
if self.get_executable_path(&browser_dir).is_ok() {
println!(" Executable path verification successful");
return true;
} else {
println!(" Executable path verification failed");
}
for entry in entries.flatten() {
println!(" Found entry: {:?}", entry.path());
if entry.path().extension().is_some_and(|ext| ext == "app") {
println!(" Found .app file: {:?}", entry.path());
// Try to get the executable path as a final verification
if self.get_executable_path(&browser_dir).is_ok() {
println!(" Executable path verification successful");
return true;
} else {
println!(" Executable path verification failed");
}
}
}
@@ -535,7 +530,7 @@ mod tests {
let chromium_dir = binaries_dir.join("chromium").join("1465660");
fs::create_dir_all(&chromium_dir).unwrap();
let chromium_app_dir = chromium_dir.join("Chromium.app");
fs::create_dir_all(&chromium_app_dir.join("Contents").join("MacOS")).unwrap();
fs::create_dir_all(chromium_app_dir.join("Contents").join("MacOS")).unwrap();
// Create a mock executable
let executable_path = chromium_app_dir
+25 -25
View File
@@ -92,8 +92,8 @@ impl BrowserRunner {
// Check if this is the correct browser type
let is_correct_browser = match profile.browser.as_str() {
"mullvad-browser" => self.is_tor_or_mullvad_browser(&exe_name, &cmd, "mullvad-browser"),
"tor-browser" => self.is_tor_or_mullvad_browser(&exe_name, &cmd, "tor-browser"),
"mullvad-browser" => self.is_tor_or_mullvad_browser(&exe_name, cmd, "mullvad-browser"),
"tor-browser" => self.is_tor_or_mullvad_browser(&exe_name, cmd, "tor-browser"),
_ => return false,
};
@@ -108,12 +108,12 @@ impl BrowserRunner {
// Check profile path match
let profile_path_match = cmd.iter().any(|s| {
let arg = s.to_str().unwrap_or("");
arg == &profile.profile_path
arg == profile.profile_path
|| arg == format!("-profile={}", profile.profile_path)
|| (arg == "-profile"
&& cmd
.iter()
.any(|s2| s2.to_str().unwrap_or("") == &profile.profile_path))
.any(|s2| s2.to_str().unwrap_or("") == profile.profile_path))
});
if !profile_path_match {
@@ -128,10 +128,10 @@ impl BrowserRunner {
"PID {} validated successfully for {} profile {}",
pid, profile.browser, profile.name
);
return true;
true
} else {
println!("PID {} does not exist", pid);
return false;
false
}
}
pub fn get_binaries_dir(&self) -> PathBuf {
@@ -436,7 +436,7 @@ impl BrowserRunner {
let entry = entry?;
let path = entry.path();
if path.extension().map_or(false, |ext| ext == "json") {
if path.extension().is_some_and(|ext| ext == "json") {
let content = fs::read_to_string(path)?;
let profile: BrowserProfile = serde_json::from_str(&content)?;
profiles.push(profile);
@@ -505,9 +505,9 @@ impl BrowserRunner {
let exe_name = process.name().to_string_lossy().to_lowercase();
let is_correct_browser = match profile.browser.as_str() {
"mullvad-browser" => {
self.is_tor_or_mullvad_browser(&exe_name, &cmd, "mullvad-browser")
self.is_tor_or_mullvad_browser(&exe_name, cmd, "mullvad-browser")
}
"tor-browser" => self.is_tor_or_mullvad_browser(&exe_name, &cmd, "tor-browser"),
"tor-browser" => self.is_tor_or_mullvad_browser(&exe_name, cmd, "tor-browser"),
_ => false,
};
@@ -518,12 +518,12 @@ impl BrowserRunner {
// Check for profile path match
let profile_path_match = cmd.iter().any(|s| {
let arg = s.to_str().unwrap_or("");
arg == &profile.profile_path
arg == profile.profile_path
|| arg == format!("-profile={}", profile.profile_path)
|| (arg == "-profile"
&& cmd
.iter()
.any(|s2| s2.to_str().unwrap_or("") == &profile.profile_path))
.any(|s2| s2.to_str().unwrap_or("") == profile.profile_path))
});
if profile_path_match {
@@ -1200,10 +1200,10 @@ end try
// and can't have multiple instances with the same profile
match final_profile.browser.as_str() {
"mullvad-browser" | "tor-browser" => {
return Err(format!(
Err(format!(
"Failed to open URL in existing {} browser. Cannot launch new instance due to profile conflict: {}",
final_profile.browser, e
).into());
).into())
}
_ => {
println!(
@@ -1375,16 +1375,16 @@ end try
|| profile.browser == "mullvad-browser"
|| profile.browser == "zen"
{
arg == &profile.profile_path
arg == profile.profile_path
|| arg == format!("-profile={}", profile.profile_path)
|| (arg == "-profile"
&& cmd
.iter()
.any(|s2| s2.to_str().unwrap_or("") == &profile.profile_path))
.any(|s2| s2.to_str().unwrap_or("") == profile.profile_path))
} else {
// For Chromium-based browsers, check for user-data-dir
arg.contains(&format!("--user-data-dir={}", profile.profile_path))
|| arg == &profile.profile_path
|| arg == profile.profile_path
}
});
@@ -1421,8 +1421,8 @@ end try
&& !exe_name.contains("mullvad")
}
"firefox-developer" => exe_name.contains("firefox") && exe_name.contains("developer"),
"mullvad-browser" => self.is_tor_or_mullvad_browser(&exe_name, &cmd, "mullvad-browser"),
"tor-browser" => self.is_tor_or_mullvad_browser(&exe_name, &cmd, "tor-browser"),
"mullvad-browser" => self.is_tor_or_mullvad_browser(&exe_name, cmd, "mullvad-browser"),
"tor-browser" => self.is_tor_or_mullvad_browser(&exe_name, cmd, "tor-browser"),
"zen" => exe_name.contains("zen"),
"chromium" => exe_name.contains("chromium"),
"brave" => exe_name.contains("brave"),
@@ -1443,16 +1443,16 @@ end try
|| profile.browser == "mullvad-browser"
|| profile.browser == "zen"
{
arg == &profile.profile_path
arg == profile.profile_path
|| arg == format!("-profile={}", profile.profile_path)
|| (arg == "-profile"
&& cmd
.iter()
.any(|s2| s2.to_str().unwrap_or("") == &profile.profile_path))
.any(|s2| s2.to_str().unwrap_or("") == profile.profile_path))
} else {
// For Chromium-based browsers, check for user-data-dir
arg.contains(&format!("--user-data-dir={}", profile.profile_path))
|| arg == &profile.profile_path
|| arg == profile.profile_path
}
});
@@ -1572,8 +1572,8 @@ end try
&& !exe_name.contains("mullvad")
}
"firefox-developer" => exe_name.contains("firefox") && exe_name.contains("developer"),
"mullvad-browser" => self.is_tor_or_mullvad_browser(&exe_name, &cmd, "mullvad-browser"),
"tor-browser" => self.is_tor_or_mullvad_browser(&exe_name, &cmd, "tor-browser"),
"mullvad-browser" => self.is_tor_or_mullvad_browser(&exe_name, cmd, "mullvad-browser"),
"tor-browser" => self.is_tor_or_mullvad_browser(&exe_name, cmd, "tor-browser"),
"zen" => exe_name.contains("zen"),
"chromium" => exe_name.contains("chromium"),
"brave" => exe_name.contains("brave"),
@@ -1594,11 +1594,11 @@ end try
|| profile.browser == "mullvad-browser"
|| profile.browser == "zen"
{
arg == &profile.profile_path || arg == format!("-profile={}", profile.profile_path)
arg == profile.profile_path || arg == format!("-profile={}", profile.profile_path)
} else {
// For Chromium-based browsers, check for user-data-dir
arg.contains(&format!("--user-data-dir={}", profile.profile_path))
|| arg == &profile.profile_path
|| arg == profile.profile_path
}
});
-1
View File
@@ -551,7 +551,6 @@ mod tests {
async fn test_browser_version_service_creation() {
let _service = BrowserVersionService::new();
// Test passes if we can create the service without panicking
assert!(true);
}
#[tokio::test]
+2 -3
View File
@@ -180,8 +180,8 @@ impl Downloader {
} else {
0.0
};
let eta = if speed > 0.0 && total_size.is_some() {
Some((total_size.unwrap() - downloaded) as f64 / speed)
let eta = if speed > 0.0 {
total_size.map(|total| (total - downloaded) as f64 / speed)
} else {
None
};
@@ -209,7 +209,6 @@ impl Downloader {
#[cfg(test)]
mod tests {
use super::*;
use tokio;
#[tokio::test]
async fn test_resolve_brave_download_url() {
+1 -1
View File
@@ -66,7 +66,7 @@ impl DownloadedBrowsersRegistry {
self
.browsers
.entry(info.browser.clone())
.or_insert_with(HashMap::new)
.or_default()
.insert(info.version.clone(), info);
}
+32 -41
View File
@@ -85,7 +85,7 @@ impl Extractor {
// Find the .app directory in the mount point
let app_entry = fs::read_dir(&mount_point)?
.filter_map(Result::ok)
.find(|entry| entry.path().extension().map_or(false, |ext| ext == "app"))
.find(|entry| entry.path().extension().is_some_and(|ext| ext == "app"))
.ok_or("No .app found in DMG")?;
// Copy the .app to the destination
@@ -179,35 +179,31 @@ impl Extractor {
// First, try to find any .app file in the destination directory
if let Ok(entries) = fs::read_dir(dest_dir) {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if path.extension().map_or(false, |ext| ext == "app") {
app_path = Some(path);
break;
}
// For Chromium, check subdirectories (chrome-mac folder)
if path.is_dir() {
if let Ok(sub_entries) = fs::read_dir(&path) {
for sub_entry in sub_entries {
if let Ok(sub_entry) = sub_entry {
let sub_path = sub_entry.path();
if sub_path.extension().map_or(false, |ext| ext == "app") {
// Move the app to the root destination directory
let target_path = dest_dir.join(sub_path.file_name().unwrap());
fs::rename(&sub_path, &target_path)?;
app_path = Some(target_path);
for entry in entries.flatten() {
let path = entry.path();
if path.extension().is_some_and(|ext| ext == "app") {
app_path = Some(path);
break;
}
// For Chromium, check subdirectories (chrome-mac folder)
if path.is_dir() {
if let Ok(sub_entries) = fs::read_dir(&path) {
for sub_entry in sub_entries.flatten() {
let sub_path = sub_entry.path();
if sub_path.extension().is_some_and(|ext| ext == "app") {
// Move the app to the root destination directory
let target_path = dest_dir.join(sub_path.file_name().unwrap());
fs::rename(&sub_path, &target_path)?;
app_path = Some(target_path);
// Clean up the now-empty subdirectory
let _ = fs::remove_dir_all(&path);
break;
}
}
}
if app_path.is_some() {
// Clean up the now-empty subdirectory
let _ = fs::remove_dir_all(&path);
break;
}
}
if app_path.is_some() {
break;
}
}
}
}
@@ -238,7 +234,6 @@ mod tests {
fn test_extractor_creation() {
let _extractor = Extractor::new();
// Just verify we can create an extractor instance
assert!(true);
}
#[test]
@@ -327,7 +322,7 @@ mod tests {
let entries: Vec<_> = fs::read_dir(temp_dir.path())
.unwrap()
.filter_map(Result::ok)
.filter(|entry| entry.path().extension().map_or(false, |ext| ext == "app"))
.filter(|entry| entry.path().extension().is_some_and(|ext| ext == "app"))
.collect();
assert_eq!(entries.len(), 1);
@@ -349,19 +344,15 @@ mod tests {
let mut found_app = false;
if let Ok(entries) = fs::read_dir(temp_dir.path()) {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_dir() {
if let Ok(sub_entries) = fs::read_dir(&path) {
for sub_entry in sub_entries {
if let Ok(sub_entry) = sub_entry {
let sub_path = sub_entry.path();
if sub_path.extension().map_or(false, |ext| ext == "app") {
found_app = true;
break;
}
}
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
if let Ok(sub_entries) = fs::read_dir(&path) {
for sub_entry in sub_entries.flatten() {
let sub_path = sub_entry.path();
if sub_path.extension().is_some_and(|ext| ext == "app") {
found_app = true;
break;
}
}
}
+2 -6
View File
@@ -259,7 +259,7 @@ impl VersionUpdater {
) -> Result<Vec<BackgroundUpdateResult>, Box<dyn std::error::Error + Send + Sync>> {
println!("Starting background version update for all browsers");
let browsers = vec![
let browsers = [
"firefox",
"firefox-developer",
"mullvad-browser",
@@ -416,11 +416,7 @@ impl VersionUpdater {
let elapsed = current_time.saturating_sub(state.last_update_time);
let update_interval_secs = state.update_interval_hours * 60 * 60;
if elapsed >= update_interval_secs {
0 // Update overdue
} else {
update_interval_secs - elapsed
}
update_interval_secs.saturating_sub(elapsed)
}
}
}