//! The symbol index in the engine: `index.status`, the go-to commands, //! progress and readiness events, and incremental updates from disk and //! buffers. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing )] use std::fs; use std::path::Path; use std::time::{Duration, Instant}; use lutra_engine::{Engine, Event}; use proptest::prelude::*; use serde_json::{Value, json}; use tempfile::TempDir; fn write(root: &Path, rel: &str, text: &str) { let abs = root.join(rel); fs::create_dir_all(abs.parent().unwrap()).unwrap(); fs::write(abs, text).unwrap(); } /// A temp folder with `files`, opened in a fresh engine. fn open_project(files: &[(&str, &str)]) -> (TempDir, Engine) { let dir = tempfile::tempdir().unwrap(); for (path, text) in files { write(dir.path(), path, text); } let mut engine = Engine::new().unwrap(); engine .execute("project.open", json!({"path": dir.path()})) .unwrap(); (dir, engine) } fn run(engine: &mut Engine, command: &str, params: Value) -> Value { engine.execute(command, params).unwrap().value } /// Pumps until `indexReady`; returns every event seen. fn until_ready(engine: &mut Engine) -> Vec { let deadline = Instant::now() + Duration::from_secs(30); let mut seen = Vec::new(); while Instant::now() < deadline { let events = engine.pump_vfs().unwrap(); let ready = events.iter().any(|e| matches!(e, Event::IndexReady { .. })); seen.extend(events); if ready { return seen; } std::thread::sleep(Duration::from_millis(10)); } panic!("no indexReady within 30 s"); } /// Pumps until `query` with `command` gives `count` items. fn until_count(engine: &mut Engine, command: &str, query: &str, count: usize) -> Value { let deadline = Instant::now() + Duration::from_secs(10); loop { engine.pump_vfs().unwrap(); let out = run(engine, command, json!({"query": query})); if out["items"].as_array().unwrap().len() == count { return out; } assert!( Instant::now() < deadline, "{command} {query}: wanted {count} items, got {out}" ); std::thread::sleep(Duration::from_millis(20)); } } fn names(out: &Value, field: &str) -> Vec { out["items"] .as_array() .unwrap() .iter() .map(|i| i[field].as_str().unwrap().to_string()) .collect() } #[test] fn commands_need_a_project_and_status_is_idle_without_one() { let mut engine = Engine::new().unwrap(); assert_eq!( run(&mut engine, "index.status", Value::Null), json!({"state": "idle", "files": 0, "symbols": 0}) ); for command in ["goto.file", "goto.class", "goto.symbol"] { let err = engine.execute(command, json!({"query": "a"})).unwrap_err(); assert_eq!(err.code(), "disabled"); } } #[test] fn builds_in_the_background_and_answers_go_to_queries() { let (_dir, mut engine) = open_project(&[ (".gitignore", "/var/\n/vendor/\n"), ( "src/Models/User.php", " Some((*files, *symbols)), _ => None, }) .unwrap(); assert_eq!(ready, (5, 7)); assert_eq!( run(&mut engine, "index.status", Value::Null), json!({"state": "idle", "files": 5, "symbols": 7}) ); let files = run(&mut engine, "goto.file", json!({"query": "user"})); assert_eq!(files["indexing"], false); assert_eq!( names(&files, "path"), vec![ "src/Models/User.php", "templates/user.html.twig", "vendor/acme/lib/User.php", ] ); assert_eq!(files["items"][0]["matches"], json!([[11, 15]])); assert!(files["items"][0]["score"].as_u64().unwrap() > 0); let library: Vec<&Value> = files["items"] .as_array() .unwrap() .iter() .map(|item| &item["library"]) .collect(); assert_eq!(library, vec![&json!(false), &json!(false), &json!(true)]); let classes = run(&mut engine, "goto.class", json!({"query": "user"})); assert_eq!( classes["items"], json!([ {"name": "User", "fqn": "App\\Models\\User", "kind": "class", "path": "src/Models/User.php", "line": 3, "column": 12, "library": false}, {"name": "User", "fqn": "Acme\\User", "kind": "interface", "path": "vendor/acme/lib/User.php", "line": 3, "column": 10, "library": true}, ]) ); let symbols = run( &mut engine, "goto.symbol", json!({"query": "find", "limit": 1}), ); assert_eq!( symbols["items"], json!([{"name": "find", "fqn": "App\\Models\\User::find", "kind": "method", "container": "App\\Models\\User", "path": "src/Models/User.php", "line": 7, "column": 20, "library": false}]) ); let function = run(&mut engine, "goto.symbol", json!({"query": "format_user"})); assert_eq!(function["items"][0]["container"], Value::Null); let members = run(&mut engine, "goto.symbol", json!({"query": "User::"})); assert_eq!( names(&members, "name"), vec!["ROLE", "find", "$email", "find"] ); let empty = run(&mut engine, "goto.class", json!({"query": ""})); assert_eq!(empty["items"], json!([])); for bad in [ json!({}), json!({"query": 3}), json!({"query": "a", "limit": -1}), json!({"query": "a", "extra": true}), ] { let err = engine.execute("goto.file", bad).unwrap_err(); assert_eq!(err.code(), "invalid_params"); } } #[test] fn reports_progress_and_partial_results_while_indexing() { let dir = tempfile::tempdir().unwrap(); let project = lutra_testkit::generate_php_project(dir.path(), 3_000); let mut engine = Engine::new().unwrap(); engine .execute("project.open", json!({"path": dir.path()})) .unwrap(); let status = run(&mut engine, "index.status", Value::Null); assert_eq!(status["state"], "indexing"); let early = run(&mut engine, "goto.class", json!({"query": "Service"})); assert_eq!(early["indexing"], true); let events = until_ready(&mut engine); let progress: Vec<(usize, usize)> = events .iter() .filter_map(|e| match e { Event::IndexProgress { done, total } => Some((*done, *total)), _ => None, }) .collect(); assert!( progress .iter() .all(|(_, total)| *total == project.indexed_files) ); assert!(progress.windows(2).all(|w| w[0].0 <= w[1].0)); let status = run(&mut engine, "index.status", Value::Null); assert_eq!(status["state"], "idle"); assert_eq!(status["files"], project.indexed_files); let classes = run( &mut engine, "goto.class", json!({"query": "Service1", "limit": 1000}), ); assert_eq!(classes["indexing"], false); assert!(!classes["items"].as_array().unwrap().is_empty()); } #[test] fn follows_buffers_and_disk_changes() { let (dir, mut engine) = open_project(&[("src/A.php", "