//! `search.find` and `search.cancel`: results arrive through the pump. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing )] use std::fs; use std::time::{Duration, Instant}; use lutra_engine::cmd::search::MAX_RESULTS_PER_PUMP; use lutra_engine::{Engine, Event}; use serde_json::{Value, json}; use tempfile::TempDir; /// 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 { let abs = dir.path().join(path); fs::create_dir_all(abs.parent().unwrap()).unwrap(); fs::write(abs, text).unwrap(); } let mut engine = Engine::new().unwrap(); engine .execute("project.open", json!({"path": dir.path()})) .unwrap(); (dir, engine) } fn find(engine: &mut Engine, params: Value) -> u64 { let out = engine.execute("search.find", params).unwrap(); assert!(out.events.is_empty(), "results only come from the pump"); out.value["searchId"].as_u64().unwrap() } /// Pumps until `searchDone` for `id`; returns the results (sorted by path) /// and the done event, and the number of pumps. fn collect(engine: &mut Engine, id: u64) -> (Vec, Event, usize) { let deadline = Instant::now() + Duration::from_secs(10); let mut results = Vec::new(); let mut pumps = 0; while Instant::now() < deadline { pumps += 1; for event in engine.pump_vfs().unwrap() { match &event { Event::SearchResults { search_id, .. } if *search_id == id => results.push(event), Event::SearchDone { search_id, .. } if *search_id == id => { results.sort_by_key(path); return (results, event, pumps); } _ => {} } } std::thread::sleep(Duration::from_millis(10)); } panic!("no searchDone within 10 s"); } fn path(event: &Event) -> String { match event { Event::SearchResults { path, .. } => path.clone(), _ => String::new(), } } #[test] fn finds_in_files_and_dirty_buffers() { let (_dir, mut engine) = open_project(&[ ("src/a.php", " = results.iter().map(path).collect(); assert_eq!(paths, ["src/a.php", "src/b.php", "vendor/x/c.php"]); let Event::SearchResults { matches, .. } = &results[1] else { panic!() }; assert_eq!( (matches[0].line, matches[0].column, matches[0].length), (1, 1, 6) ); assert_eq!(matches[0].preview, "$needle = 1;"); let Event::SearchDone { files, matches, truncated, cancelled, .. } = done else { panic!() }; assert_eq!((files, matches, truncated, cancelled), (3, 3, false, false)); let id2 = find( &mut engine, json!({"query": "needle", "caseSensitive": true, "include": ["src/*.php"]}), ); assert!(id2 > id); let (results, _, _) = collect(&mut engine, id2); assert_eq!(results.len(), 2); } #[test] fn invalid_queries_fail_with_invalid_params() { let (_dir, mut engine) = open_project(&[("a.php", "x")]); for params in [ json!({"query": "(", "regex": true}), json!({"query": ""}), json!({"query": "x", "include": ["["]}), json!({"query": "x", "bogus": 1}), json!({}), ] { let err = engine.execute("search.find", params.clone()).unwrap_err(); assert_eq!(err.code(), "invalid_params", "{params}"); } assert_eq!(engine.state().searches.running(), 0); engine.execute("project.close", json!({})).unwrap(); let err = engine .execute("search.find", json!({"query": "x"})) .unwrap_err(); assert_eq!(err.code(), "disabled"); } #[test] fn cancel_and_project_close_end_searches() { let dir = tempfile::tempdir().unwrap(); lutra_testkit::generate_search_tree(dir.path(), 2_000); let mut engine = Engine::new().unwrap(); engine .execute("project.open", json!({"path": dir.path()})) .unwrap(); let id = find(&mut engine, json!({"query": "function"})); let out = engine .execute("search.cancel", json!({"searchId": id})) .unwrap(); assert_eq!(out.value["cancelled"], true); let (_, done, _) = collect(&mut engine, id); let Event::SearchDone { cancelled, matches, .. } = done else { panic!() }; assert!( cancelled || matches == 10_000, "cancelled unless it had already finished" ); let out = engine .execute("search.cancel", json!({"searchId": id})) .unwrap(); assert_eq!(out.value["cancelled"], false); let id = find(&mut engine, json!({"query": "function"})); engine.execute("project.close", json!({})).unwrap(); let (results, done, _) = collect(&mut engine, id); assert!(results.is_empty()); assert!(matches!( done, Event::SearchDone { cancelled: true, .. } )); assert_eq!(engine.state().searches.running(), 0); } #[test] fn pumps_spread_many_results() { let dir = tempfile::tempdir().unwrap(); for i in 0..(MAX_RESULTS_PER_PUMP + 100) { fs::write(dir.path().join(format!("f{i}.txt")), "hit\n").unwrap(); } let mut engine = Engine::new().unwrap(); engine .execute("project.open", json!({"path": dir.path()})) .unwrap(); let id = find(&mut engine, json!({"query": "hit"})); std::thread::sleep(Duration::from_millis(500)); let first = engine.pump_vfs().unwrap(); let batch = first .iter() .filter(|e| matches!(e, Event::SearchResults { .. })) .count(); assert!(batch <= MAX_RESULTS_PER_PUMP, "{batch}"); let (rest, done, _) = collect(&mut engine, id); assert_eq!(batch + rest.len(), MAX_RESULTS_PER_PUMP + 100); assert!(matches!(done, Event::SearchDone { files, .. } if files == MAX_RESULTS_PER_PUMP + 100)); }