#![cfg(feature = "demo")]
#![allow(deprecated)]
#[cfg(test)]
mod demo_tests {
use anyhow::Result;
use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;
#[test]
#[ignore] fn test_demo_mode_in_test_directory() -> Result<()> {
let temp = TempDir::new()?;
let test_path = temp.path();
std::fs::create_dir_all(test_path.join(".git"))?;
std::fs::write(
test_path.join("main.rs"),
"fn main() { println!(\"Hello, world!\"); }",
)?;
std::fs::write(
test_path.join("lib.rs"),
"pub fn add(a: i32, b: i32) -> i32 { a + b }",
)?;
let mut cmd = Command::cargo_bin("pmat")?;
cmd.arg("demo").arg("--cli").arg("--path").arg(test_path);
cmd.assert()
.success()
.stdout(predicate::str::contains("CLI Protocol Demo"))
.stdout(predicate::str::contains("result"));
Ok(())
}
#[test]
#[ignore] fn test_demo_mode_with_json_output() -> Result<()> {
let temp = TempDir::new()?;
let test_path = temp.path();
std::fs::create_dir_all(test_path.join(".git"))?;
std::fs::write(test_path.join("test.rs"), "fn main() {}")?;
let mut cmd = Command::cargo_bin("pmat")?;
cmd.arg("demo")
.arg("--cli")
.arg("--format")
.arg("json")
.arg("--path")
.arg(test_path);
cmd.assert()
.success()
.stdout(predicate::str::contains(r#""result""#))
.stdout(predicate::str::contains(r#""cache_key""#))
.stdout(predicate::str::contains(r#""execution_time_ms""#));
Ok(())
}
#[test]
#[ignore] fn test_demo_mode_with_specific_path() -> Result<()> {
let temp = TempDir::new()?;
let repo_path = temp.path().join("test-repo");
std::fs::create_dir_all(&repo_path)?;
std::fs::create_dir_all(repo_path.join(".git"))?;
std::fs::write(
repo_path.join("main.rs"),
"fn main() { println!(\"Hello\"); }",
)?;
std::fs::write(
repo_path.join("Cargo.toml"),
"[package]\nname = \"test\"\nversion = \"0.1.0\"",
)?;
let mut cmd = Command::cargo_bin("pmat")?;
cmd.arg("demo")
.arg("--cli")
.arg("--path")
.arg(repo_path.to_str().unwrap());
cmd.assert()
.success()
.stdout(predicate::str::contains("🚀 CLI Protocol Demo"))
.stdout(predicate::str::contains("test-repo"));
Ok(())
}
#[test]
#[ignore] fn test_demo_increases_test_coverage() -> Result<()> {
let temp = TempDir::new()?;
let test_path = temp.path();
std::fs::create_dir_all(test_path.join(".git"))?;
std::fs::write(test_path.join("test.rs"), "fn main() {}")?;
let mut cmd = Command::cargo_bin("pmat")?;
cmd.arg("demo")
.arg("--cli")
.arg("--format")
.arg("json")
.arg("--path")
.arg(test_path);
let output = cmd.output()?;
let stdout = String::from_utf8_lossy(&output.stdout);
if !output.stderr.is_empty() {
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
}
if !output.status.success() {
eprintln!("Command failed with status: {}", output.status);
eprintln!("stdout: {stdout}");
panic!("Demo command failed");
}
let json_start = stdout.find('{').expect("Should have JSON in output");
let json_str = &stdout[json_start..];
let report: serde_json::Value = serde_json::from_str(json_str)?;
assert!(report["result"].is_object(), "Should have result object");
assert!(report["cache_key"].is_string(), "Should have cache key");
assert!(
report["execution_time_ms"].is_number(),
"Should have execution time"
);
let result = &report["result"];
assert!(result["analyses"].is_object(), "Should have analyses");
assert!(result["metadata"].is_object(), "Should have metadata");
Ok(())
}
#[cfg(test)]
mod mcp_demo_tests {
use super::*;
use pmat::{
cli::OutputFormat,
demo::{run_demo, DemoArgs},
stateless_server::StatelessTemplateServer,
};
use std::sync::Arc;
#[tokio::test]
async fn test_demo_runner_execution() -> Result<()> {
let server = Arc::new(StatelessTemplateServer::new()?);
let temp = TempDir::new()?;
let repo_path = temp.path().to_path_buf();
std::fs::create_dir_all(repo_path.join(".git"))?;
std::fs::write(repo_path.join("main.rs"), "fn main() {}")?;
let args = DemoArgs {
path: Some(repo_path.clone()),
url: None,
repo: None,
format: OutputFormat::Json,
no_browser: true,
port: None,
web: false,
target_nodes: 15,
centrality_threshold: 0.1,
merge_threshold: 3,
protocol: pmat::demo::Protocol::Cli,
show_api: false,
debug: false,
debug_output: None,
skip_vendor: true,
max_line_length: None,
};
run_demo(args, server).await?;
Ok(())
}
#[test]
#[ignore] fn test_repository_detection() -> Result<()> {
let temp = TempDir::new()?;
let repo_path = temp.path().join("repo");
std::fs::create_dir_all(repo_path.join(".git"))?;
assert!(repo_path.join(".git").is_dir());
Ok(())
}
}
}