//! Tests for smart waiting module

#[cfg(test)]
mod tests {
    use crate::tools::smart_wait::{SmartWaitConfig, SmartWaiter, WaitCondition};
    use crate::dom::views::PageIntent;

    #[test]
    fn test_wait_condition_variants() {
        let conditions = vec![
            WaitCondition::FixedSeconds(5),
            WaitCondition::ElementPresent("button".to_string()),
            WaitCondition::ElementGone("#spinner".to_string()),
            WaitCondition::TextPresent("Loading".to_string()),
            WaitCondition::UrlContains("search".to_string()),
            WaitCondition::PageIntent(PageIntent::SearchResults),
            WaitCondition::ElementCount { selector: "li".to_string(), count: 3 },
            WaitCondition::PageStable { stable_ms: 500 },
        ];

        for cond in conditions {
            let _ = format!("{:?}", cond);
        }
    }

    #[test]
    fn test_smart_wait_config_default() {
        let config = SmartWaitConfig::default();
        assert_eq!(config.poll_interval_ms, 250);
        assert_eq!(config.max_wait_ms, 30_000);
        assert_eq!(config.min_wait_ms, 0);
    }

    #[test]
    fn test_smart_wait_config_custom() {
        let config = SmartWaitConfig {
            poll_interval_ms: 100,
            max_wait_ms: 10_000,
            min_wait_ms: 200,
        };
        assert_eq!(config.poll_interval_ms, 100);
        assert_eq!(config.max_wait_ms, 10_000);
        assert_eq!(config.min_wait_ms, 200);
    }

    #[test]
    fn test_smart_waiter_new() {
        let waiter = SmartWaiter::new();
        let _ = waiter;
    }

    #[test]
    fn test_smart_waiter_with_config() {
        let config = SmartWaitConfig {
            poll_interval_ms: 500,
            max_wait_ms: 5_000,
            min_wait_ms: 100,
        };
        let waiter = SmartWaiter::with_config(config);
        let _ = waiter;
    }

    #[test]
    fn test_wait_condition_equality() {
        assert_eq!(
            WaitCondition::FixedSeconds(3),
            WaitCondition::FixedSeconds(3)
        );
        assert_ne!(
            WaitCondition::FixedSeconds(3),
            WaitCondition::FixedSeconds(5)
        );
        assert_eq!(
            WaitCondition::TextPresent("hello".to_string()),
            WaitCondition::TextPresent("hello".to_string())
        );
    }

    #[test]
    fn test_wait_condition_display() {
        let cond = WaitCondition::ElementPresent("#submit".to_string());
        let s = format!("{:?}", cond);
        assert!(s.contains("ElementPresent"));
        assert!(s.contains("#submit"));
    }

    #[test]
    fn test_page_intent_wait_condition() {
        let cond = WaitCondition::PageIntent(PageIntent::Login);
        assert_eq!(cond, WaitCondition::PageIntent(PageIntent::Login));
    }

    #[test]
    fn test_element_count_condition() {
        let cond = WaitCondition::ElementCount {
            selector: "div.item".to_string(),
            count: 5,
        };
        if let WaitCondition::ElementCount { selector, count } = cond {
            assert_eq!(selector, "div.item");
            assert_eq!(count, 5);
        } else {
            panic!("Expected ElementCount variant");
        }
    }
}
