#![allow(
clippy::expect_used,
clippy::unwrap_used,
reason = "Tests are allowed to use expect/unwrap for simplicity"
)]
use std::path::PathBuf;
use jscycles::{
Config, CycleDetector, DependencyGraph, ImportExtractor, PackageDiscovery, TsConfig, Workspace,
WorkspaceFormat,
};
#[test]
fn relative_package_roots_and_absolute_alias_configs_share_graph_nodes() {
let cwd = std::env::current_dir().expect("test working directory");
let root = tempfile::tempdir_in(&cwd).expect("temporary package");
std::fs::write(
root.path().join("tsconfig.json"),
r#"{"compilerOptions":{"baseUrl":".","paths":{"@/*":["*"]}}}"#,
)
.expect("aliases");
std::fs::write(
root.path().join("a.ts"),
"import {b} from '@/b'; export const a = b;",
)
.expect("source a");
std::fs::write(
root.path().join("b.ts"),
"import {a} from '@/a'; export const b = a;",
)
.expect("source b");
let config = TsConfig::load(&root.path().join("tsconfig.json")).expect("absolute config");
let relative = root
.path()
.strip_prefix(&cwd)
.expect("relative package root");
let imports = ImportExtractor::new(vec!["ts".to_owned()], Some(config))
.extract(relative)
.expect("extract imports");
assert!(
imports.iter().all(|import| import.source.is_absolute()),
"canonical source-path contract"
);
let cycles = CycleDetector::detect(&DependencyGraph::from_imports(&imports));
assert_eq!(
cycles.len(),
1,
"mixed input path spellings must retain the alias cycle"
);
}
#[test]
fn automatic_include_roots_are_recursive_and_globs_select_directories() {
let root = tempfile::tempdir().expect("temporary repository");
for (directory, name) in [
("packages/a", "a"),
("packages/a/nested", "nested"),
("packages/b", "b"),
("apps/c", "c"),
] {
let path = root.path().join(directory);
std::fs::create_dir_all(&path).expect("package directory");
std::fs::write(path.join("package.json"), format!(r#"{{"name":"{name}"}}"#))
.expect("package manifest");
}
for (includes, expected) in [
(vec!["."], vec!["a", "b", "c", "nested"]),
(vec!["packages/*"], vec!["a", "b", "nested"]),
(vec!["packages/a"], vec!["a", "nested"]),
(vec!["apps/*", "packages/b"], vec!["b", "c"]),
(Vec::new(), Vec::new()),
] {
let config = Config {
scan: jscycles::config::ScanConfig {
include: includes.iter().map(|value| (*value).to_owned()).collect(),
..jscycles::config::ScanConfig::default()
},
..Config::default()
};
let discovery = PackageDiscovery::new(&config, &[], &[]).expect("valid selection");
let mut names: Vec<_> = discovery
.discover(root.path())
.expect("discover packages")
.into_iter()
.map(|package| package.name)
.collect();
names.sort();
assert_eq!(names, expected, "recursive include roots for {includes:?}");
}
}
fn fixture_path(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures")
.canonicalize()
.expect("fixture directory should exist")
.join(name)
}
fn detect_cycles_in_fixture(fixture_name: &str) -> Vec<String> {
let path = fixture_path(fixture_name);
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(
vec![
"ts".to_owned(),
"tsx".to_owned(),
"js".to_owned(),
"jsx".to_owned(),
],
tsconfig,
);
let imports = extractor
.extract(&path)
.expect("fixture extraction should succeed");
let graph = DependencyGraph::from_imports(&imports);
let cycles = CycleDetector::detect(&graph);
let mut cycle_strings: Vec<String> = cycles
.iter()
.map(|cycle| {
cycle
.path
.iter()
.filter_map(|p| {
p.strip_prefix(&path)
.ok()
.map(|rel| rel.to_string_lossy().replace('\\', "/"))
})
.collect::<Vec<_>>()
.join(" → ")
})
.collect();
cycle_strings.sort();
cycle_strings
}
#[test]
fn test_fixture_simple_cycle() {
let cycles = detect_cycles_in_fixture("simple-cycle");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_fixture_no_cycles() {
let cycles = detect_cycles_in_fixture("no-cycles");
assert!(cycles.is_empty(), "no-cycles fixture should have no cycles");
}
#[test]
fn test_fixture_complex_cycles() {
let cycles = detect_cycles_in_fixture("complex-cycles");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_fixture_with_tsconfig() {
let cycles = detect_cycles_in_fixture("with-tsconfig");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_fixture_monorepo_discovery() {
let path = fixture_path("monorepo-mock");
let config = Config::default();
let discovery =
PackageDiscovery::new(&config, &[], &[]).expect("discovery should be created successfully");
let packages = discovery
.discover(&path)
.expect("monorepo discovery should succeed");
let mut package_names: Vec<_> = packages.iter().map(|p| p.name.clone()).collect();
package_names.sort();
insta::assert_debug_snapshot!(package_names);
}
#[test]
fn test_monorepo_per_package_cycles() {
let path = fixture_path("monorepo-mock");
let config = Config::default();
let discovery =
PackageDiscovery::new(&config, &[], &[]).expect("discovery should be created successfully");
let packages = discovery
.discover(&path)
.expect("monorepo discovery should succeed");
let mut results: Vec<(String, Vec<String>)> = packages
.iter()
.map(|package| {
let tsconfig = TsConfig::discover(&package.path);
let extractor = ImportExtractor::new(vec!["ts".to_owned(), "tsx".to_owned()], tsconfig);
let imports = extractor
.extract(&package.path)
.expect("package extraction should succeed");
let graph = DependencyGraph::from_imports(&imports);
let cycles = CycleDetector::detect(&graph);
let cycle_strs: Vec<String> = cycles
.iter()
.map(|cycle| {
cycle
.path
.iter()
.filter_map(|p| {
p.strip_prefix(&package.path)
.ok()
.map(|rel| rel.to_string_lossy().replace('\\', "/"))
})
.collect::<Vec<_>>()
.join(" → ")
})
.collect();
(package.name.clone(), cycle_strs)
})
.collect();
results.sort_by(|a, b| a.0.cmp(&b.0));
insta::assert_debug_snapshot!(results);
}
fn extract_imports_from_fixture(fixture_name: &str) -> Vec<(String, String)> {
let path = fixture_path(fixture_name);
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(
vec![
"ts".to_owned(),
"tsx".to_owned(),
"js".to_owned(),
"jsx".to_owned(),
],
tsconfig,
);
let imports = extractor
.extract(&path)
.expect("fixture extraction should succeed");
let mut pairs: Vec<(String, String)> = imports
.iter()
.map(|import| {
let source = import
.source
.strip_prefix(&path)
.unwrap_or(&import.source)
.to_string_lossy()
.replace('\\', "/");
(source, import.specifier.clone())
})
.collect();
pairs.sort();
pairs
}
fn extract_raw_imports_from_fixture(fixture_name: &str) -> Vec<jscycles::Import> {
let path = fixture_path(fixture_name);
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(
vec![
"ts".to_owned(),
"tsx".to_owned(),
"js".to_owned(),
"jsx".to_owned(),
],
tsconfig,
);
extractor
.extract(&path)
.expect("fixture extraction should succeed")
}
#[test]
fn test_fixture_import_styles() {
let imports = extract_imports_from_fixture("import-styles");
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_fixture_import_styles_cycles() {
let cycles = detect_cycles_in_fixture("import-styles");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_fixture_self_loop() {
let cycles = detect_cycles_in_fixture("self-loop");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_fixture_long_cycle() {
let cycles = detect_cycles_in_fixture("long-cycle");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_fixture_diamond_no_cycle() {
let cycles = detect_cycles_in_fixture("diamond");
assert!(
cycles.is_empty(),
"diamond dependency should not be detected as a cycle, found: {cycles:?}"
);
}
#[test]
fn test_fixture_dynamic_import_cycle() {
let cycles = detect_cycles_in_fixture("dynamic-cycle");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_fixture_tsconfig_complex_paths() {
let cycles = detect_cycles_in_fixture("tsconfig-complex");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_fixture_tsconfig_complex_imports() {
let imports = extract_imports_from_fixture("tsconfig-complex");
insta::assert_debug_snapshot!(imports);
}
use jscycles::{PackageCycleDetector, PackageDependencyGraph};
fn detect_inter_package_cycles(fixture_name: &str) -> Vec<String> {
use std::collections::HashMap;
let path = fixture_path(fixture_name);
let workspace = Workspace::discover(&path)
.expect("workspace discovery should succeed")
.expect("fixture should be a workspace");
let mut imports_by_package: HashMap<String, Vec<jscycles::Import>> = HashMap::new();
let mut pkg_names: Vec<_> = workspace.packages.keys().cloned().collect();
pkg_names.sort();
for pkg_name in pkg_names {
let pkg_path = workspace
.packages
.get(&pkg_name)
.expect("package should exist");
let tsconfig = TsConfig::discover(pkg_path);
let extractor = ImportExtractor::new(vec!["ts".to_owned(), "tsx".to_owned()], tsconfig)
.with_workspace(Some(workspace.clone()));
let imports = extractor
.extract(pkg_path)
.expect("import extraction should succeed");
let _ = imports_by_package.insert(pkg_name, imports);
}
let graph = PackageDependencyGraph::from_imports(&imports_by_package);
let cycles = PackageCycleDetector::detect(&graph);
let mut cycle_strings: Vec<String> = cycles
.iter()
.map(|cycle| cycle.packages.join(" -> "))
.collect();
cycle_strings.sort();
cycle_strings
}
#[test]
fn test_inter_package_simple_cycle() {
let cycles = detect_inter_package_cycles("inter-package-simple");
assert_eq!(
cycles.len(),
1,
"should detect exactly one inter-package cycle"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_inter_package_three_way_cycle() {
let cycles = detect_inter_package_cycles("inter-package-cycle");
assert_eq!(
cycles.len(),
1,
"should detect exactly one inter-package cycle"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_inter_package_no_cycle() {
let cycles = detect_inter_package_cycles("inter-package-no-cycle");
assert!(
cycles.is_empty(),
"should detect no inter-package cycles, found: {cycles:?}"
);
}
#[test]
fn test_pnpm_workspace_cycle() {
let cycles = detect_inter_package_cycles("pnpm-workspace");
assert_eq!(
cycles.len(),
1,
"should detect inter-package cycle in pnpm workspace"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_workspace_discovery_npm() {
let path = fixture_path("inter-package-simple");
let workspace = Workspace::discover(&path)
.expect("workspace discovery should succeed")
.expect("fixture should be a workspace");
assert_eq!(
workspace.format,
WorkspaceFormat::Npm,
"should detect npm workspace format"
);
assert_eq!(
workspace.packages.len(),
2,
"should find 2 packages in workspace"
);
assert!(
workspace.packages.contains_key("@simple/a"),
"should find @simple/a"
);
assert!(
workspace.packages.contains_key("@simple/b"),
"should find @simple/b"
);
}
#[test]
fn test_workspace_discovery_pnpm() {
let path = fixture_path("pnpm-workspace");
let workspace = Workspace::discover(&path)
.expect("workspace discovery should succeed")
.expect("fixture should be a workspace");
assert_eq!(
workspace.format,
WorkspaceFormat::Pnpm,
"should detect pnpm workspace format"
);
assert_eq!(
workspace.packages.len(),
2,
"should find 2 packages in workspace"
);
}
#[test]
fn test_workspace_package_is_detected() {
let path = fixture_path("inter-package-simple");
let workspace = Workspace::discover(&path)
.expect("workspace discovery should succeed")
.expect("fixture should be a workspace");
assert!(
workspace.is_workspace_package("@simple/a"),
"@simple/a should be workspace package"
);
assert!(
workspace.is_workspace_package("@simple/a/utils"),
"@simple/a/utils should match workspace package"
);
assert!(
!workspace.is_workspace_package("react"),
"react should not be workspace package"
);
}
#[test]
fn test_workspace_import_extraction() {
let path = fixture_path("inter-package-simple");
let workspace = Workspace::discover(&path)
.expect("workspace discovery should succeed")
.expect("fixture should be a workspace");
let pkg_a_path = workspace
.packages
.get("@simple/a")
.expect("pkg-a should exist")
.clone();
let extractor =
ImportExtractor::new(vec!["ts".to_owned()], None).with_workspace(Some(workspace));
let imports = extractor
.extract(pkg_a_path.as_path())
.expect("extraction should succeed");
let workspace_imports: Vec<_> = imports
.iter()
.filter(|i| matches!(i.target, jscycles::ImportTarget::WorkspacePackage { .. }))
.collect();
assert_eq!(
workspace_imports.len(),
1,
"pkg-a should have 1 workspace import"
);
let import = workspace_imports.first().unwrap();
assert_eq!(import.specifier, "@simple/b", "should import @simple/b");
}
#[test]
fn test_monorepo_with_file_cycle_has_inner_cycle() {
let path = fixture_path("monorepo-mock");
let config = Config::default();
let discovery =
PackageDiscovery::new(&config, &[], &[]).expect("discovery should be created successfully");
let packages = discovery
.discover(&path)
.expect("monorepo discovery should succeed");
let pkg_a = packages
.iter()
.find(|p| p.name == "@test/pkg-a")
.expect("should find pkg-a");
let tsconfig = TsConfig::discover(&pkg_a.path);
let extractor = ImportExtractor::new(vec!["ts".to_owned(), "tsx".to_owned()], tsconfig);
let imports = extractor
.extract(&pkg_a.path)
.expect("package extraction should succeed");
let graph = DependencyGraph::from_imports(&imports);
let cycles = CycleDetector::detect(&graph);
assert!(!cycles.is_empty(), "pkg-a should have internal file cycles");
}
#[test]
fn test_fixture_tsconfig_extends_chain() {
let cycles = detect_cycles_in_fixture("tsconfig-extends");
assert!(
cycles.is_empty(),
"inherited aliases must not be merged: {cycles:?}"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_fixture_tsconfig_extends_imports() {
let imports = extract_imports_from_fixture("tsconfig-extends");
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_fixture_tsconfig_extends_pkg() {
let imports = extract_imports_from_fixture("tsconfig-extends-pkg");
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_fixture_tsconfig_extends_pkg_cycles() {
let cycles = detect_cycles_in_fixture("tsconfig-extends-pkg");
assert!(
cycles.is_empty(),
"inherited aliases must not be merged: {cycles:?}"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_pnpm_workspace_complex_cycle() {
let cycles = detect_inter_package_cycles("pnpm-workspace-complex");
assert_eq!(
cycles.len(),
1,
"should detect inter-package cycle in complex pnpm workspace"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_pnpm_workspace_complex_discovery() {
let path = fixture_path("pnpm-workspace-complex");
let workspace = Workspace::discover(&path)
.expect("workspace discovery should succeed")
.expect("fixture should be a workspace");
assert_eq!(
workspace.format,
WorkspaceFormat::Pnpm,
"should detect pnpm format"
);
assert!(
workspace.packages.contains_key("@pnpm-complex/core"),
"should find @pnpm-complex/core"
);
assert!(
workspace.packages.contains_key("@pnpm-complex/utils"),
"should find @pnpm-complex/utils"
);
}
#[test]
fn test_simple_cycle_has_exactly_one_cycle() {
let cycles = detect_cycles_in_fixture("simple-cycle");
assert_eq!(cycles.len(), 1, "simple-cycle should have exactly 1 cycle");
}
#[test]
fn test_simple_cycle_involves_two_files() {
let cycles = detect_cycles_in_fixture("simple-cycle");
let cycle = cycles.first().expect("should have a cycle");
let file_count = cycle.matches('→').count() + 1;
assert!(
file_count >= 2,
"simple cycle should involve at least 2 files, found {file_count}"
);
}
#[test]
fn test_simple_cycle_import_extraction() {
let imports = extract_imports_from_fixture("simple-cycle");
assert!(
!imports.is_empty(),
"simple-cycle should have at least one import"
);
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_no_cycles_import_extraction() {
let imports = extract_imports_from_fixture("no-cycles");
assert!(
!imports.is_empty(),
"no-cycles should have imports even without cycles"
);
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_no_cycles_graph_structure() {
let path = fixture_path("no-cycles");
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(
vec![
"ts".to_owned(),
"tsx".to_owned(),
"js".to_owned(),
"jsx".to_owned(),
],
tsconfig,
);
let imports = extractor.extract(&path).expect("extraction should succeed");
let graph = DependencyGraph::from_imports(&imports);
assert!(
graph.node_count() > 0,
"graph should have nodes even without cycles"
);
}
#[test]
fn test_complex_cycles_has_multiple_cycles() {
let cycles = detect_cycles_in_fixture("complex-cycles");
assert!(
!cycles.is_empty(),
"complex-cycles should have at least one cycle"
);
}
#[test]
fn test_complex_cycles_import_extraction() {
let imports = extract_imports_from_fixture("complex-cycles");
assert!(
imports.len() >= 3,
"complex-cycles should have at least 3 imports"
);
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_complex_cycles_all_cycles_are_unique() {
let cycles = detect_cycles_in_fixture("complex-cycles");
let unique_cycles: std::collections::HashSet<_> = cycles.iter().collect();
assert_eq!(
cycles.len(),
unique_cycles.len(),
"all cycles should be unique"
);
}
#[test]
fn test_diamond_import_extraction() {
let imports = extract_imports_from_fixture("diamond");
assert!(
imports.len() >= 4,
"diamond should have at least 4 imports, found {}",
imports.len()
);
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_diamond_graph_structure() {
let path = fixture_path("diamond");
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(
vec![
"ts".to_owned(),
"tsx".to_owned(),
"js".to_owned(),
"jsx".to_owned(),
],
tsconfig,
);
let imports = extractor.extract(&path).expect("extraction should succeed");
let graph = DependencyGraph::from_imports(&imports);
assert!(
graph.node_count() >= 4,
"diamond graph should have at least 4 nodes"
);
}
#[test]
fn test_long_cycle_has_five_or_more_nodes() {
let cycles = detect_cycles_in_fixture("long-cycle");
assert_eq!(cycles.len(), 1, "long-cycle should have exactly 1 cycle");
let cycle = cycles.first().expect("should have a cycle");
let file_count = cycle.matches('→').count() + 1;
assert!(
file_count >= 5,
"long cycle should involve at least 5 files, found {file_count}"
);
}
#[test]
fn test_long_cycle_import_extraction() {
let imports = extract_imports_from_fixture("long-cycle");
assert_eq!(
imports.len(),
5,
"long-cycle should have exactly 5 imports (A->B->C->D->E->A)"
);
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_self_loop_has_single_file_cycle() {
let cycles = detect_cycles_in_fixture("self-loop");
assert_eq!(cycles.len(), 1, "self-loop should have exactly 1 cycle");
let cycle = cycles.first().expect("should have a cycle");
let part_count = cycle.split('→').count();
assert!(
part_count <= 2,
"self-loop cycle should be a single file or file -> file"
);
}
#[test]
fn test_self_loop_import_extraction() {
let imports = extract_imports_from_fixture("self-loop");
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_dynamic_cycle_detects_dynamic_imports() {
let imports = extract_imports_from_fixture("dynamic-cycle");
let has_dynamic = imports
.iter()
.any(|(_, spec)| spec.starts_with("./") || spec.starts_with("../"));
assert!(has_dynamic, "should detect dynamic imports");
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_dynamic_cycle_cycle_detection() {
let cycles = detect_cycles_in_fixture("dynamic-cycle");
assert!(
!cycles.is_empty(),
"dynamic-cycle should have at least one cycle"
);
}
#[test]
fn test_import_styles_static_imports() {
let imports = extract_imports_from_fixture("import-styles");
let has_static_import = imports.iter().any(|(_, _)| true); assert!(has_static_import, "should have static imports");
}
#[test]
fn test_import_styles_dynamic_imports() {
let imports = extract_imports_from_fixture("import-styles");
assert!(
!imports.is_empty(),
"import-styles should have various import types"
);
}
#[test]
fn test_import_styles_require() {
let imports = extract_imports_from_fixture("import-styles");
assert!(
!imports.is_empty(),
"import-styles should include require() calls"
);
}
#[test]
fn test_import_styles_reexports() {
let imports = extract_imports_from_fixture("import-styles");
assert!(
!imports.is_empty(),
"import-styles should capture re-exports"
);
}
#[test]
fn test_tsconfig_path_alias_resolution() {
let path = fixture_path("with-tsconfig");
let tsconfig = TsConfig::discover(&path);
assert!(tsconfig.is_some(), "should discover tsconfig.json");
let config = tsconfig.expect("config should exist");
assert!(
!config.paths.is_empty(),
"tsconfig should have path aliases"
);
}
#[test]
fn test_tsconfig_base_url_set() {
let path = fixture_path("with-tsconfig");
let tsconfig = TsConfig::discover(&path);
assert!(tsconfig.is_some(), "should discover tsconfig.json");
}
#[test]
fn test_tsconfig_complex_multiple_aliases() {
let path = fixture_path("tsconfig-complex");
let tsconfig = TsConfig::discover(&path);
assert!(tsconfig.is_some(), "should discover tsconfig.json");
let config = tsconfig.expect("config should exist");
assert!(
config.paths.len() >= 2,
"tsconfig-complex should have multiple path aliases"
);
}
#[test]
fn test_tsconfig_complex_alias_prefixes() {
let path = fixture_path("tsconfig-complex");
let tsconfig = TsConfig::discover(&path).expect("should discover config");
let prefixes: Vec<_> = tsconfig.paths.iter().map(|p| p.prefix.as_str()).collect();
assert!(
prefixes.iter().any(|p| p.contains("utils")),
"should have @utils/* alias"
);
}
#[test]
fn test_tsconfig_extends_three_level_chain() {
let path = fixture_path("tsconfig-extends");
let tsconfig = TsConfig::discover(&path).expect("should discover config");
let prefixes: Vec<_> = tsconfig.paths.iter().map(|p| p.prefix.as_str()).collect();
assert!(
prefixes.iter().any(|p| p.contains("app")),
"should have @app/* from child"
);
assert!(
!prefixes.iter().any(|p| p.contains("shared")),
"child paths replace parent aliases"
);
assert!(
!prefixes.iter().any(|p| p.contains("base")),
"child paths replace grandparent aliases"
);
}
#[test]
fn test_tsconfig_extends_import_resolution() {
let imports = extract_imports_from_fixture("tsconfig-extends");
let specifiers: Vec<_> = imports.iter().map(|(_, s)| s.as_str()).collect();
assert!(
specifiers.iter().any(|s| s.contains("@app")),
"should have @app/* import"
);
assert!(
specifiers.iter().any(|s| s.contains("@shared")),
"should have @shared/* import"
);
assert!(
specifiers.iter().any(|s| s.contains("@base")),
"should have @base/* import"
);
}
#[test]
fn test_tsconfig_extends_node_modules_discovery() {
let path = fixture_path("tsconfig-extends-pkg");
let tsconfig = TsConfig::discover(&path);
assert!(
tsconfig.is_some(),
"should discover tsconfig extending from node_modules"
);
}
#[test]
fn test_tsconfig_extends_node_modules_aliases() {
let path = fixture_path("tsconfig-extends-pkg");
let tsconfig = TsConfig::discover(&path).expect("should discover config");
let prefixes: Vec<_> = tsconfig.paths.iter().map(|p| p.prefix.as_str()).collect();
assert!(
prefixes.iter().any(|p| p.contains("local")),
"should have @local/* from child config"
);
assert!(
!prefixes.iter().any(|p| p.contains("company")),
"child paths replace parent paths option"
);
}
#[test]
fn test_inter_package_simple_package_count() {
let path = fixture_path("inter-package-simple");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert_eq!(
workspace.packages.len(),
2,
"should have exactly 2 packages"
);
}
#[test]
fn test_inter_package_simple_package_names() {
let path = fixture_path("inter-package-simple");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
let mut names: Vec<_> = workspace.packages.keys().cloned().collect();
names.sort();
assert_eq!(names, vec!["@simple/a", "@simple/b"]);
}
#[test]
fn test_inter_package_three_way_package_count() {
let path = fixture_path("inter-package-cycle");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert_eq!(
workspace.packages.len(),
3,
"should have exactly 3 packages"
);
}
#[test]
fn test_inter_package_no_cycle_is_linear() {
let path = fixture_path("inter-package-no-cycle");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert_eq!(
workspace.packages.len(),
3,
"linear chain should have 3 packages"
);
let cycles = detect_inter_package_cycles("inter-package-no-cycle");
assert!(cycles.is_empty(), "linear dependency should have no cycles");
}
#[test]
fn test_inter_package_cycle_normalization() {
let cycles = detect_inter_package_cycles("inter-package-cycle");
assert_eq!(cycles.len(), 1, "should have exactly one cycle");
let cycle = cycles.first().expect("should have cycle");
let first_pkg = cycle
.split(" -> ")
.next()
.expect("should have first package");
assert!(
first_pkg.starts_with('@'),
"cycle should start with scoped package"
);
}
#[test]
fn test_pnpm_workspace_format_detection() {
let path = fixture_path("pnpm-workspace");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert_eq!(
workspace.format,
WorkspaceFormat::Pnpm,
"should detect pnpm format"
);
}
#[test]
fn test_pnpm_workspace_package_paths_are_absolute() {
let path = fixture_path("pnpm-workspace");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
let mut package_names: Vec<_> = workspace.packages.keys().collect();
package_names.sort();
for name in package_names {
let pkg_path = workspace.packages.get(name).expect("key should exist");
assert!(
pkg_path.is_absolute(),
"package {name} path should be absolute"
);
assert!(pkg_path.exists(), "package {name} path should exist");
}
}
#[test]
fn test_pnpm_workspace_complex_package_count() {
let path = fixture_path("pnpm-workspace-complex");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert!(
workspace.packages.len() >= 2,
"should have at least 2 packages"
);
}
#[test]
fn test_pnpm_workspace_root_path() {
let path = fixture_path("pnpm-workspace");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert!(
workspace.root.ends_with("pnpm-workspace"),
"root should be pnpm-workspace directory"
);
}
#[test]
fn test_workspace_import_target_types() {
let path = fixture_path("inter-package-simple");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
let pkg_a_path = workspace
.packages
.get("@simple/a")
.expect("should exist")
.clone();
let extractor =
ImportExtractor::new(vec!["ts".to_owned()], None).with_workspace(Some(workspace));
let imports = extractor.extract(&pkg_a_path).expect("should succeed");
let has_workspace_imports = imports
.iter()
.any(|i| matches!(i.target, jscycles::ImportTarget::WorkspacePackage { .. }));
assert!(
has_workspace_imports,
"should have workspace package imports"
);
}
#[test]
fn test_workspace_import_specifier_preserved() {
let path = fixture_path("inter-package-simple");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
let pkg_a_path = workspace
.packages
.get("@simple/a")
.expect("should exist")
.clone();
let extractor =
ImportExtractor::new(vec!["ts".to_owned()], None).with_workspace(Some(workspace));
let imports = extractor.extract(&pkg_a_path).expect("should succeed");
let import = imports.first().expect("should have import");
assert_eq!(
import.specifier, "@simple/b",
"specifier should be preserved"
);
}
#[test]
fn test_monorepo_mock_package_names() {
let path = fixture_path("monorepo-mock");
let config = Config::default();
let discovery = PackageDiscovery::new(&config, &[], &[]).expect("should create");
let packages = discovery.discover(&path).expect("should succeed");
let mut names: Vec<_> = packages.iter().map(|p| p.name.clone()).collect();
names.sort();
assert!(
names.contains(&"@test/pkg-a".to_owned()),
"should find @test/pkg-a"
);
assert!(
names.contains(&"@test/pkg-b".to_owned()),
"should find @test/pkg-b"
);
}
#[test]
fn test_monorepo_mock_package_paths() {
let path = fixture_path("monorepo-mock");
let config = Config::default();
let discovery = PackageDiscovery::new(&config, &[], &[]).expect("should create");
let packages = discovery.discover(&path).expect("should succeed");
for pkg in &packages {
assert!(pkg.path.is_absolute(), "package path should be absolute");
assert!(pkg.path.exists(), "package path should exist");
}
}
#[test]
fn test_monorepo_mock_has_internal_cycles() {
let path = fixture_path("monorepo-mock");
let config = Config::default();
let discovery = PackageDiscovery::new(&config, &[], &[]).expect("should create");
let packages = discovery.discover(&path).expect("should succeed");
let mut found_cycle = false;
for pkg in &packages {
let tsconfig = TsConfig::discover(&pkg.path);
let extractor = ImportExtractor::new(vec!["ts".to_owned(), "tsx".to_owned()], tsconfig);
let imports = extractor.extract(&pkg.path).expect("should succeed");
let graph = DependencyGraph::from_imports(&imports);
let cycles = CycleDetector::detect(&graph);
if !cycles.is_empty() {
found_cycle = true;
}
}
assert!(
found_cycle,
"monorepo-mock should have at least one internal cycle"
);
}
#[test]
fn test_nested_cycles_detection() {
let cycles = detect_cycles_in_fixture("nested-cycles");
assert!(
!cycles.is_empty(),
"nested-cycles should have at least 1 cycle, found {}",
cycles.len()
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_nested_cycles_imports() {
let imports = extract_imports_from_fixture("nested-cycles");
assert!(
imports.len() >= 5,
"nested-cycles should have at least 5 imports"
);
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_re_export_cycle_detection() {
let cycles = detect_cycles_in_fixture("re-export-cycle");
assert!(
!cycles.is_empty(),
"re-export-cycle should detect cycles through re-exports"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_re_export_cycle_imports() {
let imports = extract_imports_from_fixture("re-export-cycle");
let has_reexport = imports.iter().any(|(src, _)| src.contains("index"));
assert!(has_reexport, "should capture re-exports from index.ts");
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_index_file_cycle_detection() {
let cycles = detect_cycles_in_fixture("index-file-cycle");
assert!(
!cycles.is_empty(),
"index-file-cycle should detect barrel file cycles"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_index_file_cycle_imports() {
let imports = extract_imports_from_fixture("index-file-cycle");
assert!(
imports.len() >= 2,
"index-file-cycle should have at least 2 imports"
);
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_deep_nesting_cycle_detection() {
let cycles = detect_cycles_in_fixture("deep-nesting");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_deep_nesting_imports() {
let imports = extract_imports_from_fixture("deep-nesting");
let has_deep_import = imports
.iter()
.any(|(src, _)| src.contains("level1") || src.contains("level2") || src.contains("level3"));
assert!(
has_deep_import,
"should have imports from nested directories"
);
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_deep_nesting_parent_directory_imports() {
let imports = extract_imports_from_fixture("deep-nesting");
let has_parent_import = imports.iter().any(|(_, spec)| spec.contains(".."));
assert!(
has_parent_import,
"should have parent directory imports (..)"
);
}
#[test]
fn test_mixed_import_cycle_detection() {
let cycles = detect_cycles_in_fixture("mixed-import-cycle");
assert!(
!cycles.is_empty(),
"mixed-import-cycle should detect cycle through mixed import styles"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_mixed_import_cycle_imports() {
let imports = extract_imports_from_fixture("mixed-import-cycle");
assert!(
imports.len() >= 3,
"mixed-import-cycle should have at least 3 imports"
);
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_workspace_diamond_no_cycle() {
let cycles = detect_inter_package_cycles("workspace-diamond");
assert!(
cycles.is_empty(),
"workspace diamond should not have cycles, found: {cycles:?}"
);
}
#[test]
fn test_workspace_diamond_package_count() {
let path = fixture_path("workspace-diamond");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert_eq!(
workspace.packages.len(),
4,
"diamond should have 4 packages: core, left, right, bottom"
);
}
#[test]
fn test_workspace_diamond_package_names() {
let path = fixture_path("workspace-diamond");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert!(
workspace.packages.contains_key("@diamond/core"),
"should have @diamond/core"
);
assert!(
workspace.packages.contains_key("@diamond/left"),
"should have @diamond/left"
);
assert!(
workspace.packages.contains_key("@diamond/right"),
"should have @diamond/right"
);
assert!(
workspace.packages.contains_key("@diamond/bottom"),
"should have @diamond/bottom"
);
}
#[test]
fn test_workspace_shared_cycle_detection() {
let cycles = detect_inter_package_cycles("workspace-shared-cycle");
assert_eq!(
cycles.len(),
1,
"workspace-shared-cycle should have exactly 1 cycle"
);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_workspace_shared_cycle_package_count() {
let path = fixture_path("workspace-shared-cycle");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert_eq!(
workspace.packages.len(),
3,
"should have 3 packages: app, shared, utils"
);
}
#[test]
fn test_workspace_shared_cycle_involves_all_packages() {
let cycles = detect_inter_package_cycles("workspace-shared-cycle");
let cycle = cycles.first().expect("should have cycle");
assert!(cycle.contains("app"), "cycle should involve app package");
assert!(
cycle.contains("shared"),
"cycle should involve shared package"
);
assert!(
cycle.contains("utils"),
"cycle should involve utils package"
);
}
#[test]
fn test_graph_node_count_simple_cycle() {
let path = fixture_path("simple-cycle");
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(
vec![
"ts".to_owned(),
"tsx".to_owned(),
"js".to_owned(),
"jsx".to_owned(),
],
tsconfig,
);
let imports = extractor.extract(&path).expect("should succeed");
let graph = DependencyGraph::from_imports(&imports);
assert!(
graph.node_count() >= 2,
"simple-cycle graph should have at least 2 nodes"
);
}
#[test]
fn test_graph_edge_count_diamond() {
let path = fixture_path("diamond");
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(
vec![
"ts".to_owned(),
"tsx".to_owned(),
"js".to_owned(),
"jsx".to_owned(),
],
tsconfig,
);
let imports = extractor.extract(&path).expect("should succeed");
let graph = DependencyGraph::from_imports(&imports);
assert!(
graph.edge_count() >= 4,
"diamond graph should have at least 4 edges"
);
}
#[test]
fn test_non_existent_fixture() {
let path = fixture_path("non-existent-fixture-that-does-not-exist");
let tsconfig = TsConfig::discover(&path);
assert!(
tsconfig.is_none(),
"should return None for non-existent directory"
);
}
#[test]
fn test_empty_directory() {
let path = fixture_path("no-cycles");
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(vec!["ts".to_owned()], tsconfig);
let result = extractor.extract(&path);
assert!(result.is_ok(), "extraction should succeed");
}
#[test]
fn test_import_extraction_ts_only() {
let path = fixture_path("simple-cycle");
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(vec!["ts".to_owned()], tsconfig);
let imports = extractor.extract(&path).expect("should succeed");
for import in &imports {
let ext = import
.source
.extension()
.map(|e| e.to_string_lossy().to_string())
.unwrap_or_default();
assert_eq!(ext, "ts", "should only extract from .ts files");
}
}
#[test]
fn test_import_extraction_multiple_extensions() {
let path = fixture_path("import-styles");
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(
vec![
"ts".to_owned(),
"tsx".to_owned(),
"js".to_owned(),
"jsx".to_owned(),
],
tsconfig,
);
let imports = extractor.extract(&path).expect("should succeed");
let extensions: std::collections::HashSet<_> = imports
.iter()
.filter_map(|i| i.source.extension())
.map(|e| e.to_string_lossy().to_string())
.collect();
assert!(
!extensions.is_empty(),
"should extract from at least one file type"
);
}
#[test]
fn test_cycle_is_closed() {
let cycles = detect_cycles_in_fixture("simple-cycle");
let cycle = cycles.first().expect("should have cycle");
let part_count = cycle.split(" → ").count();
assert!(
part_count >= 2,
"cycle should have at least 2 parts (A → B means A → B → A)"
);
}
#[test]
fn test_cycles_are_sorted() {
let cycles = detect_cycles_in_fixture("complex-cycles");
let mut sorted_cycles = cycles.clone();
sorted_cycles.sort();
assert_eq!(
cycles, sorted_cycles,
"cycles should be returned in sorted order"
);
}
#[test]
fn test_no_duplicate_cycles() {
let cycles = detect_cycles_in_fixture("complex-cycles");
let unique: std::collections::HashSet<_> = cycles.iter().collect();
assert_eq!(
cycles.len(),
unique.len(),
"should not have duplicate cycles"
);
}
#[test]
fn test_circular_type_only_detection() {
let cycles = detect_cycles_in_fixture("circular-type-only");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_circular_type_only_has_cycle() {
let cycles = detect_cycles_in_fixture("circular-type-only");
assert!(
!cycles.is_empty(),
"type-only circular imports should still be detected"
);
}
#[test]
fn test_circular_type_only_imports() {
let imports = extract_imports_from_fixture("circular-type-only");
assert!(imports.len() >= 2, "should have at least 2 type imports");
}
#[test]
fn test_multiple_entry_points_detection() {
let cycles = detect_cycles_in_fixture("multiple-entry-points");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_multiple_entry_points_has_multiple_cycles() {
let cycles = detect_cycles_in_fixture("multiple-entry-points");
assert!(
cycles.len() >= 2,
"should detect multiple independent cycles from different entry points"
);
}
#[test]
fn test_multiple_entry_points_imports() {
let imports = extract_imports_from_fixture("multiple-entry-points");
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_workspace_shared_dep_no_cycle() {
let cycles = detect_inter_package_cycles("workspace-with-shared-dep");
assert!(
cycles.is_empty(),
"workspace with shared dependency (no back-edge) should have no cycles"
);
}
#[test]
fn test_workspace_shared_dep_discovery() {
let path = fixture_path("workspace-with-shared-dep");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert_eq!(workspace.packages.len(), 3, "should have 3 packages");
}
#[test]
fn test_workspace_shared_dep_package_names() {
let path = fixture_path("workspace-with-shared-dep");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
let mut names: Vec<_> = workspace.packages.keys().cloned().collect();
names.sort();
assert_eq!(
names,
vec!["@shared-dep/app", "@shared-dep/lib", "@shared-dep/shared"]
);
}
#[test]
fn test_alias_chain_detection() {
let cycles = detect_cycles_in_fixture("alias-chain");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_alias_chain_has_cycle() {
let cycles = detect_cycles_in_fixture("alias-chain");
assert!(
!cycles.is_empty(),
"alias chain (A->B->C->A via @aliases) should detect cycle"
);
}
#[test]
fn test_alias_chain_tsconfig_loaded() {
let path = fixture_path("alias-chain");
let tsconfig = TsConfig::discover(&path);
assert!(
tsconfig.is_some(),
"should load tsconfig.json with path aliases"
);
}
#[test]
fn test_side_effect_imports_detection() {
let cycles = detect_cycles_in_fixture("side-effect-imports");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_side_effect_imports_extracts_bare_imports() {
let imports = extract_imports_from_fixture("side-effect-imports");
let has_bare_imports = imports
.iter()
.any(|(_, spec)| spec == "./polyfill" || spec == "./setup");
assert!(
has_bare_imports,
"should extract side-effect (bare) imports"
);
}
#[test]
fn test_star_exports_detection() {
let cycles = detect_cycles_in_fixture("star-exports");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_star_exports_has_cycle() {
let cycles = detect_cycles_in_fixture("star-exports");
assert!(
!cycles.is_empty(),
"star re-exports (export * from) should detect cycles"
);
}
#[test]
fn test_star_exports_imports() {
let imports = extract_imports_from_fixture("star-exports");
insta::assert_debug_snapshot!(imports);
}
#[test]
fn test_default_exports_cycle_detection() {
let cycles = detect_cycles_in_fixture("default-exports-cycle");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_default_exports_cycle_has_cycle() {
let cycles = detect_cycles_in_fixture("default-exports-cycle");
assert!(
!cycles.is_empty(),
"default export imports should detect cycles"
);
}
#[test]
fn test_namespace_imports_detection() {
let cycles = detect_cycles_in_fixture("namespace-imports");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_namespace_imports_has_cycle() {
let cycles = detect_cycles_in_fixture("namespace-imports");
assert!(
!cycles.is_empty(),
"namespace imports (import * as) should detect cycles"
);
}
#[test]
fn test_namespace_imports_extraction() {
let imports = extract_imports_from_fixture("namespace-imports");
assert!(
imports.len() >= 4,
"should extract multiple namespace imports"
);
}
#[test]
fn test_very_long_cycle_detection() {
let cycles = detect_cycles_in_fixture("very-long-cycle");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_very_long_cycle_has_exactly_one_cycle() {
let cycles = detect_cycles_in_fixture("very-long-cycle");
assert_eq!(cycles.len(), 1, "should have exactly one 15-node cycle");
}
#[test]
fn test_very_long_cycle_node_count() {
let cycles = detect_cycles_in_fixture("very-long-cycle");
let cycle = cycles.first().expect("should have cycle");
let node_count = cycle.split(" → ").count();
assert!(
node_count >= 14,
"cycle should have at least 14 nodes shown (15 files)"
);
}
#[test]
fn test_wide_graph_no_cycles() {
let cycles = detect_cycles_in_fixture("wide-graph");
assert!(
cycles.is_empty(),
"wide graph with hub importing 8 leaf nodes should have no cycles"
);
}
#[test]
fn test_wide_graph_imports() {
let imports = extract_imports_from_fixture("wide-graph");
assert!(imports.len() >= 8, "hub should import 8 leaf nodes");
}
#[test]
fn test_hub_and_spoke_detection() {
let cycles = detect_cycles_in_fixture("hub-and-spoke");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_hub_and_spoke_has_cycles() {
let cycles = detect_cycles_in_fixture("hub-and-spoke");
assert!(!cycles.is_empty(), "hub-and-spoke should detect cycles");
}
#[test]
fn test_mutual_recursion_detection() {
let cycles = detect_cycles_in_fixture("mutual-recursion");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_mutual_recursion_has_cycle() {
let cycles = detect_cycles_in_fixture("mutual-recursion");
assert!(
!cycles.is_empty(),
"mutual recursion (even/odd) should detect cycle"
);
}
fn detect_cycles_with_extensions(fixture_name: &str, extensions: Vec<&str>) -> Vec<String> {
let path = fixture_path(fixture_name);
let tsconfig = TsConfig::discover(&path);
let extractor = ImportExtractor::new(
extensions.into_iter().map(ToOwned::to_owned).collect(),
tsconfig,
);
let imports = extractor
.extract(&path)
.expect("fixture extraction should succeed");
let graph = DependencyGraph::from_imports(&imports);
let cycles = CycleDetector::detect(&graph);
let mut cycle_strings: Vec<String> = cycles
.iter()
.map(|cycle| {
cycle
.path
.iter()
.filter_map(|p| {
p.strip_prefix(&path)
.ok()
.map(|rel| rel.to_string_lossy().replace('\\', "/"))
})
.collect::<Vec<_>>()
.join(" → ")
})
.collect();
cycle_strings.sort();
cycle_strings
}
#[test]
fn test_jsx_imports_detection() {
let cycles = detect_cycles_with_extensions("jsx-imports", vec!["tsx", "jsx", "ts", "js"]);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_jsx_imports_has_cycle() {
let cycles = detect_cycles_with_extensions("jsx-imports", vec!["tsx", "jsx", "ts", "js"]);
assert!(
!cycles.is_empty(),
"JSX component cycle (App->Button->App) should be detected"
);
}
#[test]
fn test_mjs_cjs_imports_detection() {
let cycles = detect_cycles_with_extensions("mjs-cjs-imports", vec!["mjs", "cjs", "js"]);
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_mjs_cjs_has_cycle() {
let cycles = detect_cycles_with_extensions("mjs-cjs-imports", vec!["mjs", "cjs", "js"]);
assert!(!cycles.is_empty(), "mjs<->cjs cycle should be detected");
}
#[test]
fn test_multiline_imports_detection() {
let cycles = detect_cycles_in_fixture("multiline-imports");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_multiline_imports_has_cycle() {
let cycles = detect_cycles_in_fixture("multiline-imports");
assert!(
!cycles.is_empty(),
"multiline imports should be parsed and cycles detected"
);
}
#[test]
fn test_multiline_imports_extraction() {
let imports = extract_imports_from_fixture("multiline-imports");
let main_import_count = imports
.iter()
.filter(|(src, _)| src.contains("main.ts"))
.count();
assert!(main_import_count >= 2, "should extract multiline imports");
}
#[test]
fn test_comments_in_imports_detection() {
let cycles = detect_cycles_in_fixture("comments-in-imports");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_comments_in_imports_ignores_commented() {
let imports = extract_imports_from_fixture("comments-in-imports");
let has_invalid_imports = imports
.iter()
.any(|(_, spec)| spec == "./x" || spec == "./y");
assert!(
!has_invalid_imports,
"should not extract commented-out imports"
);
}
#[test]
fn test_comments_in_imports_extracts_real_imports() {
let imports = extract_imports_from_fixture("comments-in-imports");
let real_import_specs: Vec<_> = imports.iter().map(|(_, spec)| spec.as_str()).collect();
assert!(real_import_specs.contains(&"./a"), "should extract ./a");
assert!(real_import_specs.contains(&"./b"), "should extract ./b");
assert!(real_import_specs.contains(&"./c"), "should extract ./c");
}
#[test]
fn test_string_escapes_no_false_positives() {
let imports = extract_imports_from_fixture("string-escapes");
let has_fake_imports = imports.iter().any(|(_, spec)| spec.contains("fake"));
assert!(
!has_fake_imports,
"should not extract import-like strings from regular string literals"
);
}
#[test]
fn test_string_escapes_extracts_real_imports() {
let imports = extract_imports_from_fixture("string-escapes");
let specs: Vec<_> = imports.iter().map(|(_, spec)| spec.as_str()).collect();
assert!(specs.contains(&"./a"), "should extract real import ./a");
assert!(specs.contains(&"./b"), "should extract real import ./b");
}
#[test]
fn test_workspace_five_packages_cycle() {
let cycles = detect_inter_package_cycles("workspace-five-packages");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_workspace_five_packages_has_cycle() {
let cycles = detect_inter_package_cycles("workspace-five-packages");
assert!(
!cycles.is_empty(),
"5-package circular dependency should be detected"
);
}
#[test]
fn test_workspace_five_packages_discovery() {
let path = fixture_path("workspace-five-packages");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert_eq!(workspace.packages.len(), 5, "should have 5 packages");
}
#[test]
fn test_workspace_five_packages_names() {
let path = fixture_path("workspace-five-packages");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
let mut names: Vec<_> = workspace.packages.keys().cloned().collect();
names.sort();
assert_eq!(
names,
vec!["@five/a", "@five/b", "@five/c", "@five/d", "@five/e"]
);
}
#[test]
fn test_pnpm_catalog_cycle() {
let cycles = detect_inter_package_cycles("pnpm-catalog");
insta::assert_debug_snapshot!(cycles);
}
#[test]
fn test_pnpm_catalog_has_cycle() {
let cycles = detect_inter_package_cycles("pnpm-catalog");
assert!(
!cycles.is_empty(),
"pnpm workspace cycle should be detected"
);
}
#[test]
fn test_pnpm_catalog_discovery() {
let path = fixture_path("pnpm-catalog");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
assert_eq!(workspace.packages.len(), 2, "should have 2 packages");
assert_eq!(
workspace.format,
WorkspaceFormat::Pnpm,
"should be pnpm format"
);
}
#[test]
fn test_import_extraction_empty_directory() {
let path = std::env::temp_dir().join("jscycles-test-empty");
std::fs::create_dir_all(&path).expect("should create temp dir");
let extractor = ImportExtractor::new(vec!["ts".to_owned()], None);
let imports = extractor
.extract(&path)
.expect("should succeed on empty dir");
assert!(imports.is_empty(), "empty directory should have no imports");
std::fs::remove_dir(&path).expect("should remove temp dir");
}
#[test]
fn test_import_extraction_with_subdirectories() {
let imports = extract_imports_from_fixture("deep-nesting");
assert!(
!imports.is_empty(),
"should extract imports from nested directories"
);
}
#[test]
fn test_import_extraction_deduplicates() {
let imports = extract_imports_from_fixture("simple-cycle");
let unique_pairs: std::collections::HashSet<_> = imports.iter().collect();
assert_eq!(
imports.len(),
unique_pairs.len(),
"should not have duplicate imports"
);
}
#[test]
fn test_graph_isolated_nodes() {
let imports = extract_raw_imports_from_fixture("no-cycles");
let graph = DependencyGraph::from_imports(&imports);
assert!(graph.node_count() > 0, "graph should have nodes");
let cycles = CycleDetector::detect(&graph);
assert!(
cycles.is_empty(),
"graph with isolated nodes should have no cycles"
);
}
#[test]
fn test_graph_self_loop_only() {
let imports = extract_raw_imports_from_fixture("self-loop");
let graph = DependencyGraph::from_imports(&imports);
let cycles = CycleDetector::detect(&graph);
assert_eq!(
cycles.len(),
1,
"self-loop should produce exactly one cycle"
);
}
#[test]
fn test_graph_from_empty_imports() {
let imports: Vec<jscycles::Import> = vec![];
let graph = DependencyGraph::from_imports(&imports);
assert_eq!(
graph.node_count(),
0,
"empty imports should produce empty graph"
);
}
#[test]
fn test_cycle_detector_deterministic() {
let cycles1 = detect_cycles_in_fixture("complex-cycles");
let cycles2 = detect_cycles_in_fixture("complex-cycles");
let cycles3 = detect_cycles_in_fixture("complex-cycles");
assert_eq!(
cycles1, cycles2,
"cycle detection should be deterministic (1 vs 2)"
);
assert_eq!(
cycles2, cycles3,
"cycle detection should be deterministic (2 vs 3)"
);
}
#[test]
fn test_cycle_detector_all_nodes_in_cycle_present() {
let cycles = detect_cycles_in_fixture("simple-cycle");
let cycle = cycles.first().expect("should have cycle");
assert!(
cycle.contains("a.ts") && cycle.contains("b.ts"),
"cycle should contain both participating files"
);
}
#[test]
fn test_workspace_not_workspace_directory() {
let path = fixture_path("simple-cycle");
let workspace = Workspace::discover(&path).expect("should succeed");
assert!(
workspace.is_none(),
"non-workspace directory should return None"
);
}
#[test]
fn test_workspace_packages_have_valid_paths() {
let path = fixture_path("workspace-five-packages");
let workspace = Workspace::discover(&path)
.expect("should succeed")
.expect("should be workspace");
let mut pkg_names: Vec<_> = workspace.packages.keys().collect();
pkg_names.sort();
for name in pkg_names {
let pkg_path = workspace.packages.get(name).expect("key should exist");
assert!(pkg_path.exists(), "package {name} path should exist");
assert!(
pkg_path.is_absolute(),
"package {name} path should be absolute"
);
assert!(pkg_path.is_dir(), "package {name} path should be directory");
}
}
#[test]
fn test_tsconfig_discovery_of_malformed_config_returns_none() {
let root = tempfile::tempdir().expect("temporary package");
std::fs::write(root.path().join("tsconfig.json"), "{").expect("malformed config");
std::fs::create_dir(root.path().join("src")).expect("source directory");
assert!(TsConfig::discover(&root.path().join("src")).is_none());
}
#[test]
fn test_tsconfig_with_no_paths() {
let root = tempfile::tempdir().expect("temporary package");
std::fs::write(root.path().join("tsconfig.json"), "{}").expect("config");
let tsconfig = TsConfig::discover(root.path()).expect("nearest config is selected");
assert!(tsconfig.paths.is_empty(), "no paths means no aliases");
assert!(tsconfig.resolve_alias("anything").is_none());
}
#[test]
fn test_very_long_cycle_completes_quickly() {
use std::time::Instant;
let start = Instant::now();
let cycles = detect_cycles_in_fixture("very-long-cycle");
let duration = start.elapsed();
assert_eq!(cycles.len(), 1, "one 15-module cycle");
assert_eq!(
cycles[0].matches(" → ").count(),
15,
"all 15 edges are present"
);
assert!(
duration.as_secs() < 5,
"15-node cycle detection should complete in under 5 seconds"
);
}
#[test]
fn test_hub_and_spoke_completes_quickly() {
use std::time::Instant;
let start = Instant::now();
let cycles = detect_cycles_in_fixture("hub-and-spoke");
let duration = start.elapsed();
assert_eq!(cycles.len(), 1, "one representative for the hub SCC");
assert!(
duration.as_secs() < 5,
"hub-and-spoke detection should complete in under 5 seconds"
);
}
#[test]
fn legacy_workspace_selection_canonicalizes_scan_paths() {
let root = tempfile::tempdir().expect("temporary workspace");
for name in ["a", "b"] {
let package = root.path().join("packages").join(name);
std::fs::create_dir_all(&package).expect("package directory");
std::fs::write(
package.join("package.json"),
format!(r#"{{"name":"{name}"}}"#),
)
.expect("manifest");
}
std::fs::write(
root.path().join("package.json"),
r#"{"name":"root","private":true,"workspaces":["packages/*"]}"#,
)
.expect("root manifest");
let workspace = Workspace::discover(root.path())
.expect("workspace metadata")
.expect("workspace");
let discovery = PackageDiscovery::new(&Config::default(), &[], &[]).expect("discovery");
let spelled = root.path().join("packages/a/../b");
let selected = discovery.discover_workspace_packages(&workspace, &[spelled]);
let names: Vec<_> = selected
.iter()
.map(|package| package.name.as_str())
.collect();
assert_eq!(names, ["b"]);
let manifest = root.path().join("packages/a/package.json");
let by_manifest = discovery.discover_workspace_packages(&workspace, &[manifest]);
let manifest_names: Vec<_> = by_manifest
.iter()
.map(|package| package.name.as_str())
.collect();
assert_eq!(manifest_names, ["a"]);
}