canic-cli 0.110.53

Operator CLI for Canic fleet setup, builds, evidence, catalog, backup, and restore workflows
Documentation
use super::*;
use crate::metrics::{
    model::{MetricValue, MetricsKind, MetricsReport},
    parse::metric_page,
};
use candid::Principal;
use canic_core::dto::{
    metrics::{MetricEntry as MetricEntryDto, MetricValue as MetricValueDto},
    page::Page,
};

// Ensure the public kind selector accepts the expected CLI vocabulary.
#[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(_))
    );
}

// Ensure verbose metrics output is an explicit opt-in for wider diagnostics.
#[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 entries = metric_page(Page {
        entries: vec![
            MetricEntryDto {
                labels: vec!["lifecycle".into(), "init".into(), "started".into()],
                principal: None,
                value: MetricValueDto::Count(2),
            },
            MetricEntryDto {
                labels: vec!["cycles_funding".into(), "minted".into()],
                principal: Some(Principal::from_text("aaaaa-aa").expect("principal")),
                value: MetricValueDto::U128(1_000),
            },
            MetricEntryDto {
                labels: vec!["timer".into(), "tick".into()],
                principal: None,
                value: MetricValueDto::CountAndU64 {
                    count: 3,
                    value_u64: 12,
                },
            },
        ],
        total: 3,
    });

    assert_eq!(entries.len(), 3);
    assert_eq!(entries[0].labels, ["lifecycle", "init", "started"]);
    assert_eq!(entries[0].value, MetricValue::Count { count: 2 });
    assert_eq!(entries[1].principal.as_deref(), Some("aaaaa-aa"));
    assert_eq!(entries[1].value, MetricValue::U128 { value: 1_000 });
    assert_eq!(
        entries[2].value,
        MetricValue::CountAndU64 {
            count: 3,
            value_u64: 12
        }
    );
}