//! The app as the host (ledger-host app_server.rs and bridge.rs INVARIANT //! 12; plan/p15-app-and-relay.md 3.1; WB-app-1, WB-app-3): the books //! server is a child process of the window, and the bridge talks to the //! running app and says so in words when it cannot. //! //! ```text //! GOAL: the child starts, answers, opens and closes one company at a //! time on request, closes cleanly when its input does (the //! window's close, or the window's death), is never killed, and a //! start is refused while another program holds the books; and an //! agent's bridge is told, in words, that the app isn't running or //! that the company isn't open, then goes through once it is. //! SOURCE: plan/p15-app-and-relay.md 3.1 (the app starts the server as a //! child and closes it by closing its input; the bridge answers //! "Wanderbooks isn't running" or "Company X isn't open"), D-44, //! D-77, D-84, WB-app-1, WB-app-3. //! INVARIANTS: //! 1. A server started by `AppServer` is `wanderbooks server` of this //! program on the test's own root alone, says ready, and holds //! run/server.lock until it exits. //! 2. Closing its input (`stop`) closes the books as the host does: //! exit 0, the lock let go, the socket removed. //! 3. A window that is killed outright leaves no server: its input //! closes with it and the books close cleanly. //! 4. A start is refused, in words, while a server or a host holds the //! books, and touches nothing. //! 5. A stop that runs out of time says the server is still closing and //! leaves it alone: nothing is signalled. //! 6. A tab's company is opened and closed in the server by request; a //! company closed is not reachable, one opened is. //! 7. The bridge, for a root the app has served and with no host: //! "Wanderbooks isn't running" with the app not running, "Company X //! isn't open" with it running and the company not, and neither once //! both are so, in a session that was already running; it starts no //! host. //! ``` mod common; use common::{BOOK, Home, Server, clean_env}; use ledger_host::app_server::{ANOTHER_HAS_THEM, APP_MARKER, AppServer, Stopped, app_hosted}; use ledger_host::opener::{host_lock_held, server_lock_held}; use ledger_mcp::admin::{Request, Response}; use serde_json::{Value, json}; use std::io::{BufRead, BufReader, Write}; use std::path::Path; use std::process::{Child, Command, Stdio}; use std::sync::mpsc; use std::time::{Duration, Instant}; fn exe() -> &'static Path { Path::new(env!("CARGO_BIN_EXE_wanderbooks")) } /// The server the app starts, on `home`'s root alone, with `open` open. fn start(home: &Home, open: &[String]) -> AppServer { let dir = home.dir.path().to_path_buf(); AppServer::start_in(exe(), Some(&home.root()), open, move |c: &mut Command| { clean_env(c, &dir); c.env("XDG_CONFIG_HOME", dir.join(".config")); }) .unwrap_or_else(|why| panic!("{why}")) } /// Wait until `ok`, at most a minute. fn until(what: &str, mut ok: impl FnMut() -> bool) { let end = Instant::now() + Duration::from_secs(60); while !ok() { assert!(Instant::now() < end, "never: {what}"); std::thread::sleep(Duration::from_millis(20)); } } /// Whether the books `id` are open, by the server's own list. fn open_in_server(home: &Home, id: &str) -> bool { match home.admin(Request::List) { Response::Books(books) => books.iter().any(|b| b.id == id && b.open), other => panic!("{other:?}"), } } #[test] fn the_server_is_a_child_that_closes_when_its_input_does() { let home = Home::new("app-child"); let run = home.run(); let server = start(&home, &[]); server.wait_ready(Duration::from_secs(60)).unwrap(); assert!(app_hosted(&run), "the root is the app's from now on"); assert!(server_lock_held(&run), "it holds the server lock"); assert!(!host_lock_held(&run), "no host was started beside it"); assert!( matches!(home.admin(Request::List), Response::Books(_)), "it answers on its admin socket" ); assert_eq!(server.exited(), None); // Closed by closing its input, as the host does. assert_eq!(server.stop(Duration::from_secs(60)), Stopped::Closed); assert!( server .exited() .is_some_and(|how| how.contains("exit status: 0") || how.contains("exit code: 0")), "it exited cleanly: {:?}", server.exited() ); until("the lock is let go", || !server_lock_held(&run)); #[cfg(unix)] assert!( !run.join("admin.sock").exists() && !run.join("mcp.sock").exists(), "its sockets are removed" ); } #[test] fn opening_and_closing_a_company_drives_the_servers_book() { let home = Home::new("app-tabs"); let server = start(&home, &[]); server.wait_ready(Duration::from_secs(60)).unwrap(); home.create(BOOK, "USD"); assert!(open_in_server(&home, BOOK)); // A tab closed: the book is closed in the server. assert_eq!(server.close_book(BOOK), Ok(None)); assert!(!open_in_server(&home, BOOK)); // Closed again, or one that isn't there: nothing to say. assert_eq!(server.close_book(BOOK), Ok(None)); // A tab opened: the book is open again. assert_eq!(server.open_book(BOOK), Ok(None)); assert!(open_in_server(&home, BOOK)); assert_eq!(server.open_book(BOOK), Ok(None), "already open: fine"); // A company that isn't in the root is said in words. let refused = server.open_book("nope").unwrap_err(); assert!(refused.contains("no books called"), "{refused}"); assert_eq!(server.stop(Duration::from_secs(60)), Stopped::Closed); // After it, a request says so rather than hanging. assert!(server.open_book(BOOK).is_err()); } #[test] fn a_start_is_refused_when_another_program_has_the_books() { let home = Home::new("app-refused"); let run = home.run(); let dir = home.dir.path().to_path_buf(); let try_start = || { let dir = dir.clone(); AppServer::start_in(exe(), Some(&home.root()), &[], move |c: &mut Command| { clean_env(c, &dir); }) }; // A books server holds them (another window, or one still finishing). let other = Server::start_with(&home, &["--open", "none"]); let refused = try_start().err().expect("refused"); assert_eq!(refused, ANOTHER_HAS_THEM); assert!(!run.join(APP_MARKER).exists(), "a refusal touches nothing"); other.close(); until("the other let go", || !server_lock_held(&run)); // A host holds them. let lock = std::fs::OpenOptions::new() .create(true) .truncate(false) .write(true) .open(run.join("host.lock")) .unwrap(); lock.lock().unwrap(); assert_eq!(try_start().err().expect("refused"), ANOTHER_HAS_THEM); assert!(!run.join(APP_MARKER).exists()); drop(lock); // Held by nobody: it starts. let server = try_start().expect("started"); server.wait_ready(Duration::from_secs(60)).unwrap(); assert_eq!(server.stop(Duration::from_secs(60)), Stopped::Closed); } /// What the helper process below says, one line each. const CHILD_PID: &str = "wb-app-child-pid "; const READY: &str = "wb-app-ready"; /// Not a test of its own: the "window" the next test kills. It starts the /// books server as the app does and then waits to be killed; run by any /// other means it does nothing. #[test] fn a_window_that_is_about_to_die() { let Some(home) = std::env::var_os("WB_APP_DYING_HOME") else { return; }; let home = std::path::PathBuf::from(home); let root = ledger_store::root::default_root(&home); let dir = home.clone(); let server = AppServer::start_in(exe(), Some(&root), &[], move |c: &mut Command| { clean_env(c, &dir); c.env("XDG_CONFIG_HOME", dir.join(".config")); }) .expect("started"); server.wait_ready(Duration::from_secs(60)).unwrap(); println!("{CHILD_PID}{}", server.pid()); println!("{READY}"); let _ = std::io::stdout().flush(); std::thread::sleep(Duration::from_secs(600)); } #[test] fn a_window_that_dies_leaves_no_server_and_no_half_closed_book() { let home = Home::new("app-dies"); let run = home.run(); let mut window = Command::new(std::env::current_exe().unwrap()) .args(["--exact", "a_window_that_is_about_to_die", "--nocapture"]) .env("WB_APP_DYING_HOME", home.dir.path()) .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::null()) .spawn() .unwrap(); // Whatever happens, the stand-in window does not outlive this test. struct Reap(Child); impl Drop for Reap { fn drop(&mut self) { let _ = self.0.kill(); let _ = self.0.wait(); } } let mut lines = BufReader::new(window.stdout.take().unwrap()).lines(); let mut server_pid = None; loop { let line = lines.next().expect("the window said ready").unwrap(); if let Some(pid) = line.strip_prefix(CHILD_PID) { server_pid = pid.trim().parse::().ok(); } if line == READY { break; } } let mut window = Reap(window); assert!(server_lock_held(&run), "the server is running"); home.create(BOOK, "USD"); // The window is killed outright: no chance to close anything. window.0.kill().unwrap(); window.0.wait().unwrap(); until("the server let go of the books", || !server_lock_held(&run)); #[cfg(unix)] { let pid = server_pid.expect("the window gave the server's pid") as i32; until("the server's process is gone", || { // SAFETY: signal 0 only asks whether the process exists; the // pid is the server this test's own helper started. unsafe { libc::kill(pid, 0) != 0 } }); assert!( !run.join("admin.sock").exists(), "the books closed as at any close: the socket is removed" ); } let _ = server_pid; // The books are whole: a new server opens them without a word. let again = start(&home, &[BOOK.to_string()]); again.wait_ready(Duration::from_secs(60)).unwrap(); assert!(open_in_server(&home, BOOK)); assert_eq!(again.stop(Duration::from_secs(60)), Stopped::Closed); } /// A "server" that takes five seconds to close once its input ends. #[cfg(unix)] fn slow_closer(dir: &Path) -> std::path::PathBuf { use std::os::unix::fs::PermissionsExt; let path = dir.join("slow-server"); std::fs::write( &path, "#!/bin/sh\necho ready\ncat >/dev/null\nsleep 5\nexit 0\n", ) .unwrap(); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); path } #[cfg(unix)] #[test] fn a_stop_that_runs_out_of_time_leaves_the_server_closing_and_says_so() { let home = Home::new("app-slow"); let slow = slow_closer(home.dir.path()); let server = AppServer::start_in(&slow, Some(&home.root()), &[], |_| {}).unwrap(); server.wait_ready(Duration::from_secs(60)).unwrap(); let pid = server.pid() as i32; let started = Instant::now(); assert_eq!( server.stop(Duration::from_millis(300)), Stopped::StillClosing ); assert!( started.elapsed() < Duration::from_secs(4), "the window is not held for the server's whole close" ); // Left alone: still running, nothing signalled. assert_eq!(server.exited(), None); // SAFETY: signal 0 only asks whether the process exists; it is the // child this test started. assert_eq!(unsafe { libc::kill(pid, 0) }, 0, "it was not killed"); // It finishes by itself. assert_eq!(server.stop(Duration::from_secs(30)), Stopped::Closed); } /// An agent's bridge, spoken to as a client over stdio. struct Bridge { child: Child, stdin: std::process::ChildStdin, lines: mpsc::Receiver, next: u64, } impl Bridge { fn start(home: &Home) -> Self { let mut c = Command::new(exe()); c.args(["mcp", "--book", BOOK, "--root"]).arg(home.root()); clean_env(&mut c, home.dir.path()); let mut child = c .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::null()) .spawn() .unwrap(); let stdin = child.stdin.take().unwrap(); let stdout = child.stdout.take().unwrap(); let (tx, lines) = mpsc::channel(); std::thread::spawn(move || { for line in BufReader::new(stdout).lines().map_while(Result::ok) { if let Ok(v) = serde_json::from_str::(&line) { let _ = tx.send(v); } } }); Self { child, stdin, lines, next: 1, } } /// Send a request and return the answer to it. fn ask(&mut self, method: &str, params: Value) -> Value { let id = self.next; self.next += 1; let line = json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}); writeln!(self.stdin, "{line}").unwrap(); self.stdin.flush().unwrap(); let end = Instant::now() + Duration::from_secs(60); loop { let left = end.saturating_duration_since(Instant::now()); let said = self .lines .recv_timeout(left) .unwrap_or_else(|_| panic!("no answer to {method}")); if said["id"] == json!(id) { return said; } } } fn initialize(&mut self) -> Value { self.ask( "initialize", json!({"protocolVersion": common::PROTOCOL, "capabilities": {}, "clientInfo": {"name": "test", "version": "0"}}), ) } fn start_call(&mut self) -> Value { self.ask( "tools/call", json!({"name": "books.start", "arguments": {}}), ) } } impl Drop for Bridge { fn drop(&mut self) { let _ = self.child.kill(); let _ = self.child.wait(); } } fn error_of(answer: &Value) -> String { answer["error"]["message"] .as_str() .unwrap_or_else(|| panic!("an error was expected: {answer}")) .to_string() } #[test] fn the_bridge_says_the_app_isnt_running_then_works_once_it_is() { let home = Home::new("app-bridge"); let run = home.run(); // The app has served this root (it wrote its mark), and isn't running. std::fs::write(run.join(APP_MARKER), b"").unwrap(); let mut bridge = Bridge::start(&home); let not_running = format!("Wanderbooks isn't running: start Wanderbooks and open {BOOK}."); assert_eq!(error_of(&bridge.initialize()), not_running); assert_eq!(error_of(&bridge.start_call()), not_running); // No host was started for it. assert!(!host_lock_held(&run)); assert!(!run.join("lease.sock").exists()); // The app starts, with the company closed. let server = start(&home, &[]); server.wait_ready(Duration::from_secs(60)).unwrap(); home.create(BOOK, "USD"); let token = home.token_with(BOOK, "agent", &["read", "write", "post", "admin"]); ledger_host::tokens::keeper(&run) .set( &ledger_host::tokens::Account::new(&home.root(), BOOK, "agent"), &token, ) .unwrap(); assert_eq!(server.close_book(BOOK), Ok(None)); let not_open = format!("Company {BOOK} isn't open: open it in Wanderbooks."); assert_eq!(error_of(&bridge.start_call()), not_open); // Its tab opens: the same session goes through. assert_eq!(server.open_book(BOOK), Ok(None)); let answered = bridge.start_call(); assert!( answered.get("error").is_none() && answered.get("result").is_some(), "{answered}" ); assert!(!host_lock_held(&run), "still no host"); // The company's tab closes under the running session: said again. assert_eq!(server.close_book(BOOK), Ok(None)); assert_eq!(error_of(&bridge.start_call()), not_open); // The window closes: said again, in the same session. assert_eq!(server.stop(Duration::from_secs(60)), Stopped::Closed); assert_eq!(error_of(&bridge.start_call()), not_running); }