use std::fs;
use std::io::Write;
use std::process::Command;
use std::time::{Duration, SystemTime};
use tempfile::TempDir;
use crate::model::{GitInfo, ProjectReport, Safety, ScanReport};
use super::*;
fn options() -> ScanOptions {
ScanOptions {
max_depth: 6,
min_size: 0,
older_than: None,
categories: None,
rule_ids: None,
include_blocked: true,
verbose: false,
disk_attribution: false,
tmp_roots: false,
system_roots: false,
ignore_globs: Vec::new(),
git_timeout: Some(DEFAULT_GIT_TIMEOUT),
progress: false,
}
}
fn tmp_options() -> ScanOptions {
ScanOptions {
tmp_roots: true,
..options()
}
}
#[test]
fn tmp_worktree_fallback_matches_exact_top_level_patterns() {
let temp = TempDir::new().unwrap();
let root = temp.path().canonicalize().unwrap();
let accepted = [
"remem-alpha",
"rclean-issue-166",
"loom-review",
"codex-review-target-123",
"review-target",
];
let rejected = [
"remem",
"xremem-alpha",
"rclean",
"rclean_alpha",
"loom",
"loomalpha",
"review_target",
"reviewtarget",
];
for name in accepted.iter().chain(rejected.iter()).copied() {
let worktree = temp.path().join(name);
fs::create_dir(&worktree).unwrap();
fs::write(worktree.join("Cargo.toml"), "[package]\nname = \"tmp\"\n").unwrap();
fs::write(worktree.join("source.rs"), "fn main() {}\n").unwrap();
}
let report = scan(&[temp.path().to_path_buf()], &tmp_options()).unwrap();
let candidates = report
.projects
.iter()
.flat_map(|project| project.candidates.iter())
.collect::<Vec<_>>();
assert_eq!(candidates.len(), accepted.len());
for name in accepted {
let path = root.join(name).display().to_string();
let candidate = candidates
.iter()
.find(|candidate| candidate.path == path)
.unwrap_or_else(|| panic!("{name} should be reported"));
assert_eq!(candidate.rule_id, "agent.tmp_worktree");
assert_eq!(candidate.safety, Safety::Caution);
}
for name in rejected {
let path = root.join(name).display().to_string();
assert!(
candidates.iter().all(|candidate| candidate.path != path),
"{name} should not be reported"
);
}
}
#[test]
fn tmp_worktree_fallback_requires_project_marker() {
let temp = TempDir::new().unwrap();
let worktree = temp.path().join("rclean-no-marker");
fs::create_dir(&worktree).unwrap();
fs::write(worktree.join("notes.txt"), "not a project").unwrap();
let report = scan(&[temp.path().to_path_buf()], &tmp_options()).unwrap();
assert_eq!(report.summary.candidates, 0);
}
#[test]
fn tmp_worktree_fallback_only_emits_when_no_nested_cleanable_candidate_exists() {
let temp = TempDir::new().unwrap();
let root = temp.path().canonicalize().unwrap();
let with_nested = temp.path().join("rclean-with-target");
fs::create_dir(&with_nested).unwrap();
fs::write(
with_nested.join("Cargo.toml"),
"[package]\nname = \"with-target\"\n",
)
.unwrap();
fs::create_dir(with_nested.join("target")).unwrap();
fs::write(with_nested.join("target").join("blob"), "artifact").unwrap();
let fallback = temp.path().join("rclean-no-target");
fs::create_dir(&fallback).unwrap();
fs::write(
fallback.join("Cargo.toml"),
"[package]\nname = \"no-target\"\n",
)
.unwrap();
fs::write(fallback.join("source.rs"), "fn main() {}\n").unwrap();
let report = scan(&[temp.path().to_path_buf()], &tmp_options()).unwrap();
let candidates = report
.projects
.iter()
.flat_map(|project| project.candidates.iter())
.map(|candidate| (candidate.path.as_str(), candidate.rule_id.as_str()))
.collect::<Vec<_>>();
assert!(candidates.iter().any(|(path, rule_id)| {
*path
== root
.join("rclean-with-target")
.join("target")
.display()
.to_string()
&& *rule_id == "rust.target"
}));
assert!(candidates.iter().all(|(path, rule_id)| {
!(*path == root.join("rclean-with-target").display().to_string()
&& *rule_id == "agent.tmp_worktree")
}));
assert!(candidates.iter().any(|(path, rule_id)| {
*path == root.join("rclean-no-target").display().to_string()
&& *rule_id == "agent.tmp_worktree"
}));
}
#[test]
fn detects_root_node_project() {
let temp = TempDir::new().unwrap();
fs::write(temp.path().join("package.json"), "{}").unwrap();
fs::create_dir(temp.path().join("node_modules")).unwrap();
fs::write(temp.path().join("node_modules").join("x"), "abc").unwrap();
let report = scan(&[temp.path().to_path_buf()], &options()).unwrap();
assert_eq!(report.summary.candidates, 1);
assert_eq!(
report.projects[0].candidates[0].rule_id,
"node.node_modules"
);
assert_eq!(report.projects[0].total_bytes, 3);
assert_eq!(report.projects[0].project_bytes, 5);
assert_eq!(report.projects[0].artifact_percent, 60.0);
}
#[test]
fn blocks_plain_python_venv_without_marker() {
let temp = TempDir::new().unwrap();
fs::write(temp.path().join("pyproject.toml"), "[project]\n").unwrap();
fs::create_dir(temp.path().join("venv")).unwrap();
let report = scan(&[temp.path().to_path_buf()], &options()).unwrap();
assert_eq!(report.projects[0].candidates[0].safety, Safety::Blocked);
}
#[test]
fn generic_build_without_marker_is_ignored() {
let temp = TempDir::new().unwrap();
fs::create_dir(temp.path().join("build")).unwrap();
let report = scan(&[temp.path().to_path_buf()], &options()).unwrap();
assert_eq!(report.summary.candidates, 0);
}
#[test]
fn symlink_candidate_is_blocked() {
let temp = TempDir::new().unwrap();
fs::write(temp.path().join("package.json"), "{}").unwrap();
let real = temp.path().join("real_modules");
fs::create_dir(&real).unwrap();
let link = temp.path().join("node_modules");
#[cfg(unix)]
std::os::unix::fs::symlink(&real, &link).unwrap();
#[cfg(windows)]
std::os::windows::fs::symlink_dir(&real, &link).unwrap();
let report = scan(&[temp.path().to_path_buf()], &options()).unwrap();
assert_eq!(report.projects[0].candidates[0].safety, Safety::Blocked);
}
#[test]
fn detects_gradle_dart_dotnet_and_ruby_rules() {
let temp = TempDir::new().unwrap();
let gradle = temp.path().join("gradle");
fs::create_dir(&gradle).unwrap();
fs::write(gradle.join("build.gradle"), "plugins {}\n").unwrap();
fs::create_dir(gradle.join("build")).unwrap();
let dart = temp.path().join("dart");
fs::create_dir(&dart).unwrap();
fs::write(dart.join("pubspec.yaml"), "name: app\n").unwrap();
fs::create_dir(dart.join(".dart_tool")).unwrap();
let dotnet = temp.path().join("dotnet");
fs::create_dir(&dotnet).unwrap();
fs::write(dotnet.join("app.csproj"), "<Project />\n").unwrap();
fs::create_dir(dotnet.join("bin")).unwrap();
let ruby = temp.path().join("ruby");
fs::create_dir_all(ruby.join("vendor").join("bundle")).unwrap();
fs::write(ruby.join("Gemfile"), "source 'https://rubygems.org'\n").unwrap();
let report = scan(&[temp.path().to_path_buf()], &options()).unwrap();
let rule_ids = report
.projects
.iter()
.flat_map(|project| project.candidates.iter())
.map(|candidate| candidate.rule_id.as_str())
.collect::<Vec<_>>();
assert!(rule_ids.contains(&"java.gradle_build"));
assert!(rule_ids.contains(&"dart.tool"));
assert!(rule_ids.contains(&"dotnet.bin"));
assert!(rule_ids.contains(&"ruby.vendor_bundle"));
}
#[test]
fn dirty_git_marks_candidate_caution() {
let temp = TempDir::new().unwrap();
Command::new("git")
.arg("-C")
.arg(temp.path())
.arg("init")
.output()
.unwrap();
fs::write(temp.path().join("package.json"), "{}").unwrap();
fs::create_dir(temp.path().join("node_modules")).unwrap();
let mut file = fs::File::create(temp.path().join("node_modules").join("x")).unwrap();
writeln!(file, "abc").unwrap();
let report = scan(&[temp.path().to_path_buf()], &options()).unwrap();
assert_eq!(report.projects[0].candidates[0].safety, Safety::Caution);
}
#[test]
fn risk_score_is_zero_when_no_markers_trip() {
let temp = TempDir::new().unwrap();
fs::write(temp.path().join("Cargo.lock"), "[]").unwrap();
let old = SystemTime::now() - Duration::from_secs(30 * 24 * 60 * 60);
assert_eq!(compute_risk_score(None, old, temp.path()), 0.0);
}
#[test]
fn risk_score_weights_match_spec() {
let temp = TempDir::new().unwrap();
let old = SystemTime::now() - Duration::from_secs(30 * 24 * 60 * 60);
assert!((compute_risk_score(None, old, temp.path()) - 0.20).abs() < 1e-6);
fs::write(temp.path().join("Cargo.lock"), "[]").unwrap();
let recent = SystemTime::now();
assert!((compute_risk_score(None, recent, temp.path()) - 0.25).abs() < 1e-6);
let dirty = GitInfo {
repo_root: temp.path().display().to_string(),
dirty: true,
};
let score = compute_risk_score(Some(&dirty), recent, temp.path());
assert!((score - 0.65).abs() < 1e-6, "expected 0.65, got {score}");
}
#[test]
fn risk_score_max_is_0_85_until_root_boundary_axis_lands() {
let temp = TempDir::new().unwrap();
let recent = SystemTime::now();
let dirty = GitInfo {
repo_root: temp.path().display().to_string(),
dirty: true,
};
let score = compute_risk_score(Some(&dirty), recent, temp.path());
assert!(score <= 1.0, "clamp invariant: score never exceeds 1.0");
assert!(
(score - 0.85).abs() < 1e-6,
"current max is 0.85 (root_boundary deferred); got {score}"
);
}
#[test]
fn sibling_projects_source_bytes_do_not_cross_contaminate() {
let temp = TempDir::new().unwrap();
let frontend = temp.path().join("frontend");
let backend = temp.path().join("backend");
fs::create_dir(&frontend).unwrap();
fs::create_dir(&backend).unwrap();
let frontend_marker = b"{}";
let frontend_readme = b"hi\n";
fs::write(frontend.join("package.json"), frontend_marker).unwrap();
fs::write(frontend.join("README.md"), frontend_readme).unwrap();
fs::create_dir(frontend.join("node_modules")).unwrap();
fs::write(frontend.join("node_modules").join("a"), b"xyz").unwrap();
let frontend_source_expected = (frontend_marker.len() + frontend_readme.len()) as u64;
let backend_marker = b"[package]\nname=\"b\"\n";
let backend_readme = b"hello\n";
fs::write(backend.join("Cargo.toml"), backend_marker).unwrap();
fs::write(backend.join("README.md"), backend_readme).unwrap();
fs::create_dir(backend.join("target")).unwrap();
fs::write(backend.join("target").join("a"), b"build-output").unwrap();
let backend_source_expected = (backend_marker.len() + backend_readme.len()) as u64;
let report = scan(&[temp.path().to_path_buf()], &options()).unwrap();
let projects: std::collections::HashMap<&str, &ProjectReport> = report
.projects
.iter()
.map(|p| {
let name = std::path::Path::new(&p.path)
.file_name()
.and_then(|n| n.to_str())
.unwrap();
(name, p)
})
.collect();
let front = projects.get("frontend").expect("frontend project missing");
let back = projects.get("backend").expect("backend project missing");
let front_source = front.project_bytes - front.total_bytes;
let back_source = back.project_bytes - back.total_bytes;
assert_eq!(
front_source, frontend_source_expected,
"frontend should only count its own files"
);
assert_eq!(
back_source, backend_source_expected,
"backend should only count its own files"
);
}
#[test]
fn scan_sizes_one_large_candidate_and_many_small_projects_deterministically() {
const SMALL_PROJECTS: usize = 24;
const SMALL_BYTES: u64 = 4;
const HUGE_DIRS: usize = 16;
const HUGE_FILES_PER_DIR: usize = 8;
const HUGE_FILE_BYTES: u64 = 1024;
let temp = TempDir::new().unwrap();
for i in 0..SMALL_PROJECTS {
let project = temp.path().join(format!("small_{i:03}"));
fs::create_dir(&project).unwrap();
fs::write(project.join("package.json"), "{}").unwrap();
fs::write(project.join("index.js"), "console.log(1);\n").unwrap();
let node_modules = project.join("node_modules");
fs::create_dir(&node_modules).unwrap();
let file = fs::File::create(node_modules.join("blob")).unwrap();
file.set_len(SMALL_BYTES).unwrap();
}
let huge = temp.path().join("huge_rust");
fs::create_dir(&huge).unwrap();
fs::write(huge.join("Cargo.toml"), "[package]\nname = \"huge_rust\"\n").unwrap();
fs::write(huge.join("main.rs"), "fn main() {}\n").unwrap();
let target = huge.join("target");
for dir_index in 0..HUGE_DIRS {
let dir = target
.join("debug")
.join("deps")
.join(format!("crate_{dir_index:03}"));
fs::create_dir_all(&dir).unwrap();
for file_index in 0..HUGE_FILES_PER_DIR {
let file = fs::File::create(dir.join(format!("artifact_{file_index:03}.o"))).unwrap();
file.set_len(HUGE_FILE_BYTES).unwrap();
}
}
let first = scan(&[temp.path().to_path_buf()], &options()).unwrap();
let second = scan(&[temp.path().to_path_buf()], &options()).unwrap();
let huge_bytes = HUGE_DIRS as u64 * HUGE_FILES_PER_DIR as u64 * HUGE_FILE_BYTES;
let expected_total = SMALL_PROJECTS as u64 * SMALL_BYTES + huge_bytes;
assert_eq!(first.summary.candidates, SMALL_PROJECTS + 1);
assert_eq!(first.summary.total_bytes, expected_total);
let huge_project = first
.projects
.iter()
.find(|project| project.path.ends_with("huge_rust"))
.expect("huge Rust project missing");
assert_eq!(huge_project.candidates.len(), 1);
assert_eq!(huge_project.candidates[0].rule_id, "rust.target");
assert_eq!(huge_project.candidates[0].bytes, huge_bytes);
assert_eq!(scan_signature(&first), scan_signature(&second));
}
type CandidateSignature = (String, String, u64, String);
type ProjectSignature = (String, Vec<CandidateSignature>);
fn scan_signature(report: &ScanReport) -> Vec<ProjectSignature> {
report
.projects
.iter()
.map(|project| {
let candidates = project
.candidates
.iter()
.map(|candidate| {
(
candidate.path.clone(),
candidate.rule_id.clone(),
candidate.bytes,
format!("{:?}", candidate.safety),
)
})
.collect();
(project.path.clone(), candidates)
})
.collect()
}
#[test]
fn git_cache_returns_none_for_non_repo() {
let temp = TempDir::new().unwrap();
let cache = GitCache::new();
assert!(cache.info_for(temp.path()).is_none());
assert!(cache.info_for(temp.path()).is_none());
}
#[test]
fn git_cache_shares_dirty_flag_across_sibling_projects() {
let temp = TempDir::new().unwrap();
Command::new("git")
.arg("-C")
.arg(temp.path())
.arg("init")
.output()
.unwrap();
fs::create_dir(temp.path().join("a")).unwrap();
fs::create_dir(temp.path().join("b")).unwrap();
fs::write(temp.path().join("dirty.txt"), "x").unwrap();
let cache = GitCache::new();
let a = cache.info_for(&temp.path().join("a")).unwrap();
let b = cache.info_for(&temp.path().join("b")).unwrap();
assert_eq!(a.repo_root, b.repo_root);
assert!(a.dirty);
assert!(b.dirty);
}