use openrtc::connection_manager::{ConnectionHealth, ConnectionManager, ConnectionState};
#[tokio::test]
async fn delayed_generation_close_and_observations_cannot_mutate_replacement() {
let manager = ConnectionManager::new();
let connection_id = "generation-fence";
manager
.upsert_pending(
connection_id.into(),
Some("node-generation-fence".into()),
Some("device-generation-fence".into()),
Some("endpoint-generation-fence".into()),
)
.await;
manager
.set_connected_with_transport(
connection_id,
Some("endpoint-generation-fence".into()),
Some(41),
Some("generation-n".into()),
)
.await
.expect("generation N should bind");
manager
.set_connected_with_transport(
connection_id,
Some("endpoint-generation-fence".into()),
Some(42),
Some("generation-n-plus-one".into()),
)
.await
.expect("generation N+1 should replace N");
assert!(manager
.set_closed_if_current(connection_id, 41, Some("delayed-generation-n-close".into()))
.await
.is_none());
assert!(manager
.set_failed_if_current(
connection_id,
41,
Some("delayed-generation-n-failure".into())
)
.await
.is_none());
assert!(manager
.mark_transport_replaced_if_current(
connection_id,
41,
Some("delayed-generation-n-loss".into()),
Some("replacement-in-progress".into()),
)
.await
.is_none());
assert!(manager
.report_transport_status_if_current(
connection_id,
41,
1,
0,
"webrtc".into(),
Some("iroh-relay".into()),
)
.await
.is_none());
assert!(manager
.set_settled_if_current(connection_id, 41, 1, 0, true)
.await
.is_none());
let current = manager
.get_by_connection_id(connection_id)
.await
.expect("replacement should remain present");
assert_eq!(current.state, ConnectionState::Connected);
assert_eq!(current.transport_stable_id, Some(42));
assert_eq!(current.transport_generation, 2);
assert_eq!(current.route_generation, 0);
assert_eq!(
current.transport_source.as_deref(),
Some("generation-n-plus-one")
);
assert_eq!(current.status_reason, None);
let snapshot = manager
.peer_snapshot(connection_id)
.await
.expect("replacement snapshot should remain present");
assert_eq!(snapshot.health, ConnectionHealth::Unknown);
}