Files
lucisv/crates/lucisv_server/src/state/tests/interleave.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

188 lines
8.7 KiB
Rust

//! An interleaved scan against the one-shot scan it must equal: the
//! publication cadence, the tables however often they were published, a
//! buffer opened mid-scan, and the events in the same order either way.
use super::*;
/// Run a scan the way `server.rs` does, publishing after **every** file —
/// the worst case the interleaved path can produce, and the one a
/// one-shot scan has to agree with.
fn scan_publishing_every_file(ws: &mut Workspace) -> Vec<String> {
let mut scan = ws.begin_scan(&mut |_| {}, false);
while ws.scan_step(&mut scan, &mut |_| {}) {
ws.publish_scan(&mut scan);
}
ws.finish_scan(scan)
}
#[test]
fn the_publication_cadence_holds_a_publish_until_enough_has_been_staged() {
// **The whole of the cadence, at its exact boundary.** A publish is a
// pass over the whole index, and before §61.4 the drain paid one per
// burst of requests — 13 to 18 in one warm opentitan scan, 46 % of it
// (`docs/features/perf5r-publication/README.md`). `publish_is_due`
// spaces them by work done rather than by how often a client asks: no
// publish is due until `PUBLISH_EVERY` records are staged, one is due
// the moment the 512th lands, and publishing clears the debt.
let tree = Tree::new("publishcadence");
for n in 0..scan::PUBLISH_EVERY + 2 {
tree.package(&format!("p{n:04}.sv"), &format!("p{n:04}"));
}
let mut ws = Workspace::default();
ws.client.index_scope = IndexScope::Workspace;
ws.adopt_roots(vec![tree.path.clone()]);
let mut scan = ws.begin_scan(&mut |_| {}, false);
for taken in 1..scan::PUBLISH_EVERY {
assert!(ws.scan_step(&mut scan, &mut |_| {}), "premise: file {taken} exists");
assert!(!scan.publish_is_due(), "{taken} staged is not yet a publish");
}
assert!(ws.scan_step(&mut scan, &mut |_| {}), "premise: the 512th file exists");
assert!(scan.publish_is_due(), "{} staged is", scan::PUBLISH_EVERY);
ws.publish_scan(&mut scan);
assert!(!scan.publish_is_due(), "and publishing clears the debt");
// The cadence is a latency decision and nothing else: what this scan
// lands is what a one-shot scan over the same tree lands.
while ws.scan_step(&mut scan, &mut |_| {}) {}
ws.finish_scan(scan);
let mut one_shot = Workspace::default();
one_shot.open_whole_workspace(vec![tree.path.clone()]);
assert_eq!(everything(&one_shot), everything(&ws));
}
#[test]
fn publishing_between_every_file_leaves_what_a_one_shot_scan_leaves() {
// The round's central invariant. The interleaved scan and the one-shot
// scan are the same three calls with and without a drain between them,
// so this pins the thing a drain could break: that folding records in
// as they are taken lands the same index, the same completion tables
// and the same include graph as folding them all in at the end.
let tree = tangled("interleavesame");
let mut one_shot = Workspace::default();
one_shot.open_whole_workspace(vec![tree.path.clone()]);
let mut stepped = Workspace::default();
stepped.client.index_scope = IndexScope::Workspace;
stepped.adopt_roots(vec![tree.path.clone()]);
scan_publishing_every_file(&mut stepped);
assert_eq!(everything(&one_shot), everything(&stepped));
assert!(one_shot.index().files().len() == 4, "premise: every file was scanned");
}
#[test]
fn a_rescan_that_publishes_as_it_goes_still_drops_what_left_the_scope() {
// A publish only ever folds records *in*, so the removal a rescan owes
// — files the last scan held that this one's work list does not name —
// has to survive every publish that happened before it.
let tree = tangled("interleavegone");
let mut ws = Workspace::default();
ws.open_whole_workspace(vec![tree.path.clone()]);
assert!(!ws.index().units("c").is_empty(), "premise: c.sv is indexed");
std::fs::remove_file(tree.path.join("c.sv")).expect("remove");
scan_publishing_every_file(&mut ws);
let mut fresh = Workspace::default();
fresh.open_whole_workspace(vec![tree.path.clone()]);
assert_eq!(everything(&fresh), everything(&ws));
assert!(ws.index().units("c").is_empty(), "the file that left the scope is gone");
assert_eq!(ws.index().files().len(), 3);
}
#[test]
fn a_buffer_opened_or_changed_mid_scan_does_not_change_what_the_scan_indexes() {
// The scan holds the text every file had when its work list was taken,
// and indexes *that* — which is exactly what a `didOpen` or a
// `didChange` landing a moment after the scan finished would leave in
// the index too (neither touches it; a save is what does). So a
// message answered in the middle of a scan cannot move the scan's
// result, and the fingerprint is the witness.
let tree = tangled("interleavemidchange");
let mut one_shot = Workspace::default();
one_shot.open_whole_workspace(vec![tree.path.clone()]);
let mut stepped = Workspace::default();
stepped.client.index_scope = IndexScope::Workspace;
stepped.adopt_roots(vec![tree.path.clone()]);
let mut scan = stepped.begin_scan(&mut |_| {}, false);
assert!(stepped.scan_step(&mut scan, &mut |_| {}), "premise: there is a first file");
stepped.publish_scan(&mut scan);
// The client opens `b.sv` and types in it, mid-scan.
let opened = uri(&format!("file://{}", tree.path.join("b.sv").display()));
stepped.set_document(opened.clone(), "module b;\n localparam MID = 1;\nendmodule\n".into());
while stepped.scan_step(&mut scan, &mut |_| {}) {
stepped.publish_scan(&mut scan);
}
stepped.finish_scan(scan);
assert_eq!(everything(&one_shot), everything(&stepped));
assert_eq!(
stepped.document(&opened).map(|doc| doc.text.as_str()),
Some("module b;\n localparam MID = 1;\nendmodule\n"),
"the buffer keeps the text it was given, index or no index"
);
}
#[test]
fn a_file_opened_mid_scan_is_indexed_next_and_the_index_cannot_tell() {
// The open-first rule for a file opened *after* the work list was
// taken. It moves to the front of what is left, everything it passed
// keeps its place behind it, and the finished index is byte-identical
// — records are merged in path order whatever order they arrived in.
let tree = tangled("interleaveopenfirst");
let mut one_shot = Workspace::default();
one_shot.open_whole_workspace(vec![tree.path.clone()]);
let mut stepped = Workspace::default();
stepped.client.index_scope = IndexScope::Workspace;
stepped.adopt_roots(vec![tree.path.clone()]);
let mut scan = stepped.begin_scan(&mut |_| {}, false);
assert!(stepped.scan_step(&mut scan, &mut |_| {}), "premise: a.sv goes first");
assert!(stepped.index().units("c").is_empty(), "premise: c.sv is not indexed yet");
// The client opens `c.sv`, and the work list is re-ordered around the
// buffers open now — since 2026-09-08 that is one call rather than one
// per opened path, because a buffer brings its folder with it
// (`docs/features/perf5-p4/README.md` §P4b3).
let jumped = uri(&format!("file://{}", tree.path.join("c.sv").display()));
let jumped_text = fs::read_to_string(tree.path.join("c.sv")).expect("the fixture");
stepped.set_document(jumped, jumped_text);
stepped.open_next(&mut scan, &mut |_| {});
assert!(stepped.scan_step(&mut scan, &mut |_| {}));
stepped.publish_scan(&mut scan);
assert!(!stepped.index().units("c").is_empty(), "c.sv was indexed next, not last");
while stepped.scan_step(&mut scan, &mut |_| {}) {}
stepped.finish_scan(scan);
assert_eq!(everything(&one_shot), everything(&stepped));
}
#[test]
fn a_scan_reports_the_same_events_in_the_same_order_however_it_is_driven() {
// The progress bar and the per-file log are the same two things a
// one-shot scan reports, in the same order, against the same total:
// interleaving adds drains between the events, never events.
let tree = tangled("interleaveevents");
let mut one_shot = Workspace::default();
one_shot.client.index_scope = IndexScope::Workspace;
one_shot.adopt_roots(vec![tree.path.clone()]);
let mut said = Vec::new();
one_shot.rebuild_index_with(&mut |event| said.push(seen(&event)));
let mut stepped = Workspace::default();
stepped.client.index_scope = IndexScope::Workspace;
stepped.adopt_roots(vec![tree.path.clone()]);
let mut also = Vec::new();
let mut scan = stepped.begin_scan(&mut |event| also.push(seen(&event)), false);
while stepped.scan_step(&mut scan, &mut |event| also.push(seen(&event))) {
stepped.publish_scan(&mut scan);
}
assert_eq!(scan.skipped(), 0);
assert_eq!(scan.cached(), 0);
stepped.finish_scan(scan);
assert_eq!(said, also);
assert_eq!(said.len(), 4, "one event per file, and nothing else: {said:?}");
}