mod testprotocol;
mod testsupport;

use crate::testsupport::get_object_store;
use saas_rs_sdk::storage::Error;
use std::path::PathBuf;

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn can_create_object() {
    const FILENAME: &str = "build.rs";
    let object_store = get_object_store!();
    let key = xid::new().to_string();
    let path = PathBuf::from(FILENAME);
    object_store
        .put(&key, &path)
        .await
        .expect("Expected to be able to create object");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn cannot_find_non_existent_object() {
    let object_store = get_object_store!();
    let key = xid::new().to_string();
    let res = object_store.get(&key).await;
    assert!(res.is_err());
    assert!(matches!(res.err(), Some(Error::NotFound { .. })));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn can_perform_crud_operations() {
    // Create object
    const FILENAME: &str = "CHANGELOG.md";
    let object_store = get_object_store!();
    let key = xid::new().to_string();
    let path = PathBuf::from(FILENAME);
    object_store
        .put(&key, &path)
        .await
        .expect("Expected to be able to create object");

    // Find
    let file = object_store
        .get(&key)
        .await
        .expect("Expected to be able to download newly created object");
    let expected = std::fs::read_to_string(FILENAME).unwrap();
    let actual = std::fs::read_to_string(file.path()).unwrap();
    assert_eq!(expected, actual);

    // Delete
    object_store
        .del(&key)
        .await
        .expect("Expected to be able to object_store.del newly created object");

    // Find should return a not found error
    let res = object_store.get(&key).await;
    assert!(res.is_err());
    assert!(matches!(res.err().unwrap(), Error::NotFound { .. }));
}
