test: deepen and complete project-wide test coverage (#297)

* test: deepen coverage for health doctor provider and tunnels

* test: add broad trait and module re-export coverage
This commit is contained in:
Chummy 2026-02-16 18:58:24 +08:00 committed by GitHub
parent 79a6f180a8
commit 49fcc7a2c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 1156 additions and 0 deletions

View file

@ -104,3 +104,84 @@ pub fn snapshot_json() -> serde_json::Value {
})
})
}
#[cfg(test)]
mod tests {
use super::*;
fn unique_component(prefix: &str) -> String {
format!("{prefix}-{}", uuid::Uuid::new_v4())
}
#[test]
fn mark_component_ok_initializes_component_state() {
let component = unique_component("health-ok");
mark_component_ok(&component);
let snapshot = snapshot();
let entry = snapshot
.components
.get(&component)
.expect("component should be present after mark_component_ok");
assert_eq!(entry.status, "ok");
assert!(entry.last_ok.is_some());
assert!(entry.last_error.is_none());
}
#[test]
fn mark_component_error_then_ok_clears_last_error() {
let component = unique_component("health-error");
mark_component_error(&component, "first failure");
let error_snapshot = snapshot();
let errored = error_snapshot
.components
.get(&component)
.expect("component should exist after mark_component_error");
assert_eq!(errored.status, "error");
assert_eq!(errored.last_error.as_deref(), Some("first failure"));
mark_component_ok(&component);
let recovered_snapshot = snapshot();
let recovered = recovered_snapshot
.components
.get(&component)
.expect("component should exist after recovery");
assert_eq!(recovered.status, "ok");
assert!(recovered.last_error.is_none());
assert!(recovered.last_ok.is_some());
}
#[test]
fn bump_component_restart_increments_counter() {
let component = unique_component("health-restart");
bump_component_restart(&component);
bump_component_restart(&component);
let snapshot = snapshot();
let entry = snapshot
.components
.get(&component)
.expect("component should exist after restart bump");
assert_eq!(entry.restart_count, 2);
}
#[test]
fn snapshot_json_contains_registered_component_fields() {
let component = unique_component("health-json");
mark_component_ok(&component);
let json = snapshot_json();
let component_json = &json["components"][&component];
assert_eq!(component_json["status"], "ok");
assert!(component_json["updated_at"].as_str().is_some());
assert!(component_json["last_ok"].as_str().is_some());
assert!(json["uptime_seconds"].as_u64().is_some());
}
}