test: Add unit tests for config merging and project name sanitization (#6)

* test: Add unit tests for config merging and project name sanitization

* Update src/utils/project.rs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* test: Update assertion for follow_symlinks in scanner configuration

* test: Fix typo in test function name for project info retrieval

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Eli Peter 2025-06-24 23:18:01 +02:00 committed by GitHub
parent a0c9d0f9d4
commit 8497800b13
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 140 additions and 1 deletions

View file

@ -282,3 +282,42 @@ fn merge_configs(mut default: Config, user: Config) -> Config {
default
}
#[test]
fn merge_configs_dedupes_and_keeps_order() {
let mut default_cfg = Config::default();
default_cfg.scanner.excluded_extensions = vec!["rs".into(), "toml".into()];
let mut user_cfg = Config::default();
user_cfg.scanner.excluded_extensions = vec!["jpg".into(), "rs".into()];
let merged = merge_configs(default_cfg, user_cfg);
assert_eq!(merged.scanner.excluded_extensions, vec!["jpg", "rs", "toml"]);
}
#[test]
fn load_creates_example_and_reads_user_overrides() {
let cfg_dir = tempfile::tempdir().unwrap();
let cfg_path = cfg_dir.path();
let user_toml = r#"
[scanner]
one_file_system = true
excluded_extensions = ["foo"]
[output]
quiet = true
"#;
fs::write(cfg_path.join("nyx.local"), user_toml).unwrap();
let cfg = Config::load(cfg_path).expect("Config::load should succeed");
assert!(cfg_path.join("nyx.conf").is_file());
assert!(cfg.scanner.one_file_system);
assert!(cfg.output.quiet);
assert!(cfg.scanner.excluded_extensions.contains(&"foo".to_string()));
assert!(!cfg.scanner.follow_symlinks);
}

View file

@ -12,3 +12,19 @@ pub fn lowercase_ext(path: &std::path::Path) -> Option<&'static str> {
_ => None,
})
}
#[test]
fn lowercase_ext_recognises_known_extensions() {
let cases = [
("file.rs", Some("rs")),
("FILE.RS", Some("rs")),
("main.cpp", Some("cpp")),
("script.PY",Some("py")),
("index.tsx",Some("ts")),
("style.css",None), // unsupported
];
for (file, expected) in cases {
assert_eq!(lowercase_ext(std::path::Path::new(file)), expected, "case: {file}");
}
}

View file

@ -4,5 +4,4 @@ pub mod project;
pub(crate) mod query_cache;
pub use config::Config;
// Re-export commonly used functions for convenience
pub use project::get_project_info;

View file

@ -28,3 +28,36 @@ pub fn sanitize_project_name(name: &str) -> String {
.collect::<Vec<_>>()
.join("_")
}
#[test]
fn sanitize_project_name_is_idempotent_and_lossless_enough() {
let samples = [
("My Project", "my_project"),
("Hello-World", "hello-world"),
("mixed_case", "mixed_case"),
("tabs\tspaces\n", "tabs_spaces"),
(" multiple ", "multiple"),
("weird@$*chars", "weird_chars"),
];
for (input, expected) in samples {
assert_eq!(sanitize_project_name(input), expected, "input: {}", input);
assert_eq!(sanitize_project_name(expected), expected);
}
}
#[test]
fn get_project_info_uses_sanitized_name_in_sqlite_path() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let project_dir = root.join("Example Project");
std::fs::create_dir(&project_dir).unwrap();
let (project_name, db_path) =
get_project_info(&project_dir, root).expect("should detect project");
assert_eq!(project_name, "Example Project");
assert_eq!(db_path, root.join("example_project.sqlite"));
}