Lutra/crates/testkit/src/lib.rs
danny a3f83102f1 M0.7: control channel over JSON lines
lutra_control::start binds 127.0.0.1:PORT and serves app.ping,
engine.execute, engine.listCommands and ui.screenshot through the Backend
trait the host implements. Lines are capped at 16 MiB, every input gets a
well-formed response with a stable error code, and backend panics become
internal errors. Proptests cover arbitrary lines; a TCP integration test
uses the new testkit EngineBackend.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 06:47:30 +02:00

65 lines
1.9 KiB
Rust

//! Test helpers and fixtures for Lutra crates.
//!
//! Used only as a dev-dependency; never part of a shipped binary. Test code
//! may unwrap and panic, so the no-panic lints are allowed here.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use lutra_control::Backend;
use lutra_engine::{CommandInfo, Effect, Engine, EngineError};
use serde_json::Value;
/// A control [`Backend`] over a bare engine, without a window.
///
/// Effects are recorded instead of applied; screenshots write a tiny marker
/// file so that tests can check the path handling.
pub struct EngineBackend {
/// The engine, behind a lock like in the desktop app.
pub engine: Mutex<Engine>,
/// Effects returned by executed commands, in order.
pub effects: Mutex<Vec<Effect>>,
/// Paths passed to [`Backend::screenshot`].
pub screenshots: Mutex<Vec<PathBuf>>,
}
impl EngineBackend {
/// A backend with the built-in commands.
pub fn new() -> Self {
Self {
engine: Mutex::new(Engine::new().unwrap()),
effects: Mutex::new(Vec::new()),
screenshots: Mutex::new(Vec::new()),
}
}
}
impl Default for EngineBackend {
fn default() -> Self {
Self::new()
}
}
impl Backend for EngineBackend {
fn execute(&self, command: &str, params: Value) -> Result<Value, EngineError> {
let out = self.engine.lock().unwrap().execute(command, params)?;
self.effects.lock().unwrap().extend(out.effects);
Ok(out.value)
}
fn list_commands(&self) -> Vec<CommandInfo> {
self.engine.lock().unwrap().list_commands()
}
fn screenshot(&self, path: &Path) -> Result<(), String> {
self.screenshots.lock().unwrap().push(path.to_path_buf());
Err("no window in tests".into())
}
}