use std::io::Write;
use std::path::PathBuf;
use serde::Serialize;
use crate::cycles::{Cycle, PackageCycle, PackageCycleWithFiles};
use crate::terminal::label;
fn display_path(path: &std::path::Path, base_path: &std::path::Path) -> PathBuf {
path.strip_prefix(base_path).map_or_else(
|_| crate::input::absolute_form(path),
std::path::Path::to_path_buf,
)
}
#[derive(Debug, Clone)]
pub struct PackageResult {
pub name: String,
pub path: PathBuf,
pub cycles: Vec<Cycle>,
}
#[derive(Debug, Clone)]
pub struct Results {
pub packages: Vec<PackageResult>,
}
impl Results {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
packages: Vec::new(),
}
}
#[inline]
#[must_use]
pub fn has_cycles(&self) -> bool {
self.packages.iter().any(|p| !p.cycles.is_empty())
}
#[inline]
#[must_use]
pub fn total_cycles(&self) -> usize {
self.packages.iter().map(|p| p.cycles.len()).sum()
}
#[inline]
#[must_use]
pub fn packages_with_cycles(&self) -> usize {
self.packages
.iter()
.filter(|p| !p.cycles.is_empty())
.count()
}
}
impl Default for Results {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CycleFilter {
#[default]
All,
Inner,
Outer,
}
#[derive(Debug, Clone, Default)]
pub struct UnifiedResults {
pub file_results: Vec<PackageResult>,
pub package_cycles: Vec<PackageCycle>,
pub package_cycles_with_files: Vec<PackageCycleWithFiles>,
}
impl UnifiedResults {
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[inline]
#[must_use]
pub fn has_file_cycles(&self) -> bool {
self.file_results.iter().any(|p| !p.cycles.is_empty())
}
#[inline]
#[must_use]
pub const fn has_package_cycles(&self) -> bool {
!self.package_cycles.is_empty() || !self.package_cycles_with_files.is_empty()
}
#[inline]
#[must_use]
pub fn has_any_cycles(&self) -> bool {
self.has_file_cycles() || self.has_package_cycles()
}
#[inline]
#[must_use]
pub fn total_file_cycles(&self) -> usize {
self.file_results.iter().map(|p| p.cycles.len()).sum()
}
#[inline]
#[must_use]
pub fn packages_with_cycles(&self) -> usize {
self.file_results
.iter()
.filter(|p| !p.cycles.is_empty())
.count()
}
}
pub trait OutputFormatter {
fn format(&self, results: &Results, writer: &mut dyn Write) -> std::io::Result<()>;
}
pub trait UnifiedOutputFormatter {
fn format_unified(
&self,
results: &UnifiedResults,
filter: CycleFilter,
writer: &mut dyn Write,
) -> std::io::Result<()>;
}
#[derive(Debug, Default)]
pub struct HumanFormatter {
pub use_color: bool,
pub verbose: bool,
}
impl HumanFormatter {
#[must_use]
pub fn escape_label(text: &str) -> String {
label(text)
}
#[inline]
#[must_use]
pub const fn new(use_color: bool) -> Self {
Self {
use_color,
verbose: false,
}
}
#[inline]
#[must_use]
pub const fn with_verbose(mut self, verbose: bool) -> Self {
self.verbose = verbose;
self
}
fn format_cycle(cycle: &Cycle, base_path: &std::path::Path) -> String {
cycle
.path
.iter()
.map(|p| label(&display_path(p, base_path).to_string_lossy()))
.collect::<Vec<_>>()
.join(" -> ")
}
fn format_package(
&self,
package: &PackageResult,
writer: &mut dyn Write,
) -> std::io::Result<bool> {
if package.cycles.is_empty() {
if self.verbose {
self.format_clean_package(package, writer)?;
return Ok(true);
}
return Ok(false);
}
self.format_cyclic_package(package, writer)?;
Ok(true)
}
fn format_clean_package(
&self,
package: &PackageResult,
writer: &mut dyn Write,
) -> std::io::Result<()> {
if self.use_color {
writeln!(
writer,
"\x1b[32m✓\x1b[0m {}: no cycles",
label(&package.name)
)
} else {
writeln!(writer, "✓ {}: no cycles", label(&package.name))
}
}
fn format_cyclic_package(
&self,
package: &PackageResult,
writer: &mut dyn Write,
) -> std::io::Result<()> {
let cycle_word = if package.cycles.len() == 1 {
"cycle"
} else {
"cycles"
};
let name = label(&package.name);
let count = package.cycles.len();
if self.use_color {
writeln!(writer, "\x1b[31m✗\x1b[0m {name}: {count} {cycle_word}")?;
} else {
writeln!(writer, "✗ {name}: {count} {cycle_word}")?;
}
for cycle in &package.cycles {
let formatted = Self::format_cycle(cycle, &package.path);
writeln!(writer, " {formatted}")?;
}
Ok(())
}
fn format_summary(results: &Results, writer: &mut dyn Write) -> std::io::Result<()> {
let total = results.packages.len();
let with_cycles = results.packages_with_cycles();
let total_cycles = results.total_cycles();
let packages = counted(total, "package", "packages");
let cycles = counted(total_cycles, "total cycle", "total cycles");
writeln!(
writer,
"Summary: {packages} checked, {with_cycles} with cycles, {cycles}"
)
}
fn format_package_cycles_section(
&self,
results: &UnifiedResults,
writer: &mut dyn Write,
) -> std::io::Result<()> {
if !results.package_cycles_with_files.is_empty() {
return self.format_package_cycles_with_files(results, writer);
}
if results.package_cycles.is_empty() {
if self.use_color {
writeln!(writer, "\x1b[32m✓\x1b[0m No inter-package cycles")?;
} else {
writeln!(writer, "✓ No inter-package cycles")?;
}
} else {
let count = results.package_cycles.len();
let cycle_word = if count == 1 { "cycle" } else { "cycles" };
if self.use_color {
writeln!(
writer,
"\x1b[31m✗\x1b[0m {count} inter-package {cycle_word}"
)?;
} else {
writeln!(writer, "✗ {count} inter-package {cycle_word}")?;
}
for cycle in &results.package_cycles {
let formatted = cycle
.packages
.iter()
.map(|name| label(name))
.collect::<Vec<_>>()
.join(" -> ");
writeln!(writer, " {formatted}")?;
}
}
writeln!(writer)
}
fn format_package_cycles_with_files(
&self,
results: &UnifiedResults,
writer: &mut dyn Write,
) -> std::io::Result<()> {
let count = results.package_cycles_with_files.len();
if count == 0 {
if self.use_color {
writeln!(writer, "\x1b[32m✓\x1b[0m No inter-package cycles")?;
} else {
writeln!(writer, "✓ No inter-package cycles")?;
}
return writeln!(writer);
}
let cycle_word = if count == 1 { "cycle" } else { "cycles" };
if self.use_color {
writeln!(
writer,
"\x1b[31m✗\x1b[0m {count} inter-package {cycle_word}"
)?;
} else {
writeln!(writer, "✗ {count} inter-package {cycle_word}")?;
}
for cycle in &results.package_cycles_with_files {
Self::format_single_cycle_with_files(cycle, writer)?;
}
writeln!(writer)
}
fn format_single_cycle_with_files(
cycle: &PackageCycleWithFiles,
writer: &mut dyn Write,
) -> std::io::Result<()> {
let formatted = cycle
.packages
.iter()
.map(|name| label(name))
.collect::<Vec<_>>()
.join(" -> ");
writeln!(writer, "\n {formatted}")?;
for edge in &cycle.edges {
Self::format_cycle_edge(edge, writer)?;
}
Ok(())
}
fn format_cycle_edge(
edge: &crate::cycles::PackageCycleEdge,
writer: &mut dyn Write,
) -> std::io::Result<()> {
writeln!(
writer,
" {} -> {}",
label(&edge.from_package),
label(&edge.to_package)
)?;
for (file, subpath) in &edge.files {
Self::format_edge_file(file, &edge.to_package, subpath.as_deref(), writer)?;
}
Ok(())
}
fn format_edge_file(
file: &std::path::Path,
package: &str,
subpath: Option<&str>,
writer: &mut dyn Write,
) -> std::io::Result<()> {
let file_display = label(&file.to_string_lossy());
if let Some(subpath_value) = subpath {
let target = label(&format!("{package}/{subpath_value}"));
writeln!(writer, " - {file_display} (imports {target})")
} else {
writeln!(writer, " - {file_display}")
}
}
fn format_file_cycles_section(
&self,
results: &UnifiedResults,
writer: &mut dyn Write,
) -> std::io::Result<()> {
if results.file_results.is_empty() {
writeln!(writer, "No packages found.")?;
return Ok(());
}
for package in &results.file_results {
let written = self.format_package(package, writer)?;
if written {
writeln!(writer)?;
}
}
Ok(())
}
fn format_unified_summary(
results: &UnifiedResults,
filter: CycleFilter,
writer: &mut dyn Write,
) -> std::io::Result<()> {
let total_pkgs = results.file_results.len();
let with_file_cycles = results.packages_with_cycles();
let total_file_cycles = results.total_file_cycles();
let pkg_cycles = if results.package_cycles_with_files.is_empty() {
results.package_cycles.len()
} else {
results.package_cycles_with_files.len()
};
let packages = counted(total_pkgs, "package", "packages");
let package_cycles = counted(pkg_cycles, "inter-package cycle", "inter-package cycles");
match filter {
CycleFilter::All => writeln!(
writer,
"Summary: {packages}, {with_file_cycles} with file cycles \
({total_file_cycles} total), {package_cycles}"
),
CycleFilter::Inner => writeln!(
writer,
"Summary: {packages}, {with_file_cycles} with file cycles \
({total_file_cycles} total)"
),
CycleFilter::Outer => writeln!(writer, "Summary: {package_cycles}"),
}
}
}
fn counted(count: usize, singular: &str, plural: &str) -> String {
format!("{count} {}", if count == 1 { singular } else { plural })
}
impl OutputFormatter for HumanFormatter {
fn format(&self, results: &Results, writer: &mut dyn Write) -> std::io::Result<()> {
if results.packages.is_empty() {
writeln!(writer, "No packages found.")?;
return Ok(());
}
for package in &results.packages {
let written = self.format_package(package, writer)?;
if written {
writeln!(writer)?;
}
}
Self::format_summary(results, writer)
}
}
impl UnifiedOutputFormatter for HumanFormatter {
fn format_unified(
&self,
results: &UnifiedResults,
filter: CycleFilter,
writer: &mut dyn Write,
) -> std::io::Result<()> {
let show_file = matches!(filter, CycleFilter::All | CycleFilter::Inner);
let show_package = matches!(filter, CycleFilter::All | CycleFilter::Outer);
if show_package {
self.format_package_cycles_section(results, writer)?;
}
if show_file {
self.format_file_cycles_section(results, writer)?;
}
Self::format_unified_summary(results, filter, writer)
}
}
#[derive(Debug, Default)]
pub struct JsonFormatter;
#[derive(Debug, Serialize)]
struct JsonOutput {
has_cycles: bool,
packages: Vec<JsonPackage>,
}
#[derive(Debug, Serialize)]
struct JsonPackage {
name: String,
path: String,
cycles: Vec<Vec<String>>,
}
impl JsonFormatter {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self
}
fn convert_package(package: &PackageResult) -> JsonPackage {
let cycles: Vec<Vec<String>> = package
.cycles
.iter()
.map(|c| Self::convert_cycle(c, &package.path))
.collect();
JsonPackage {
name: package.name.clone(),
path: package.path.to_string_lossy().to_string(),
cycles,
}
}
fn convert_cycle(cycle: &Cycle, base_path: &std::path::Path) -> Vec<String> {
cycle
.path
.iter()
.map(|path| display_path(path, base_path).to_string_lossy().into_owned())
.collect()
}
fn convert_cycle_with_files(cycle: &PackageCycleWithFiles) -> JsonPackageCycleWithFiles {
let edges = cycle
.edges
.iter()
.map(|edge| JsonPackageCycleEdge {
from_package: edge.from_package.clone(),
to_package: edge.to_package.clone(),
files: edge
.files
.iter()
.map(|(path, subpath)| JsonEdgeFile {
path: path.to_string_lossy().to_string(),
subpath: subpath.clone(),
})
.collect(),
})
.collect();
JsonPackageCycleWithFiles {
packages: cycle.packages.clone(),
edges,
}
}
fn convert_basic_cycle(cycle: &PackageCycle) -> JsonPackageCycleWithFiles {
let mut edges = Vec::new();
for window in cycle.packages.windows(2) {
let (Some(from), Some(to)) = (window.first(), window.get(1)) else {
continue;
};
edges.push(JsonPackageCycleEdge {
from_package: from.clone(),
to_package: to.clone(),
files: Vec::new(),
});
}
JsonPackageCycleWithFiles {
packages: cycle.packages.clone(),
edges,
}
}
}
impl OutputFormatter for JsonFormatter {
fn format(&self, results: &Results, writer: &mut dyn Write) -> std::io::Result<()> {
let packages: Vec<JsonPackage> =
results.packages.iter().map(Self::convert_package).collect();
let output = JsonOutput {
has_cycles: results.has_cycles(),
packages,
};
serde_json::to_writer_pretty(&mut *writer, &output).map_err(std::io::Error::other)?;
writeln!(writer)?;
Ok(())
}
}
#[derive(Debug, Serialize)]
struct JsonPackageCycleEdge {
from_package: String,
to_package: String,
files: Vec<JsonEdgeFile>,
}
#[derive(Debug, Serialize)]
struct JsonEdgeFile {
path: String,
#[serde(skip_serializing_if = "Option::is_none")]
subpath: Option<String>,
}
#[derive(Debug, Serialize)]
struct JsonPackageCycleWithFiles {
packages: Vec<String>,
edges: Vec<JsonPackageCycleEdge>,
}
#[derive(Debug, Serialize)]
#[expect(
clippy::struct_field_names,
reason = "JSON field names are part of API"
)]
struct JsonUnifiedOutput {
has_cycles: bool,
has_file_cycles: bool,
has_package_cycles: bool,
file_cycles: Vec<JsonPackage>,
package_cycles: Vec<JsonPackageCycleWithFiles>,
}
impl UnifiedOutputFormatter for JsonFormatter {
fn format_unified(
&self,
results: &UnifiedResults,
filter: CycleFilter,
writer: &mut dyn Write,
) -> std::io::Result<()> {
let file_cycles: Vec<JsonPackage> = match filter {
CycleFilter::All | CycleFilter::Inner => results
.file_results
.iter()
.map(Self::convert_package)
.collect(),
CycleFilter::Outer => Vec::new(),
};
let package_cycles: Vec<JsonPackageCycleWithFiles> = match filter {
CycleFilter::All | CycleFilter::Outer => {
if results.package_cycles_with_files.is_empty() {
results
.package_cycles
.iter()
.map(Self::convert_basic_cycle)
.collect()
} else {
results
.package_cycles_with_files
.iter()
.map(Self::convert_cycle_with_files)
.collect()
}
},
CycleFilter::Inner => Vec::new(),
};
let output = JsonUnifiedOutput {
has_cycles: results.has_any_cycles(),
has_file_cycles: results.has_file_cycles(),
has_package_cycles: results.has_package_cycles(),
file_cycles,
package_cycles,
};
serde_json::to_writer_pretty(&mut *writer, &output).map_err(std::io::Error::other)?;
writeln!(writer)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counts_use_the_singular_only_for_one() {
assert_eq!(counted(0, "package", "packages"), "0 packages");
assert_eq!(counted(1, "package", "packages"), "1 package");
assert_eq!(counted(2, "package", "packages"), "2 packages");
}
#[test]
fn cycle_members_outside_the_package_are_shown_absolute_in_user_form() {
let (base, inside, outside, shown) = if cfg!(windows) {
(
r"\\?\C:\repo\a",
r"\\?\C:\repo\a\x.ts",
r"\\?\C:\repo\lib\y.ts",
r"C:\repo\lib\y.ts",
)
} else {
(
"/repo/a",
"/repo/a/x.ts",
"/repo/lib/y.ts",
"/repo/lib/y.ts",
)
};
let base = std::path::Path::new(base);
assert_eq!(
display_path(std::path::Path::new(inside), base),
PathBuf::from("x.ts")
);
assert_eq!(
display_path(std::path::Path::new(outside), base),
PathBuf::from(shown)
);
}
#[test]
fn human_reports_expose_every_bidi_control_and_unicode_line_separator() {
for character in [
'\u{061c}', '\u{200e}', '\u{200f}', '\u{202a}', '\u{202b}', '\u{202c}', '\u{202d}',
'\u{202e}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{2069}', '\u{2028}', '\u{2029}',
] {
let name = format!("pkg-{character}-λ");
let mut results = Results::new();
results.packages.push(PackageResult {
name: name.clone(),
path: PathBuf::from(&name),
cycles: vec![Cycle::new(
["a.ts", "b.ts", "a.ts"].map(PathBuf::from).to_vec(),
)],
});
let mut output = Vec::new();
HumanFormatter::new(false)
.format(&results, &mut output)
.expect("human report");
let text = String::from_utf8(output).expect("UTF-8 report");
assert!(
!text.contains(character),
"no invisible directional or line control"
);
assert!(
text.contains(&character.escape_unicode().to_string()),
"visible escape"
);
assert!(text.contains('λ'), "ordinary Unicode is preserved");
}
}
#[test]
fn test_empty_results() {
let results = Results::new();
assert!(!results.has_cycles(), "empty results should have no cycles");
assert_eq!(
results.total_cycles(),
0,
"empty results should have 0 total cycles"
);
}
#[test]
fn test_has_cycles() {
let mut results = Results::new();
results.packages.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("a.ts"),
PathBuf::from("b.ts"),
PathBuf::from("a.ts"),
])],
});
assert!(
results.has_cycles(),
"results with cycles should report has_cycles"
);
assert_eq!(
results.total_cycles(),
1,
"results should have 1 total cycle"
);
assert_eq!(
results.packages_with_cycles(),
1,
"results should have 1 package with cycles"
);
}
#[test]
fn test_human_formatter() {
let formatter = HumanFormatter::new(false);
let results = Results::new();
let mut output = Vec::new();
formatter
.format(&results, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("No packages found"),
"empty results should show 'No packages found'"
);
}
#[test]
fn test_json_formatter() {
let formatter = JsonFormatter::new();
let mut results = Results::new();
results.packages.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![],
});
let mut output = Vec::new();
formatter
.format(&results, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("\"has_cycles\": false"),
"JSON output should contain has_cycles: false"
);
}
#[test]
fn test_unified_results_new() {
let results = UnifiedResults::new();
assert!(
results.file_results.is_empty(),
"new unified results should have no file results"
);
assert!(
results.package_cycles.is_empty(),
"new unified results should have no package cycles"
);
}
#[test]
fn test_unified_results_has_cycles() {
let mut results = UnifiedResults::new();
assert!(
!results.has_file_cycles(),
"empty should have no file cycles"
);
assert!(
!results.has_package_cycles(),
"empty should have no package cycles"
);
assert!(!results.has_any_cycles(), "empty should have no cycles");
results.file_results.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("a.ts"),
PathBuf::from("b.ts"),
PathBuf::from("a.ts"),
])],
});
assert!(
results.has_file_cycles(),
"should have file cycles after adding"
);
assert!(
results.has_any_cycles(),
"should have any cycles after adding file cycle"
);
assert!(
!results.has_package_cycles(),
"should not have package cycles yet"
);
results.package_cycles.push(PackageCycle::new(vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
]));
assert!(
results.has_package_cycles(),
"should have package cycles after adding"
);
assert!(
results.has_any_cycles(),
"should have any cycles after adding package cycle"
);
}
#[test]
fn test_unified_results_totals() {
let mut results = UnifiedResults::new();
assert_eq!(
results.total_file_cycles(),
0,
"empty should have 0 file cycles"
);
assert_eq!(
results.packages_with_cycles(),
0,
"empty should have 0 packages with cycles"
);
results.file_results.push(PackageResult {
name: "@test/pkg-a".to_owned(),
path: PathBuf::from("/test/a"),
cycles: vec![
Cycle::new(vec![
PathBuf::from("a.ts"),
PathBuf::from("b.ts"),
PathBuf::from("a.ts"),
]),
Cycle::new(vec![
PathBuf::from("c.ts"),
PathBuf::from("d.ts"),
PathBuf::from("c.ts"),
]),
],
});
results.file_results.push(PackageResult {
name: "@test/pkg-b".to_owned(),
path: PathBuf::from("/test/b"),
cycles: vec![Cycle::new(vec![
PathBuf::from("x.ts"),
PathBuf::from("y.ts"),
PathBuf::from("x.ts"),
])],
});
results.file_results.push(PackageResult {
name: "@test/pkg-c".to_owned(),
path: PathBuf::from("/test/c"),
cycles: vec![],
});
assert_eq!(
results.total_file_cycles(),
3,
"should have 3 total file cycles"
);
assert_eq!(
results.packages_with_cycles(),
2,
"should have 2 packages with cycles"
);
}
#[test]
fn test_cycle_filter_default() {
let filter = CycleFilter::default();
assert_eq!(filter, CycleFilter::All, "default filter should be All");
}
#[test]
fn test_human_formatter_with_cycles() {
let formatter = HumanFormatter::new(false);
let mut results = Results::new();
results.packages.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("/test/a.ts"),
PathBuf::from("/test/b.ts"),
PathBuf::from("/test/a.ts"),
])],
});
let mut output = Vec::new();
formatter
.format(&results, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("@test/pkg"),
"should contain package name"
);
assert!(output_str.contains("1 cycle"), "should show cycle count");
assert!(
output_str.contains("a.ts") && output_str.contains("b.ts"),
"should show cycle files"
);
}
#[test]
fn test_human_formatter_with_color() {
let formatter = HumanFormatter::new(true).with_verbose(true);
let mut results = Results::new();
results.packages.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![],
});
let mut output = Vec::new();
formatter
.format(&results, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("\x1b[32m"),
"should contain green color code for no cycles"
);
}
#[test]
fn test_json_formatter_with_cycles() {
let formatter = JsonFormatter::new();
let mut results = Results::new();
results.packages.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("/test/a.ts"),
PathBuf::from("/test/b.ts"),
PathBuf::from("/test/a.ts"),
])],
});
let mut output = Vec::new();
formatter
.format(&results, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("\"has_cycles\": true"),
"JSON should report has_cycles: true"
);
assert!(
output_str.contains("@test/pkg"),
"JSON should contain package name"
);
assert!(
output_str.contains("a.ts"),
"JSON should contain cycle file"
);
}
#[test]
fn test_unified_human_formatter_all_filter() {
let formatter = HumanFormatter::new(false);
let mut results = UnifiedResults::new();
results.file_results.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("/test/a.ts"),
PathBuf::from("/test/b.ts"),
PathBuf::from("/test/a.ts"),
])],
});
results.package_cycles.push(PackageCycle::new(vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
]));
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::All, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("inter-package"),
"should show inter-package cycles with All filter"
);
assert!(
output_str.contains("@test/pkg"),
"should show file-level package with All filter"
);
}
#[test]
fn test_unified_human_formatter_inner_filter() {
let formatter = HumanFormatter::new(false);
let mut results = UnifiedResults::new();
results.file_results.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("/test/a.ts"),
PathBuf::from("/test/b.ts"),
PathBuf::from("/test/a.ts"),
])],
});
results.package_cycles.push(PackageCycle::new(vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
]));
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Inner, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("@test/pkg"),
"should show file-level package with Inner filter"
);
assert!(
output_str.contains("file cycles"),
"should mention file cycles in summary"
);
}
#[test]
fn test_unified_human_formatter_outer_filter() {
let formatter = HumanFormatter::new(false);
let mut results = UnifiedResults::new();
results.file_results.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("/test/a.ts"),
PathBuf::from("/test/b.ts"),
PathBuf::from("/test/a.ts"),
])],
});
results.package_cycles.push(PackageCycle::new(vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
]));
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Outer, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("inter-package"),
"should show inter-package cycles with Outer filter"
);
}
#[test]
fn test_unified_json_formatter_all_filter() {
let formatter = JsonFormatter::new();
let mut results = UnifiedResults::new();
results.file_results.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("/test/a.ts"),
PathBuf::from("/test/b.ts"),
PathBuf::from("/test/a.ts"),
])],
});
results.package_cycles.push(PackageCycle::new(vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
]));
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::All, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("\"has_cycles\": true"),
"JSON should report has_cycles"
);
assert!(
output_str.contains("\"has_file_cycles\": true"),
"JSON should report has_file_cycles"
);
assert!(
output_str.contains("\"has_package_cycles\": true"),
"JSON should report has_package_cycles"
);
assert!(
output_str.contains("\"file_cycles\""),
"JSON should include file_cycles"
);
assert!(
output_str.contains("\"package_cycles\""),
"JSON should include package_cycles"
);
}
#[test]
fn test_unified_json_formatter_inner_filter() {
let formatter = JsonFormatter::new();
let mut results = UnifiedResults::new();
results.file_results.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![],
});
results.package_cycles.push(PackageCycle::new(vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
]));
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Inner, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("\"package_cycles\": []"),
"JSON should have empty package_cycles with Inner filter"
);
}
#[test]
fn test_unified_json_formatter_outer_filter() {
let formatter = JsonFormatter::new();
let mut results = UnifiedResults::new();
results.file_results.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("a.ts"),
PathBuf::from("a.ts"),
])],
});
results.package_cycles.push(PackageCycle::new(vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
]));
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Outer, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("\"file_cycles\": []"),
"JSON should have empty file_cycles with Outer filter"
);
assert!(
output_str.contains("@pkg/a"),
"JSON should include package cycle"
);
}
#[test]
fn test_human_formatter_multiple_cycles() {
let formatter = HumanFormatter::new(false);
let mut results = Results::new();
results.packages.push(PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![
Cycle::new(vec![
PathBuf::from("/test/a.ts"),
PathBuf::from("/test/b.ts"),
PathBuf::from("/test/a.ts"),
]),
Cycle::new(vec![
PathBuf::from("/test/c.ts"),
PathBuf::from("/test/d.ts"),
PathBuf::from("/test/c.ts"),
]),
],
});
let mut output = Vec::new();
formatter
.format(&results, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("2 cycles"),
"should show plural 'cycles' for multiple"
);
}
#[test]
fn test_results_default() {
let results = Results::default();
assert!(
results.packages.is_empty(),
"default results should be empty"
);
assert!(
!results.has_cycles(),
"default results should have no cycles"
);
}
#[test]
fn test_unified_results_default() {
let results = UnifiedResults::default();
assert!(
results.file_results.is_empty(),
"default unified results should have no file results"
);
assert!(
results.package_cycles.is_empty(),
"default unified results should have no package cycles"
);
}
#[test]
fn test_package_result_clone() {
let result = PackageResult {
name: "@test/pkg".to_owned(),
path: PathBuf::from("/test"),
cycles: vec![Cycle::new(vec![
PathBuf::from("a.ts"),
PathBuf::from("a.ts"),
])],
};
let cloned = result.clone();
assert_eq!(cloned.name, result.name, "cloned name should match");
assert_eq!(cloned.path, result.path, "cloned path should match");
assert_eq!(
cloned.cycles.len(),
result.cycles.len(),
"cloned cycles should match"
);
}
#[test]
fn test_human_formatter_no_package_cycles() {
let formatter = HumanFormatter::new(false);
let results = UnifiedResults::new();
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Outer, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("No inter-package cycles"),
"should show no inter-package cycles message"
);
}
#[test]
fn test_human_formatter_package_cycle_format() {
let formatter = HumanFormatter::new(false);
let mut results = UnifiedResults::new();
results.package_cycles.push(PackageCycle::new(vec![
"@myorg/a".to_owned(),
"@myorg/b".to_owned(),
"@myorg/c".to_owned(),
"@myorg/a".to_owned(),
]));
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Outer, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("@myorg/a -> @myorg/b -> @myorg/c -> @myorg/a"),
"should format package cycle with arrows"
);
}
#[test]
fn test_human_formatter_cycle_with_files() {
use crate::cycles::{PackageCycleEdge, PackageCycleWithFiles};
let formatter = HumanFormatter::new(false);
let mut results = UnifiedResults::new();
let mut edge1 = PackageCycleEdge::new("@pkg/a".to_owned(), "@pkg/b".to_owned());
edge1
.files
.push((PathBuf::from("/project/a/index.ts"), None));
let mut edge2 = PackageCycleEdge::new("@pkg/b".to_owned(), "@pkg/a".to_owned());
edge2.files.push((
PathBuf::from("/project/b/utils.ts"),
Some("helpers".to_owned()),
));
results
.package_cycles_with_files
.push(PackageCycleWithFiles {
packages: vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
],
edges: vec![edge1, edge2],
});
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Outer, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("@pkg/a -> @pkg/b -> @pkg/a"),
"should show cycle summary: {output_str}"
);
assert!(
output_str.contains("@pkg/a -> @pkg/b"),
"should show first edge: {output_str}"
);
assert!(
output_str.contains("@pkg/b -> @pkg/a"),
"should show second edge: {output_str}"
);
assert!(
output_str.contains("/project/a/index.ts"),
"should show first file path: {output_str}"
);
assert!(
output_str.contains("/project/b/utils.ts"),
"should show second file path: {output_str}"
);
assert!(
output_str.contains("(imports @pkg/a/helpers)"),
"should show subpath: {output_str}"
);
}
#[test]
fn test_human_formatter_cycle_with_files_multiple_files() {
use crate::cycles::{PackageCycleEdge, PackageCycleWithFiles};
let formatter = HumanFormatter::new(false);
let mut results = UnifiedResults::new();
let mut edge1 = PackageCycleEdge::new("@pkg/a".to_owned(), "@pkg/b".to_owned());
edge1
.files
.push((PathBuf::from("/project/a/index.ts"), None));
edge1.files.push((
PathBuf::from("/project/a/helpers.ts"),
Some("utils".to_owned()),
));
edge1
.files
.push((PathBuf::from("/project/a/types.ts"), None));
let mut edge2 = PackageCycleEdge::new("@pkg/b".to_owned(), "@pkg/a".to_owned());
edge2.files.push((PathBuf::from("/project/b/lib.ts"), None));
results
.package_cycles_with_files
.push(PackageCycleWithFiles {
packages: vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
],
edges: vec![edge1, edge2],
});
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Outer, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("/project/a/index.ts"),
"should show first file"
);
assert!(
output_str.contains("/project/a/helpers.ts"),
"should show second file"
);
assert!(
output_str.contains("/project/a/types.ts"),
"should show third file"
);
assert!(
output_str.contains("/project/b/lib.ts"),
"should show fourth file"
);
}
#[test]
fn test_human_formatter_uses_cycles_with_files_when_available() {
use crate::cycles::{PackageCycleEdge, PackageCycleWithFiles};
let formatter = HumanFormatter::new(false);
let mut results = UnifiedResults::new();
results.package_cycles.push(PackageCycle::new(vec![
"@pkg/x".to_owned(),
"@pkg/y".to_owned(),
"@pkg/x".to_owned(),
]));
let mut edge = PackageCycleEdge::new("@pkg/a".to_owned(), "@pkg/b".to_owned());
edge.files.push((PathBuf::from("/detailed.ts"), None));
results
.package_cycles_with_files
.push(PackageCycleWithFiles {
packages: vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
],
edges: vec![
edge,
PackageCycleEdge::new("@pkg/b".to_owned(), "@pkg/a".to_owned()),
],
});
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Outer, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
assert!(
output_str.contains("/detailed.ts"),
"should use detailed format when available: {output_str}"
);
}
#[test]
fn test_json_formatter_cycle_with_files() {
use crate::cycles::{PackageCycleEdge, PackageCycleWithFiles};
let formatter = JsonFormatter::new();
let mut results = UnifiedResults::new();
let mut edge = PackageCycleEdge::new("@pkg/a".to_owned(), "@pkg/b".to_owned());
edge.files
.push((PathBuf::from("/src/index.ts"), Some("lib".to_owned())));
results
.package_cycles_with_files
.push(PackageCycleWithFiles {
packages: vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
],
edges: vec![
edge,
PackageCycleEdge::new("@pkg/b".to_owned(), "@pkg/a".to_owned()),
],
});
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Outer, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
let json: serde_json::Value =
serde_json::from_str(&output_str).expect("output should be valid JSON");
assert!(
json.get("has_package_cycles")
.and_then(serde_json::Value::as_bool)
.unwrap_or(false),
"has_package_cycles should be true"
);
let package_cycles = json
.get("package_cycles")
.and_then(|v| v.as_array())
.expect("package_cycles should be array");
assert_eq!(package_cycles.len(), 1, "should have 1 cycle");
let cycle = package_cycles.first().expect("should have first cycle");
let edges = cycle
.get("edges")
.and_then(|v| v.as_array())
.expect("should have edges");
assert_eq!(edges.len(), 2, "should have 2 edges");
let first_edge = edges.first().expect("should have first edge");
assert_eq!(
first_edge.get("from_package").and_then(|v| v.as_str()),
Some("@pkg/a"),
"from_package should be @pkg/a"
);
assert_eq!(
first_edge.get("to_package").and_then(|v| v.as_str()),
Some("@pkg/b"),
"to_package should be @pkg/b"
);
let files = first_edge
.get("files")
.and_then(|v| v.as_array())
.expect("should have files");
assert_eq!(files.len(), 1, "should have 1 file");
let file = files.first().expect("should have first file");
assert_eq!(
file.get("path").and_then(|v| v.as_str()),
Some("/src/index.ts"),
"file path should match"
);
assert_eq!(
file.get("subpath").and_then(|v| v.as_str()),
Some("lib"),
"file subpath should match"
);
}
#[test]
fn json_edge_file_without_subpath_omits_the_key() {
let value = serde_json::to_value(JsonEdgeFile {
path: "/src/index.ts".to_owned(),
subpath: None,
})
.expect("serializable");
assert_eq!(value, serde_json::json!({"path": "/src/index.ts"}));
}
#[test]
fn test_json_formatter_basic_cycle_without_files() {
let formatter = JsonFormatter::new();
let mut results = UnifiedResults::new();
results.package_cycles.push(PackageCycle::new(vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
]));
let mut output = Vec::new();
formatter
.format_unified(&results, CycleFilter::Outer, &mut output)
.expect("test formatting should succeed");
let output_str = String::from_utf8(output).expect("test output should be valid UTF-8");
let json: serde_json::Value =
serde_json::from_str(&output_str).expect("output should be valid JSON");
let package_cycles = json
.get("package_cycles")
.and_then(|v| v.as_array())
.expect("package_cycles should be array");
assert_eq!(package_cycles.len(), 1, "should have 1 cycle");
let cycle = package_cycles.first().expect("should have first cycle");
let edges = cycle
.get("edges")
.and_then(|v| v.as_array())
.expect("should have edges");
let first_edge = edges.first().expect("should have first edge");
let files = first_edge
.get("files")
.and_then(|v| v.as_array())
.expect("should have files array");
assert!(files.is_empty(), "files should be empty for basic cycle");
}
#[test]
fn test_unified_results_has_package_cycles_with_files() {
use crate::cycles::{PackageCycleEdge, PackageCycleWithFiles};
let mut results = UnifiedResults::new();
assert!(
!results.has_package_cycles(),
"empty should have no package cycles"
);
results
.package_cycles_with_files
.push(PackageCycleWithFiles {
packages: vec![
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
"@pkg/a".to_owned(),
],
edges: vec![PackageCycleEdge::new(
"@pkg/a".to_owned(),
"@pkg/b".to_owned(),
)],
});
assert!(
results.has_package_cycles(),
"should have package cycles after adding cycles_with_files"
);
assert!(
results.has_any_cycles(),
"should have any cycles after adding"
);
}
}