Files
lucisv/crates/lucisv_server/src/state/tests/folder_scope.rs
T
lucioandClaude Opus 5.5 4fae8fb63a TODO §55: split lucisv_server state/mod.rs by responsibility (pure move)
state/mod.rs 986 -> 63 production lines: document.rs (one analyzed
buffer), buffers.rs (open buffers), views.rs (closed-file view cache),
scope.rs (roots, settings, scope ladder), rescan.rs (scan and widen
protocol, 411), incremental.rs (one saved file re-indexed), sources.rs
(file sets for refactorings, 170). The 3 944-line inline test module moved
to state/tests/ (14 topic files). No function body changed; only forced
pub(super) widenings. Lead's line-multiset check: no code line lost or
added. CLAUDE.md measurement refreshed (2026-10-14); a stale test path in
server/tests/folders.rs corrected.

Gates on the merged tree: fmt and clippy -D warnings clean; lucisv_server
1 823 passed, 0 failed, 38 ignored (= before); the lane's workspace run 80
suites, 5 078 passed. Refactor dev Claude Fable 5.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 13:38:04 -07:00

407 lines
17 KiB
Rust

//! The folder scope (index-scope, 2026-08-27): rung 3's climb and the two
//! clamps on it, what outranks it, and the inherited obligations from that
//! round's review.
use super::*;
/// A project laid out the way the owner described it: the file being
/// worked on in `rtl/`, a header folder beside it, and a third folder that
/// has nothing to do with either.
fn two_folder_tree(name: &str) -> Tree {
let tree = Tree::new(name);
tree.package("rtl/open.sv", "open_pkg");
tree.package("rtl/beside.sv", "beside_pkg");
tree.package("rtl/deeper/below.sv", "below_pkg");
tree.package("include/defs.sv", "defs_pkg");
tree.package("elsewhere/far.sv", "far_pkg");
tree
}
/// The four fixture packages, as the ones that are in the index.
fn scoped(ws: &Workspace) -> Vec<String> {
indexed(ws, &["open_pkg", "beside_pkg", "below_pkg", "defs_pkg", "far_pkg"])
}
#[test]
fn the_folder_scope_at_depth_zero_is_the_open_files_own_folder_and_everything_under_it() {
// The headline of the index-scope round: a workspace root is open, but
// the index covers the folder the user is working in, not the project.
// At depth 0 — the shipped default until 2026-08-28, and still exactly
// what an explicit `scopeDepth: 0` asks for.
let tree = two_folder_tree("scopedefault");
let mut ws = Workspace::default();
own_folder_only(&mut ws);
ws.open_roots(vec![tree.path.clone()]);
assert!(scoped(&ws).is_empty(), "premise: nothing is open, so nothing is in scope");
open(&mut ws, &tree, "rtl/open.sv");
ws.rebuild_index();
assert_eq!(
scoped(&ws),
["open_pkg", "beside_pkg", "below_pkg"],
"the buffer's own folder and its subtree — and no sibling folder"
);
}
#[test]
fn a_scope_depth_of_one_reaches_the_folder_beside_it() {
// `rtl/` plus `../include`: the layout the setting exists for.
let tree = two_folder_tree("scopedepth");
let mut ws = Workspace::default();
ws.set_client_settings(ClientSettings { index_scope_depth: 1, ..Default::default() });
ws.open_roots(vec![tree.path.clone()]);
open(&mut ws, &tree, "rtl/open.sv");
ws.rebuild_index();
assert_eq!(
scoped(&ws),
["open_pkg", "beside_pkg", "below_pkg", "defs_pkg", "far_pkg"],
"one level up is the whole tree here, sibling folders included"
);
}
#[test]
fn the_climb_stops_at_the_workspace_root() {
// The clamp that keeps a depth of 99 in a two-deep project from
// indexing the user's home directory. Asserted on the directories
// themselves rather than on an index, because the failure this guards
// against is a walk that should never be started.
let tree = two_folder_tree("scopeclamp");
let mut ws = Workspace::default();
ws.set_client_settings(ClientSettings { index_scope_depth: 99, ..Default::default() });
ws.open_roots(vec![tree.path.clone()]);
open(&mut ws, &tree, "rtl/deeper/below.sv");
assert_eq!(ws.scope_dirs(), vec![tree.path.clone()], "clamped at the root, not above it");
// The deepest containing root wins, so a nested root clamps tighter.
assert!(ws.change_roots(vec![tree.path.join("rtl")], &[]));
assert_eq!(ws.scope_dirs(), vec![tree.path.join("rtl")], "the deepest root is the floor");
}
#[test]
fn a_rootless_session_does_not_climb_at_all() {
// Changed 2026-08-28, with the shipped depth. The climb used to run
// with `/` as its only floor, which was survivable while the shipped
// depth was 0 and nobody climbed by accident. It is not survivable at
// depth 2: a lone file opened from `~/work/chip/hw/ip/aes/rtl/` would
// raise into `~/work/chip/hw/ip/` and walk a tree the user never
// named. The clamp is what makes a raise safe, so with nothing to
// clamp against there is no raise, whatever the depth says.
let tree = two_folder_tree("scoperootless");
for depth in [0, 1, 99] {
let mut ws = Workspace::default();
ws.set_client_settings(ClientSettings { index_scope_depth: depth, ..Default::default() });
open(&mut ws, &tree, "rtl/open.sv");
assert!(!ws.has_roots(), "the premise: no workspace folder to clamp against");
assert_eq!(
ws.scope_dirs(),
vec![tree.path.join("rtl")],
"depth {depth}: the buffer's own directory, and nothing above it"
);
}
}
#[test]
fn a_rootless_session_reads_its_own_directory_flat_until_the_depth_is_raised() {
// The `folder_files` exception, from the other side. The *directory*
// is the buffer's own whatever the depth (no root, no climb — see
// above), but the depth still chooses how it is read: 0 reads it flat,
// one level, no `.gitignore`, and a raise means the user has asked for
// a tree, so the tree below the buffer is walked.
let tree = two_folder_tree("scoperootlesswalk");
for (depth, expected) in
[(0, vec!["open_pkg", "beside_pkg"]), (1, vec!["open_pkg", "beside_pkg", "below_pkg"])]
{
let mut ws = Workspace::default();
ws.set_client_settings(ClientSettings { index_scope_depth: depth, ..Default::default() });
open(&mut ws, &tree, "rtl/open.sv");
ws.rebuild_index();
assert_eq!(scoped(&ws), expected, "depth {depth}");
}
}
#[test]
fn the_workspace_scope_setting_restores_the_walk_of_every_root() {
// The migration line in the README, pinned: with `scope: "workspace"`
// the index is the whole tree with no buffer open at all — which is
// exactly what this fixture gave before this round.
let tree = two_folder_tree("scopewide");
let mut ws = Workspace::default();
ws.set_client_settings(ClientSettings {
index_scope: IndexScope::Workspace,
..Default::default()
});
ws.open_roots(vec![tree.path.clone()]);
assert_eq!(
scoped(&ws),
["open_pkg", "beside_pkg", "below_pkg", "defs_pkg", "far_pkg"],
"every file under the root, no buffer needed"
);
// And the depth is not consulted at all on that rung.
let mut raised = Workspace::default();
raised.set_client_settings(ClientSettings {
index_scope: IndexScope::Workspace,
index_scope_depth: 3,
..Default::default()
});
raised.open_roots(vec![tree.path.clone()]);
assert_eq!(raised.index().files(), ws.index().files(), "the depth changes nothing here");
}
#[test]
fn a_filelist_still_defines_the_scope_exactly_whatever_the_folder_rung_would_say() {
// Rung 1 outranks rung 3 as it always outranked rung 2: the filelist
// is the project file, and it names files in folders no buffer has
// been opened from.
let tree = two_folder_tree("scopefilelist");
tree.write("files.f", "include/defs.sv\nelsewhere/far.sv\n");
let mut ws = Workspace::default();
ws.set_client_settings(using_filelist("files.f"));
ws.open_roots(vec![tree.path.clone()]);
open(&mut ws, &tree, "rtl/open.sv");
ws.rebuild_index();
assert_eq!(
scoped(&ws),
["open_pkg", "defs_pkg", "far_pkg"],
"the filelist, plus the open buffer that overlays every rung"
);
assert!(ws.scope_dirs().is_empty(), "and no `didOpen` can widen a filelist");
}
#[test]
fn closing_a_file_never_narrows_the_scope() {
// The decision, pinned: the scope only grows within a session. A
// rebuild after a close must not take away answers the user had a
// moment ago, and no `didClose` costs a rescan.
let tree = two_folder_tree("scopeclose");
let mut ws = Workspace::default();
ws.set_client_settings(ClientSettings { index_scope_depth: 0, ..Default::default() });
ws.open_roots(vec![tree.path.clone()]);
open(&mut ws, &tree, "rtl/open.sv");
open(&mut ws, &tree, "include/defs.sv");
ws.rebuild_index();
assert_eq!(scoped(&ws), ["open_pkg", "beside_pkg", "below_pkg", "defs_pkg"]);
let closed = crate::uri::path_to_uri(&tree.path.join("include/defs.sv")).expect("uri");
ws.close_document(&closed);
ws.rebuild_index();
assert_eq!(
scoped(&ws),
["open_pkg", "beside_pkg", "below_pkg", "defs_pkg"],
"the closed file's folder is still indexed"
);
}
#[test]
fn the_folder_scope_honours_every_exclude_and_the_gitignores_below_it() {
// Rung 3 walks with the same function rung 2 walks with, so nothing a
// user excluded comes back because the scope narrowed — with **one
// honest exception**, pinned here rather than left to be discovered:
// `lucisv_core`'s walk reads `.gitignore` files from its own starting
// directory downwards, so a rule written at the workspace root does
// not reach a walk that starts below it. The `exclude` patterns are
// matched against whole paths and are unaffected either way. Stated in
// `walked_files` and in `docs/features/index-scope/README.md`.
let tree = Tree::new("scopefilters");
tree.write("lucisv.toml", "exclude = [\"vendor\"]\n");
tree.write(".gitignore", "generated/\n");
tree.write("rtl/.gitignore", "dumps/\n");
tree.package("rtl/open.sv", "open_pkg");
tree.package("rtl/vendor/ip.sv", "vendor_pkg");
tree.package("rtl/dumps/net.sv", "dumps_pkg");
tree.package("rtl/generated/net.sv", "generated_pkg");
tree.package("rtl/scratch/wip.sv", "scratch_pkg");
let names = &["open_pkg", "vendor_pkg", "dumps_pkg", "generated_pkg", "scratch_pkg"];
let mut ws = Workspace::default();
ws.set_client_settings(excluding(&["scratch"]));
ws.open_roots(vec![tree.path.clone()]);
open(&mut ws, &tree, "rtl/open.sv");
ws.rebuild_index();
assert_eq!(
indexed(&ws, names),
["open_pkg", "generated_pkg"],
"both excludes and the .gitignore inside the scope applied; the one at the \
root, above where this walk starts, did not"
);
// ...and the wide scope, whose walk starts at the root, does apply it.
let mut wide = Workspace::default();
wide.set_client_settings(ClientSettings {
exclude: vec!["scratch".to_string()],
index_scope: IndexScope::Workspace,
..Default::default()
});
wide.open_roots(vec![tree.path.clone()]);
assert_eq!(indexed(&wide, names), ["open_pkg"], "everything filtered, from the root");
}
#[test]
fn a_folder_added_mid_session_widens_nothing_until_a_file_in_it_is_opened() {
// The interaction between the two ways a scope can move. Under the
// folder rung a workspace folder is a *clamp*, not a work list, so
// adding one changes which files exist only once a buffer in it makes
// it a scope directory.
let first = Tree::new("scopefolderfirst");
first.package("a.sv", "a");
let second = Tree::new("scopefoldersecond");
second.package("b.sv", "b");
let mut ws = Workspace::default();
ws.open_roots(vec![first.path.clone()]);
open(&mut ws, &first, "a.sv");
ws.rebuild_index();
assert_eq!(indexed(&ws, &["a", "b"]), ["a"], "premise: the first folder's file");
assert!(ws.change_roots(vec![second.path.clone()], &[]), "the root set moved");
ws.rebuild_index();
assert_eq!(indexed(&ws, &["a", "b"]), ["a"], "but no buffer lives there yet");
open(&mut ws, &second, "b.sv");
ws.rebuild_index();
assert_eq!(indexed(&ws, &["a", "b"]), ["a", "b"], "opening one widens the scope");
}
#[test]
fn a_scope_or_a_depth_change_asks_for_a_rescan() {
// Both change which files exist, so both join `exclude`, `filelist`
// and the size cap in the rescan test.
let mut ws = Workspace::default();
let wide = ClientSettings { index_scope: IndexScope::Workspace, ..Default::default() };
assert!(ws.set_client_settings(wide.clone()), "a new scope rescans");
assert!(!ws.set_client_settings(wide.clone()), "the same one does not");
assert!(ws.set_client_settings(ClientSettings::default()), "and going back does");
let deep = ClientSettings { index_scope_depth: 5, ..Default::default() };
assert!(ws.set_client_settings(deep.clone()), "a new depth rescans");
assert!(!ws.set_client_settings(deep), "the same one does not");
assert!(ws.set_client_settings(ClientSettings::default()), "and going back does");
}
#[test]
fn the_open_files_are_indexed_first_and_the_index_cannot_tell() {
// Two halves of one claim. First: the progress events name the open
// buffers before anything else, which is what makes a big rescan feel
// like it is working on the user's own files.
let tree = Tree::new("scopeorder");
tree.package("rtl/a_first.sv", "a_first");
tree.package("rtl/m_open.sv", "m_open");
tree.package("rtl/z_last.sv", "z_last");
let mut ws = Workspace::default();
ws.open_roots(vec![tree.path.clone()]);
open(&mut ws, &tree, "rtl/m_open.sv");
// Every file of the work list, however it got into the index: since
// 2026-08-28 the open buffer goes in from its own analysis and reports
// `Analyzed` rather than `Parsing`, and the claim under test is about
// the *order* the work list is walked in, not about who parses.
let mut seen: Vec<PathBuf> = Vec::new();
ws.rebuild_index_with(&mut |event| match event {
IndexEvent::Parsing { path, .. }
| IndexEvent::Cached { path, .. }
| IndexEvent::Analyzed { path, .. } => seen.push(path.to_path_buf()),
IndexEvent::Skipped { .. } => {}
});
assert_eq!(
seen,
vec![
tree.path.join("rtl/m_open.sv"),
tree.path.join("rtl/a_first.sv"),
tree.path.join("rtl/z_last.sv"),
],
"the open buffer first, then the rest in path order"
);
// Second, and the reason the first is allowed to be true: the index
// is byte-identical to the one the plain path order builds. The
// builder sorts inside `finish`, so the order above is a reporting
// decision and nothing more (`vlog_semantic`'s
// `no_insertion_order_can_change_what_the_builder_produces` is the
// other end of this).
let mut plain = Workspace::default();
plain.set_client_settings(ClientSettings {
index_scope: IndexScope::Workspace,
..Default::default()
});
plain.open_roots(vec![tree.path.clone()]);
assert_eq!(ws.index().files(), plain.index().files(), "same files, same ids");
assert_eq!(ws.index().counts(), plain.index().counts(), "and the same index");
}
#[test]
fn lowering_the_scope_depth_narrows_the_index_on_the_next_rescan() {
// The scope grows only along the *open* axis — a `didClose` never
// narrows it. A settings change is the other axis and moves both ways,
// because it is a deliberate statement about how much of this machine's
// time to spend. Verified by hand by the previous round's QA; pinned
// here.
let tree = two_folder_tree("depthdown");
let mut ws = Workspace::default();
ws.set_client_settings(ClientSettings { index_scope_depth: 1, ..Default::default() });
ws.open_roots(vec![tree.path.clone()]);
open(&mut ws, &tree, "rtl/open.sv");
ws.rebuild_index();
assert!(scoped(&ws).contains(&"defs_pkg".to_string()), "premise: depth 1 reaches beside");
assert!(
ws.set_client_settings(ClientSettings { index_scope_depth: 0, ..Default::default() }),
"lowering the depth asks for a rescan"
);
ws.rebuild_index();
assert_eq!(scoped(&ws), ["open_pkg", "beside_pkg", "below_pkg"], "narrowed, not kept");
}
#[test]
fn a_filelist_appearing_mid_session_flips_the_ladder_to_rung_one() {
// Not just "asks for a rescan" — that the index really becomes the
// filelist's files, from a folder scope that was covering something
// else entirely.
let tree = two_folder_tree("filelistmid");
tree.write("files.f", "elsewhere/far.sv\n");
let mut ws = Workspace::default();
own_folder_only(&mut ws);
ws.open_roots(vec![tree.path.clone()]);
open(&mut ws, &tree, "rtl/open.sv");
ws.rebuild_index();
assert_eq!(scoped(&ws), ["open_pkg", "beside_pkg", "below_pkg"], "premise: the folder rung");
assert!(ws.set_client_settings(using_filelist("files.f")), "a rescan is owed");
ws.rebuild_index();
assert_eq!(
scoped(&ws),
["open_pkg", "far_pkg"],
"rung 1 defines the scope exactly — plus the open buffer, which overlays every rung"
);
}
#[test]
fn a_buffer_opened_outside_every_root_rides_the_folder_rung_like_a_rootless_one() {
// `raised()`'s floor is the deepest root *containing* the directory, and
// there is none here — so the climb stops at `/`, exactly as it does
// with no root at all. Verified by hand by the previous round's QA;
// pinned here, on the directories rather than by walking one.
let inside = Tree::new("outsideroot_in");
inside.package("rtl/a.sv", "a_pkg");
let outside = Tree::new("outsideroot_out");
outside.package("other/b.sv", "b_pkg");
let mut ws = Workspace::default();
own_folder_only(&mut ws);
ws.open_roots(vec![inside.path.clone()]);
open(&mut ws, &outside, "other/b.sv");
assert_eq!(
ws.scope_dirs(),
vec![outside.path.join("other")],
"at depth 0 it is the buffer's own directory and nothing else"
);
ws.rebuild_index();
assert_eq!(indexed(&ws, &["a_pkg", "b_pkg"]), ["b_pkg"], "the root's own tree is not in it");
ws.set_client_settings(ClientSettings { index_scope_depth: 99, ..Default::default() });
assert_eq!(
ws.scope_dirs(),
vec![PathBuf::from("/")],
"and with nothing to clamp against the climb stops at the filesystem root"
);
}