//! The language server wiring over a real TCP control connection: //! `lspStatus` and `diagnosticsChanged` arrive as events while a buffer is //! edited, and `diagnostics.list` / `lsp.status` answer from the engine. //! The server is M3.2's fake, which publishes each document's text as its //! one diagnostic. #![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::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use lutra_engine::Engine; use lutra_engine::lsp::{Launch, ServerOptions}; use lutra_testkit::EngineBackend; use serde_json::{Value, json}; /// A client that keeps responses and event lines apart. 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(30))) .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 30 s"); serde_json::from_str(&line).unwrap() } 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; } } } /// The next event (queued or read) for which `pick` holds. fn event(&mut self, pick: impl Fn(&Value) -> bool) -> Value { if let Some(i) = self.events.iter().position(&pick) { return self.events.remove(i); } loop { let line = self.line(); if let Some(event) = line.get("event") { if pick(event) { return event.clone(); } self.events.push(event.clone()); } } } } fn execute(command: &str, params: Value) -> Value { json!({"id": command, "method": "engine.execute", "params": {"command": command, "params": params}}) } /// Pumps the backend every 20 ms, like the desktop's pump thread, until /// dropped. struct Pump(Arc); impl Pump { fn start(backend: Arc) -> Self { let stop = Arc::new(AtomicBool::new(false)); let flag = stop.clone(); std::thread::spawn(move || { while !flag.load(Ordering::Relaxed) { backend.pump(); std::thread::sleep(Duration::from_millis(20)); } }); Self(stop) } } impl Drop for Pump { fn drop(&mut self) { self.0.store(true, Ordering::Relaxed); } } /// The fake server binary, built on demand when this test runs alone. fn fake_server() -> std::path::PathBuf { let exe = format!("lutra-lsp-fake{}", std::env::consts::EXE_SUFFIX); let deps = std::env::current_exe().unwrap(); let path = deps.parent().unwrap().parent().unwrap().join(exe); if !path.exists() { let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into()); let status = std::process::Command::new(cargo) .args(["build", "-p", "lutra-lsp", "--bin", "lutra-lsp-fake"]) .status() .unwrap(); assert!(status.success(), "building lutra-lsp-fake failed"); } path } #[test] fn diagnostics_over_tcp() { let dir = tempfile::tempdir().unwrap(); std::fs::write( dir.path().join("a.php"), "