use super::*;
use crate::metrics::{
model::{MetricValue, MetricsKind, MetricsReport},
parse::metric_page,
};
use canic_contracts::dto::{
metrics::{MetricEntry as MetricEntryDto, MetricValue as MetricValueDto},
page::Page,
};
use ic_platform::principal::ExternalPrincipal as Principal;
fn roundtrip_metric_page(page: Page<MetricEntryDto>) -> super::model::MetricsPage {
use canic_contracts::{dto::metric_page, protocol::protected_metrics as protocol};
let response: Result<Page<metric_page::MetricEntry>, canic_contracts::dto::error::Error> =
Ok(page.into());
let bytes =
ic_wire::native::encode_response(protocol::METHOD_ID, &response, protocol::RESPONSE_LIMITS)
.unwrap();
let decoded: Result<Page<metric_page::MetricEntry>, canic_contracts::dto::error::Error> =
ic_wire::native::decode_response(protocol::METHOD_ID, &bytes, protocol::RESPONSE_LIMITS)
.unwrap();
metric_page(decoded.unwrap().into())
}
#[test]
fn parses_metric_kind_selectors() {
let options =
MetricsOptions::parse_info([OsString::from("test")]).expect("default metrics kind");
assert_eq!(options.fleet, "test");
assert_eq!(options.kind, MetricsKind::Core);
assert!(!options.verbose);
let options = MetricsOptions::parse_info([
OsString::from("test"),
OsString::from("--kind"),
OsString::from("security"),
])
.expect("security metrics kind");
assert_eq!(options.kind, MetricsKind::Security);
std::assert_matches!(
MetricsOptions::parse_info([
OsString::from("test"),
OsString::from("--kind"),
OsString::from("cycles"),
]),
Err(MetricsCommandError::Usage(_))
);
std::assert_matches!(
MetricsOptions::parse_info([
OsString::from("test"),
OsString::from("--limit"),
OsString::from("banana"),
]),
Err(MetricsCommandError::Usage(_))
);
std::assert_matches!(
MetricsOptions::parse_info([
OsString::from("test"),
OsString::from("--limit"),
OsString::from("0"),
]),
Err(MetricsCommandError::Usage(_))
);
}
#[test]
fn parses_metrics_verbose_option() {
let options = MetricsOptions::parse_info([OsString::from("test"), OsString::from("--verbose")])
.expect("parse metrics verbose option");
assert!(options.verbose);
let options = MetricsOptions::parse_info([OsString::from("test"), OsString::from("-v")])
.expect("parse metrics short verbose option");
assert!(options.verbose);
}
#[test]
fn metrics_usage_uses_fleet_wording() {
let text = info_usage();
assert!(text.contains("Usage: canic info metrics [OPTIONS] <fleet>"));
assert!(text.contains("Installed Fleet name to inspect"));
assert!(text.contains("--verbose"));
}
#[test]
fn metrics_report_json_uses_fleet_identity_field() {
let value = serde_json::to_value(MetricsReport {
fleet: "demo-local".to_string(),
environment: "local".to_string(),
kind: MetricsKind::Core,
canisters: Vec::new(),
})
.expect("serialize metrics report");
assert_eq!(value["fleet"], "demo-local");
}
#[test]
fn missing_metrics_fleet_preserves_canonical_typed_error() {
let error = MetricsCommandError::from(CurrentFleetInventoryError::NotConverged {
environment: "local".to_string(),
fleet: "demo-local".to_string(),
});
std::assert_matches!(
error,
MetricsCommandError::CurrentFleet(CurrentFleetInventoryError::NotConverged {
environment,
fleet,
}) if environment == "local" && fleet == "demo-local"
);
}
#[test]
fn parses_typed_metrics_page() {
let page = metric_page(Page {
entries: vec![
MetricEntryDto {
labels: vec!["lifecycle".into(), "init".into(), "started".into()],
principal: None,
value: MetricValueDto::Count {
count: 2,
exact: true,
},
},
MetricEntryDto {
labels: vec!["cycles_funding".into(), "minted".into()],
principal: Some(Principal::from_text("aaaaa-aa").expect("principal")),
value: MetricValueDto::U128 {
value: 1_000,
exact: true,
},
},
MetricEntryDto {
labels: vec!["timer".into(), "tick".into()],
principal: None,
value: MetricValueDto::Measurement {
count: 3,
total: u128::from(u64::MAX) + 1,
count_overflowed: false,
total_overflowed: false,
},
},
],
total: 1500,
});
assert_eq!(page.total, 1500);
let entries = page.entries;
assert_eq!(entries.len(), 3);
assert_eq!(entries[0].labels, ["lifecycle", "init", "started"]);
assert_eq!(
entries[0].value,
MetricValue::Count {
count: 2,
exact: true
}
);
assert_eq!(entries[1].principal.as_deref(), Some("aaaaa-aa"));
assert_eq!(
entries[1].value,
MetricValue::U128 {
value: 1_000,
exact: true
}
);
assert_eq!(
entries[2].value,
MetricValue::Measurement {
count: 3,
total: u128::from(u64::MAX) + 1,
count_overflowed: false,
total_overflowed: false
}
);
}
#[test]
fn metric_measurement_quality_survives_wire_and_json() {
for (count, total, count_overflowed, total_overflowed) in [
(0, 0, false, false),
(1, 0, false, false),
(u64::MAX, u128::from(u64::MAX), false, false),
(2, u128::from(u64::MAX) + 1, false, false),
(u64::MAX, 0, true, false),
(u64::MAX, u128::MAX, true, true),
] {
let page = Page {
entries: vec![MetricEntryDto {
labels: vec!["perf".into(), "instructions".into()],
principal: None,
value: MetricValueDto::Measurement {
count,
total,
count_overflowed,
total_overflowed,
},
}],
total: 1,
};
let rows = roundtrip_metric_page(page).entries;
let json = serde_json::to_string(&rows).unwrap();
assert!(json.contains(&format!("\"total\":{total}")));
assert!(json.contains(&format!("\"count_overflowed\":{count_overflowed}")));
assert!(json.contains(&format!("\"total_overflowed\":{total_overflowed}")));
assert_eq!(
rows[0].value,
MetricValue::Measurement {
count,
total,
count_overflowed,
total_overflowed
}
);
}
}
#[test]
fn scalar_quality_survives_wire_and_json_at_full_width() {
for (value, exact) in [
(0, true),
(u128::from(u64::MAX) + 1, true),
(u128::MAX, true),
(u128::MAX, false),
] {
let page = Page {
entries: vec![
MetricEntryDto {
labels: vec!["events".into()],
principal: None,
value: MetricValueDto::Count {
count: value,
exact,
},
},
MetricEntryDto {
labels: vec!["cycles".into()],
principal: None,
value: MetricValueDto::U128 { value, exact },
},
],
total: 2,
};
let rows = roundtrip_metric_page(page).entries;
assert_eq!(
rows[0].value,
MetricValue::Count {
count: value,
exact
}
);
assert_eq!(rows[1].value, MetricValue::U128 { value, exact });
let json = serde_json::to_string(&rows).unwrap();
assert!(json.contains(&format!("\"count\":{value}")));
assert!(json.contains(&format!("\"value\":{value}")));
assert_eq!(json.matches(&format!("\"exact\":{exact}")).count(), 2);
}
}