mod common;
use common::{copy_store_to_temp, corpus_a, dbmd};
#[test]
fn regression_rebuild_layer_refreshes_root_index_counts() {
let (_tmp, store) = copy_store_to_temp(&corpus_a());
let root_before = std::fs::read_to_string(store.join("index.md")).unwrap();
assert!(
root_before.contains("- [[records/contacts/index|Contacts]] (4)\n"),
"precondition: corpus-a root must list 4 contacts:\n{root_before}"
);
assert!(
root_before.contains("## Records (505)\n"),
"precondition: corpus-a records layer total must be 505:\n{root_before}"
);
std::fs::write(
store.join("records/contacts/nadia-petrov.md"),
"---\n\
type: contact\n\
created: 2026-05-30T09:00:00-07:00\n\
updated: 2026-05-30T09:00:00-07:00\n\
summary: \"New contact added on disk before indexing\"\n\
name: Nadia Petrov\n\
email: nadia.petrov@northstar.example\n\
role: Operations Lead\n\
tags: [customer]\n\
status: active\n\
---\n\n\
Body.\n",
)
.unwrap();
dbmd()
.current_dir(&store)
.args(["index", "rebuild", "--layer", "records"])
.assert()
.success();
let contacts_jsonl =
std::fs::read_to_string(store.join("records/contacts/index.jsonl")).unwrap();
assert!(
contacts_jsonl.contains("records/contacts/nadia-petrov.md"),
"folder sidecar must catalog the new contact:\n{contacts_jsonl}"
);
let records_layer = std::fs::read_to_string(store.join("records/index.md")).unwrap();
assert!(
records_layer.contains("- [[records/contacts/index|Contacts]] (5) —"),
"layer rollup must show 5 contacts:\n{records_layer}"
);
let root_after = std::fs::read_to_string(store.join("index.md")).unwrap();
assert!(
root_after.contains("- [[records/contacts/index|Contacts]] (5)\n"),
"root index.md must reflect 5 contacts after `index rebuild --layer records`, \
not the stale 4 (root/folder count desync):\n{root_after}"
);
assert!(
root_after.contains("## Records (506)\n"),
"root index.md records-layer total must be refreshed to 506 after the \
layer rebuild, not the stale 505:\n{root_after}"
);
assert!(
!root_after.contains("- [[records/contacts/index|Contacts]] (4)\n"),
"stale `Contacts (4)` count must be gone from root:\n{root_after}"
);
}