Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions rust/src/cli/serve/dashboard/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub struct WindowPayload {
pub used_percent: f64,
pub remaining_percent: f64,
pub reset_at: Option<chrono::DateTime<chrono::Utc>>,
/// Preserve the engine's measurement validity for provider and account clients.
#[serde(skip_serializing_if = "is_false")]
pub is_informational: bool,
/// Display-only hint. Script clients can ignore this additive schema-v1 key.
#[serde(skip_serializing_if = "is_false")]
pub idle: bool,
Expand All @@ -23,6 +26,36 @@ fn is_false(value: &bool) -> bool {
!*value
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn informational_window_keeps_its_authoritative_flag() {
let payload = make_window_with_idle(
"session",
"Session",
&RateWindow::no_active_session(),
false,
);
let json = serde_json::to_value(payload).unwrap();
assert_eq!(json["isInformational"], true);
assert_eq!(json["usedPercent"], 0.0);
assert!(json["resetAt"].is_null());
}

#[test]
fn measured_zero_without_reset_is_not_informational() {
for kind in ["session", "weekly"] {
let payload = make_window_with_idle(kind, kind, &RateWindow::new(0.0), false);
let json = serde_json::to_value(payload).unwrap();
assert!(json.get("isInformational").is_none());
assert_eq!(json["usedPercent"], 0.0);
assert!(json["resetAt"].is_null());
}
}
}

pub(super) fn make_window_with_idle(
kind: &str,
label: &str,
Expand All @@ -36,6 +69,7 @@ pub(super) fn make_window_with_idle(
used_percent: used,
remaining_percent: (100.0 - used).clamp(0.0, 100.0),
reset_at: window.resets_at,
is_informational: window.is_informational,
idle,
unavailable: window.is_informational,
}
Expand Down