use std::sync::Arc;
use leviath_runtime::control_socket::{
ControlClient, ControlRequest, ControlResponse, bind_control_listener, control_id,
};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::task::JoinHandle;
pub(super) fn no_daemon_client() -> ControlClient {
ControlClient::new(control_id(std::path::Path::new("/no/such/daemon")))
}
pub(super) fn fake_daemon(
respond: impl Fn(ControlRequest) -> ControlResponse + Send + Sync + 'static,
) -> (ControlClient, tempfile::TempDir, JoinHandle<()>) {
let dir = tempfile::tempdir().unwrap();
let id = control_id(dir.path());
let mut listener = bind_control_listener(&id).unwrap();
let respond = Arc::new(respond);
let handle = tokio::spawn(async move {
let stream = listener
.accept()
.await
.expect("accept succeeds")
.expect("our own connection is admitted");
let (read_half, mut write_half) = tokio::io::split(stream);
let mut lines = BufReader::new(read_half).lines();
let line = lines.next_line().await.unwrap().unwrap();
let req = serde_json::from_str::<ControlRequest>(&line).unwrap();
let mut out = serde_json::to_string(&respond(req)).unwrap();
out.push('\n');
let _ = write_half.write_all(out.as_bytes()).await;
});
(ControlClient::new(id), dir, handle)
}