//! 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, EventSink, Subscribers}; 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; events are published to /// subscribers like the desktop host does; screenshots record the path and /// fail, because there is no window. pub struct EngineBackend { /// The engine, behind a lock like in the desktop app. pub engine: Mutex, /// Effects returned by executed commands, in order. pub effects: Mutex>, /// Paths passed to [`Backend::screenshot`]. pub screenshots: Mutex>, /// Event subscribers. pub subscribers: Subscribers, } 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()), subscribers: Subscribers::default(), } } /// Applies pending changes on disk and publishes the resulting events, /// as the desktop host's pump thread does. pub fn pump(&self) { let events = self.engine.lock().unwrap().pump_vfs().unwrap(); self.subscribers.publish(&events); } } impl Default for EngineBackend { fn default() -> Self { Self::new() } } impl Backend for EngineBackend { fn execute(&self, command: &str, params: Value) -> Result { let out = self.engine.lock().unwrap().execute(command, params)?; self.effects.lock().unwrap().extend(out.effects); self.subscribers.publish(&out.events); Ok(out.value) } fn list_commands(&self) -> Vec { 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()) } fn subscribe(&self, sink: EventSink) { self.subscribers.add(sink); } }