Lutra/apps/lutra-cli/tests/cli.rs
danny e1638af88a M3.7: robust CLI test deadlines
The --wait end-to-end test hit its 20 s wall-clock deadline under
parallel-build load. It now waits on the backend's bufferOpened event
and on the process exit through channels, with a generous 120 s limit
that a passing run never approaches. The single-instance control tests
use a 60 s timeout instead of 10 s. The other CLI tests already wait on
the process without a deadline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 20:57:16 +02:00

166 lines
5.3 KiB
Rust

//! The `lutra` binary end to end against a test backend that claims a
//! temporary rendezvous directory (`LUTRA_RENDEZVOUS_DIR`).
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
use std::path::Path;
use std::process::{Command, Output, Stdio};
use std::sync::Arc;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use lutra_control::rendezvous::{self, Instance};
use lutra_engine::Event;
use lutra_testkit::EngineBackend;
use serde_json::json;
/// A backend with a token listener and the UI command `editor.reveal`
/// registered, as a loaded window would have it.
fn instance(dir: &Path) -> Arc<EngineBackend> {
use lutra_control::Backend;
let backend = Arc::new(EngineBackend::new());
backend
.execute(
"ui.registerCommands",
json!({"commands": [{"id": "editor.reveal", "labelKey": "command.editor.reveal"}]}),
)
.unwrap();
let token = rendezvous::new_token().unwrap();
let server = lutra_control::start_with_token(0, backend.clone(), &token).unwrap();
rendezvous::write(
dir,
&Instance {
port: server.local_addr().port(),
token,
pid: std::process::id(),
version: "test".into(),
},
)
.unwrap();
backend
}
/// How long the tests wait for the `lutra` process. Generous, because a
/// cold process start under parallel builds can take tens of seconds; a
/// passing run returns as soon as the awaited event arrives.
const LOAD_DEADLINE: Duration = Duration::from_secs(120);
fn lutra(dir: &Path, args: &[&str]) -> Command {
let mut command = Command::new(env!("CARGO_BIN_EXE_lutra"));
command
.args(args)
.env(rendezvous::DIR_ENV, dir)
.env("LUTRA_DESKTOP", dir.join("no-such-desktop"))
.stdout(Stdio::piped())
.stderr(Stdio::piped());
command
}
fn run(dir: &Path, args: &[&str]) -> Output {
lutra(dir, args).output().unwrap()
}
fn buffer_ids(backend: &EngineBackend) -> Vec<u64> {
backend.engine.lock().unwrap().state().buffers.ids()
}
#[test]
fn version_help_and_usage_errors() {
let dir = tempfile::tempdir().unwrap();
let out = run(dir.path(), &["--version"]);
assert_eq!(out.status.code(), Some(0));
assert!(String::from_utf8_lossy(&out.stdout).starts_with("lutra "));
let out = run(dir.path(), &["--help"]);
assert_eq!(out.status.code(), Some(0));
assert!(String::from_utf8_lossy(&out.stdout).contains("--wait"));
assert_eq!(run(dir.path(), &["--bogus"]).status.code(), Some(2));
assert_eq!(run(dir.path(), &["a.php:0"]).status.code(), Some(2));
assert_eq!(run(dir.path(), &[""]).status.code(), Some(2));
}
#[test]
fn errors_exit_with_one() {
let dir = tempfile::tempdir().unwrap();
let missing = dir.path().join("missing.php");
let out = run(dir.path(), &[missing.to_str().unwrap()]);
assert_eq!(out.status.code(), Some(1));
let out = run(dir.path(), &[]);
assert_eq!(
out.status.code(),
Some(1),
"no instance and no desktop to start"
);
assert!(String::from_utf8_lossy(&out.stderr).contains("no-such-desktop"));
}
#[test]
fn opens_a_file_at_a_line_in_the_running_instance() {
let rv = tempfile::tempdir().unwrap();
let project = tempfile::tempdir().unwrap();
let file = project.path().join("a.php");
std::fs::write(&file, "<?php\n".repeat(20)).unwrap();
let backend = instance(rv.path());
let (sink, events) = std::sync::mpsc::sync_channel(64);
lutra_control::Backend::subscribe(backend.as_ref(), sink);
let arg = format!("{}:10:3", file.display());
let out = run(rv.path(), &[&arg]);
assert_eq!(
out.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(buffer_ids(&backend).len(), 1);
let reveal = events
.try_iter()
.find(|e| matches!(e, Event::UiCommand { .. }))
.unwrap();
assert_eq!(
reveal,
Event::UiCommand {
id: "editor.reveal".into(),
params: json!({"path": "a.php", "line": 9, "column": 2}),
}
);
}
#[test]
fn wait_returns_after_the_tab_is_closed() {
use lutra_control::Backend;
let rv = tempfile::tempdir().unwrap();
let project = tempfile::tempdir().unwrap();
let file = project.path().join("COMMIT_EDITMSG");
std::fs::write(
&file, "msg
",
)
.unwrap();
let backend = instance(rv.path());
let (sink, events) = mpsc::sync_channel(64);
backend.subscribe(sink);
let mut child = lutra(rv.path(), &["--wait", file.to_str().unwrap()])
.spawn()
.unwrap();
let id = loop {
match events
.recv_timeout(LOAD_DEADLINE)
.expect("the file was never opened")
{
Event::BufferOpened { id, .. } => break id,
_ => continue,
}
};
thread::sleep(Duration::from_millis(300));
assert!(child.try_wait().unwrap().is_none(), "--wait returned early");
let (done, exited) = mpsc::channel();
thread::spawn(move || done.send(child.wait().unwrap()).unwrap());
backend.execute("buffer.close", json!({"id": id})).unwrap();
let status = exited
.recv_timeout(LOAD_DEADLINE)
.expect("--wait did not return");
assert_eq!(status.code(), Some(0));
}