//! The indexing bar: when it goes up, what it counts, when it comes down. use super::*; #[test] fn the_startup_index_build_runs_under_the_first_bar() { // It is the slowest rescan of the session, and it happens after // `initialize_finish` — the earliest moment a server request is legal. let tree = Tree::new("progressstartup"); tree.package("rtl/a.sv", "a"); let session = Session::wide_with(vec![tree.path.clone()], watching_progress()); // One `report` because the tree holds one file: the counting reports // this round added ride the same path the begin/end pair does. assert_eq!( session.progress(), vec![ ("create", "lucisv-index-0".to_string()), ("begin", "lucisv-index-0".to_string()), ("report", "lucisv-index-0".to_string()), ("end", "lucisv-index-0".to_string()), ] ); } #[test] fn a_whole_rescan_puts_a_bar_up_and_takes_it_down_again() { // Superseded this round (incremental-index, 2026-08-27): // `a_save_puts_a_bar_up_and_takes_it_down_again` pinned the bar on a // save, and a save of a file the index already holds is now one parse // on the incremental path — see // `a_one_file_save_costs_no_bar_and_no_log_line` below. The bar's own // rule is unchanged and is pinned here on a trigger that really does // rescan: two files changing at once. let tree = Tree::new("progresssave"); tree.package("rtl/a.sv", "a"); tree.package("rtl/b.sv", "b"); let mut session = Session::wide_with(vec![tree.path.clone()], watching_progress()); session.open(&tree.path.join("rtl/a.sv"), "package a;\nendpackage\n"); let _ = session.drain(); session.watched(&[tree.path.join("rtl/a.sv"), tree.path.join("rtl/b.sv")]); // One create, one begin, one report per file, one end — the rescan's // own token, not the startup one. assert_eq!( session.progress(), vec![ ("create", "lucisv-index-1".to_string()), ("begin", "lucisv-index-1".to_string()), ("report", "lucisv-index-1".to_string()), ("report", "lucisv-index-1".to_string()), ("end", "lucisv-index-1".to_string()), ] ); } #[test] fn the_fast_path_is_taken_for_one_held_file_and_for_nothing_else() { // The trigger table, all four rows in one place. The bar is the tell: // a whole rescan puts one up, the incremental path never does. let tree = Tree::new("fastpathtable"); tree.package("rtl/a.sv", "a"); tree.package("rtl/b.sv", "b"); let mut session = Session::wide_with(vec![tree.path.clone()], watching_progress()); let uri = session.open(&tree.path.join("rtl/a.sv"), "package a;\n localparam K = 1;\nendpackage\n"); let _ = session.drain(); // 1. A save naming one file the index holds — incremental. session .notify(DidSaveTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": uri } })); assert!(session.progress().is_empty(), "one held file on a save is incremental"); // 2. One watched file — incremental too, same rule. session.watched(&[tree.path.join("rtl/a.sv")]); assert!(session.progress().is_empty(), "and one watched file is the same case"); // 3. Two files in one watcher event — the whole rescan. session.watched(&[tree.path.join("rtl/a.sv"), tree.path.join("rtl/b.sv")]); assert!(!session.progress().is_empty(), "two files is a rescan"); // 4. A settings change that moves which files exist — the whole // rescan, whatever it names. session.notify( DidChangeConfiguration::METHOD, serde_json::json!({ "settings": { "lucisv": { "exclude": ["rtl/b.sv"], "index": { "scope": "workspace" } } } }), ); assert!(!session.progress().is_empty(), "a settings change is a rescan"); // 5. A save naming a file the index does not hold — the whole rescan, // which is how a new file gets in at all. let fresh = tree.path.join("rtl/c.sv"); tree.package("rtl/c.sv", "c"); let fresh_uri = crate::uri::path_to_uri(&fresh).expect("uri"); session.notify( DidSaveTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": fresh_uri } }), ); assert!(!session.progress().is_empty(), "a file the index does not hold is a rescan"); assert_eq!(indexed(&session, "c"), 1, "and the new file is in the index after it"); } /// QA (incremental-index, 2026-08-27): the fast-path discipline under a /// burst. `process_batch` handles one message at a time /// (`for message in batch`), so two `didSave`s that arrive in the same /// burst are not coalesced into one rescan — each is its own notification, /// each independently names one held file, and each takes the incremental /// path on its own. Not a rebuild, and not a dropped update either. #[test] fn two_saves_of_different_held_files_in_one_batch_are_two_incremental_updates() { let tree = Tree::new("batchtwosaves"); tree.write("a.sv", "module a;\nendmodule\n"); tree.write("sub.sv", "module sub;\nendmodule\n"); let mut session = Session::wide_with(vec![tree.path.clone()], watching_progress()); let uri_a = session.open(&tree.path.join("a.sv"), "module a;\nendmodule\n"); let uri_sub = session.open(&tree.path.join("sub.sv"), "module sub;\nendmodule\n"); let _ = session.drain(); let before = session.server.generation; let a_with_sub = "module a;\n sub u1 ();\nendmodule\n"; let sub_v2 = "module sub;\n logic marker;\nendmodule\n"; let save = |uri: &Uri, text: &str| { Message::Notification(Notification::new( DidSaveTextDocument::METHOD.to_string(), serde_json::json!({ "textDocument": { "uri": uri }, "text": text }), )) }; session.batch(vec![save(&uri_a, a_with_sub), save(&uri_sub, sub_v2)]); assert!(session.progress().is_empty(), "neither save alone is a whole rescan"); // Each save bumps the generation twice — once in `set_document` for // the buffer's own new text (every text change does that, save or // not), once in `reindex_one` for the fast-path update — so two saves // is four, not two and not one shared bump for the whole batch. assert_eq!( session.server.generation, before + 4, "two independent fast-path updates, not one bump shared by the batch" ); assert_eq!( session.server.workspace.index().instantiations_of("sub").len(), 1, "a.sv's edit reached the index" ); assert_eq!( session.server.workspace.candidates().words("marker").len(), 1, "sub.sv's own edit reached the index too, not just a.sv's" ); } #[test] fn a_one_file_save_still_bumps_the_generation_and_refreshes_the_lenses() { // Everything the fast path keeps: a pulling client's cached // diagnostics are stale from here, and every lens a client is holding // may now read a different count. let tree = lens_tree("fastpathafter"); let abilities = ClientAbilities { code_lens_refresh: true, ..Default::default() }; let mut session = Session::with(vec![tree.path.clone()], abilities); let uri = session .open(&tree.path.join("top.sv"), "module top;\n sub u_a ();\n sub u_b ();\nendmodule\n"); let _ = session.drain(); let before = session.server.generation; let one_site = "module top;\n sub u_a ();\nendmodule\n"; std::fs::write(tree.path.join("top.sv"), one_site).expect("write"); session.notify( DidSaveTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": uri }, "text": one_site }), ); assert!(session.server.generation > before, "the generation moved"); let refreshes = session.sent().iter().filter(|request| request.method == CodeLensRefresh::METHOD).count(); assert_eq!(refreshes, 1, "one refresh, as a whole rescan sends"); } #[test] fn a_one_file_save_answers_from_the_file_it_saved() { // The point of the whole path: one file's edit really does reach the // index, and the rest of the workspace really is left alone. let tree = lens_tree("fastpathanswer"); let mut session = Session::new(vec![tree.path.clone()], false); let two_sites = "module top;\n sub u_a ();\n sub u_b ();\nendmodule\n"; let uri = session.open(&tree.path.join("top.sv"), two_sites); let _ = session.drain(); assert_eq!( session.server.workspace.index().instantiations_of("sub").len(), 2, "premise: top.sv instantiates `sub` twice" ); let emptied = "module top;\nendmodule\n"; std::fs::write(tree.path.join("top.sv"), emptied).expect("write"); session.notify( DidSaveTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": uri }, "text": emptied }), ); assert!( session.server.workspace.index().instantiations_of("sub").is_empty(), "the saved file's edges are gone" ); assert_eq!( session.server.workspace.index().units("sub").len(), 1, "and sub.sv, which nothing named, is untouched" ); } #[test] fn a_one_file_save_costs_no_bar_and_no_log_line() { // The fast path's whole visible protocol delta: a save that names one // file the index already holds does its work inside a frame, so there // is no bar to put up and no scan to narrate. What it does still do — // the generation bump and the code-lens refresh — is pinned by // `a_one_file_save_still_bumps_the_generation_and_refreshes_the_lenses`. let tree = Tree::new("progressonefile"); tree.package("rtl/a.sv", "a"); let abilities = ClientAbilities { work_done_progress: true, ..Default::default() }; let mut session = Session::wide_with(vec![tree.path.clone()], abilities); let uri = session.open(&tree.path.join("rtl/a.sv"), "package a;\nendpackage\n"); let _ = session.drain(); session .notify(DidSaveTextDocument::METHOD, serde_json::json!({ "textDocument": { "uri": uri } })); assert!(session.progress().is_empty(), "one file is not a scan to report"); assert!(session.logged().is_empty(), "and not a scan to narrate either"); } #[test] fn the_bar_says_how_many_files_were_indexed() { // The one number this costs nothing to know: the index's own file // list. Anything richer would mean plumbing a count through // `rebuild_index`. let tree = Tree::new("progresscount"); tree.package("rtl/a.sv", "a"); tree.package("rtl/b.sv", "b"); let session = Session::wide_with(vec![tree.path.clone()], watching_progress()); assert_eq!(session.ended(), vec![Some("2 files indexed".to_string())]); let one = Tree::new("progresscountone"); one.package("rtl/a.sv", "a"); let session = Session::wide_with(vec![one.path.clone()], watching_progress()); assert_eq!(session.ended(), vec![Some("1 file indexed".to_string())], "not `1 files`"); } #[test] fn the_bar_counts_the_files_as_it_goes() { // The whole of feature 2: how many of how many, which file, and a // percentage that was measured rather than guessed. let tree = Tree::new("progressreports"); tree.package("rtl/a.sv", "a"); tree.package("rtl/b.sv", "b"); let session = Session::wide_with(vec![tree.path.clone()], watching_progress()); assert_eq!( session.reported(), vec![(Some("1/2 a.sv".to_string()), Some(50)), (Some("2/2 b.sv".to_string()), Some(100)),], "counts ascending, the file named, the percentage monotonic and ending at 100" ); } #[test] fn the_reports_thin_out_when_the_workspace_is_huge() { // A report per file is right for hundreds and wrong for tens of // thousands. Past `MAX_REPORTS` files they thin out — but the last // file always reports, so the bar always finishes where the work did. let tree = Tree::new("progressflood"); let files = MAX_REPORTS + 100; for i in 0..files { tree.package(&format!("f{i:05}.sv",), &format!("p{i}")); } let session = Session::wide_with(vec![tree.path.clone()], watching_progress()); let reports = session.reported(); assert!(reports.len() <= MAX_REPORTS, "{} reports for {files} files", reports.len()); assert!(reports.len() > MAX_REPORTS / 2, "but not silence: {}", reports.len()); let percentages: Vec = reports.iter().filter_map(|(_, p)| *p).collect(); assert_eq!(percentages.len(), reports.len(), "every report carries one"); assert!(percentages.windows(2).all(|w| w[0] <= w[1]), "monotonic: {percentages:?}"); assert_eq!(percentages.last(), Some(&100), "and it ends at 100"); let last = reports.last().expect("a report").0.clone().expect("a message"); assert!(last.starts_with(&format!("{files}/{files} ")), "the last file reports: {last}"); } #[test] fn the_report_schedule_never_floods_and_never_stops_short() { // The rule itself, across both regimes and their boundary — cheaper to // check exhaustively here than to build 40,000 files. for total in [1, 2, 999, 1000, 1001, 1999, 2000, 12_345, 40_000] { let reporting: Vec = (0..total).filter(|d| reports(*d, total)).collect(); assert!(!reporting.is_empty(), "{total} files must report something"); assert!(reporting.len() <= MAX_REPORTS, "{total} files: {} reports", reporting.len()); assert_eq!( reporting.last(), Some(&(total - 1)), "{total} files: the last file must report" ); assert!(reporting.windows(2).all(|w| w[0] < w[1]), "{total}: strictly ascending"); // The percentage a client sees only ever counts up, and lands on // 100 exactly once, at the end. let percentages: Vec = reporting.iter().map(|d| (d + 1) * 100 / total).collect(); assert!(percentages.windows(2).all(|w| w[0] <= w[1]), "{total}: monotonic"); assert_eq!(percentages.last(), Some(&100), "{total}: ends at 100"); } } #[test] fn the_bar_is_down_before_the_republish() { // Pinned because either order is defensible and a reader would // wonder: the bar names the *scan*, so it ends the moment the scan // returns — the republish that follows is per-open-buffer work the // user already pays on every keystroke. let tree = Tree::new("progressorder"); tree.package("rtl/a.sv", "a"); tree.package("rtl/b.sv", "b"); let mut session = Session::with(vec![tree.path.clone()], watching_progress()); session.open(&tree.path.join("rtl/a.sv"), "package a;\nendpackage\n"); let _ = session.drain(); // Two files at once, so this is a whole rescan and there is a bar to // order the republish against (incremental-index, 2026-08-27). session.watched(&[tree.path.join("rtl/a.sv"), tree.path.join("rtl/b.sv")]); let order: Vec = session .drain() .into_iter() .filter_map(|message| match &message { Message::Notification(n) if n.method == PublishDiagnostics::METHOD => { Some("publish".to_string()) } _ => progress_step(message).map(|(step, _)| step.to_string()), }) .collect(); assert_eq!(order, vec!["create", "begin", "report", "report", "end", "publish"]); }