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>
This commit is contained in:
danny 2026-10-14 06:47:30 +02:00
parent 07adedadbd
commit a3f83102f1
9 changed files with 979 additions and 3 deletions

111
crates/control/tests/tcp.rs Normal file
View file

@ -0,0 +1,111 @@
//! End-to-end test of the control channel over a real TCP socket.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
use std::io::{BufRead, BufReader, Write};
use std::net::TcpStream;
use std::sync::Arc;
use lutra_control::protocol::MAX_LINE_BYTES;
use lutra_engine::Effect;
use lutra_testkit::EngineBackend;
use serde_json::{Value, json};
/// A connected client with line-based send and receive.
struct Client {
reader: BufReader<TcpStream>,
writer: TcpStream,
}
impl Client {
fn connect(backend: Arc<EngineBackend>) -> Self {
let server = lutra_control::start(0, backend).unwrap();
let stream = TcpStream::connect(server.local_addr()).unwrap();
Self {
writer: stream.try_clone().unwrap(),
reader: BufReader::new(stream),
}
}
fn send_raw(&mut self, bytes: &[u8]) {
self.writer.write_all(bytes).unwrap();
}
fn recv(&mut self) -> Value {
let mut line = String::new();
self.reader.read_line(&mut line).unwrap();
serde_json::from_str(&line).unwrap()
}
fn call(&mut self, request: Value) -> Value {
self.send_raw(format!("{request}\n").as_bytes());
self.recv()
}
}
#[test]
fn serves_requests_over_tcp() {
let backend = Arc::new(EngineBackend::new());
let mut client = Client::connect(Arc::clone(&backend));
let r = client.call(json!({"id": 1, "method": "app.ping"}));
assert_eq!(r["id"], 1);
assert_eq!(r["result"]["pong"], true);
let r = client.call(json!({"id": 2, "method": "engine.listCommands"}));
let ids: Vec<&str> = r["result"]
.as_array()
.unwrap()
.iter()
.map(|c| c["id"].as_str().unwrap())
.collect();
assert_eq!(ids, ["app.about", "app.quit"]);
let r = client.call(json!({
"id": "about",
"method": "engine.execute",
"params": {"command": "app.about", "params": {}}
}));
assert_eq!(r["result"]["slogan"], "An otterly perfect IDE");
let r =
client.call(json!({"id": 3, "method": "ui.screenshot", "params": {"path": "shot.png"}}));
assert_eq!(r["error"]["code"], "screenshot_failed");
client.send_raw(b"\r\n{broken\r\n");
assert_eq!(client.recv()["error"]["code"], "parse_error");
client.send_raw(&[0xff, 0xfe, b'\n']);
assert_eq!(client.recv()["error"]["code"], "parse_error");
let r = client
.call(json!({"id": 4, "method": "engine.execute", "params": {"command": "app.quit"}}));
assert_eq!(r["result"], Value::Null);
assert_eq!(*backend.effects.lock().unwrap(), vec![Effect::Quit]);
assert_eq!(backend.screenshots.lock().unwrap().len(), 1);
}
#[test]
fn rejects_oversized_lines_and_closes() {
let mut client = Client::connect(Arc::new(EngineBackend::new()));
let big = vec![b'x'; MAX_LINE_BYTES + 10];
let writer = client.writer.try_clone().unwrap();
let sender = std::thread::spawn(move || {
let mut writer = writer;
let _ = writer.write_all(&big);
let _ = writer.write_all(b"\n");
});
assert_eq!(client.recv()["error"]["code"], "line_too_long");
let mut rest = String::new();
let n = client.reader.read_line(&mut rest).unwrap_or(0);
assert_eq!(n, 0, "connection should be closed");
sender.join().unwrap();
}
#[test]
fn reports_a_taken_port() {
let backend = Arc::new(EngineBackend::new());
let first = lutra_control::start(0, backend.clone()).unwrap();
let second = lutra_control::start(first.local_addr().port(), backend);
assert!(second.is_err());
}