#![allow(clippy::unwrap_used, clippy::panic)] #![allow(clippy::print_stderr)]
use std::time::Duration;
use fraiseql_core::schema::CompiledSchema;
use fraiseql_server::{ServerConfig, capability::REGISTRY};
use tokio::net::TcpListener;
use super::run::build_server;
fn compiled_in(name: &str) -> bool {
REGISTRY.iter().any(|entry| entry.meta.name == name && entry.compiled_in)
}
#[tokio::test]
async fn run_starts_a_compiled_in_observer_runtime() {
if !compiled_in("observers") {
eprintln!("SKIP run_starts_a_compiled_in_observer_runtime: observers not compiled in");
return;
}
let Some(url) = fraiseql_test_support::try_database_url() else {
eprintln!("SKIP run_starts_a_compiled_in_observer_runtime: no DATABASE_URL");
return;
};
let pool = fraiseql_server::sqlx::PgPool::connect(&url).await.unwrap();
fraiseql_server::migration_lock::run_migrations(
&pool,
&[
fraiseql_observers::migrations::observer_management_sql(),
fraiseql_observers::migrations::entity_change_log_contract_sql(),
fraiseql_observers::migrations::observer_dispatch_sql(),
],
)
.await
.unwrap();
pool.close().await;
let config: ServerConfig = serde_json::from_value(serde_json::json!({
"database_url": url,
"cors_enabled": false,
"observers": { "enabled": true },
}))
.unwrap();
let server = build_server(config, CompiledSchema::new(), None).await.unwrap();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (stop, stopped) = tokio::sync::oneshot::channel::<()>();
let serving = tokio::spawn(server.serve_on_listener(listener, async {
let _ = stopped.await;
}));
let observers = tokio::time::timeout(Duration::from_secs(10), async {
loop {
let health: serde_json::Value = reqwest::get(format!("http://{addr}/health"))
.await
.unwrap()
.json()
.await
.unwrap();
if health["observers"]["running"] == true {
return health["observers"].clone();
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
})
.await;
let _ = stop.send(());
serving.await.unwrap().unwrap();
assert!(
observers.is_ok(),
"the server `fraiseql run` builds holds no database pool: a compiled-in observer \
runtime never started"
);
}
#[test]
fn run_is_declared_to_start_what_the_binary_starts() {
for entry in REGISTRY {
let declared = entry.meta.entry_points;
assert_eq!(
declared.run, declared.binary,
"`{}` declares `fraiseql run` and the binary differently, but they share one \
assembly and one entry point",
entry.meta.name
);
}
}