use ralph::tools::file_tools::{explain_code, load_files, write_file};
use std::fs;
use tempfile::TempDir;
#[test]
fn load_files_returns_matching_contents() {
let dir = TempDir::new().unwrap();
write_file(&dir.path().join("a.rs"), "fn a() {}").unwrap();
write_file(&dir.path().join("b.rs"), "fn b() {}").unwrap();
write_file(&dir.path().join("c.txt"), "not rust").unwrap();
let result = load_files("*.rs", dir.path()).unwrap();
assert!(result.contains("fn a()"), "should include a.rs");
assert!(result.contains("fn b()"), "should include b.rs");
assert!(!result.contains("not rust"), "should exclude c.txt");
}
#[test]
fn load_files_includes_path_headers() {
let dir = TempDir::new().unwrap();
write_file(&dir.path().join("hello.rs"), "// hello").unwrap();
let result = load_files("*.rs", dir.path()).unwrap();
assert!(
result.contains("=== hello.rs ==="),
"should have a path header"
);
}
#[test]
fn load_files_no_match_returns_message() {
let dir = TempDir::new().unwrap();
let result = load_files("*.nonexistent", dir.path()).unwrap();
assert!(
result.contains("No files matched"),
"should report no matches"
);
}
#[test]
fn load_files_nested_glob() {
let dir = TempDir::new().unwrap();
fs::create_dir_all(dir.path().join("src/sub")).unwrap();
write_file(&dir.path().join("src/top.rs"), "// top").unwrap();
write_file(&dir.path().join("src/sub/deep.rs"), "// deep").unwrap();
let result = load_files("src/**/*.rs", dir.path()).unwrap();
assert!(result.contains("// top"), "should include top.rs");
assert!(result.contains("// deep"), "should include deep.rs");
}
#[test]
fn explain_code_detects_rust_project() {
let dir = TempDir::new().unwrap();
write_file(
&dir.path().join("Cargo.toml"),
"[package]\nname = \"myapp\"\nversion = \"0.1.0\"\n",
)
.unwrap();
fs::create_dir_all(dir.path().join("src")).unwrap();
write_file(&dir.path().join("src/main.rs"), "fn main() {}").unwrap();
let report = explain_code(dir.path()).unwrap();
assert!(report.contains("Rust"), "should detect Rust project");
assert!(report.contains("Cargo.toml"), "should mention Cargo.toml");
}
#[test]
fn explain_code_includes_directory_structure() {
let dir = TempDir::new().unwrap();
fs::create_dir_all(dir.path().join("src")).unwrap();
write_file(&dir.path().join("src/lib.rs"), "// lib").unwrap();
let report = explain_code(dir.path()).unwrap();
assert!(
report.contains("Directory Structure"),
"should have directory section"
);
assert!(report.contains("src"), "should list src directory");
}
#[test]
fn explain_code_lists_entry_points() {
let dir = TempDir::new().unwrap();
fs::create_dir_all(dir.path().join("src")).unwrap();
write_file(&dir.path().join("src/main.rs"), "fn main() {}").unwrap();
write_file(&dir.path().join("src/lib.rs"), "// lib").unwrap();
let report = explain_code(dir.path()).unwrap();
assert!(
report.contains("Entry Points"),
"should have entry points section"
);
assert!(report.contains("src/main.rs"), "should list main.rs");
assert!(report.contains("src/lib.rs"), "should list lib.rs");
}
#[test]
fn explain_code_includes_source_preview() {
let dir = TempDir::new().unwrap();
write_file(
&dir.path().join("Cargo.toml"),
"[package]\nname = \"test\"\n",
)
.unwrap();
fs::create_dir_all(dir.path().join("src")).unwrap();
write_file(
&dir.path().join("src/main.rs"),
"fn main() { println!(\"hi\"); }",
)
.unwrap();
let report = explain_code(dir.path()).unwrap();
assert!(
report.contains("Source Files"),
"should have source files section"
);
assert!(
report.contains("fn main()"),
"should preview source content"
);
}
#[test]
fn explain_code_unknown_project_type() {
let dir = TempDir::new().unwrap();
write_file(&dir.path().join("README.txt"), "hello").unwrap();
let report = explain_code(dir.path()).unwrap();
assert!(
report.contains("Unknown"),
"should say unknown when no manifest found"
);
}