// Copyright © 2023 Kim Altintop <kim@eagain.io>
// SPDX-License-Identifier: GPL-2.0-only

use std::path::PathBuf;

use tempfile::tempdir;
use test_log::test;
use yapma_common::http;
use yapma_identities::forge::{
    Identity,
    Platform,
};

#[test]
fn smoke() {
    let mut cache_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    cache_path.extend(["tests", "tests"]);

    let client = http::client::caching(http::client(), cache_path);
    let (gh, gl) = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            tokio::try_join!(
                Identity::resolve(Platform::Github, "kim", &client),
                Identity::resolve(Platform::Gitlab, "kalt", &client)
            )
        })
        .unwrap();

    let tmp = tempdir().unwrap();

    gh.store(tmp.path()).unwrap();
    assert_eq!(
        gh,
        Identity::load(tmp.path(), Platform::Github, "kim".into())
            .unwrap()
            .expect("GH identity not found")
    );

    gl.store(tmp.path()).unwrap();
    assert_eq!(
        gl,
        Identity::load(tmp.path(), Platform::Gitlab, "kalt".into())
            .unwrap()
            .expect("GL identity not found")
    );
}