use confers::utils::file_format::{detect_format_by_content, detect_format_by_extension, detect_format_smart, FileFormat};
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_format_detection_json() {
let mut file = NamedTempFile::with_suffix(".json").unwrap();
file.write_all(b"{\n \"name\": \"test\",\n \"value\": 42\n}").unwrap();
let result = detect_format_by_content(file.path());
assert_eq!(result, Some(FileFormat::Json), "Should detect JSON format");
}
#[test]
fn test_format_detection_toml() {
let mut file = NamedTempFile::with_suffix(".toml").unwrap();
file.write_all(b"[app]\nname = \"test\"\nvalue = 42\n").unwrap();
let result = detect_format_by_content(file.path());
assert_eq!(result, Some(FileFormat::Toml), "Should detect TOML format");
}
#[test]
fn test_format_detection_yaml() {
let mut file = NamedTempFile::with_suffix(".yaml").unwrap();
file.write_all(b"---\napp:\n name: test\n value: 42\n").unwrap();
let result = detect_format_by_content(file.path());
assert_eq!(result, Some(FileFormat::Yaml), "Should detect YAML format");
}
#[test]
fn test_format_detection_by_extension() {
let json_file = NamedTempFile::with_suffix(".json").unwrap();
let toml_file = NamedTempFile::with_suffix(".toml").unwrap();
let yaml_file = NamedTempFile::with_suffix(".yaml").unwrap();
let yml_file = NamedTempFile::with_suffix(".yml").unwrap();
assert_eq!(
detect_format_by_extension(json_file.path()),
Some(FileFormat::Json)
);
assert_eq!(
detect_format_by_extension(toml_file.path()),
Some(FileFormat::Toml)
);
assert_eq!(
detect_format_by_extension(yaml_file.path()),
Some(FileFormat::Yaml)
);
assert_eq!(
detect_format_by_extension(yml_file.path()),
Some(FileFormat::Yaml)
);
}
#[test]
fn test_format_detection_smart() {
let mut file = NamedTempFile::with_suffix(".json").unwrap();
file.write_all(b"{\n \"name\": \"test\"\n}").unwrap();
let result = detect_format_smart(file.path());
assert_eq!(result, Some(FileFormat::Json), "Smart detection should work");
}
#[test]
fn test_format_detection_unknown() {
let mut file = NamedTempFile::with_suffix(".txt").unwrap();
file.write_all(b"unknown content").unwrap();
let result = detect_format_by_content(file.path());
assert_eq!(result, None, "Should return None for unknown format");
}
#[test]
fn test_format_detection_empty_file() {
let file = NamedTempFile::new().unwrap();
let result = detect_format_by_content(file.path());
assert_eq!(result, None, "Should return None for empty file");
}
#[test]
fn test_format_detection_yaml_with_document_marker() {
let mut file = NamedTempFile::with_suffix(".yaml").unwrap();
file.write_all(b"---\n# YAML document\nname: test\n").unwrap();
let result = detect_format_by_content(file.path());
assert_eq!(result, Some(FileFormat::Yaml), "Should detect YAML with document marker");
}
#[test]
fn test_format_detection_json_array() {
let mut file = NamedTempFile::with_suffix(".json").unwrap();
file.write_all(b"[\n {\"name\": \"test1\"},\n {\"name\": \"test2\"}\n]").unwrap();
let result = detect_format_by_content(file.path());
assert_eq!(result, Some(FileFormat::Json), "Should detect JSON array");
}
#[test]
fn test_format_detection_toml_with_nested_tables() {
let mut file = NamedTempFile::with_suffix(".toml").unwrap();
file.write_all(b"[database]\nhost = \"localhost\"\n[database.connection]\nport = 5432\n").unwrap();
let result = detect_format_by_content(file.path());
assert_eq!(result, Some(FileFormat::Toml), "Should detect TOML with nested tables");
}