Files
lucisv/crates/lucisv_server/src/server/tests/file_ops.rs
T
lucioandClaude Fable 5 b96827c51d Decomposition, lane C: lucisv_server's server.rs and completion.rs become modules, a pure move
TODO section 28, lane C (the file-size rule in CLAUDE.md).
server.rs (2 559 production + 6 293 test lines) becomes server/{mod,
dispatch, client, index, notification, progress, diagnostics, batch,
formatting, large_file, semantic_tokens}.rs with the tests in
server/tests/ (16 files, one per former banner section, 15 shared
helpers hoisted into the harness); completion.rs (1 716 + 2 463)
becomes completion/{mod, member, module, tiers, word, site,
harness}.rs with each provider's tests beside it. answer_request (393)
and handle_notification (197) stay whole as the dispatch tables they
are; perf.rs stays whole because its one sentence has no and. Every
import elsewhere in the crate resolves through pub(crate) re-exports,
so main.rs, inline.rs, instantiate.rs, hover.rs, nav.rs and state.rs
are byte-identical. Proof: per-binary counts identical (1 435 / 85 / 3;
workspace 3 215 / 48), e2e and the vsix smoke green, a live LSP session
against opentitan answering byte-identically from this build and the
base, and a line-multiset + signature census (360/360, 188/188) done
by the dev and redone from scratch by QA.

docs/features/decomposition-server/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-09-02 21:33:48 -07:00

176 lines
7.3 KiB
Rust

