// SPDX-License-Identifier: GPL-3.0-or-later
//! Generator to get all redirects
//!
//! See the [`AllRedirects`] type documentation for specifics.
use super::{Generator, SortDirection};

/// Get all pages in a given namespace
///
/// See [API documentation](https://www.mediawiki.org/wiki/API:Allredirects) for more details.
#[derive(Generator)]
#[params(generator = "allredirects", garlimit = "max")]
pub struct AllRedirects {
    /// The namespace to enumerate
    #[param("garnamespace")]
    namespace: Option<u32>,
    /// The page title to start enumerating from.
    #[param("garfrom")]
    from: Option<String>,
    /// The page title to stop enumerating at.
    #[param("garto")]
    to: Option<String>,
    /// The title prefix for filtering.
    #[param("garprefix")]
    prefix: Option<String>,
    /// Sort direction
    #[param("gardir")]
    sort: Option<SortDirection>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::testwp;
    use std::collections::HashMap;

    #[tokio::test]
    async fn test_allredirects() {
        let bot = testwp().await;
        let gen = AllRedirects::new().namespace(0u32);
        dbg!(gen.params());
        assert_eq!(
            gen.params(),
            HashMap::from([
                ("generator", "allredirects".to_string()),
                ("garlimit", "max".to_string()),
                ("garnamespace", "0".to_string())
            ])
        );
        let mut pages = gen.generate(&bot);
        let mut count = 0;
        while let Some(page) = pages.recv().await {
            page.unwrap();
            count += 1;
            if count == 5 {
                break;
            }
        }
        assert_eq!(count, 5);
    }
}
