use par_term::config::Config;
use std::time::{Duration, Instant};
#[test]
fn test_activity_notification_config_defaults() {
let config = Config::default();
assert!(!config.notifications.notification_activity_enabled);
assert_eq!(config.notifications.notification_activity_threshold, 10);
}
#[test]
fn test_silence_notification_config_defaults() {
let config = Config::default();
assert!(!config.notifications.notification_silence_enabled);
assert_eq!(config.notifications.notification_silence_threshold, 300);
}
#[test]
fn test_activity_notification_yaml_deserialization() {
let yaml = r#"
notification_activity_enabled: true
notification_activity_threshold: 30
"#;
let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
assert!(config.notifications.notification_activity_enabled);
assert_eq!(config.notifications.notification_activity_threshold, 30);
}
#[test]
fn test_silence_notification_yaml_deserialization() {
let yaml = r#"
notification_silence_enabled: true
notification_silence_threshold: 600
"#;
let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
assert!(config.notifications.notification_silence_enabled);
assert_eq!(config.notifications.notification_silence_threshold, 600);
}
#[test]
fn test_both_notification_types_enabled() {
let yaml = r#"
notification_activity_enabled: true
notification_activity_threshold: 15
notification_silence_enabled: true
notification_silence_threshold: 120
"#;
let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
assert!(config.notifications.notification_activity_enabled);
assert_eq!(config.notifications.notification_activity_threshold, 15);
assert!(config.notifications.notification_silence_enabled);
assert_eq!(config.notifications.notification_silence_threshold, 120);
}
#[test]
fn test_notification_config_yaml_serialization() {
let config = Config::default();
let yaml = serde_yaml_ng::to_string(&config).unwrap();
assert!(yaml.contains("notification_activity_enabled: false"));
assert!(yaml.contains("notification_activity_threshold: 10"));
assert!(yaml.contains("notification_silence_enabled: false"));
assert!(yaml.contains("notification_silence_threshold: 300"));
}
#[test]
fn test_notification_config_aliases() {
let yaml = r#"
activity_notifications: true
activity_threshold: 25
"#;
let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
assert!(config.notifications.notification_activity_enabled);
assert_eq!(config.notifications.notification_activity_threshold, 25);
let yaml = r#"
silence_notifications: true
silence_threshold: 180
"#;
let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
assert!(config.notifications.notification_silence_enabled);
assert_eq!(config.notifications.notification_silence_threshold, 180);
}
#[test]
fn test_activity_detection_timing() {
let activity_threshold = Duration::from_secs(10);
let last_activity = Instant::now();
let time_since_activity = last_activity.elapsed();
assert!(
time_since_activity < activity_threshold,
"Recently active terminal should not trigger activity notification"
);
let idle_duration = Duration::from_secs(15);
assert!(
idle_duration >= activity_threshold,
"Terminal idle for {} seconds should trigger activity notification on resume",
idle_duration.as_secs()
);
}
#[test]
fn test_silence_detection_timing() {
let silence_threshold = Duration::from_secs(300);
let short_silence = Duration::from_secs(60);
assert!(
short_silence < silence_threshold,
"Short silence should not trigger notification"
);
let long_silence = Duration::from_secs(400);
assert!(
long_silence >= silence_threshold,
"Long silence should trigger notification"
);
}
#[test]
fn test_silence_notification_deduplication() {
let mut silence_notified = false;
let silence_threshold = Duration::from_secs(300);
let time_since_activity = Duration::from_secs(400);
if !silence_notified && time_since_activity >= silence_threshold {
silence_notified = true;
}
assert!(silence_notified, "First check should set flag");
let should_notify = !silence_notified && time_since_activity >= silence_threshold;
assert!(
!should_notify,
"Second check should not trigger duplicate notification"
);
}
#[test]
fn test_activity_resets_silence_flag() {
let mut silence_notified = true;
let mut last_seen_generation: u64 = 5;
let current_generation: u64 = 6;
if current_generation > last_seen_generation {
last_seen_generation = current_generation;
silence_notified = false; }
assert!(!silence_notified, "Activity should reset silence flag");
assert_eq!(last_seen_generation, 6, "Generation should be updated");
}
#[test]
fn test_threshold_boundary_conditions() {
let threshold = Duration::from_secs(10);
let at_threshold = Duration::from_secs(10);
assert!(at_threshold >= threshold, "At threshold should trigger");
let under_threshold = Duration::from_millis(9999);
assert!(
under_threshold < threshold,
"Under threshold should not trigger"
);
}
#[test]
fn test_minimum_threshold_values() {
let yaml = r#"
notification_activity_threshold: 1
notification_silence_threshold: 1
"#;
let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
assert_eq!(config.notifications.notification_activity_threshold, 1);
assert_eq!(config.notifications.notification_silence_threshold, 1);
}
#[test]
fn test_large_threshold_values() {
let yaml = r#"
notification_activity_threshold: 3600
notification_silence_threshold: 86400
"#;
let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
assert_eq!(config.notifications.notification_activity_threshold, 3600);
assert_eq!(config.notifications.notification_silence_threshold, 86400);
}