// Copyright 2024 KVCache.AI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::env;
use std::time::{SystemTime, UNIX_EPOCH};

use mooncake_store::MooncakeStore;

fn env_flag(name: &str) -> bool {
    matches!(
        env::var(name)
            .unwrap_or_default()
            .trim()
            .to_ascii_lowercase()
            .as_str(),
        "1" | "true" | "yes" | "on"
    )
}

fn env_or(name: &str, default: &str) -> String {
    env::var(name).unwrap_or_else(|_| default.to_string())
}

#[test]
fn smoke_round_trip() {
    if !env_flag("MC_RUST_STORE_RUN_INTEGRATION") {
        eprintln!("skipping Mooncake Store Rust integration smoke test");
        return;
    }

    let store = MooncakeStore::new().expect("create store handle");
    store
        .setup(
            &env_or("MC_RUST_STORE_LOCAL_HOSTNAME", "127.0.0.1"),
            &env_or("MC_METADATA_SERVER", "http://127.0.0.1:8080/metadata"),
            512 << 20,
            128 << 20,
            &env_or("MC_RUST_STORE_PROTOCOL", "tcp"),
            &env_or("MC_RUST_STORE_DEVICE_NAME", ""),
            &env_or("MC_RUST_STORE_MASTER_ADDR", "127.0.0.1:50051"),
        )
        .expect("setup store");

    store.health_check().expect("health check");

    let unique_suffix = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system clock before unix epoch")
        .as_nanos();
    let key = format!("rust-smoke-{unique_suffix}");
    let value = b"hello from rust smoke test";

    store.put(&key, value, None).expect("put value");
    assert!(store.is_exist(&key).expect("check existence"));
    assert_eq!(store.get_size(&key).expect("get size") as usize, value.len());
    assert_eq!(store.get(&key).expect("get value"), value);
    assert!(!store.get_hostname().expect("get hostname").is_empty());

    store.remove(&key, false).expect("remove key");
    assert!(!store.is_exist(&key).expect("check absence"));
}
