diff --git a/Cargo.lock b/Cargo.lock index 1a7cbd5..0e23b15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -949,6 +949,25 @@ dependencies = [ "uuid", ] +[[package]] +name = "maestro-e2e" +version = "0.1.0" +dependencies = [ + "chrono", + "futures-util", + "maestro-protocol", + "reqwest", + "serde", + "serde_json", + "tempfile", + "thiserror 1.0.69", + "tokio", + "tokio-tungstenite 0.21.0", + "toml", + "tracing", + "uuid", +] + [[package]] name = "maestro-installer" version = "0.1.0" diff --git a/crates/maestro-e2e/Cargo.toml b/crates/maestro-e2e/Cargo.toml new file mode 100644 index 0000000..125d6f0 --- /dev/null +++ b/crates/maestro-e2e/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "maestro-e2e" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +publish.workspace = true +description = "Cross-process integration tests: real registry + daemons as subprocesses" + +[lib] +path = "src/lib.rs" + +[dependencies] +chrono.workspace = true +futures-util.workspace = true +maestro-protocol.workspace = true +reqwest.workspace = true +serde.workspace = true +serde_json.workspace = true +tempfile.workspace = true +thiserror.workspace = true +tokio.workspace = true +tokio-tungstenite.workspace = true +toml.workspace = true +tracing.workspace = true +uuid.workspace = true + +[dev-dependencies] +tokio = { workspace = true, features = ["macros", "rt-multi-thread", "test-util"] } diff --git a/crates/maestro-e2e/src/build.rs b/crates/maestro-e2e/src/build.rs new file mode 100644 index 0000000..fe6e0a2 --- /dev/null +++ b/crates/maestro-e2e/src/build.rs @@ -0,0 +1,72 @@ +//! Locate / build the maestro binaries needed by the harness. +//! +//! `cargo test -p maestro-e2e` does not, by default, rebuild bins from other +//! workspace crates. So we re-invoke cargo from the first test that needs a +//! binary and cache the resolved paths. The re-invocation acquires its own +//! workspace lock — by the time tests start, the outer cargo has already +//! released it. + +use std::path::{Path, PathBuf}; +use std::sync::OnceLock; + +/// Absolute path of the workspace root (the dir containing the top-level +/// `Cargo.toml` whose body has `[workspace]`). +pub fn workspace_root() -> &'static Path { + static ROOT: OnceLock = OnceLock::new(); + ROOT.get_or_init(|| { + let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let mut cur: &Path = manifest.as_path(); + loop { + let candidate = cur.join("Cargo.toml"); + if candidate.exists() { + if let Ok(body) = std::fs::read_to_string(&candidate) { + if body.contains("[workspace]") { + return cur.to_path_buf(); + } + } + } + match cur.parent() { + Some(p) => cur = p, + None => panic!( + "could not find workspace root starting from {}", + PathBuf::from(env!("CARGO_MANIFEST_DIR")).display() + ), + } + } + }) +} + +/// Resolve `target//` under the workspace root, after running +/// `cargo build --bin -p ` to make sure it exists. +pub fn workspace_target_bin(package: &str, bin: &str) -> PathBuf { + static BUILDS: OnceLock>> = OnceLock::new(); + let builds = BUILDS.get_or_init(|| std::sync::Mutex::new(Vec::new())); + + let already = { + let lock = builds.lock().unwrap(); + lock.iter().any(|b| b == bin) + }; + let release = !cfg!(debug_assertions); + if !already { + let mut cmd = std::process::Command::new(env!("CARGO")); + cmd.args(["build", "--quiet", "-p", package, "--bin", bin]); + if release { + cmd.arg("--release"); + } + let status = cmd + .current_dir(workspace_root()) + .status() + .unwrap_or_else(|e| panic!("failed to invoke cargo build for {bin}: {e}")); + assert!(status.success(), "cargo build for `{bin}` failed"); + builds.lock().unwrap().push(bin.to_string()); + } + + let profile = if release { "release" } else { "debug" }; + let path = workspace_root().join("target").join(profile).join(bin); + assert!( + path.exists(), + "expected built binary at {} after `cargo build`", + path.display() + ); + path +} diff --git a/crates/maestro-e2e/src/client.rs b/crates/maestro-e2e/src/client.rs new file mode 100644 index 0000000..99aa0f2 --- /dev/null +++ b/crates/maestro-e2e/src/client.rs @@ -0,0 +1,231 @@ +//! Thin HTTP+WS client used by tests to drive the registry. +//! +//! Cookies are managed automatically by `reqwest`'s built-in cookie store. + +#![allow(clippy::result_large_err)] + +use std::time::Duration; + +use futures_util::{SinkExt, StreamExt}; +use maestro_protocol::{ConnectInfo, CreateSessionRequest, DaemonInfo, RegistryEvent, Session}; +use reqwest::Client as HttpClient; +use std::sync::Arc; +use tokio_tungstenite::tungstenite::client::IntoClientRequest; +use tokio_tungstenite::tungstenite::Message as WsMessage; +use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream}; +use uuid::Uuid; + +#[derive(Debug, thiserror::Error)] +#[allow(clippy::result_large_err)] +pub enum ClientError { + #[error("http: {0}")] + Http(#[from] reqwest::Error), + #[error("ws: {0}")] + Ws(#[from] tokio_tungstenite::tungstenite::Error), + #[error("json: {0}")] + Json(#[from] serde_json::Error), + #[error("unexpected status {status}: {body}")] + Status { status: u16, body: String }, +} + +/// Sticky-cookie HTTP client + a few WS-aware helpers. +#[derive(Debug, Clone)] +pub struct TestClient { + http_base: String, + ws_base: String, + http: HttpClient, +} + +impl TestClient { + pub fn new(http_base: &str, ws_base: &str) -> Self { + let http = HttpClient::builder() + .cookie_store(true) + .timeout(Duration::from_secs(5)) + .build() + .expect("reqwest client"); + Self { + http_base: http_base.trim_end_matches('/').to_string(), + ws_base: ws_base.trim_end_matches('/').to_string(), + http, + } + } + + /// Hit any GET endpoint so the registry issues the auth cookie that all + /// subsequent calls reuse. + pub async fn bootstrap(&self) -> Result<(), ClientError> { + let _ = self.list_daemons().await?; + Ok(()) + } + + pub async fn list_daemons(&self) -> Result, ClientError> { + let r = self + .http + .get(format!("{}/api/daemons", self.http_base)) + .send() + .await?; + check_ok(&r)?; + Ok(r.json().await?) + } + + pub async fn list_sessions(&self) -> Result, ClientError> { + let r = self + .http + .get(format!("{}/api/sessions", self.http_base)) + .send() + .await?; + check_ok(&r)?; + Ok(r.json().await?) + } + + pub async fn spawn_session(&self, req: &CreateSessionRequest) -> Result { + let r = self + .http + .post(format!("{}/api/sessions", self.http_base)) + .json(req) + .send() + .await?; + let status = r.status(); + if !status.is_success() { + let body = r.text().await.unwrap_or_default(); + return Err(ClientError::Status { + status: status.as_u16(), + body, + }); + } + Ok(r.json().await?) + } + + pub async fn kill_session(&self, id: Uuid) -> Result<(), ClientError> { + let r = self + .http + .delete(format!("{}/api/sessions/{id}", self.http_base)) + .send() + .await?; + check_ok(&r)?; + Ok(()) + } + + pub async fn connect_info(&self, id: Uuid) -> Result { + let r = self + .http + .get(format!("{}/api/sessions/{id}/connect-info", self.http_base)) + .send() + .await?; + check_ok(&r)?; + Ok(r.json().await?) + } + + /// Connect to the daemon's per-session WS and return the stream. The + /// caller drives the conversation. + pub async fn attach( + &self, + info: &ConnectInfo, + id: Uuid, + ) -> Result>, ClientError> { + let url = format!( + "{}/sessions/{id}/stream?token={}", + info.daemon_url.trim_end_matches('/'), + info.jwt + ); + let request = url.into_client_request()?; + let (ws, _resp) = connect_async(request).await?; + Ok(ws) + } + + /// Subscribe to the registry's `/api/events` WS. Returns a tokio task + /// handle that drives the WS and pushes parsed events into an Arc>. + pub async fn subscribe_events(&self) -> EventStream { + // Forward the latest cookie value with the upgrade — `reqwest` and + // `tungstenite` don't share cookie jars, so we copy the cookie out by + // hand. The registry will accept us either way (missing cookie just + // gets a fresh one), but reusing the same identity keeps log noise + // down. + let cookie_header = self.grab_cookie().await.unwrap_or_default(); + let url = format!("{}/api/events", self.ws_base); + let mut request = url.into_client_request().expect("ws req"); + if !cookie_header.is_empty() { + request.headers_mut().insert( + reqwest::header::COOKIE, + cookie_header.parse().expect("cookie hv"), + ); + } + let (ws, _resp) = connect_async(request).await.expect("events ws"); + + let events = Arc::new(tokio::sync::Mutex::new(Vec::new())); + let stream_events = events.clone(); + let (mut sink, mut stream) = ws.split(); + let task = tokio::spawn(async move { + while let Some(msg) = stream.next().await { + match msg { + Ok(WsMessage::Text(t)) => { + if let Ok(ev) = serde_json::from_str::(&t) { + stream_events.lock().await.push(ev); + } + } + Ok(WsMessage::Close(_)) | Err(_) => break, + _ => {} + } + } + let _ = sink.close().await; + }); + EventStream { events, task } + } + + async fn grab_cookie(&self) -> Option { + // Force a request to make sure the jar has a cookie, then read it back + // via a debug header roundtrip. reqwest's CookieStore isn't directly + // exposed; the simplest path is to ask the registry for an auth + // cookie and capture the Set-Cookie from the response. + let r = self + .http + .get(format!("{}/api/daemons", self.http_base)) + .send() + .await + .ok()?; + // The cookie store has the cookie now, but reqwest doesn't surface + // it. We rebuild from Set-Cookie if present. + r.headers() + .get_all(reqwest::header::SET_COOKIE) + .iter() + .find_map(|hv| hv.to_str().ok().and_then(extract_cookie_pair)) + } +} + +fn extract_cookie_pair(set_cookie: &str) -> Option { + // Take everything before the first ';', which is `name=value`. + set_cookie + .split(';') + .next() + .map(str::trim) + .map(String::from) +} + +fn check_ok(r: &reqwest::Response) -> Result<(), ClientError> { + let status = r.status(); + if !status.is_success() { + return Err(ClientError::Status { + status: status.as_u16(), + body: format!("HTTP {status}"), + }); + } + Ok(()) +} + +/// Handle to a background task draining the events WS into a vector. +#[derive(Debug)] +pub struct EventStream { + events: Arc>>, + task: tokio::task::JoinHandle<()>, +} + +impl EventStream { + pub async fn snapshot(&self) -> Vec { + self.events.lock().await.clone() + } +} + +impl Drop for EventStream { + fn drop(&mut self) { + self.task.abort(); + } +} diff --git a/crates/maestro-e2e/src/daemon.rs b/crates/maestro-e2e/src/daemon.rs new file mode 100644 index 0000000..0e692fd --- /dev/null +++ b/crates/maestro-e2e/src/daemon.rs @@ -0,0 +1,165 @@ +//! Subprocess wrapper around `maestrod`. + +use std::net::{Ipv4Addr, SocketAddr}; +use std::path::PathBuf; +use std::process::{Child, Command, Stdio}; +use std::time::Duration; + +use tempfile::TempDir; + +use crate::build::workspace_target_bin; +use crate::util::{free_port, wait_for_async}; + +/// A live `maestrod` running in a child process. Kill the child on drop. +/// +/// `state_dir`, `home`, and `tmux_tmpdir` are kept as `TempDir`s so they +/// outlive the process and get cleaned up automatically. +#[derive(Debug)] +pub struct Daemon { + pub daemon_id: String, + pub bind_addr: SocketAddr, + pub daemon_url: String, + child: Option, + home: TempDir, + state: TempDir, + tmux: TempDir, + registry_url: String, + token: String, +} + +impl Daemon { + /// Spawn a fresh daemon pointing at `registry_url` (the daemon-facing WS + /// URL — usually `ws:///daemon`). + pub async fn spawn(daemon_id: &str, registry_url: &str, token: &str) -> Self { + let port = free_port(); + let bind = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); + Self::spawn_with_port(daemon_id, registry_url, token, bind).await + } + + /// Stop the underlying process, but keep its state dirs around so we can + /// `restart()` against the same `$HOME` / `state` / `TMUX_TMPDIR`. + pub fn stop(&mut self) { + if let Some(mut c) = self.child.take() { + let _ = c.kill(); + let _ = c.wait(); + } + } + + /// Re-spawn the daemon on the same port + state. The previous tmux + /// server (under `TMUX_TMPDIR`) is left running so `recover()` can adopt + /// its sessions. + pub async fn restart(&mut self) { + self.stop(); + let child = launch( + &self.daemon_id, + self.bind_addr, + &self.daemon_url, + &self.registry_url, + &self.token, + self.home.path(), + self.state.path(), + self.tmux.path(), + ); + self.child = Some(child); + self.wait_until_attach_ready().await; + } + + async fn spawn_with_port( + daemon_id: &str, + registry_url: &str, + token: &str, + bind: SocketAddr, + ) -> Self { + let home = TempDir::new().expect("home tmpdir"); + let state = TempDir::new().expect("state tmpdir"); + let tmux = TempDir::new().expect("tmux tmpdir"); + + let daemon_url = format!("ws://{bind}"); + let child = launch( + daemon_id, + bind, + &daemon_url, + registry_url, + token, + home.path(), + state.path(), + tmux.path(), + ); + let me = Self { + daemon_id: daemon_id.to_string(), + bind_addr: bind, + daemon_url, + child: Some(child), + home, + state, + tmux, + registry_url: registry_url.to_string(), + token: token.to_string(), + }; + me.wait_until_attach_ready().await; + me + } + + async fn wait_until_attach_ready(&self) { + let addr = self.bind_addr; + wait_for_async( + "daemon attach port", + Duration::from_secs(5), + || async move { tokio::net::TcpStream::connect(addr).await.is_ok() }, + ) + .await; + } + + pub fn home(&self) -> &std::path::Path { + self.home.path() + } + + pub fn state_dir(&self) -> PathBuf { + // Matches maestro_daemon::default_state_dir which uses XDG_STATE_HOME. + self.home.path().join(".local/state/maestrod") + } +} + +impl Drop for Daemon { + fn drop(&mut self) { + if let Some(mut c) = self.child.take() { + let _ = c.kill(); + let _ = c.wait(); + } + } +} + +#[allow(clippy::too_many_arguments)] +fn launch( + daemon_id: &str, + bind: SocketAddr, + daemon_url: &str, + registry_url: &str, + token: &str, + home: &std::path::Path, + _state: &std::path::Path, + tmux_tmpdir: &std::path::Path, +) -> Child { + let bin = workspace_target_bin("maestro-daemon", "maestrod"); + Command::new(&bin) + .env_clear() + .env( + "PATH", + std::env::var("PATH").unwrap_or_else(|_| "/usr/bin:/bin".into()), + ) + .env("HOME", home) + .env("XDG_CONFIG_HOME", home.join(".config")) + .env("XDG_STATE_HOME", home.join(".local/state")) + .env("TMUX_TMPDIR", tmux_tmpdir) + .env("MAESTRO_TOKEN", token) + .env("MAESTROD_DAEMON_ID", daemon_id) + .env("MAESTROD_REGISTRY_URL", registry_url) + .env("MAESTROD_BIND_ADDR", bind.to_string()) + .env("MAESTROD_DAEMON_URL", daemon_url) + .env("RUST_LOG", "warn") + .stdout(Stdio::null()) + .stderr(Stdio::inherit()) + .arg("run") + .spawn() + .unwrap_or_else(|e| panic!("spawn {} failed: {e}", bin.display())) +} diff --git a/crates/maestro-e2e/src/lib.rs b/crates/maestro-e2e/src/lib.rs new file mode 100644 index 0000000..fa6d85a --- /dev/null +++ b/crates/maestro-e2e/src/lib.rs @@ -0,0 +1,20 @@ +//! Cross-process integration test harness for Maestro. +//! +//! Each test spawns a real `maestro-registry` and one or more `maestrod` +//! subprocesses, isolates them in temp dirs (`$HOME`, `TMUX_TMPDIR`, state +//! dir), and drives them through their public HTTP/WS APIs. +//! +//! See `tests/` for the actual scenarios. + +#![deny(missing_debug_implementations)] + +pub mod build; +pub mod client; +pub mod daemon; +pub mod registry; +pub mod util; + +pub use build::workspace_target_bin; +pub use client::TestClient; +pub use daemon::Daemon; +pub use registry::Registry; diff --git a/crates/maestro-e2e/src/registry.rs b/crates/maestro-e2e/src/registry.rs new file mode 100644 index 0000000..af5919d --- /dev/null +++ b/crates/maestro-e2e/src/registry.rs @@ -0,0 +1,94 @@ +//! Subprocess wrapper around `maestro-registry`. + +use std::net::{Ipv4Addr, SocketAddr}; +use std::path::PathBuf; +use std::process::{Child, Command, Stdio}; +use std::time::Duration; + +use tempfile::TempDir; + +use crate::build::workspace_target_bin; +use crate::util::{free_port, wait_for_async}; + +/// A `maestro-registry` running in a child process. The state directory is +/// kept as a `TempDir` so it survives the `Registry`'s lifetime and gets +/// cleaned up on drop. +#[derive(Debug)] +pub struct Registry { + pub addr: SocketAddr, + pub token: String, + child: Option, + _home: TempDir, +} + +impl Registry { + pub async fn spawn(token: &str) -> Self { + let port = free_port(); + let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); + let home = TempDir::new().expect("home tmpdir"); + + let bin = workspace_target_bin("maestro-registry", "maestro-registry"); + let child = Command::new(&bin) + .env_clear() + // PATH so tmux etc. resolve. We need a few essentials. + .env( + "PATH", + std::env::var("PATH").unwrap_or_else(|_| "/usr/bin:/bin".into()), + ) + .env("HOME", home.path()) + .env("XDG_CONFIG_HOME", home.path().join(".config")) + .env("XDG_STATE_HOME", home.path().join(".local/state")) + .env("MAESTRO_TOKEN", token) + .env("MAESTRO_REGISTRY_BIND_ADDR", addr.to_string()) + .env("RUST_LOG", "warn") + .stdout(Stdio::null()) + .stderr(Stdio::inherit()) + .spawn() + .unwrap_or_else(|e| panic!("spawn {} failed: {e}", bin.display())); + + let url = format!("http://{addr}/api/daemons"); + let http = reqwest::Client::builder() + .timeout(Duration::from_secs(2)) + .build() + .expect("reqwest client"); + wait_for_async("registry to bind", Duration::from_secs(5), || { + let http = http.clone(); + let url = url.clone(); + async move { http.get(&url).send().await.is_ok() } + }) + .await; + + Self { + addr, + token: token.to_string(), + child: Some(child), + _home: home, + } + } + + pub fn http_base(&self) -> String { + format!("http://{}", self.addr) + } + + pub fn ws_base(&self) -> String { + format!("ws://{}", self.addr) + } + + /// URL daemons dial. + pub fn daemon_ws_url(&self) -> String { + format!("ws://{}/daemon", self.addr) + } + + pub fn state_dir(&self) -> PathBuf { + self._home.path().to_path_buf() + } +} + +impl Drop for Registry { + fn drop(&mut self) { + if let Some(mut c) = self.child.take() { + let _ = c.kill(); + let _ = c.wait(); + } + } +} diff --git a/crates/maestro-e2e/src/util.rs b/crates/maestro-e2e/src/util.rs new file mode 100644 index 0000000..64de161 --- /dev/null +++ b/crates/maestro-e2e/src/util.rs @@ -0,0 +1,31 @@ +//! Helpers shared between harness modules and tests. + +use std::future::Future; +use std::net::{Ipv4Addr, SocketAddr, TcpListener}; +use std::time::{Duration, Instant}; + +/// Pick a free TCP port by binding to `127.0.0.1:0` and immediately releasing. +/// +/// There is a small race: another process may grab the port between drop and +/// the child re-binding, but for local single-user test runs it's negligible. +pub fn free_port() -> u16 { + let l = TcpListener::bind(SocketAddr::from((Ipv4Addr::LOCALHOST, 0))).expect("bind :0"); + l.local_addr().expect("local_addr").port() +} + +/// Poll `cond` every 50ms until it returns `true` or `timeout` elapses. +/// Panics on timeout with `label`. +pub async fn wait_for_async(label: &str, timeout: Duration, mut cond: F) +where + F: FnMut() -> Fut, + Fut: Future, +{ + let deadline = Instant::now() + timeout; + while Instant::now() < deadline { + if cond().await { + return; + } + tokio::time::sleep(Duration::from_millis(50)).await; + } + panic!("timed out after {timeout:?} waiting for: {label}"); +} diff --git a/crates/maestro-e2e/tests/e2e.rs b/crates/maestro-e2e/tests/e2e.rs new file mode 100644 index 0000000..d16eb65 --- /dev/null +++ b/crates/maestro-e2e/tests/e2e.rs @@ -0,0 +1,329 @@ +//! Cross-process end-to-end tests for the headless Maestro slice. +//! +//! Each test brings up a real `maestro-registry` plus 1-2 real `maestrod` +//! subprocesses, isolates them in temp dirs, and drives them through the +//! public HTTP+WS surface using the same client APIs a real CLI/web client +//! would. +//! +//! These tests require `tmux` and `git` on `$PATH`. + +#![allow(clippy::result_large_err)] + +use std::collections::HashMap; +use std::process::Command; +use std::time::{Duration, Instant}; + +use futures_util::{SinkExt, StreamExt}; +use maestro_e2e::util::wait_for_async; +use maestro_e2e::{Daemon, Registry, TestClient}; +use maestro_protocol::{CreateSessionRequest, WorktreeSpec}; +use tempfile::TempDir; +use tokio_tungstenite::tungstenite::Message as WsMessage; + +const TOKEN: &str = "e2e-shared-token"; + +async fn bring_up(daemon_ids: &[&str]) -> (Registry, Vec, TestClient) { + let registry = Registry::spawn(TOKEN).await; + let mut daemons = Vec::with_capacity(daemon_ids.len()); + for id in daemon_ids { + let d = Daemon::spawn(id, ®istry.daemon_ws_url(), TOKEN).await; + daemons.push(d); + } + let client = TestClient::new(®istry.http_base(), ®istry.ws_base()); + client.bootstrap().await.expect("bootstrap"); + + // Wait until both daemons have connected and announced themselves. + let expected: Vec = daemon_ids.iter().map(|s| s.to_string()).collect(); + wait_for_async("daemons to register", Duration::from_secs(10), || { + let client = client.clone(); + let expected = expected.clone(); + async move { + match client.list_daemons().await { + Ok(ds) => expected + .iter() + .all(|id| ds.iter().any(|d| &d.daemon_id == id)), + Err(_) => false, + } + } + }) + .await; + + (registry, daemons, client) +} + +fn create_req(daemon_id: &str, cwd: &std::path::Path, command: &str) -> CreateSessionRequest { + CreateSessionRequest { + daemon_id: daemon_id.into(), + cwd: cwd.to_path_buf(), + command: command.into(), + env: HashMap::new(), + metadata: HashMap::new(), + worktree: None, + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn lists_sessions_from_both_daemons() { + let (_reg, _daemons, client) = bring_up(&["alpha", "beta"]).await; + let cwd = TempDir::new().unwrap(); + + let a = client + .spawn_session(&create_req("alpha", cwd.path(), "sleep 30")) + .await + .expect("spawn alpha"); + let b = client + .spawn_session(&create_req("beta", cwd.path(), "sleep 30")) + .await + .expect("spawn beta"); + + let sessions = client.list_sessions().await.expect("list"); + assert!(sessions + .iter() + .any(|s| s.id == a.id && s.daemon_id == "alpha")); + assert!(sessions + .iter() + .any(|s| s.id == b.id && s.daemon_id == "beta")); + + client.kill_session(a.id).await.expect("kill a"); + client.kill_session(b.id).await.expect("kill b"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn multi_attach_sees_same_output() { + let (_reg, _daemons, client) = bring_up(&["solo"]).await; + let cwd = TempDir::new().unwrap(); + + let s = client + .spawn_session(&create_req("solo", cwd.path(), "bash --noprofile --norc")) + .await + .expect("spawn"); + + let info = client.connect_info(s.id).await.expect("connect-info"); + let mut a = client.attach(&info, s.id).await.expect("attach a"); + let mut b = client.attach(&info, s.id).await.expect("attach b"); + + // Drive input through client A; both attachments should observe it via + // the daemon's PTY broadcast. + let send = b"echo MAESTRO_E2E_SHARED\n".to_vec(); + a.send(WsMessage::Binary(send)).await.expect("send keys"); + + let want = b"MAESTRO_E2E_SHARED"; + let deadline = Instant::now() + Duration::from_secs(5); + let mut buf_a = Vec::new(); + let mut buf_b = Vec::new(); + while Instant::now() < deadline { + tokio::select! { + msg = a.next() => match msg { + Some(Ok(WsMessage::Binary(bytes))) => buf_a.extend(bytes), + Some(Ok(_)) => {} + _ => break, + }, + msg = b.next() => match msg { + Some(Ok(WsMessage::Binary(bytes))) => buf_b.extend(bytes), + Some(Ok(_)) => {} + _ => break, + }, + _ = tokio::time::sleep(Duration::from_millis(50)) => {} + } + if buf_a.windows(want.len()).any(|w| w == want) + && buf_b.windows(want.len()).any(|w| w == want) + { + break; + } + } + assert!( + buf_a.windows(want.len()).any(|w| w == want), + "client A never saw the echo" + ); + assert!( + buf_b.windows(want.len()).any(|w| w == want), + "client B never saw the echo" + ); + + let _ = a.close(None).await; + let _ = b.close(None).await; + client.kill_session(s.id).await.expect("kill"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn cleanup_on_kill_removes_worktree() { + let (_reg, _daemons, client) = bring_up(&["host"]).await; + + let base = TempDir::new().unwrap(); + init_repo(base.path()); + + let req = CreateSessionRequest { + daemon_id: "host".into(), + cwd: base.path().to_path_buf(), + command: "sleep 30".into(), + env: HashMap::new(), + metadata: HashMap::new(), + worktree: Some(WorktreeSpec { + base_repo: base.path().to_path_buf(), + branch: "feature/e2e".into(), + cleanup_on_kill: true, + }), + }; + + let s = client.spawn_session(&req).await.expect("spawn worktree"); + // The daemon's WorktreeManager puts new worktrees under + // `/worktrees/`. We can't peek the daemon's state dir + // directly from here, but we can verify by asking git: a worktree must + // exist that lives outside the original repo. + let worktrees = git_worktree_list(base.path()); + assert!( + worktrees.iter().any(|w| w.contains("feature/e2e")), + "expected a worktree for feature/e2e branch, got: {worktrees:?}" + ); + + client.kill_session(s.id).await.expect("kill"); + + // Give the daemon a moment to remove the worktree, then verify. + let deadline = Instant::now() + Duration::from_secs(5); + while Instant::now() < deadline { + let after = git_worktree_list(base.path()); + if !after.iter().any(|w| w.contains("feature/e2e")) { + return; + } + tokio::time::sleep(Duration::from_millis(100)).await; + } + panic!( + "worktree feature/e2e still present after kill: {:?}", + git_worktree_list(base.path()) + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn daemon_restart_recovers_session() { + let (_reg, mut daemons, client) = bring_up(&["restarter"]).await; + let cwd = TempDir::new().unwrap(); + + let s = client + .spawn_session(&create_req("restarter", cwd.path(), "sleep 300")) + .await + .expect("spawn"); + assert_eq!(s.daemon_id, "restarter"); + + // Stop the daemon and wait for the registry to notice the disconnect. + // The catalog drops sessions for a daemon that's no longer connected. + daemons[0].stop(); + wait_for_async( + "registry drops disconnected daemon", + Duration::from_secs(10), + || { + let client = client.clone(); + async move { + client + .list_daemons() + .await + .map(|ds| ds.iter().all(|d| d.daemon_id != "restarter")) + .unwrap_or(false) + } + }, + ) + .await; + + // Bring the daemon back. It should recover its tmux session and the + // registry catalog should repopulate it. + daemons[0].restart().await; + + wait_for_async("restarter re-registers", Duration::from_secs(10), || { + let client = client.clone(); + async move { + client + .list_daemons() + .await + .map(|ds| ds.iter().any(|d| d.daemon_id == "restarter")) + .unwrap_or(false) + } + }) + .await; + + wait_for_async( + "session reappears in catalog", + Duration::from_secs(10), + || { + let client = client.clone(); + let id = s.id; + async move { + client + .list_sessions() + .await + .map(|ss| ss.iter().any(|x| x.id == id)) + .unwrap_or(false) + } + }, + ) + .await; + + client.kill_session(s.id).await.expect("kill"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn concurrent_spawns_each_get_unique_ids() { + let (_reg, _daemons, client) = bring_up(&["parallel"]).await; + let cwd = TempDir::new().unwrap(); + + let mut handles = Vec::new(); + for _ in 0..5 { + let client = client.clone(); + let path = cwd.path().to_path_buf(); + handles.push(tokio::spawn(async move { + client + .spawn_session(&create_req("parallel", &path, "sleep 30")) + .await + .expect("spawn") + })); + } + let mut ids = Vec::new(); + for h in handles { + ids.push(h.await.unwrap().id); + } + ids.sort(); + ids.dedup(); + assert_eq!(ids.len(), 5, "expected 5 distinct session ids"); + + for id in ids { + client.kill_session(id).await.expect("kill"); + } +} + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +fn init_repo(dir: &std::path::Path) { + run_git(&["init", "-q", "-b", "main"], dir); + run_git(&["config", "user.email", "e2e@example.com"], dir); + run_git(&["config", "user.name", "E2E"], dir); + std::fs::write(dir.join("README"), b"hello\n").unwrap(); + run_git(&["add", "."], dir); + run_git(&["commit", "-q", "-m", "initial"], dir); +} + +fn run_git(args: &[&str], cwd: &std::path::Path) { + let out = Command::new("git") + .arg("-C") + .arg(cwd) + .args(args) + .output() + .expect("git spawn"); + assert!( + out.status.success(), + "git {args:?} failed: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +fn git_worktree_list(dir: &std::path::Path) -> Vec { + let out = Command::new("git") + .arg("-C") + .arg(dir) + .args(["worktree", "list", "--porcelain"]) + .output() + .expect("git worktree list"); + String::from_utf8_lossy(&out.stdout) + .lines() + .map(str::to_string) + .collect() +}