use std::borrow::Cow;
use std::sync::atomic::{AtomicU64, Ordering};
use supabase_testcontainers_modules::{Functions, FUNCTIONS_PORT};
const JWT_SECRET: &str = "super-secret-jwt-token-with-at-least-32-characters-for-hs256";
static TEST_COUNTER: AtomicU64 = AtomicU64::new(0);
fn unique_test_id() -> String {
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis();
let counter = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
format!("{}-{}", timestamp, counter)
}
#[cfg(test)]
mod tests {
use super::*;
use testcontainers_modules::testcontainers::core::WaitFor;
use testcontainers_modules::testcontainers::Image;
#[test]
fn test_default_configuration() {
let functions = Functions::default();
assert_eq!(functions.name(), "supabase/edge-runtime");
assert!(functions.tag().starts_with("v1."));
}
#[test]
fn test_jwt_secret_configuration() {
let functions = Functions::default().with_jwt_secret(JWT_SECRET);
assert_eq!(functions.name(), "supabase/edge-runtime");
}
#[test]
fn test_full_configuration() {
let _test_id = unique_test_id();
let functions = Functions::default()
.with_jwt_secret(JWT_SECRET)
.with_verify_jwt(false)
.with_worker_timeout_ms(30000)
.with_max_parallelism(4)
.with_supabase_url("http://kong:8000")
.with_anon_key("test-anon-key")
.with_service_role_key("test-service-key")
.with_db_url("postgres://user:pass@localhost:5432/db")
.with_main_service_path("/custom/functions")
.with_tag("v1.67.4")
.with_env("CUSTOM_VAR", "custom_value");
assert_eq!(functions.name(), "supabase/edge-runtime");
assert_eq!(functions.tag(), "v1.67.4");
}
#[test]
fn test_exposed_port() {
let functions = Functions::default();
let ports = functions.expose_ports();
assert_eq!(ports.len(), 1);
assert_eq!(FUNCTIONS_PORT, 9000);
}
#[test]
fn test_ready_conditions() {
let functions = Functions::default();
let conditions = functions.ready_conditions();
assert_eq!(conditions.len(), 1);
match &conditions[0] {
WaitFor::Log(log_wait) => {
assert!(
format!("{:?}", log_wait).contains("Listening on"),
"Should wait for 'Listening on' message"
);
}
_ => panic!("Expected Log wait condition"),
}
}
#[test]
fn test_command() {
let functions = Functions::default();
let cmd: Vec<Cow<'_, str>> = functions.cmd().into_iter().map(|s| s.into()).collect();
assert_eq!(cmd.len(), 3);
assert_eq!(cmd[0], "start");
assert_eq!(cmd[1], "--main-service");
assert_eq!(cmd[2], "/home/deno/functions");
}
#[test]
fn test_custom_main_service_path() {
let functions = Functions::default().with_main_service_path("/custom/path");
let cmd: Vec<Cow<'_, str>> = functions.cmd().into_iter().map(|s| s.into()).collect();
assert_eq!(cmd[2], "/custom/path");
}
}