use std::time::Duration;
use super::*;
use crate::result::BoxError;
fn registry() -> ResourceRegistry {
ResourceRegistry::new("example_service").expect("static service name is valid")
}
fn required(id: &str, name: &str, kind: ResourceKind) -> ResourceDescriptor {
ResourceDescriptor::required(id, name, kind).expect("static descriptor is valid")
}
fn optional(id: &str, name: &str, kind: ResourceKind) -> ResourceDescriptor {
ResourceDescriptor::optional(id, name, kind).expect("static descriptor is valid")
}
#[tokio::test]
async fn registry_runs_static_probes_and_rolls_up_status() {
let registry = registry()
.with_probe(static_resource_probe(
required("contracts", "HTTP Contracts", ResourceKind::Configuration),
ResourceDiagnostic::healthy,
"contracts are valid",
))
.with_probe(static_resource_probe(
optional("smtp", "SMTP", ResourceKind::Messaging),
ResourceDiagnostic::degraded,
"SMTP is not configured",
));
let report = registry.run().await.expect("diagnostics should run");
assert!(report.ready());
assert_eq!(report.status, ResourceStatus::Degraded);
assert_eq!(report.summary.total, 2);
assert_eq!(report.summary.healthy, 1);
assert_eq!(report.summary.degraded, 1);
}
#[tokio::test]
async fn registry_runs_closure_probes_with_captured_state() {
let expected_schema_version = 1;
let registry = registry().with_probe(resource_probe(
required("contracts", "HTTP Contracts", ResourceKind::Http),
move |resource| async move {
Ok(ResourceDiagnostic::healthy(resource, "contracts are valid")
.with_detail(format!("schema_version={expected_schema_version}")))
},
));
let report = registry.run().await.expect("diagnostics should run");
assert!(report.ready());
assert_eq!(report.status, ResourceStatus::Healthy);
assert_eq!(report.resources[0].details, ["schema_version=1".to_owned()]);
}
#[tokio::test]
async fn required_unhealthy_resource_blocks_readiness() {
let registry = registry().with_probe(static_resource_probe(
required("oidc-key", "OIDC Key", ResourceKind::Cryptography),
ResourceDiagnostic::unhealthy,
"signing key is missing",
));
let report = registry.run().await.expect("diagnostics should run");
assert!(!report.ready());
assert_eq!(report.status, ResourceStatus::Unhealthy);
assert_eq!(report.summary.readiness_blocking, 1);
assert_eq!(report.readiness_blockers()[0].resource.id, "oidc-key");
}
#[tokio::test]
async fn required_unknown_resource_blocks_readiness() {
let registry = registry().with_probe(static_resource_probe(
required("database", "Database", ResourceKind::Database),
ResourceDiagnostic::unknown,
"database health was not checked",
));
let report = registry.run().await.expect("diagnostics should run");
assert!(!report.ready());
assert_eq!(report.status, ResourceStatus::Unknown);
assert_eq!(report.summary.readiness_blocking, 1);
}
#[tokio::test]
async fn registry_rejects_duplicate_resource_ids() {
let descriptor = required("config", "Configuration", ResourceKind::Configuration);
let registry = registry()
.with_probe(static_resource_probe(
descriptor.clone(),
ResourceDiagnostic::healthy,
"ok",
))
.with_probe(static_resource_probe(
descriptor,
ResourceDiagnostic::healthy,
"ok",
));
assert!(matches!(
registry.run().await,
Err(error) if error.to_string().contains("duplicate resource diagnostic id")
));
}
#[tokio::test]
async fn registry_rejects_empty_service_name() {
assert!(matches!(
ResourceRegistry::new(" "),
Err(ResourceRegistryError::InvalidServiceName)
));
}
#[tokio::test]
async fn registry_anchors_probe_results_to_registered_descriptor() {
let registry = registry().with_probe(resource_probe(
required("database", "Database", ResourceKind::Database),
|_| async move {
Ok(ResourceDiagnostic::healthy(
optional("other", "Other", ResourceKind::Other),
"database is reachable",
))
},
));
let report = registry.run().await.expect("diagnostics should run");
assert_eq!(report.resources[0].resource.id, "database");
assert_eq!(report.resources[0].resource.name, "Database");
assert_eq!(
report.resources[0].resource.criticality,
ResourceCriticality::Required
);
assert_eq!(report.resources[0].summary, "database is reachable");
}
#[tokio::test]
async fn registry_bounds_each_probe_and_reports_timeout() {
let registry = registry()
.with_probe_timeout(Duration::from_millis(1))
.with_probe(resource_probe(
required("database", "Database", ResourceKind::Database),
|_| async move { std::future::pending::<Result<ResourceDiagnostic, BoxError>>().await },
));
let report = registry
.run()
.await
.expect("timeout should become a diagnostic");
assert!(!report.ready());
assert_eq!(report.resources[0].summary, "resource probe timed out");
assert!(report.resources[0].details.is_empty());
}
#[tokio::test]
async fn registry_does_not_copy_probe_errors_into_serialized_reports() {
let private_detail = "private-probe-error";
let registry = registry().with_probe(resource_probe(
required("database", "Database", ResourceKind::Database),
move |_| async move {
Err::<ResourceDiagnostic, BoxError>(Box::new(std::io::Error::other(private_detail)))
},
));
let report = registry
.run()
.await
.expect("failure should become a diagnostic");
let wire = serde_json::to_string(&report).expect("report should serialize");
assert_eq!(report.resources[0].summary, "resource probe failed");
assert!(report.resources[0].details.is_empty());
assert!(!wire.contains(private_detail));
}
#[tokio::test]
async fn registry_rejects_zero_probe_timeout() {
let error = registry()
.with_probe_timeout(Duration::ZERO)
.run()
.await
.expect_err("zero timeout should fail validation");
assert_eq!(
error.to_string(),
"resource diagnostic probe timeout must be greater than zero"
);
}
#[test]
fn descriptor_construction_and_deserialization_share_validation() {
assert!(matches!(
ResourceDescriptor::required("", "Database", ResourceKind::Database),
Err(ResourceDescriptorError::EmptyId)
));
assert!(matches!(
ResourceDescriptor::required("bad id", "Database", ResourceKind::Database),
Err(ResourceDescriptorError::InvalidId)
));
assert!(matches!(
required("database", "Database", ResourceKind::Database).with_label("", "true"),
Err(ResourceDescriptorError::EmptyLabelKey)
));
assert!(
serde_json::from_value::<ResourceDescriptor>(serde_json::json!({
"id": "",
"name": "Database",
"kind": "database",
"criticality": "required"
}))
.is_err()
);
}
#[test]
fn diagnostic_debug_redacts_labels_and_operator_messages_without_changing_wire_data() {
let label = "private-resource-label";
let summary = "private-resource-summary";
let detail = "private-resource-detail";
let remediation = "private-resource-remediation";
let descriptor = required("database", "Database", ResourceKind::Database)
.with_label("endpoint", label)
.expect("resource label should validate");
let diagnostic = ResourceDiagnostic::unhealthy(descriptor, summary)
.with_detail(detail)
.with_remediation(remediation);
let output = format!("{diagnostic:?}");
assert!(output.contains("label_count"));
for sensitive in [label, summary, detail, remediation] {
assert!(!output.contains(sensitive));
}
let wire = serde_json::to_value(&diagnostic).expect("diagnostic should serialize");
assert_eq!(wire["resource"]["labels"]["endpoint"], label);
assert_eq!(wire["summary"], summary);
assert_eq!(wire["details"][0], detail);
assert_eq!(wire["remediations"][0], remediation);
}
#[test]
fn report_deserialization_recomputes_status_and_summary() {
let value = serde_json::json!({
"service_name": "example_service",
"status": "healthy",
"summary": {
"total": 0,
"healthy": 0,
"degraded": 0,
"unhealthy": 0,
"unknown": 0,
"readiness_blocking": 0
},
"resources": [{
"resource": {
"id": "database",
"name": "Database",
"kind": "database",
"criticality": "required"
},
"status": "unhealthy",
"summary": "database is unavailable"
}]
});
let report: ResourceDiagnosticsReport =
serde_json::from_value(value).expect("report JSON should deserialize");
assert_eq!(report.status, ResourceStatus::Unhealthy);
assert_eq!(report.summary.total, 1);
assert_eq!(report.summary.readiness_blocking, 1);
}