// SPDX-FileCopyrightText: 2026 Contributors to brew-proxy <https://codeberg.org/HastD/brew-proxy>
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::{thread::sleep, time::Duration};

use brew_proxy_utils::idle;

fn is_newly_idle() -> bool {
    idle::idle_time().is_some_and(|t| t < Duration::from_millis(5))
}

#[test]
fn idle_tracker() {
    idle::init_idle_tracker();
    assert!(is_newly_idle());
    {
        let _client = idle::ActiveClient::new();
        assert!(idle::idle_time().is_none());
        {
            let _client = idle::ActiveClient::new();
            assert!(idle::idle_time().is_none());
        }
        assert!(idle::idle_time().is_none());
        sleep(Duration::from_millis(10));
    }
    assert!(is_newly_idle());
    sleep(Duration::from_millis(10));
    assert!(idle::idle_time().is_some_and(|t| t > Duration::from_millis(10)));
}
