crates/monetize-server/src/estate_order/tests/narrate_site.rs
//! ★ **DEV-NARRATE on the ONE box** (lane N2, 2026-10-20): a test site that says
//! `[estates.<label>] narrate = true` is laid (and its firewall written again) open to the ONE
//! box's `narrate_from` alone; every other estate — a serving site, a site that does not say
//! so, an estate SOLD on the narrated site — is laid closed. Its installer link file outlives
//! the hand-off (in a seal of its own), its logs are read by the operator.
use super::*;
const ONE_BOX: &str = "203.0.113.7";
/// `test3-gunnar-rs` narrated, `gunnar-rs` (the serving site) not, `kunde-se` a customer's —
/// on a box that says it is heard from [`ONE_BOX`] (or from nowhere, `from` empty).
fn narrate_rig(name: &str, from: &str) -> (Arc<AppState>, HoldPay, Arc<MockCloud>, PathBuf) {
let dir = tmp(name);
let pay = HoldPay::default();
let cloud = Arc::new(MockCloud::new());
let kit = FakeKit::new("PASS");
let site =
|vendor: &str, site: &str, owner: &str, operator_site: bool| crate::config::EstateConfig {
product: "gunnar".into(),
vendor: vendor.into(),
site: site.into(),
owner: owner.into(),
operator_site,
..Default::default()
};
let reg = Registry::new()
.sells("gunnar", pay.clone())
.vendor(SitePay("sitepay-kunde"))
.cloud(cloud.clone())
.shared_product(shop_product())
.home("gunnar", "gunnar-rs")
.estate("gunnar-rs", site("holdpay", "gunnar.rs", "ignalina", true))
.estate(
"test3-gunnar-rs",
crate::config::EstateConfig {
narrate: true,
terminable: true,
..site("holdpay", "test3.gunnar.rs", "ignalina", true)
},
)
.estate(
"kunde-se",
site("sitepay-kunde", "kunde.se", "kunde", false),
);
let (state, _clock, _mails, _) = build_in(
reg,
&dir,
EstateOrderConfig {
vendor: String::new(),
apex: String::new(),
narrate_from: from.into(),
..cfg()
},
kit,
None,
None,
);
(state, pay, cloud, dir)
}
fn lay_site(state: &Arc<AppState>, pay: &HoldPay, label: &str) -> EstateRun {
let run = site::start_site(state, label, "ops@example.invalid", &EstateSizes::default())
.expect("the site order is taken");
pay.authorize(&run.handle.clone().unwrap().id);
drive(state, &run.company);
state.estates.get(&run.company).unwrap()
}
fn narrate_of(cloud: &MockCloud, reference: &str) -> Vec<String> {
cloud
.narrate_from
.lock()
.unwrap()
.iter()
.filter(|(r, _)| r == reference)
.map(|(_, f)| f.clone())
.collect()
}
/// ★ **The lay plan opens udp/51822 only when narrate is on, and only from the ONE box**: the
/// narrated site's estate is laid with the box's address as its narration source; the serving
/// site, and an estate sold ON the narrated site, are laid with none; a box that names no
/// `narrate_from` lays even the narrated site closed.
#[test]
fn only_a_narrated_sites_own_estate_is_laid_open_and_only_to_the_one_box() {
let (state, pay, cloud, _dir) = narrate_rig("narrate-lay", ONE_BOX);
let t3 = lay_site(&state, &pay, "test3-gunnar-rs");
assert_eq!(t3.phase, phase::CAPTURED, "{:#?}", t3.history);
assert_eq!(
narrate_of(&cloud, "estate/test3gunnarrs"),
vec![ONE_BOX.to_string()],
"the narrated site: open to the ONE box"
);
assert_eq!(
t3.laid.as_ref().unwrap().narrate_from,
ONE_BOX,
"what was laid says so"
);
let g = lay_site(&state, &pay, "gunnar-rs");
assert_eq!(g.phase, phase::CAPTURED);
assert_eq!(
narrate_of(&cloud, "estate/gunnarrs"),
vec![String::new()],
"the serving site is never narrated"
);
let sold = start_with_on(
&state,
Some("test3-gunnar-rs"),
"acme",
"buyer@example.com",
"",
"alice",
&EstateSizes::default(),
&OrderedBy::Buyer,
)
.expect("an estate sold on the narrated site");
pay.authorize(&sold.handle.clone().unwrap().id);
drive(&state, "acme");
assert_eq!(
narrate_of(&cloud, "estate/acme"),
vec![String::new()],
"a customer's estate bought on a narrated site is laid closed"
);
let (state, pay, cloud, _dir) = narrate_rig("narrate-lay-nowhere", "");
lay_site(&state, &pay, "test3-gunnar-rs");
assert_eq!(
narrate_of(&cloud, "estate/test3gunnarrs"),
vec![String::new()],
"no narrate_from: nothing is opened, whatever the site says"
);
}
/// ★ **An update writes the firewall as the site says NOW**: the narrated site's estate is
/// written open to the ONE box, the serving site's closed — whatever the laid record kept.
#[test]
fn an_update_writes_the_narration_rule_as_the_site_says_now() {
let (state, pay, cloud, _dir) = narrate_rig("narrate-update", ONE_BOX);
lay_site(&state, &pay, "test3-gunnar-rs");
lay_site(&state, &pay, "gunnar-rs");
// A record laid before narrate existed says nothing; the update writes what the site says.
let cfg = state.estates.cfg.clone().unwrap();
for company in ["test3gunnarrs", "gunnarrs"] {
let _pen = state.estates.pen_for_a_call(&cfg, company).unwrap();
let mut run = state.estates.get(company).unwrap();
run.laid.as_mut().unwrap().narrate_from = String::new();
save(&state, &run).unwrap();
}
cloud.narrate_from.lock().unwrap().clear();
let _ = update::start(&state, "test3-gunnar-rs", "", "operator");
let _ = update::start(&state, "gunnar-rs", "", "operator");
assert_eq!(
*cloud.narrate_from.lock().unwrap(),
vec![
("estate/test3gunnarrs".to_string(), ONE_BOX.to_string()),
("estate/gunnarrs".to_string(), String::new())
],
"the update's firewall write, as each site says now"
);
}
/// ★ **The narrated site keeps its installer link file past the hand-off — and only it**; the
/// serving site's custody is forgotten whole. The narrated estates and their boxes are the
/// receiver's targets (`<public IPv4>:51822`), and the operator's tail reads each box's log.
#[test]
fn a_narrated_site_keeps_its_link_file_and_its_logs_are_read_by_the_box() {
let (state, pay, _cloud, _dir) = narrate_rig("narrate-handoff", ONE_BOX);
let custody = state
.estates
.custody
.as_ref()
.expect("the shop keeps a custody");
let link =
"installer_private = \"BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc=\"\nvm_public = \"x\"\n"
.to_string();
for label in ["test3-gunnar-rs", "gunnar-rs"] {
let run = site::start_site(
&state,
label,
"ops@example.invalid",
&EstateSizes::default(),
)
.unwrap();
custody.mint(&run.company).unwrap();
custody
.seal_pairing(
&run.company,
vec![(crate::custody::LINK_KEYS_FILE.into(), link.clone())],
)
.unwrap();
pay.authorize(&run.handle.clone().unwrap().id);
drive(&state, &run.company);
drive(&state, &run.company);
let run = state.estates.get(&run.company).unwrap();
assert!(
run.secrets_forgotten_at_ms > 0,
"{label}: the hand-off forgot the secrets: {:#?}",
run.history
);
assert!(
!custody.holds(&run.company),
"{label}: the estate's seal is gone"
);
}
assert_eq!(
custody.installer_link("test3gunnarrs").unwrap().as_deref(),
Some(link.as_str()),
"the narrated site keeps its link file"
);
assert_eq!(
custody.installer_link("gunnarrs").unwrap(),
None,
"the serving site keeps nothing"
);
assert_eq!(narrate::installer_secret_of(&link).unwrap(), [7u8; 32]);
let n = narrate::narrated(&state);
assert_eq!(
n.iter().map(|n| n.label.as_str()).collect::<Vec<_>>(),
vec!["test3-gunnar-rs"],
"only the narrated site is listened to"
);
assert_eq!(
n[0].vms.iter().map(|v| v.role.as_str()).collect::<Vec<_>>(),
monetize_cloud::lay::ROLES
);
assert!(
n[0].vms.iter().all(|v| v.addr.port() == narrate::PORT),
"{:?}",
n[0].vms
);
let path = narrate::log_path(&n[0].dir, "main");
narrate::RotatingLog::new(path, narrate::LOG_CAP)
.append(&[
"── narrate: gunnar-server - boot 7".into(),
"korp-start: STAGE entered".into(),
])
.unwrap();
let logs = narrate::tail(&state, "test3-gunnar-rs", "", 0).unwrap();
assert!(logs.narrated && logs.words.contains(ONE_BOX), "{logs:?}");
let main = logs
.vms
.iter()
.find(|v| v.vm == "main")
.expect("main's log");
assert!(
main.text.ends_with("korp-start: STAGE entered\n") && main.bytes > 0,
"{main:?}"
);
assert!(
logs.vms
.iter()
.any(|v| v.vm == "front" && v.text.is_empty()),
"a box not heard yet reads empty"
);
assert_eq!(
narrate::tail(&state, "test3gunnarrs", "main", 0)
.unwrap()
.vms
.len(),
1,
"one box, by its company too"
);
assert!(narrate::tail(&state, "test3-gunnar-rs", "nobody", 0)
.unwrap_err()
.contains("no machine"));
let g = narrate::tail(&state, "gunnar-rs", "", 0).unwrap();
assert!(!g.narrated && g.words.contains("not narrated"), "{g:?}");
assert!(narrate::tail(&state, "nowhere", "", 0).is_err());
}
/// ★ **`narrate` is said of an operator's own site only**: a customer's site that says it is
/// refused at load, by name.
#[test]
fn narrate_on_a_customers_site_is_refused_at_load() {
let cfg = |operator_site: bool| {
let mut c = crate::config::Config::default();
c.vendors.insert(
"sandbox/a".into(),
crate::config::VendorConfig {
enabled: true,
..Default::default()
},
);
c.estates.insert(
"one".into(),
crate::config::EstateConfig {
product: "gunnar".into(),
vendor: "sandbox/a".into(),
owner: "ignalina".into(),
operator_site,
narrate: true,
..Default::default()
},
);
c.check_estates()
};
cfg(true).expect("an operator's own site may say narrate");
let refused = cfg(false).expect_err("a customer's site may not");
assert!(
format!("{refused:#}").contains("narrate = true"),
"{refused:#}"
);
}
/// ★ **Every site that says narrate is accounted for** (2026-10-20: after a re-exec the box's
/// receiver said nothing at all): the window's targets are each such site, hearable or with
/// the reason it is not — not laid, no `narrate_from`, no installer link file.
#[test]
fn every_site_that_says_narrate_is_a_target_heard_or_with_its_reason() {
let (state, pay, _cloud, _dir) = narrate_rig("narrate-sites", ONE_BOX);
let s = narrate::sites(&state);
assert_eq!(
s.iter().map(|(l, _)| l.as_str()).collect::<Vec<_>>(),
vec!["test3-gunnar-rs"],
"only the site that says narrate"
);
assert!(
s[0].1.as_ref().unwrap_err().contains("no site order"),
"{s:?}"
);
lay_site(&state, &pay, "test3-gunnar-rs");
let s = narrate::sites(&state);
assert_eq!(
s[0].1.as_ref().expect("laid: heard").label,
"test3-gunnar-rs"
);
let k = narrate::keyed(&state);
assert!(
k[0].1.as_ref().unwrap_err().contains("installer link"),
"laid with no link file: {:?}",
k[0].1.as_ref().err()
);
let (state, pay, _cloud, _dir) = narrate_rig("narrate-sites-nowhere", "");
lay_site(&state, &pay, "test3-gunnar-rs");
let s = narrate::sites(&state);
assert!(
s[0].1.as_ref().unwrap_err().contains("narrate_from"),
"{s:?}"
);
let logs = narrate::tail(&state, "test3-gunnar-rs", "", 0).unwrap();
assert!(
!logs.narrated && logs.words.contains("narrate_from"),
"{logs:?}"
);
}
/// ★ **A window is queued for a site that says narrate even when nothing of it is heard**, so
/// it says why once per window (before: nothing hearable, nothing queued, nothing said).
#[test]
fn a_window_is_queued_when_a_site_says_narrate_though_nothing_is_heard() {
let (state, _pay, _cloud, _dir) = narrate_rig("narrate-look", "");
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
.unwrap();
let st = state.clone();
rt.block_on(async move {
tokio::task::spawn_blocking(move || narrate::look(&st))
.await
.unwrap()
});
assert!(
state.estates.narrate_lane.get().is_some(),
"the window was queued"
);
drop(state);
rt.shutdown_background();
}
/// ★ **A box's install log, as this box read it over the installer's link** (2026-10-20: the
/// narrate store had trimmed a front's first boot lines — DHCP, the network up — and nobody
/// could read them): `<run>/pairing/logs/<vm>.log`, every line of the VM's `/install/log`
/// the pairing read, from line `since`; a box not laid is refused by name, a log never kept
/// is said so.
#[test]
fn a_boxs_install_log_is_read_as_the_pairing_kept_it() {
let (state, pay, _cloud, _dir) = narrate_rig("install-log", ONE_BOX);
let run = lay_site(&state, &pay, "test3-gunnar-rs");
let refused = setup_log::install_log(&state, "test3-gunnar-rs", "front", 0).unwrap_err();
assert!(refused.contains("no install log"), "{refused}");
let logs = state
.estates
.run_dir(&run.company)
.unwrap()
.join(setup_log::PAIRING_LOGS);
std::fs::create_dir_all(&logs).unwrap();
std::fs::write(
logs.join("front.log"),
"korp-start: dhcp eth0 lease 10.0.0.5\nkorp-start: network up\nfront: STAGE listen\n",
)
.unwrap();
let all = setup_log::install_log(&state, "test3-gunnar-rs", "front", 0).unwrap();
assert_eq!(
(all.estate.as_str(), all.vm.as_str(), all.lines, all.next),
("test3-gunnar-rs", "front", 3, 3)
);
assert!(
all.text.starts_with("korp-start: dhcp eth0")
&& all.text.ends_with("front: STAGE listen\n"),
"{all:?}"
);
let later = setup_log::install_log(&state, "test3gunnarrs", "front", 2).unwrap();
assert_eq!(
(later.text.as_str(), later.next),
("front: STAGE listen\n", 3),
"from line 2, by its company too"
);
assert_eq!(
setup_log::install_log(&state, "test3-gunnar-rs", "front", 9)
.unwrap()
.text,
"",
"past the end: nothing new"
);
assert!(
setup_log::install_log(&state, "test3-gunnar-rs", "nobody", 0)
.unwrap_err()
.contains("no machine")
);
assert!(setup_log::install_log(&state, "nowhere", "front", 0).is_err());
}