#![allow(dead_code)]
use foundation_db::{D1Store, R2Store};
pub const ENV_ENABLE: &str = "CF_INTEGRATION_TEST";
pub const ENV_BASE_URL: &str = "LOCAL_CF_API_BASE";
pub const ENV_D1_DB_ID: &str = "LOCAL_D1_DATABASE_ID";
pub const ENV_R2_BUCKET: &str = "LOCAL_R2_BUCKET";
pub const DEFAULT_BASE_URL: &str = "http://localhost:8789";
fn env_or(name: &str, default: &str) -> String {
std::env::var(name).unwrap_or_else(|_| default.to_string())
}
pub fn local_cf_api_base() -> String {
env_or(ENV_BASE_URL, DEFAULT_BASE_URL)
}
pub fn is_local_cf_available() -> bool {
if std::env::var(ENV_ENABLE).ok().as_deref() != Some("1") {
return false;
}
let base = local_cf_api_base();
let response = std::process::Command::new("curl")
.args(["-s", "-o", "/dev/null", "-w", "%{http_code}", &base])
.output();
match response {
Ok(output) => {
let status = String::from_utf8_lossy(&output.stdout);
let status = status.trim();
status == "200" || status == "404"
}
Err(_) => false,
}
}
static POOL_GUARD: std::sync::Mutex<Option<foundation_core::valtron::PoolGuard>> =
std::sync::Mutex::new(None);
pub fn init_valtron() {
let mut guard = POOL_GUARD.lock().unwrap();
if guard.is_none() {
*guard = Some(foundation_core::valtron::initialize_pool(42, None));
}
}
pub fn make_d1_store() -> Option<D1Store> {
if !is_local_cf_available() {
return None;
}
let db_id = env_or(ENV_D1_DB_ID, "test-db");
Some(D1Store::new_kv_with_base_url(
"test-token",
"test-account",
&db_id,
"test",
&local_cf_api_base(),
))
}
pub fn make_r2_store() -> Option<R2Store> {
if !is_local_cf_available() {
return None;
}
let bucket = env_or(ENV_R2_BUCKET, "test-bucket");
Some(R2Store::new_blob_with_base_url(
"test-token",
"test-account",
&bucket,
"test",
&local_cf_api_base(),
))
}