use assert_cmd::Command;
use predicates::str::contains;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_coverage_min_threshold_failure() {
let dir = TempDir::new().unwrap();
let policy_path = dir.path().join("policy.yaml");
let trace_path = dir.path().join("trace.jsonl");
fs::write(
&policy_path,
r#"
version: "1"
name: threshold_policy
tools:
allow: [ToolA, ToolB]
"#,
)
.unwrap();
fs::write(
&trace_path,
r#"
{"type": "call_tool", "tool": "ToolA", "test_id": "test1"}
"#,
)
.unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_assay"));
cmd.arg("coverage")
.arg("--policy")
.arg(&policy_path)
.arg("--traces")
.arg(&trace_path)
.arg("--min-coverage")
.arg("80") .assert()
.failure()
.stderr(contains("Minimum coverage not met"));
}
#[test]
fn test_coverage_min_threshold_success() {
let dir = TempDir::new().unwrap();
let policy_path = dir.path().join("policy.yaml");
let trace_path = dir.path().join("trace.jsonl");
fs::write(
&policy_path,
r#"
version: "1"
name: threshold_policy
tools:
allow: [ToolA, ToolB]
"#,
)
.unwrap();
fs::write(
&trace_path,
r#"
{"type": "call_tool", "tool": "ToolA", "test_id": "test1"}
{"type": "call_tool", "tool": "ToolB", "test_id": "test1"}
"#,
)
.unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_assay"));
cmd.arg("coverage")
.arg("--policy")
.arg(&policy_path)
.arg("--traces")
.arg(&trace_path)
.arg("--min-coverage")
.arg("80")
.assert()
.success();
}
#[test]
fn test_coverage_baseline_regression_failure() {
let dir = TempDir::new().unwrap();
let policy_path = dir.path().join("policy.yaml");
let trace_full = dir.path().join("trace_full.jsonl");
let trace_partial = dir.path().join("trace_partial.jsonl");
let baseline_path = dir.path().join("baseline.json");
fs::write(
&policy_path,
r#"
version: "1"
name: regression_policy
tools:
allow: [ToolA, ToolB]
"#,
)
.unwrap();
fs::write(
&trace_full,
r#"
{"type": "call_tool", "tool": "ToolA", "test_id": "test1"}
{"type": "call_tool", "tool": "ToolB", "test_id": "test1"}
"#,
)
.unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_assay"));
cmd.arg("coverage")
.arg("--policy")
.arg(&policy_path)
.arg("--traces")
.arg(&trace_full)
.arg("--export-baseline")
.arg(&baseline_path)
.assert()
.success();
assert!(baseline_path.exists(), "Baseline file should exist");
let content = fs::read_to_string(&baseline_path).unwrap();
assert!(
content.contains("\"metric\": \"overall\""),
"Baseline should contain overall metric"
);
assert!(content.contains("100.0"), "Baseline score should be 100%");
fs::write(
&trace_partial,
r#"
{"type": "call_tool", "tool": "ToolA", "test_id": "test1"}
"#,
)
.unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_assay"));
cmd.arg("coverage")
.arg("--policy")
.arg(&policy_path)
.arg("--traces")
.arg(&trace_partial)
.arg("--baseline")
.arg(&baseline_path)
.assert()
.failure()
.stderr(contains("REGRESSION DETECTED"));
}
#[test]
fn test_coverage_baseline_no_regression() {
let dir = TempDir::new().unwrap();
let policy_path = dir.path().join("policy.yaml");
let trace_path = dir.path().join("trace.jsonl");
let baseline_path = dir.path().join("baseline.json");
fs::write(
&policy_path,
r#"
version: "1"
name: stable_policy
tools:
allow: [ToolA]
"#,
)
.unwrap();
fs::write(
&trace_path,
r#"
{"type": "call_tool", "tool": "ToolA", "test_id": "test1"}
"#,
)
.unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_assay"));
cmd.arg("coverage")
.arg("--policy")
.arg(&policy_path)
.arg("--traces")
.arg(&trace_path)
.arg("--export-baseline")
.arg(&baseline_path)
.assert()
.success();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_assay"));
cmd.arg("coverage")
.arg("--policy")
.arg(&policy_path)
.arg("--traces")
.arg(&trace_path)
.arg("--baseline")
.arg(&baseline_path)
.assert()
.success()
.stderr(contains("No regression against baseline"));
}
#[test]
fn test_coverage_combined_failures() {
let dir = TempDir::new().unwrap();
let policy_path = dir.path().join("policy.yaml");
let trace_full = dir.path().join("trace_full.jsonl");
let trace_bad = dir.path().join("trace_bad.jsonl");
let baseline_path = dir.path().join("baseline.json");
fs::write(
&policy_path,
r#"
version: "1"
name: combined_policy
tools:
allow: [SafeTool]
deny: [CriticalTool]
"#,
)
.unwrap();
fs::write(
&trace_full,
r#"
{"type": "call_tool", "tool": "SafeTool", "test_id": "test1"}
{"type": "call_tool", "tool": "CriticalTool", "test_id": "test2"}
"#,
)
.unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_assay"));
cmd.arg("coverage")
.arg("--policy")
.arg(&policy_path)
.arg("--traces")
.arg(&trace_full)
.arg("--export-baseline")
.arg(&baseline_path)
.assert()
.failure();
fs::write(&trace_bad, "").unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_assay"));
cmd.arg("coverage")
.arg("--policy")
.arg(&policy_path) .arg("--traces")
.arg(&trace_bad)
.arg("--baseline")
.arg(&baseline_path) .arg("--min-coverage")
.arg("80") .assert()
.failure()
.stderr(contains("REGRESSION DETECTED"))
.stderr(contains("High Risk Gaps Detected"))
.stderr(contains("Minimum coverage not met"));
}
#[test]
fn test_coverage_high_risk_gap_failure() {
let dir = TempDir::new().unwrap();
let policy_path = dir.path().join("policy.yaml");
let trace_path = dir.path().join("trace.jsonl");
fs::write(
&policy_path,
r#"
version: "1"
name: strict_policy
tools:
allow: [SafeTool]
deny: [CriticalTool]
"#,
)
.unwrap();
fs::write(
&trace_path,
r#"
{"type": "call_tool", "tool": "SafeTool", "test_id": "test1"}
"#,
)
.unwrap();
let mut cmd = Command::new(env!("CARGO_BIN_EXE_assay"));
cmd.arg("coverage")
.arg("--policy")
.arg(&policy_path)
.arg("--traces")
.arg(&trace_path)
.assert()
.failure()
.stderr(contains("High Risk Gaps Detected"));
}