use std::time::SystemTime;
use crate::{GuestDeclaration, HostStamp, StampedDeclaration};
pub fn stamp(guest: GuestDeclaration, stamp: HostStamp) -> StampedDeclaration {
StampedDeclaration {
cell_id: stamp.cell_id,
run_id: stamp.run_id,
host_received_at: stamp.host_received_at,
spec_signature_hash: stamp.spec_signature_hash,
probe_source: guest.probe_source,
guest_pid: guest.guest_pid,
guest_comm: guest.guest_comm,
guest_monotonic_ns: guest.guest_monotonic_ns,
}
}
pub fn stamp_now(
guest: GuestDeclaration,
cell_id: impl Into<String>,
run_id: impl Into<String>,
spec_signature_hash: impl Into<String>,
) -> StampedDeclaration {
stamp(
guest,
HostStamp {
cell_id: cell_id.into(),
run_id: run_id.into(),
host_received_at: SystemTime::now(),
spec_signature_hash: spec_signature_hash.into(),
},
)
}
#[cfg(test)]
mod tests {
use super::*;
fn fixture_guest() -> GuestDeclaration {
GuestDeclaration {
probe_source: "process.spawned".to_string(),
guest_pid: 1234,
guest_comm: "workload".to_string(),
guest_monotonic_ns: 42_000_000,
}
}
#[test]
fn host_stamp_overrides_attribution() {
let stamped = stamp_now(fixture_guest(), "cell-A", "run-1", "sha256:deadbeef");
assert_eq!(stamped.cell_id, "cell-A");
assert_eq!(stamped.run_id, "run-1");
assert_eq!(stamped.spec_signature_hash, "sha256:deadbeef");
assert_eq!(stamped.probe_source, "process.spawned");
assert_eq!(stamped.guest_pid, 1234);
assert_eq!(stamped.guest_comm, "workload");
assert_eq!(stamped.guest_monotonic_ns, 42_000_000);
}
#[test]
fn explicit_stamp_preserves_received_at() {
let t = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000);
let stamped = stamp(
fixture_guest(),
HostStamp {
cell_id: "c".into(),
run_id: "r".into(),
host_received_at: t,
spec_signature_hash: "sha256:00".into(),
},
);
assert_eq!(stamped.host_received_at, t);
}
}