mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
test: add tests for UI status helpers
Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
parent
39ba5d90bb
commit
30fb752cbb
1 changed files with 375 additions and 338 deletions
65
src/ui.rs
65
src/ui.rs
|
|
@ -39,6 +39,26 @@ fn ntp_service_toggle(start: bool) {
|
||||||
let _ = Command::new("systemctl").args(&[action, "chrony"]).status();
|
let _ = Command::new("systemctl").args(&[action, "chrony"]).status();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_sync_status(delta_ms: i64) -> &'static str {
|
||||||
|
if delta_ms.abs() <= 8 {
|
||||||
|
"IN SYNC"
|
||||||
|
} else if delta_ms > 10 {
|
||||||
|
"CLOCK AHEAD"
|
||||||
|
} else {
|
||||||
|
"CLOCK BEHIND"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_jitter_status(jitter_ms: i64) -> &'static str {
|
||||||
|
if jitter_ms.abs() < 10 {
|
||||||
|
"GOOD"
|
||||||
|
} else if jitter_ms.abs() < 40 {
|
||||||
|
"AVERAGE"
|
||||||
|
} else {
|
||||||
|
"BAD"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn start_ui(
|
pub fn start_ui(
|
||||||
state: Arc<Mutex<LtcState>>,
|
state: Arc<Mutex<LtcState>>,
|
||||||
serial_port: String,
|
serial_port: String,
|
||||||
|
|
@ -124,13 +144,7 @@ pub fn start_ui(
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6️⃣ sync status wording
|
// 6️⃣ sync status wording
|
||||||
let sync_status = if cached_delta_ms.abs() <= 8 {
|
let sync_status = get_sync_status(cached_delta_ms);
|
||||||
"IN SYNC"
|
|
||||||
} else if cached_delta_ms > 10 {
|
|
||||||
"CLOCK AHEAD"
|
|
||||||
} else {
|
|
||||||
"CLOCK BEHIND"
|
|
||||||
};
|
|
||||||
|
|
||||||
// 7️⃣ auto‑sync (same as manual but delayed)
|
// 7️⃣ auto‑sync (same as manual but delayed)
|
||||||
if sync_status != "IN SYNC" {
|
if sync_status != "IN SYNC" {
|
||||||
|
|
@ -249,13 +263,7 @@ pub fn start_ui(
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
// jitter & lock ratio
|
// jitter & lock ratio
|
||||||
let jstatus = if avg_jitter_ms.abs() < 10 {
|
let jstatus = get_jitter_status(avg_jitter_ms);
|
||||||
"GOOD"
|
|
||||||
} else if avg_jitter_ms.abs() < 40 {
|
|
||||||
"AVERAGE"
|
|
||||||
} else {
|
|
||||||
"BAD"
|
|
||||||
};
|
|
||||||
let jcol = if jstatus == "GOOD" {
|
let jcol = if jstatus == "GOOD" {
|
||||||
Color::Green
|
Color::Green
|
||||||
} else if jstatus == "AVERAGE" {
|
} else if jstatus == "AVERAGE" {
|
||||||
|
|
@ -336,3 +344,32 @@ pub fn start_ui(
|
||||||
thread::sleep(Duration::from_millis(25));
|
thread::sleep(Duration::from_millis(25));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_sync_status() {
|
||||||
|
assert_eq!(get_sync_status(0), "IN SYNC");
|
||||||
|
assert_eq!(get_sync_status(8), "IN SYNC");
|
||||||
|
assert_eq!(get_sync_status(-8), "IN SYNC");
|
||||||
|
assert_eq!(get_sync_status(9), "CLOCK BEHIND");
|
||||||
|
assert_eq!(get_sync_status(10), "CLOCK BEHIND");
|
||||||
|
assert_eq!(get_sync_status(11), "CLOCK AHEAD");
|
||||||
|
assert_eq!(get_sync_status(-9), "CLOCK BEHIND");
|
||||||
|
assert_eq!(get_sync_status(-100), "CLOCK BEHIND");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_jitter_status() {
|
||||||
|
assert_eq!(get_jitter_status(5), "GOOD");
|
||||||
|
assert_eq!(get_jitter_status(-5), "GOOD");
|
||||||
|
assert_eq!(get_jitter_status(9), "GOOD");
|
||||||
|
assert_eq!(get_jitter_status(10), "AVERAGE");
|
||||||
|
assert_eq!(get_jitter_status(39), "AVERAGE");
|
||||||
|
assert_eq!(get_jitter_status(-39), "AVERAGE");
|
||||||
|
assert_eq!(get_jitter_status(40), "BAD");
|
||||||
|
assert_eq!(get_jitter_status(-40), "BAD");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue