use assert_cmd::Command;
use insta::assert_snapshot;
use predicates::str::contains;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
const TS_FIXTURE: &str = "tests/fixtures/typescript-simple";
fn copy_dir_all(src: &Path, dest: &Path) {
std::fs::create_dir_all(dest).unwrap();
for entry in std::fs::read_dir(src).unwrap() {
let entry = entry.unwrap();
let src_path = entry.path();
let dest_path = dest.join(entry.file_name());
if src_path.is_dir() {
copy_dir_all(&src_path, &dest_path);
} else {
std::fs::copy(&src_path, &dest_path).unwrap();
}
}
}
fn setup_indexed_fixture() -> (TempDir, PathBuf) {
let dir = TempDir::new().unwrap();
let fixture = Path::new(TS_FIXTURE);
copy_dir_all(fixture, dir.path());
Command::cargo_bin("scope")
.unwrap()
.arg("init")
.current_dir(dir.path())
.assert()
.success();
Command::cargo_bin("scope")
.unwrap()
.args(["index", "--full"])
.current_dir(dir.path())
.assert()
.success();
let root = dir.path().to_path_buf();
(dir, root)
}
fn normalize_paths(output: &str, root: &Path) -> String {
let root_str = root.to_string_lossy();
let root_forward = root_str.replace('\\', "/");
let output_forward = output.replace('\\', "/");
output_forward.replace(&*root_forward, "<PROJECT_ROOT>")
}
#[test]
fn test_refs_finds_callers() {
let (_dir, root) = setup_indexed_fixture();
Command::cargo_bin("scope")
.unwrap()
.args(["refs", "processPayment"])
.current_dir(&root)
.assert()
.success()
.stdout(contains("processPayment"));
}
#[test]
fn test_refs_class_shows_grouped_output() {
let (_dir, root) = setup_indexed_fixture();
Command::cargo_bin("scope")
.unwrap()
.args(["refs", "PaymentService"])
.current_dir(&root)
.assert()
.success()
.stdout(contains("PaymentService"));
}
#[test]
fn test_refs_with_kind_filter() {
let (_dir, root) = setup_indexed_fixture();
Command::cargo_bin("scope")
.unwrap()
.args(["refs", "PaymentService", "--kind", "imports"])
.current_dir(&root)
.assert()
.success()
.stdout(contains("PaymentService"));
}
#[test]
fn test_refs_with_limit() {
let (_dir, root) = setup_indexed_fixture();
Command::cargo_bin("scope")
.unwrap()
.args([
"refs",
"PaymentService",
"--kind",
"imports",
"--limit",
"1",
])
.current_dir(&root)
.assert()
.success()
.stdout(contains("more"));
}
#[test]
fn test_refs_unknown_symbol_fails() {
let (_dir, root) = setup_indexed_fixture();
Command::cargo_bin("scope")
.unwrap()
.args(["refs", "UnknownThing"])
.current_dir(&root)
.assert()
.failure()
.stderr(contains("not found"));
}
#[test]
fn test_refs_json_output() {
let (_dir, root) = setup_indexed_fixture();
let output = Command::cargo_bin("scope")
.unwrap()
.args(["refs", "PaymentService", "--json"])
.current_dir(&root)
.assert()
.success()
.get_output()
.stdout
.clone();
let json: serde_json::Value =
serde_json::from_slice(&output).expect("stdout should be valid JSON");
assert_eq!(
json["command"], "refs",
"JSON envelope must have command=refs"
);
assert!(
!json["data"].is_null(),
"JSON envelope must have a non-null data field"
);
}
#[test]
fn test_refs_output_format() {
let (_dir, root) = setup_indexed_fixture();
let raw = Command::cargo_bin("scope")
.unwrap()
.args(["refs", "PaymentService"])
.current_dir(&root)
.output()
.unwrap();
let stdout = String::from_utf8(raw.stdout).unwrap();
let normalized = normalize_paths(&stdout, &root);
assert_snapshot!("refs_payment_service", normalized);
}
#[test]
fn test_callers_with_depth_shows_transitive() {
let (_dir, root) = setup_indexed_fixture();
let output = Command::cargo_bin("scope")
.unwrap()
.args(["callers", "processPayment", "--depth", "2"])
.current_dir(&root)
.assert()
.success()
.get_output()
.stdout
.clone();
let stdout = String::from_utf8(output).unwrap();
assert!(
stdout.contains("Direct callers") || stdout.contains("Impact analysis"),
"Expected depth-grouped output, got:\n{stdout}"
);
}
#[test]
fn test_callers_default_depth_is_flat() {
let (_dir, root) = setup_indexed_fixture();
let callers_output = Command::cargo_bin("scope")
.unwrap()
.args(["callers", "processPayment"])
.current_dir(&root)
.assert()
.success()
.get_output()
.stdout
.clone();
let stdout = String::from_utf8(callers_output).unwrap();
assert!(
stdout.contains("reference"),
"Expected flat refs output with 'reference' header, got:\n{stdout}"
);
assert!(
!stdout.contains("Direct callers"),
"Default depth=1 should use flat format, not impact grouping"
);
}