State holds the open project (VFS project, watcher, watched directories)
and versioned buffers. New commands: project.open/close/children,
file.open/save/saveAll, buffer.get/edit/close. buffer.edit takes UTF-16
change sets based on a version and fails with the new stale_version code;
closing dirty buffers or projects without force fails with
unsaved_changes. Commands queue events (projectOpened, treeChanged,
bufferChanged, dirtyChanged, fileChangedOnDisk, ...) that the engine
returns in Outcome.events; Engine::pump_vfs maps changes on disk to
events and reloads clean buffers. project.children watches listed
directories, capped at 256 native / 32 polling, least recently listed
dropped first.
Control channel: events.subscribe streams {"event": ...} lines, with
bounded per-subscriber queues (lutra_control::Subscribers) and method docs
in methods.rs. testkit's EngineBackend and the desktop Host implement the
new Backend::subscribe. Verified with 14 engine integration tests, a TCP
events test and the running app over the control channel.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
2.4 KiB
Rust
81 lines
2.4 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, 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<Engine>,
|
|
/// Effects returned by executed commands, in order.
|
|
pub effects: Mutex<Vec<Effect>>,
|
|
/// Paths passed to [`Backend::screenshot`].
|
|
pub screenshots: Mutex<Vec<PathBuf>>,
|
|
/// 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<Value, EngineError> {
|
|
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<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())
|
|
}
|
|
|
|
fn subscribe(&self, sink: EventSink) {
|
|
self.subscribers.add(sink);
|
|
}
|
|
}
|