Files
khadhroony-solana-project/crates/ksp-onchain-transport-lib/unit_tests/ws_lifecycle.rs
2026-08-23 15:41:54 +02:00

136 lines
5.8 KiB
Rust

// file: crates/ksp-onchain-transport-lib/unit_tests/ws_lifecycle.rs
// version: 5
fn non_zero(value: u64) -> std::num::NonZeroU64 {
return std::num::NonZeroU64::new(value).expect("test ID must be non-zero");
}
#[test]
fn websocket_local_ids_preserve_ordering_and_numeric_identity() {
let first_session = crate::WsSessionId::new(non_zero(1));
let second_session = crate::WsSessionId::new(non_zero(2));
let subscription = crate::WsSubscriptionId::new(non_zero(7));
assert!(first_session < second_session);
assert_eq!(subscription.get(), 7);
}
#[test]
fn websocket_state_models_expose_concurrent_lifecycle_states() {
assert_eq!(crate::WsSessionState::Reconnecting { attempt: 3 }, crate::WsSessionState::Reconnecting { attempt: 3 });
assert_eq!(crate::WsSubscriptionState::Resubscribing, crate::WsSubscriptionState::Resubscribing);
assert_ne!(crate::WsSubscriptionState::Cancelling, crate::WsSubscriptionState::Closed);
}
#[test]
fn websocket_subscription_kinds_cover_all_nine_standard_families() {
let kinds = [
crate::WsSubscriptionKind::Account,
crate::WsSubscriptionKind::Block,
crate::WsSubscriptionKind::Logs,
crate::WsSubscriptionKind::Program,
crate::WsSubscriptionKind::Root,
crate::WsSubscriptionKind::Signature,
crate::WsSubscriptionKind::Slot,
crate::WsSubscriptionKind::SlotsUpdates,
crate::WsSubscriptionKind::Vote,
];
assert_eq!(kinds.len(), 9);
assert_eq!(kinds[7].as_str(), "slots_updates");
}
#[test]
fn websocket_snapshots_expose_safe_metadata_without_remote_ids_or_urls() {
let subscription = crate::WsSubscriptionSnapshot::new(
crate::WsSubscriptionId::new(non_zero(9)),
crate::WsSubscriptionKind::Slot,
crate::WsSubscriptionState::Active,
true,
std::option::Option::None,
);
let snapshot = crate::WsSessionSnapshot::new(
crate::WsSessionId::new(non_zero(3)),
"devnet_public",
crate::WsProviderName::new("solana-public"),
crate::WsClusterName::new("devnet"),
crate::WsProtocolKind::SolanaStandard,
crate::WsSessionState::Active,
2,
1,
0,
std::vec![subscription],
);
assert_eq!(snapshot.id().get(), 3);
assert_eq!(snapshot.endpoint_name(), "devnet_public");
assert_eq!(snapshot.pending_request_count(), 2);
assert_eq!(snapshot.continuity_gap_count(), 1);
assert_eq!(snapshot.subscription_count(), 1);
assert!(snapshot.subscriptions()[0].remote_bound());
assert_eq!(snapshot.subscriptions()[0].terminal_error_code(), std::option::Option::None);
assert_eq!(snapshot.overflow_count(), 0);
let rendered = format!("{snapshot:?}");
assert!(!rendered.contains("wss://"));
assert!(!rendered.contains("remote_subscription_id"));
}
#[test]
fn websocket_subscription_snapshot_preserves_only_safe_terminal_error_code() {
let snapshot = crate::WsSubscriptionSnapshot::new(
crate::WsSubscriptionId::new(non_zero(10)),
crate::WsSubscriptionKind::Logs,
crate::WsSubscriptionState::Failed,
false,
std::option::Option::Some(crate::ERROR_CODE_WS_BACKPRESSURE_OVERFLOW),
);
assert_eq!(snapshot.state(), crate::WsSubscriptionState::Failed);
assert_eq!(snapshot.terminal_error_code(), std::option::Option::Some(crate::ERROR_CODE_WS_BACKPRESSURE_OVERFLOW));
assert!(!format!("{snapshot:?}").contains("remote_subscription_id"));
}
#[test]
fn websocket_subscription_kinds_map_exact_standard_method_triplets() {
let cases = [
(crate::WsSubscriptionKind::Account, "accountSubscribe", "accountUnsubscribe", "accountNotification"),
(crate::WsSubscriptionKind::Block, "blockSubscribe", "blockUnsubscribe", "blockNotification"),
(crate::WsSubscriptionKind::Logs, "logsSubscribe", "logsUnsubscribe", "logsNotification"),
(crate::WsSubscriptionKind::Program, "programSubscribe", "programUnsubscribe", "programNotification"),
(crate::WsSubscriptionKind::Root, "rootSubscribe", "rootUnsubscribe", "rootNotification"),
(crate::WsSubscriptionKind::Signature, "signatureSubscribe", "signatureUnsubscribe", "signatureNotification"),
(crate::WsSubscriptionKind::Slot, "slotSubscribe", "slotUnsubscribe", "slotNotification"),
(crate::WsSubscriptionKind::SlotsUpdates, "slotsUpdatesSubscribe", "slotsUpdatesUnsubscribe", "slotsUpdatesNotification"),
(crate::WsSubscriptionKind::Vote, "voteSubscribe", "voteUnsubscribe", "voteNotification"),
];
for (kind, subscribe, unsubscribe, notification) in cases {
assert_eq!(kind.subscribe_method(), subscribe);
assert_eq!(kind.unsubscribe_method(), unsubscribe);
assert_eq!(kind.notification_method(), notification);
}
}
#[test]
fn websocket_unstable_subscription_partition_is_exact() {
let cases = [
(crate::WsSubscriptionKind::Account, false),
(crate::WsSubscriptionKind::Block, true),
(crate::WsSubscriptionKind::Logs, false),
(crate::WsSubscriptionKind::Program, false),
(crate::WsSubscriptionKind::Root, false),
(crate::WsSubscriptionKind::Signature, false),
(crate::WsSubscriptionKind::Slot, false),
(crate::WsSubscriptionKind::SlotsUpdates, true),
(crate::WsSubscriptionKind::Vote, true),
];
for (kind, unstable) in cases {
assert_eq!(kind.is_unstable(), unstable);
}
}
#[test]
fn helius_transaction_subscription_kind_maps_exact_provider_method_triplet_without_expanding_standard_partition() {
let kind = crate::WsSubscriptionKind::HeliusTransaction;
assert_eq!(kind.as_str(), "helius_transaction");
assert_eq!(kind.subscribe_method(), "transactionSubscribe");
assert_eq!(kind.unsubscribe_method(), "transactionUnsubscribe");
assert_eq!(kind.notification_method(), "transactionNotification");
assert!(!kind.is_unstable());
}