#![allow(clippy::unwrap_used, clippy::expect_used)]
use std::process::Command;
use std::time::Duration;
use omni_dev::daemon::client::DaemonClient;
async fn wait_for<F>(mut f: F) -> bool
where
F: FnMut() -> bool,
{
for _ in 0..200 {
if f() {
return true;
}
tokio::time::sleep(Duration::from_millis(25)).await;
}
false
}
#[tokio::test]
async fn daemon_run_status_stop_roundtrip() {
let dir = tempfile::tempdir().unwrap();
let socket = dir.path().join("d.sock");
let mut child = Command::new(env!("CARGO_BIN_EXE_omni-dev"))
.arg("daemon")
.arg("run")
.arg("--socket")
.arg(&socket)
.arg("--bridge-control-port")
.arg("0")
.arg("--bridge-ws-port")
.arg("0")
.arg("--no-menu")
.spawn()
.expect("failed to spawn `omni-dev daemon run`");
let client = DaemonClient::new(&socket);
let mut ready = false;
for _ in 0..200 {
if client.ping().await.is_ok() {
ready = true;
break;
}
tokio::time::sleep(Duration::from_millis(25)).await;
}
assert!(ready, "daemon did not become ready");
let report = client.status().await.expect("status request failed");
assert!(
report
.services
.iter()
.any(|s| s.name == "browser-bridge" && s.healthy),
"expected a healthy browser-bridge service, got {report:?}"
);
client.shutdown().await.expect("shutdown request failed");
let exited = wait_for(|| matches!(child.try_wait(), Ok(Some(_)))).await;
if !exited {
let _ = child.kill();
}
assert!(exited, "daemon did not exit after shutdown");
let status = child.wait().unwrap();
assert!(status.success(), "daemon exited with failure: {status:?}");
}