//! Phpactor UX (H3): `window/showMessage` as notifications, //! `$/progress` as `lspProgress`, the fast tick while the server indexes, //! and Lutra's own `php -l` on save. //! //! The fake server (`lutra-lsp-fake --script`) sends the messages; the //! `php -l` tests need PHP on `PATH` (or `LUTRA_PHP`) and skip with a //! message without it. The ignored `real_phpactor_first_completion` //! measures the time to the first useful completion on a generated //! project. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing )] use std::ffi::OsString; use std::fs; use std::path::PathBuf; use std::time::{Duration, Instant}; use lutra_engine::lsp::Launch; use lutra_engine::{Engine, EngineError}; use lutra_lsp::ServerOptions; use serde_json::{Value, json}; use tempfile::TempDir; /// The fake server binary, built on demand when this test runs without /// the rest of the workspace (`cargo test -p lutra-engine`). fn fake_server() -> 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 } /// A temp project with `files`, opened in an engine; with `script`, the /// fake server runs with it (see the fake server's docs), else none. fn project(files: &[(&str, &str)], script: Option) -> (TempDir, TempDir, Engine) { let dir = tempfile::tempdir().unwrap(); let scripts = tempfile::tempdir().unwrap(); for (name, text) in files { fs::write(dir.path().join(name), text).unwrap(); } let mut engine = Engine::new().unwrap(); engine.set_php_lint(None); if let Some(script) = script { let file = scripts.path().join("script.json"); fs::write(&file, script.to_string()).unwrap(); engine.set_language_server(Some(Launch { command: fake_server().into(), args: vec![OsString::from("--script"), file.into_os_string()], options: ServerOptions::default(), tick: None, })); } engine .execute("project.open", json!({"path": dir.path()})) .unwrap(); (dir, scripts, engine) } /// Pumps until an event satisfies `done` (at most 10 s); returns every /// event seen. fn pump_until(engine: &mut Engine, done: impl Fn(&Value) -> bool) -> Vec { let started = Instant::now(); let mut seen = Vec::new(); loop { for event in pump(engine) { let hit = done(&event); seen.push(event); if hit { return seen; } } assert!( started.elapsed() < Duration::from_secs(10), "timed out; events: {seen:#?}" ); std::thread::sleep(Duration::from_millis(5)); } } /// The version of buffer `id`. fn version(engine: &mut Engine, id: u64) -> Value { engine .execute("buffer.get", json!({"id": id})) .unwrap() .value["version"] .clone() } /// A `$/progress` notification. fn progress(token: &str, value: Value) -> Value { json!({"method": "$/progress", "params": {"token": token, "value": value}}) } #[test] fn messages_become_notifications_and_progress_reaches_the_status() { let show = |kind: u8, message: &str| json!({"method": "window/showMessage", "params": {"type": kind, "message": message}}); let script = json!({"notifications": [ show(2, "Index is stale"), show(2, "Diagnostic provider \"php\" errored with \"CreateProcess failed\", removing from pool"), show(2, "Index is stale"), show(1, "File watcher died"), {"method": "window/logMessage", "params": {"type": 4, "message": "quiet"}}, progress("idx", json!({"kind": "begin", "title": "Indexing workspace", "message": "3 PHP files", "percentage": 0})), progress("idx", json!({"kind": "report", "message": "2/3", "percentage": 67})), progress("other", json!({"kind": "report", "percentage": 5})), progress("idx", json!({"kind": "end", "message": "done"})), progress("refs", json!({"kind": "begin", "title": "Finding references", "percentage": 250})), show(3, "last") ]}); let (_dir, _scripts, mut engine) = project(&[("a.php", " = events .iter() .filter(|e| e["messageKey"] == "notification.lsp.message") .map(|e| { ( e["level"].as_str().unwrap(), e["params"]["message"].as_str().unwrap(), ) }) .collect(); assert_eq!( notes, [ ("warning", "Index is stale"), ("error", "File watcher died"), ("info", "last") ] ); let progress: Vec<&Value> = events .iter() .filter(|e| e["kind"] == "lspProgress") .collect(); assert_eq!( progress, [ &json!({"kind": "lspProgress", "active": true, "title": "Indexing workspace", "message": "3 PHP files", "percentage": 0}), &json!({"kind": "lspProgress", "active": true, "title": "Indexing workspace", "message": "2/3", "percentage": 67}), &json!({"kind": "lspProgress", "active": false}), &json!({"kind": "lspProgress", "active": true, "title": "Finding references", "percentage": 100}), ] ); let status = engine.execute("lsp.status", json!({})).unwrap().value; assert_eq!(status["progress"]["title"], "Finding references"); let log = engine.execute("lsp.log", json!({})).unwrap().value; let lines: Vec<&str> = log["lines"] .as_array() .unwrap() .iter() .map(|l| l.as_str().unwrap()) .collect(); assert!(lines.contains(&"[info] hello"), "{lines:?}"); assert!(lines.contains(&"[log] quiet"), "{lines:?}"); assert!( lines.iter().any(|l| l.contains("CreateProcess failed")), "{lines:?}" ); let after = events_of(&mut engine, "lsp.restart"); assert!( after.contains(&json!({"kind": "lspProgress", "active": false})), "{after:?}" ); } /// PHP for the `php -l` tests: `LUTRA_PHP` or `php` on `PATH`; `None` /// (the test skips) when it does not run. fn php() -> Option { let php = PathBuf::from(std::env::var("LUTRA_PHP").unwrap_or_else(|_| "php".into())); let runs = std::process::Command::new(&php) .arg("-v") .output() .is_ok_and(|o| o.status.success()); if !runs { eprintln!("skipped: no PHP (set LUTRA_PHP or put php on PATH)"); } runs.then_some(php) } /// The `php -l` items of a `diagnosticsChanged` event. fn lint_items(event: &Value) -> Vec<&Value> { event["items"] .as_array() .map(|items| items.iter().filter(|i| i["source"] == "php -l").collect()) .unwrap_or_default() } #[test] fn php_lint_reports_syntax_errors_on_open_and_save() { let Some(php) = php() else { return; }; let broken = " u64 { engine .execute("file.open", json!({"path": path})) .unwrap() .value["id"] .as_u64() .unwrap() } /// Runs `command` and waits for its pending result, as a host does. fn ask(engine: &mut Engine, command: &str, params: Value) -> Result { let mut outcome = engine.execute(command, params)?; match outcome.pending.take() { Some(pending) => Ok(engine.resolve(pending.wait())?.value), None => Ok(outcome.value), } } /// Runs `command` and returns the events it queued, as JSON. fn events_of(engine: &mut Engine, command: &str) -> Vec { engine .execute(command, json!({})) .unwrap() .events .iter() .map(|e| serde_json::to_value(e).unwrap()) .collect() } /// Pumps once and returns the events as JSON. fn pump(engine: &mut Engine) -> Vec { engine .pump_vfs() .unwrap() .iter() .map(|e| serde_json::to_value(e).unwrap()) .collect() } /// A shop-like project: `classes` classes in `src/` plus the file the /// test completes in. fn shop(classes: usize) -> Vec<(String, String)> { let mut files = Vec::new(); for i in 0..classes { files.push(( format!("src/Product{i}.php"), format!( "name;\n }}\n\n /** The price in cents, times `$count`. */\n public function price(int $count = 1, bool $gross = true): int\n {{\n return $this->price * $count;\n }}\n}}\n" ), )); } files.push(( "src/Cart.php".into(), "\n }\n}\n".into(), )); files } /// Time from opening a generated project to the first completion list /// that has the method of a class Phpactor must have indexed /// (`Product1999`, or the last class of a smaller project), with the /// `lspProgress` events on the way: /// /// `LUTRA_PHPACTOR= [LUTRA_PHP=php] [LUTRA_SHOP_CLASSES=2000] cargo test -p lutra-engine --test lsp_ux real_phpactor_first_completion -- --ignored --nocapture` #[test] #[ignore = "needs PHP and Phpactor (LUTRA_PHPACTOR)"] fn real_phpactor_first_completion() { let phar = std::env::var("LUTRA_PHPACTOR").expect("LUTRA_PHPACTOR"); let php = std::env::var("LUTRA_PHP").unwrap_or_else(|_| "php".into()); let classes: usize = std::env::var("LUTRA_SHOP_CLASSES") .ok() .and_then(|n| n.parse().ok()) .unwrap_or(2000); let dir = tempfile::tempdir().unwrap(); for (name, text) in shop(classes) { let text = text.replace("Product1999", &format!("Product{}", classes - 1)); let path = dir.path().join(name); fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path, text).unwrap(); } let mut engine = Engine::new().unwrap(); engine.set_language_server(Some(lutra_engine::lsp::phpactor_launch( &php, &phar, cfg!(windows), ))); engine .execute("project.open", json!({"path": dir.path()})) .unwrap(); let started = Instant::now(); let id = open(&mut engine, "src/Cart.php"); let at_arrow = json!({"id": id, "position": {"line": 8, "column": 18}, "triggerCharacter": ">"}); let mut ready = None; let mut attempts = 0; loop { for event in pump(&mut engine) { match event["kind"].as_str() { Some("lspStatus") if event["state"] == "ready" && ready.is_none() => { ready = Some(started.elapsed()); println!("ready after {:?}", started.elapsed()); } Some("lspProgress") => println!("{:?} {event}", started.elapsed()), Some("notification") => println!("{:?} {event}", started.elapsed()), _ => {} } } if ready.is_some() { attempts += 1; let out = ask(&mut engine, "lsp.completion", at_arrow.clone()).unwrap(); let found = out["items"] .as_array() .unwrap() .iter() .any(|i| i["label"] == "price"); if found { break; } } assert!( started.elapsed() < Duration::from_secs(180), "no useful completion within 180 s" ); std::thread::sleep(Duration::from_millis(250)); } println!( "FIRST USEFUL COMPLETION {:?} after project.open ({classes} classes, {attempts} attempts)", started.elapsed() ); std::thread::sleep(Duration::from_secs(2)); for event in pump(&mut engine) { if event["kind"] == "notification" { println!("{event}"); } } let log = engine.execute("lsp.log", json!({})).unwrap().value; for line in log["lines"].as_array().unwrap() { if line.as_str().unwrap().starts_with("[show") { println!("log: {line}"); } } let requests: usize = std::env::var("LUTRA_UX_REQUESTS") .ok() .and_then(|n| n.parse().ok()) .unwrap_or(0); if requests > 0 { std::thread::sleep(Duration::from_secs(5)); let epoch = || { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_millis() }; let cases = [ ("member", " $product->pri", None::<&str>), ("class", " $x = new Produc", None), ]; for (name, line, trigger) in cases { let source = fs::read_to_string(dir.path().join("src/Cart.php")).unwrap(); let text: String = source .split('\n') .enumerate() .map(|(i, l)| if i == 8 { line } else { l }) .collect::>() .join("\n"); let current = engine .execute("buffer.get", json!({"id": id})) .unwrap() .value; engine .execute( "buffer.edit", json!({"id": id, "baseVersion": current["version"], "edits": [{"from": 0, "to": current["text"].as_str().unwrap().encode_utf16().count(), "insert": text}]}), ) .unwrap(); let at = json!({"id": id, "position": {"line": 8, "column": line.len()}, "triggerCharacter": trigger}); let _ = ask(&mut engine, "lsp.completion", at.clone()); println!("PHASE {name} start {}", epoch()); let mut ms = Vec::new(); let mut last = Value::Null; for _ in 0..requests { let t = Instant::now(); last = ask(&mut engine, "lsp.completion", at.clone()).unwrap(); ms.push(t.elapsed().as_secs_f64() * 1000.0); } println!("PHASE {name} end {}", epoch()); ms.sort_by(f64::total_cmp); println!( "{name}: {requests} requests, median {:.0} ms, max {:.0} ms, items {}, incomplete {}", ms[ms.len() / 2], ms[ms.len() - 1], last["items"].as_array().unwrap().len(), last["incomplete"] ); } } let idle: u64 = std::env::var("LUTRA_UX_IDLE_SECS") .ok() .and_then(|n| n.parse().ok()) .unwrap_or(0); if idle > 0 { let epoch = || { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_millis() }; println!("PHASE idle start {} at {:?}", epoch(), started.elapsed()); std::thread::sleep(Duration::from_secs(idle)); println!("PHASE idle end {} at {:?}", epoch(), started.elapsed()); } }