use paper_gap::{arxiv::Paper, repos};

fn paper() -> Paper {
    Paper {
        id: "http://arxiv.org/abs/2603.21852v1".into(),
        title: "Fast FFT Methods".into(),
        abstract_text: String::new(),
        authors: Vec::new(),
        published_date: None,
        updated_date: None,
        doi: Some("10.1234/example".into()),
        arxiv_id: Some("2603.21852v1".into()),
        url: "http://arxiv.org/abs/2603.21852v1".into(),
        categories: Vec::new(),
        extracted_methods: vec!["FFT".into()],
        source_provider: "arxiv".into(),
    }
}

#[test]
fn builds_repository_search_terms() {
    assert_eq!(
        repos::search_terms(&paper()),
        vec!["Fast FFT Methods", "2603.21852", "10.1234/example", "FFT"]
    );
}

#[test]
fn builds_provider_urls() {
    assert_eq!(
        repos::github_search_url("FFT solver", 5),
        "https://api.github.com/search/repositories?q=FFT%20solver&per_page=5"
    );
    assert_eq!(
        repos::gitlab_search_url("FFT solver", 5),
        "https://gitlab.com/api/v4/projects?search=FFT%20solver&per_page=5"
    );
    assert_eq!(
        repos::forgejo_search_url("https://codeberg.org/api/v1/", "FFT solver", 5),
        "https://codeberg.org/api/v1/repos/search?q=FFT%20solver&limit=5"
    );
}

#[test]
fn parses_github_search_results() {
    let json = r#"{"items":[{"html_url":"https://github.com/a/b","name":"b","full_name":"a/b","owner":{"login":"a"},"description":"desc","language":"Rust","stargazers_count":7,"forks_count":2,"license":{"spdx_id":"MIT","name":"MIT License"},"archived":false,"default_branch":"main","pushed_at":"2026-01-01T00:00:00Z"}]}"#;
    let repos = repos::parse_github_search(json).unwrap();
    assert_eq!(repos.len(), 1);
    assert_eq!(repos[0].forge, "github");
    assert_eq!(repos[0].owner, "a");
    assert_eq!(repos[0].name, "b");
    assert_eq!(repos[0].primary_language.as_deref(), Some("Rust"));
    assert_eq!(repos[0].license.as_deref(), Some("MIT"));
}

#[test]
fn parses_gitlab_search_results() {
    let json = r#"[{"web_url":"https://gitlab.com/a/b","path":"b","path_with_namespace":"a/b","description":"desc","star_count":3,"forks_count":1,"archived":false,"default_branch":"main","last_activity_at":"2026-01-01T00:00:00Z"}]"#;
    let repos = repos::parse_gitlab_search(json).unwrap();
    assert_eq!(repos.len(), 1);
    assert_eq!(repos[0].forge, "gitlab");
    assert_eq!(repos[0].owner, "a");
    assert_eq!(repos[0].name, "b");
}

#[test]
fn parses_forgejo_search_results() {
    let json = r#"{"data":[{"html_url":"https://codeberg.org/a/b","name":"b","full_name":"a/b","owner":{"login":"a"},"description":"desc","language":"Rust","stars_count":4,"forks_count":1,"archived":false,"default_branch":"main","updated_at":"2026-01-01T00:00:00Z"}]}"#;
    let repos = repos::parse_forgejo_search("codeberg", json).unwrap();
    assert_eq!(repos.len(), 1);
    assert_eq!(repos[0].forge, "codeberg");
    assert_eq!(repos[0].owner, "a");
    assert_eq!(repos[0].url, "https://codeberg.org/a/b");
}
