use serde_json::json;
use super::*;
use crate::console_metrics::make_report;
use crate::host_metrics::HostSampler;
fn sample(id: &str, cpu: Option<f32>, rss: Option<u64>) -> ServiceSample {
ServiceSample {
id: id.to_string(),
status: ServiceStatus::Running,
cpu_pct: cpu,
rss_bytes: rss,
}
}
#[test]
fn request_types_accept_absent_null_or_an_object() {
assert_eq!(
serde_json::to_value(MachineStatusRequest::default()).expect("encode"),
json!({})
);
assert_eq!(
serde_json::to_value(HistoryRequest::default()).expect("encode"),
json!({})
);
for params in [json!(null), json!({}), json!({"later": 1}), json!([])] {
let m: MachineStatusRequest = serde_json::from_value(params.clone()).expect("decode m");
assert_eq!(m, MachineStatusRequest {});
let h: HistoryRequest = serde_json::from_value(params).expect("decode h");
assert_eq!(h, HistoryRequest {});
}
}
#[test]
fn request_types_refuse_a_scalar_or_a_non_empty_array() {
for params in [json!("x"), json!(3), json!(true), json!([1])] {
assert!(
serde_json::from_value::<MachineStatusRequest>(params.clone()).is_err(),
"machine_status params {params} must be refused"
);
assert!(
serde_json::from_value::<HistoryRequest>(params.clone()).is_err(),
"history params {params} must be refused"
);
}
}
#[test]
fn machine_status_not_ready_golden_json() {
let value = serde_json::to_value(ConsoleMachineStatus::default()).expect("encode");
assert_eq!(value, json!({"ready": false, "machine": null}));
let back: ConsoleMachineStatus = serde_json::from_value(json!({})).expect("decode");
assert!(!back.ready && back.machine.is_none());
}
#[test]
fn machine_status_ready_round_trips() {
let report = make_report(
"trusty-search",
"Search",
"1.0.0",
ServiceHealth::Ok,
json!({}),
1,
);
let answer = ConsoleMachineStatus {
ready: true,
machine: Some(MachineStatus::assemble(
HostSampler::new().sample(),
&[report],
)),
};
let value = serde_json::to_value(&answer).expect("encode");
let back: ConsoleMachineStatus = serde_json::from_value(value.clone()).expect("decode");
assert!(back.ready);
assert_eq!(back.machine.as_ref().expect("machine").services.total, 1);
assert_eq!(serde_json::to_value(&back).expect("re-encode"), value);
}
#[test]
fn history_snapshot_golden_json() {
let mut service_samples = BTreeMap::new();
service_samples.insert(
"trusty-search".to_string(),
vec![sample("trusty-search", Some(12.5), Some(4096))],
);
let snapshot = HistorySnapshot {
samples: Vec::new(),
transitions: vec![ServiceTransition {
service_id: "trusty-search".into(),
display_name: "Trusty Search".into(),
from: ServiceState::Up,
to: ServiceState::Down,
at_unix: 1_700_000_000,
}],
service_samples,
sample_capacity: 600,
service_sample_capacity: 600,
transition_capacity: 256,
sample_interval_secs: 1,
sse_client_count: 0,
schema_version: MACHINE_HISTORY_SCHEMA_VERSION,
};
let value = serde_json::to_value(&snapshot).expect("encode");
assert_eq!(
value,
json!({
"samples": [],
"transitions": [{
"service_id": "trusty-search",
"display_name": "Trusty Search",
"from": "up",
"to": "down",
"at_unix": 1_700_000_000u64,
}],
"service_samples": {"trusty-search": [{
"id": "trusty-search",
"status": "running",
"cpu_pct": 12.5,
"rss_bytes": 4096,
}]},
"sample_capacity": 600,
"service_sample_capacity": 600,
"transition_capacity": 256,
"sample_interval_secs": 1,
"sse_client_count": 0,
"schema_version": 4,
})
);
let back: HistorySnapshot = serde_json::from_value(value.clone()).expect("decode");
assert_eq!(serde_json::to_value(&back).expect("re-encode"), value);
}
#[test]
fn history_snapshot_decodes_a_payload_missing_fields() {
let back: HistorySnapshot =
serde_json::from_value(json!({"added_later": true})).expect("decode");
assert!(back.samples.is_empty() && back.service_samples.is_empty());
assert_eq!(back.schema_version, 0);
}
#[test]
fn service_sample_serialises_absent_figures_as_null() {
let value = serde_json::to_value(sample("trusty-review", None, None)).expect("encode");
assert!(value.get("cpu_pct").is_some_and(|v| v.is_null()));
assert!(value.get("rss_bytes").is_some_and(|v| v.is_null()));
let old: ServiceSample =
serde_json::from_value(json!({"id": "a", "status": "absent", "cpu_pct": 1.0}))
.expect("decode a pre-#6773 row");
assert_eq!(old.rss_bytes, None);
}
#[test]
fn service_state_maps_from_self_reported_health() {
assert_eq!(ServiceState::from(&ServiceHealth::Ok), ServiceState::Up);
assert_eq!(
ServiceState::from(&ServiceHealth::Degraded),
ServiceState::Degraded
);
assert_eq!(
ServiceState::from(&ServiceHealth::Error),
ServiceState::Down
);
assert_eq!(
serde_json::to_value(ServiceState::Unknown).expect("encode"),
json!("unknown")
);
}
#[test]
fn history_snapshot_decodes_unknown_state_and_status_as_unknown() {
let back: HistorySnapshot = serde_json::from_value(json!({
"transitions": [{
"service_id": "trusty-search",
"display_name": "Search",
"from": "up",
"to": "hibernating",
"at_unix": 7
}],
"service_samples": {"trusty-search": [{
"id": "trusty-search",
"status": "quarantined",
"cpu_pct": 2.5,
"rss_bytes": 9
}]},
"schema_version": 1
}))
.expect("an unknown variant must not fail the whole answer");
let t = &back.transitions[0];
assert_eq!((t.from, t.to), (ServiceState::Up, ServiceState::Unknown));
let s = &back.service_samples["trusty-search"][0];
assert_eq!(
serde_json::to_value(&s.status).expect("encode"),
json!("unknown")
);
assert_eq!((s.cpu_pct, s.rss_bytes), (Some(2.5), Some(9)));
assert_eq!(back.schema_version, 1);
}
#[test]
fn service_state_decodes_an_unknown_spelling_as_unknown() {
let back: HistorySnapshot = serde_json::from_value(json!({
"transitions": [{
"service_id": "trusty-search",
"display_name": "Search",
"from": "up",
"to": "hibernating",
"at_unix": 7
}]
}))
.expect("an unknown state must not fail the whole answer");
assert_eq!(back.transitions[0].to, ServiceState::Unknown);
}