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

@ -114,3 +114,89 @@ fn parse_rfc3339(raw: &str) -> Option<DateTime<Utc>> {
.ok()
.map(|dt| dt.with_timezone(&Utc))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use serde_json::json;
use tempfile::TempDir;
fn test_config(tmp: &TempDir) -> Config {
let mut config = Config::default();
config.workspace_dir = tmp.path().join("workspace");
config.config_path = tmp.path().join("config.toml");
config
}
#[test]
fn parse_rfc3339_accepts_valid_timestamp() {
let parsed = parse_rfc3339("2025-01-02T03:04:05Z");
assert!(parsed.is_some());
}
#[test]
fn parse_rfc3339_rejects_invalid_timestamp() {
let parsed = parse_rfc3339("not-a-timestamp");
assert!(parsed.is_none());
}
#[test]
fn run_returns_ok_when_state_file_missing() {
let tmp = TempDir::new().unwrap();
let config = test_config(&tmp);
let result = run(&config);
assert!(result.is_ok());
}
#[test]
fn run_returns_error_for_invalid_json_state_file() {
let tmp = TempDir::new().unwrap();
let config = test_config(&tmp);
let state_file = crate::daemon::state_file_path(&config);
std::fs::write(&state_file, "not-json").unwrap();
let result = run(&config);
assert!(result.is_err());
let error_text = result.unwrap_err().to_string();
assert!(error_text.contains("Failed to parse"));
}
#[test]
fn run_accepts_well_formed_state_snapshot() {
let tmp = TempDir::new().unwrap();
let config = test_config(&tmp);
let state_file = crate::daemon::state_file_path(&config);
let now = Utc::now().to_rfc3339();
let snapshot = json!({
"updated_at": now,
"components": {
"scheduler": {
"status": "ok",
"last_ok": now,
"last_error": null,
"updated_at": now,
"restart_count": 0
},
"channel:discord": {
"status": "ok",
"last_ok": now,
"last_error": null,
"updated_at": now,
"restart_count": 0
}
}
});
std::fs::write(&state_file, serde_json::to_vec_pretty(&snapshot).unwrap()).unwrap();
let result = run(&config);
assert!(result.is_ok());
}
}