Skip to content

Commit ef7e55b

Browse files
committed
test: more granular tests
1 parent 104de7d commit ef7e55b

File tree

8 files changed

+132
-0
lines changed

8 files changed

+132
-0
lines changed

apps/desktop/src-tauri/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/desktop/src-tauri/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ percent-encoding = "2"
3535
tauri-plugin-os = "2.3.1"
3636
tauri-plugin-store = "2.4.0"
3737

38+
[dev-dependencies]
39+
tempfile = "3.13.0"
40+
3841
[target.'cfg(target_os = "windows")'.dependencies]
3942
winreg = "0.52"
4043

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#![cfg(target_os = "windows")]
2+
3+
use desktop_lib::browser_details::{get_chrome_profiles, Browsers};
4+
use std::fs;
5+
use std::io::Write;
6+
use std::path::PathBuf;
7+
use tempfile::TempDir;
8+
9+
fn write_local_state(temp: &TempDir, relative: &str, contents: &str) -> PathBuf {
10+
let mut path = temp.path().to_path_buf();
11+
path.push(relative);
12+
if let Some(parent) = path.parent() {
13+
fs::create_dir_all(parent).expect("failed to create Local State parent");
14+
}
15+
let mut file = fs::File::create(&path).expect("failed to create Local State file");
16+
file.write_all(contents.as_bytes())
17+
.expect("failed to write Local State");
18+
path
19+
}
20+
21+
fn install_local_state(temp: &TempDir, browser: Browsers, json: &str) {
22+
let relative = match browser {
23+
Browsers::Chrome => "Google/Chrome/User Data/Local State",
24+
Browsers::Edge => "Microsoft/Edge/User Data/Local State",
25+
Browsers::Brave => "BraveSoftware/Brave-Browser/User Data/Local State",
26+
_ => panic!("unexpected browser for chrome profile test"),
27+
};
28+
write_local_state(temp, relative, json);
29+
}
30+
31+
fn with_localappdata<F: FnOnce()>(temp: &TempDir, f: F) {
32+
use std::env;
33+
let original = env::var_os("LOCALAPPDATA");
34+
env::set_var("LOCALAPPDATA", temp.path());
35+
f();
36+
match original {
37+
Some(val) => env::set_var("LOCALAPPDATA", val),
38+
None => env::remove_var("LOCALAPPDATA"),
39+
}
40+
}
41+
42+
#[test]
43+
fn reads_profiles_and_adds_default_when_missing() {
44+
let temp = TempDir::new().expect("temp dir");
45+
let local_state = r#"{
46+
"profile": {
47+
"info_cache": {
48+
"Profile 1": {
49+
"profile_dir": "Profile 1",
50+
"name": "Personal"
51+
},
52+
"Profile 2": {
53+
"gaia_name": "Work"
54+
}
55+
}
56+
}
57+
}"#;
58+
install_local_state(&temp, Browsers::Chrome, local_state);
59+
60+
with_localappdata(&temp, || {
61+
let profiles = get_chrome_profiles(Browsers::Chrome).expect("profiles");
62+
let labels: Vec<_> = profiles.iter().map(|p| &p.display_name).collect();
63+
assert!(labels.contains(&"Personal".to_string()));
64+
assert!(labels.contains(&"Work".to_string()));
65+
assert!(labels.contains(&"Default".to_string()));
66+
});
67+
}
68+
69+
#[test]
70+
fn deduplicates_profile_directories() {
71+
let temp = TempDir::new().expect("temp dir");
72+
let local_state = r#"{
73+
"profile": {
74+
"info_cache": {
75+
"Profile 1": {
76+
"profile_dir": "Profile 1",
77+
"name": "Personal"
78+
},
79+
"Duplicate": {
80+
"profile_dir": "Profile 1",
81+
"name": "Duplicate"
82+
}
83+
}
84+
}
85+
}"#;
86+
install_local_state(&temp, Browsers::Chrome, local_state);
87+
88+
with_localappdata(&temp, || {
89+
let profiles = get_chrome_profiles(Browsers::Chrome).expect("profiles");
90+
let dirs: Vec<_> = profiles.iter().map(|p| &p.directory).collect();
91+
assert_eq!(dirs.iter().filter(|d| d.as_str() == "Profile 1").count(), 1);
92+
});
93+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![cfg(target_os = "windows")]
2+
3+
use desktop_lib::browser_details::get_firefox_profiles;
4+
use std::fs;
5+
use tempfile::TempDir;
6+
7+
fn with_localappdata<F: FnOnce()>(temp: &TempDir, f: F) {
8+
use std::env;
9+
let original = env::var_os("LOCALAPPDATA");
10+
env::set_var("LOCALAPPDATA", temp.path());
11+
f();
12+
match original {
13+
Some(val) => env::set_var("LOCALAPPDATA", val),
14+
None => env::remove_var("LOCALAPPDATA"),
15+
}
16+
}
17+
18+
#[test]
19+
fn discovers_profile_directories() {
20+
let temp = TempDir::new().expect("temp dir");
21+
let base = temp.path().join("Mozilla/Firefox/Profiles");
22+
fs::create_dir_all(base.join("abcd.default-release")).expect("create profile dir");
23+
fs::create_dir_all(base.join("custom.work" )).expect("create profile dir");
24+
// include a file to ensure non-dirs skipped
25+
fs::write(base.join("not_a_dir"), b"noop").expect("create dummy file");
26+
27+
with_localappdata(&temp, || {
28+
let mut profiles = get_firefox_profiles().expect("profiles");
29+
profiles.sort_by(|a, b| a.display_name.cmp(&b.display_name));
30+
let names: Vec<_> = profiles.iter().map(|p| &p.display_name).collect();
31+
assert!(names.contains(&"abcd.default-release".to_string()));
32+
assert!(names.contains(&"custom.work".to_string()));
33+
assert_eq!(profiles.len(), 2);
34+
});
35+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)