//! UI commands over a real TCP socket: after the frontend registered them, //! `engine.listCommands` lists them with `ui: true` and running //! `palette.open` through `engine.execute` streams `uiCommand` to //! `events.subscribe` clients, the way the desktop window gets it and opens //! the palette. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing, clippy::panic )] use std::io::{BufRead, BufReader, Write}; use std::net::TcpStream; use std::sync::Arc; use std::time::Duration; use lutra_testkit::EngineBackend; use serde_json::{Value, json}; /// A client that collects event lines and returns responses. struct Client { reader: BufReader, writer: TcpStream, events: Vec, } impl Client { fn connect(addr: std::net::SocketAddr) -> Self { let stream = TcpStream::connect(addr).unwrap(); stream .set_read_timeout(Some(Duration::from_secs(5))) .unwrap(); Self { writer: stream.try_clone().unwrap(), reader: BufReader::new(stream), events: Vec::new(), } } fn line(&mut self) -> Value { let mut line = String::new(); self.reader.read_line(&mut line).expect("line within 5 s"); serde_json::from_str(&line).unwrap() } /// Sends a request and returns its response, collecting event lines that /// arrive before it. fn call(&mut self, request: Value) -> Value { self.writer .write_all(format!("{request}\n").as_bytes()) .unwrap(); loop { let line = self.line(); if line.get("event").is_some() { self.events.push(line["event"].clone()); } else { return line; } } } /// Waits for the next event of a kind (events arrive from a subscriber /// queue, possibly after the response) and drops the events before it. fn next(&mut self, kind: &str) -> Value { loop { if let Some(at) = self.events.iter().position(|e| e["kind"] == kind) { let theme = self.events.remove(at); self.events.drain(..at); return theme; } let line = self.line(); if line.get("event").is_some() { self.events.push(line["event"].clone()); } } } } fn execute(command: &str, params: Value) -> Value { json!({"id": 2, "method": "engine.execute", "params": {"command": command, "params": params}}) } #[test] fn palette_open_over_control_reaches_subscribers() { let backend = Arc::new(EngineBackend::new()); let server = lutra_control::start(0, backend.clone()).unwrap(); let mut client = Client::connect(server.local_addr()); client.call(json!({"id": 1, "method": "events.subscribe"})); let r = client.call(execute("palette.open", Value::Null)); assert_eq!(r["error"]["code"], "unknown_command", "{r}"); let r = client.call(execute( "ui.registerCommands", json!({"commands": [ {"id": "palette.open", "labelKey": "command.palette.open", "defaultShortcut": "Mod+Shift+P"}, ]}), )); assert_eq!(r["result"]["registered"], 1, "{r}"); let r = client.call(json!({"id": 3, "method": "engine.listCommands"})); let listed = r["result"] .as_array() .unwrap() .iter() .find(|c| c["id"] == "palette.open") .cloned() .unwrap(); assert_eq!(listed["ui"], true); assert_eq!(listed["defaultShortcut"], "Mod+Shift+P"); let r = client.call(execute("palette.open", json!({}))); assert_eq!(r["result"], Value::Null, "{r}"); let event = client.next("uiCommand"); assert_eq!( event, json!({"kind": "uiCommand", "id": "palette.open", "params": {}}) ); }