//! File operations (`docs/features/file-operations/README.md`).
use super::*;
// ------------------------------------------------------------------
// file operations (`docs/features/file-operations/README.md`)
// ------------------------------------------------------------------
/// One `workspace/willRenameFiles` for `from` → `to`, driven through the
/// real dispatch.
fn will_rename(session: &mut Session, from: &Path, to: &Path) -> Value {
session.result(
WillRenameFiles::METHOD,
serde_json::json!({ "files": [{
"oldUri": path_to_uri(from).expect("uri"),
"newUri": path_to_uri(to).expect("uri"),
}] }),
)
}
#[test]
fn will_rename_answers_the_edit_that_keeps_an_include_resolving() {
let tree = Tree::new("willrename");
tree.write("defs.svh", "`define W 8\n");
tree.write("top.sv", "`include \"defs.svh\"\nmodule t;\nendmodule\n");
let mut session = Session::wide(vec![tree.path.clone()], false);
let (from, to) = (tree.path.join("defs.svh"), tree.path.join("widths.svh"));
let answer = will_rename(&mut session, &from, &to);
let uri = path_to_uri(&tree.path.join("top.sv")).expect("uri");
let edits = &answer["changes"][uri.as_str()];
assert_eq!(edits[0]["newText"], serde_json::json!("widths.svh"), "{answer:#?}");
// The range is the path token: line 0, inside the quotes.
assert_eq!(edits[0]["range"]["start"], serde_json::json!({ "line": 0, "character": 10 }));
assert_eq!(edits[0]["range"]["end"], serde_json::json!({ "line": 0, "character": 18 }));
assert!(session.shown().is_empty(), "a rewrite everything could reach says nothing");
}
#[test]
fn will_rename_works_for_a_file_no_editor_ever_opened() {
// Includes live in unopened files; the sweep reads them off disk
// through the same `sources()` the index is built from.
let tree = Tree::new("willrenameunopened");
tree.write("defs.svh", "`define W 8\n");
tree.write("top.sv", "`include \"defs.svh\"\n");
let mut session = Session::wide(vec![tree.path.clone()], false);
assert!(session.server.workspace.open_uris().is_empty(), "premise: nothing open");
let answer =
will_rename(&mut session, &tree.path.join("defs.svh"), &tree.path.join("widths.svh"));
let uri = path_to_uri(&tree.path.join("top.sv")).expect("uri");
assert_eq!(
answer["changes"][uri.as_str()][0]["newText"],
serde_json::json!("widths.svh"),
"{answer:#?}"
);
}
#[test]
fn will_rename_answers_null_when_there_is_nothing_to_rewrite() {
let tree = Tree::new("willrenamenull");
tree.package("rtl/a.sv", "a");
tree.write("notes.md", "prose\n");
let mut session = Session::new(vec![tree.path.clone()], false);
// A source file nothing includes.
let answer =
will_rename(&mut session, &tree.path.join("rtl/a.sv"), &tree.path.join("rtl/b.sv"));
assert_eq!(answer, Value::Null, "{answer:#?}");
// And a file this server has no opinion about at all.
let answer =
will_rename(&mut session, &tree.path.join("notes.md"), &tree.path.join("NOTES.md"));
assert_eq!(answer, Value::Null, "{answer:#?}");
assert!(session.shown().is_empty());
}
#[test]
fn an_include_a_rename_strands_is_shown_once_and_logged() {
// The same "first shown, all logged" rule the scope warnings follow —
// a rename that strands forty includes must not open forty popups.
let tree = Tree::new("willrenamewarn");
std::fs::create_dir_all(tree.path.join("inc")).expect("mkdir");
tree.write("inc/defs.svh", "`define W 8\n");
tree.write("a.sv", "`include <defs.svh>\n");
tree.write("b.sv", "`include <defs.svh>\n");
tree.write("files.f", "+incdir+inc\na.sv\nb.sv\n");
tree.write("lucisv.toml", "filelist = \"files.f\"\n");
let mut session = Session::new(vec![tree.path.clone()], false);
let _ = session.drain();
// The angled form resolves against incdirs only, so a move out of the
// one include path cannot be spelled from either file.
let answer =
will_rename(&mut session, &tree.path.join("inc/defs.svh"), &tree.path.join("far.svh"));
assert_eq!(answer, Value::Null, "nothing invented: {answer:#?}");
let shown = session.drain();
let popups: Vec<&Message> = shown
.iter()
.filter(|m| matches!(m, Message::Notification(n) if n.method == ShowMessage::METHOD))
.collect();
let logs: Vec<&Message> = shown
.iter()
.filter(|m| matches!(m, Message::Notification(n) if n.method == LogMessage::METHOD))
.collect();
assert_eq!(popups.len(), 1, "one popup for two stranded includes: {popups:#?}");
assert_eq!(logs.len(), 2, "both are logged: {logs:#?}");
}
/// One `workspace/didRenameFiles` for `from` → `to`.
fn did_rename(session: &mut Session, from: &Path, to: &Path) {
session.notify(
DidRenameFiles::METHOD,
serde_json::json!({ "files": [{
"oldUri": path_to_uri(from).expect("uri"),
"newUri": path_to_uri(to).expect("uri"),
}] }),
);
}
#[test]
fn a_rename_notification_rebuilds_the_index() {
let tree = Tree::new("didrename");
tree.package("rtl/a.sv", "a");
let mut session = Session::wide(vec![tree.path.clone()], false);
assert_eq!(indexed(&session, "a"), 1, "premise: indexed under its old name");
std::fs::rename(tree.path.join("rtl/a.sv"), tree.path.join("rtl/b.sv")).expect("rename");
did_rename(&mut session, &tree.path.join("rtl/a.sv"), &tree.path.join("rtl/b.sv"));
assert_eq!(indexed(&session, "a"), 1, "still one package, found at its new path");
// And the same nudge from the watcher for the same move changes
// nothing: the rebuild is a re-read of the tree, not an increment.
session.watched(&[tree.path.join("rtl/b.sv")]);
assert_eq!(indexed(&session, "a"), 1, "reindexing twice is harmless");
}
#[test]
fn a_delete_notification_rebuilds_the_index() {
let tree = Tree::new("diddelete");
tree.package("rtl/a.sv", "a");
tree.package("rtl/gone.sv", "gone_pkg");
let mut session = Session::wide(vec![tree.path.clone()], false);
assert_eq!(indexed(&session, "gone_pkg"), 1, "premise: indexed");
std::fs::remove_file(tree.path.join("rtl/gone.sv")).expect("remove");
session.notify(
DidDeleteFiles::METHOD,
serde_json::json!({ "files": [
{ "uri": path_to_uri(&tree.path.join("rtl/gone.sv")).expect("uri") },
] }),
);
assert_eq!(indexed(&session, "gone_pkg"), 0, "gone from the index too");
assert_eq!(indexed(&session, "a"), 1, "and the rest survives");
}
#[test]
fn a_file_operation_notification_for_nothing_the_index_reads_costs_no_rebuild() {
let tree = Tree::new("didrenamestray");
tree.package("rtl/open.sv", "open_pkg");
let mut session = Session::new(vec![tree.path.clone()], false);
session.open(
&tree.path.join("rtl/open.sv"),
"package open_pkg;\n localparam K = 1;\nendpackage\n",
);
assert_eq!(session.publishes(), 1, "premise: `didOpen` published once");
did_rename(&mut session, &tree.path.join("README.md"), &tree.path.join("READ.md"));
session.notify(
DidDeleteFiles::METHOD,
serde_json::json!({ "files": [
{ "uri": path_to_uri(&tree.path.join("notes.txt")).expect("uri") },
] }),
);
assert_eq!(session.publishes(), 0, "neither is a path the index reads");
}