use super::*;
use std::fs::File;
use std::io::Write;
use tempfile::TempDir;
fn create_registry(dir: &Path, content: &str) {
let path = dir.join(".sea-registry.toml");
let mut file = File::create(path).unwrap();
file.write_all(content.as_bytes()).unwrap();
}
fn create_file(dir: &Path, path: &str) {
let full_path = dir.join(path);
std::fs::create_dir_all(full_path.parent().unwrap()).unwrap();
File::create(full_path).unwrap();
}
#[test]
fn test_exact_match_precedence() {
let temp = TempDir::new().unwrap();
create_registry(
temp.path(),
r#"
version = 1
[[namespaces]]
namespace = "glob"
patterns = ["domains/*.sea"]
[[namespaces]]
namespace = "exact"
patterns = ["domains/specific.sea"]
"#,
);
create_file(temp.path(), "domains/specific.sea");
create_file(temp.path(), "domains/other.sea");
let registry = NamespaceRegistry::discover(temp.path()).unwrap().unwrap();
assert_eq!(
registry.namespace_for(temp.path().join("domains/specific.sea")),
Some("exact")
);
assert_eq!(
registry.namespace_for(temp.path().join("domains/other.sea")),
Some("glob")
);
}
#[test]
fn test_overlapping_globs_longest_prefix() {
let temp = TempDir::new().unwrap();
create_registry(
temp.path(),
r#"
version = 1
[[namespaces]]
namespace = "general"
patterns = ["src/**/*.rs"]
[[namespaces]]
namespace = "specific"
patterns = ["src/tests/**/*.rs"]
"#,
);
create_file(temp.path(), "src/main.rs");
create_file(temp.path(), "src/tests/test_api.rs");
let registry = NamespaceRegistry::discover(temp.path()).unwrap().unwrap();
assert_eq!(
registry.namespace_for(temp.path().join("src/main.rs")),
Some("general")
);
assert_eq!(
registry.namespace_for(temp.path().join("src/tests/test_api.rs")),
Some("specific")
);
}
#[test]
fn test_ambiguity_failure() {
let temp = TempDir::new().unwrap();
create_registry(
temp.path(),
r#"
version = 1
[[namespaces]]
namespace = "a"
patterns = ["common/*.sea"]
[[namespaces]]
namespace = "b"
patterns = ["common/*.sea"]
"#,
);
create_file(temp.path(), "common/model.sea");
let registry = NamespaceRegistry::discover(temp.path()).unwrap().unwrap();
assert_eq!(
registry.namespace_for(temp.path().join("common/model.sea")),
Some("a")
);
let result = registry.namespace_for_with_options(temp.path().join("common/model.sea"), true);
assert!(matches!(result, Err(RegistryError::Ambiguous { .. })));
}
#[test]
fn test_multi_pattern_namespace() {
let temp = TempDir::new().unwrap();
create_registry(
temp.path(),
r#"
version = 1
[[namespaces]]
namespace = "mixed"
patterns = ["utils/*.sea", "helpers/*.sea"]
"#,
);
create_file(temp.path(), "utils/a.sea");
create_file(temp.path(), "helpers/b.sea");
let registry = NamespaceRegistry::discover(temp.path()).unwrap().unwrap();
assert_eq!(
registry.namespace_for(temp.path().join("utils/a.sea")),
Some("mixed")
);
assert_eq!(
registry.namespace_for(temp.path().join("helpers/b.sea")),
Some("mixed")
);
}
#[test]
fn test_default_namespace_fallback() {
let temp = TempDir::new().unwrap();
create_registry(
temp.path(),
r#"
version = 1
default_namespace = "fallback"
[[namespaces]]
namespace = "main"
patterns = ["main/*.sea"]
"#,
);
create_file(temp.path(), "other/file.sea");
let registry = NamespaceRegistry::discover(temp.path()).unwrap().unwrap();
assert_eq!(
registry.namespace_for(temp.path().join("other/file.sea")),
Some("fallback")
);
}
#[test]
fn test_nested_wildcards() {
let temp = TempDir::new().unwrap();
create_registry(
temp.path(),
r#"
version = 1
[[namespaces]]
namespace = "deep"
patterns = ["**/*/deep.sea"]
"#,
);
create_file(temp.path(), "a/b/c/deep.sea");
create_file(temp.path(), "deep.sea");
let registry = NamespaceRegistry::discover(temp.path()).unwrap().unwrap();
assert_eq!(
registry.namespace_for(temp.path().join("a/b/c/deep.sea")),
Some("deep")
);
}
#[test]
fn test_windows_paths_normalization() {
}