use super::*;
use crate::wallet::HypercallWallet;
use hypercall_ws_protocol::QpServerMessage;
use tokio::net::TcpListener;
use tokio_tungstenite::accept_async;
const TEST_PRIVATE_KEY: &str = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
fn scope(label: &str) -> ScopeId {
ScopeId::from_label(label).expect("test scope label fits")
}
fn quote(instrument: &str) -> IndicativeQuote {
IndicativeQuote {
instrument: instrument.to_string(),
bid_price: "100".to_string(),
ask_price: "101".to_string(),
max_bid_size: "10".to_string(),
max_ask_size: "10".to_string(),
}
}
#[test]
fn set_conflates_per_scope_and_counts_superseded() {
let mut state = ScopedOutboundState::default();
state.set(scope("A"), vec![quote("A-1")]);
state.set(scope("A"), vec![quote("A-2")]);
state.set(scope("B"), vec![quote("B-1")]);
assert_eq!(state.take_superseded(), 1, "A-1 superseded while dirty");
let first = state.next_frame(true).unwrap();
let second = state.next_frame(true).unwrap();
assert!(state.next_frame(true).is_none());
match (first, second) {
(
OutboundFrame::Scoped {
scope: s1,
quotes: q1,
},
OutboundFrame::Scoped {
scope: s2,
quotes: q2,
},
) => {
assert_eq!(s1, scope("A"));
assert_eq!(q1[0].instrument, "A-2", "latest snapshot won");
assert_eq!(s2, scope("B"));
assert_eq!(q2[0].instrument, "B-1");
}
other => panic!("expected two scoped frames, got {other:?}"),
}
}
#[test]
fn outbound_frame_wire_shape_matches_client_outbound() {
let scoped = OutboundFrame::Scoped {
scope: scope("MSFT"),
quotes: Arc::new(vec![quote("MSFT-C-450")]),
};
let owned = ClientOutbound::ScopedIndicativeQuoteUpdate {
scope: scope("MSFT"),
quotes: vec![quote("MSFT-C-450")],
};
assert_eq!(
scoped.to_json().unwrap(),
serde_json::to_string(&owned).unwrap()
);
let union = OutboundFrame::Union(vec![quote("A-1"), quote("B-1")]);
let owned = ClientOutbound::IndicativeQuoteUpdate {
quotes: vec![quote("A-1"), quote("B-1")],
};
assert_eq!(
union.to_json().unwrap(),
serde_json::to_string(&owned).unwrap()
);
}
#[test]
fn empty_snapshot_replays_until_confirmed_then_slot_is_dropped() {
let mut state = ScopedOutboundState::default();
state.set(scope("A"), vec![quote("A-1")]);
let _ = state.next_frame(true).unwrap();
state.set(scope("A"), Vec::new());
match state.next_frame(true).unwrap() {
OutboundFrame::Scoped { quotes, .. } => {
assert!(quotes.is_empty(), "explicit clear goes to the wire");
}
other => panic!("expected scoped frame, got {other:?}"),
}
state.mark_all_dirty();
match state.next_frame(true).unwrap() {
OutboundFrame::Scoped { quotes, .. } => {
assert!(quotes.is_empty(), "unconfirmed clear is replayed");
}
other => panic!("expected scoped frame, got {other:?}"),
}
state.finish_scoped_clear(scope("A"));
state.mark_all_dirty();
assert!(state.next_frame(true).is_none());
}
#[test]
fn confirmed_clear_keeps_slot_when_scope_was_reset_meanwhile() {
let mut state = ScopedOutboundState::default();
state.set(scope("A"), Vec::new());
let _ = state.next_frame(true).unwrap();
state.set(scope("A"), vec![quote("A-2")]);
state.finish_scoped_clear(scope("A"));
match state.next_frame(true).unwrap() {
OutboundFrame::Scoped { quotes, .. } => {
assert_eq!(quotes[0].instrument, "A-2");
}
other => panic!("expected scoped frame, got {other:?}"),
}
}
#[test]
fn mark_all_dirty_replays_every_live_slot() {
let mut state = ScopedOutboundState::default();
state.set(scope("A"), vec![quote("A-1")]);
state.set(scope("B"), vec![quote("B-1")]);
while state.next_frame(true).is_some() {}
state.mark_all_dirty();
let mut replayed = Vec::new();
while let Some(OutboundFrame::Scoped { scope, .. }) = state.next_frame(true) {
replayed.push(scope);
}
replayed.sort_by_key(|s| s.to_hex());
assert_eq!(replayed, vec![scope("A"), scope("B")]);
}
#[test]
fn union_clear_replays_until_confirmed() {
let mut state = ScopedOutboundState::default();
state.set(scope("A"), vec![quote("A-1")]);
let _ = state.next_frame(false).unwrap();
state.finish_union_flush();
state.set(scope("A"), Vec::new());
match state.next_frame(false).unwrap() {
OutboundFrame::Union(quotes) => assert!(quotes.is_empty()),
other => panic!("expected union, got {other:?}"),
}
state.mark_all_dirty();
match state.next_frame(false).unwrap() {
OutboundFrame::Union(quotes) => assert!(quotes.is_empty(), "clear replayed"),
other => panic!("expected union, got {other:?}"),
}
state.finish_union_flush();
state.mark_all_dirty();
assert!(state.next_frame(false).is_none());
}
#[test]
fn fallback_flushes_union_as_one_full_snapshot() {
let mut state = ScopedOutboundState::default();
state.set(scope("A"), vec![quote("A-1")]);
state.set(scope("B"), vec![quote("B-1")]);
match state.next_frame(false).unwrap() {
OutboundFrame::Union(quotes) => {
let mut instruments: Vec<_> = quotes.iter().map(|q| q.instrument.as_str()).collect();
instruments.sort_unstable();
assert_eq!(instruments, vec!["A-1", "B-1"]);
}
other => panic!("expected full snapshot, got {other:?}"),
}
assert!(
state.next_frame(false).is_none(),
"one union frame discharges all dirtiness"
);
}
struct NoopTestCallbacks;
impl QpClientCallbacks for NoopTestCallbacks {}
struct ReentrantCallbacks {
handle: std::sync::Mutex<Option<QpScopedHandle>>,
}
impl QpClientCallbacks for ReentrantCallbacks {
fn on_stale_messages_drained(&self, _count: u64) {
if let Some(handle) = self.handle.lock().unwrap().as_ref() {
handle.set_indicative_quotes(scope("REENTER"), Vec::new());
}
}
}
async fn mock_qp_session(
listener: &TcpListener,
capabilities: Vec<String>,
frames_tx: mpsc::Sender<ClientOutbound>,
close_after: usize,
) {
let (stream, _) = listener.accept().await.unwrap();
let mut ws = accept_async(stream).await.unwrap();
let auth = ws.next().await.unwrap().unwrap();
assert!(matches!(auth, Message::Text(_)));
let authenticated = QpServerMessage::Authenticated {
wallet: "0x0000000000000000000000000000000000000000".to_string(),
capabilities,
};
ws.send(Message::Text(
serde_json::to_string(&authenticated).unwrap(),
))
.await
.unwrap();
let mut received = 0usize;
while received < close_after {
match ws.next().await {
Some(Ok(Message::Text(text))) => {
let msg: ClientOutbound = serde_json::from_str(&text).unwrap();
frames_tx.send(msg).await.unwrap();
received += 1;
}
Some(Ok(Message::Ping(data))) => {
let _ = ws.send(Message::Pong(data)).await;
}
Some(Ok(_)) => {}
_ => return,
}
}
}
#[tokio::test]
async fn scoped_frames_flow_and_replay_after_reconnect() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (frames_tx, mut frames_rx) = mpsc::channel(16);
let server = tokio::spawn(async move {
mock_qp_session(
&listener,
vec![CAP_SCOPED_INDICATIVE.to_string()],
frames_tx.clone(),
2,
)
.await;
mock_qp_session(
&listener,
vec![CAP_SCOPED_INDICATIVE.to_string()],
frames_tx.clone(),
2,
)
.await;
});
let wallet = Arc::new(HypercallWallet::from_private_key(TEST_PRIVATE_KEY, 999).unwrap());
let (handle, runtime) = qp_scoped_channel();
handle.set_indicative_quotes(scope("MSFT"), vec![quote("MSFT-C-450")]);
handle.set_indicative_quotes(scope("SPX"), vec![quote("SP500-C-5000")]);
let mut config = QpClientConfig::new(format!("http://{addr}"));
config.reconnect_delay = Duration::from_millis(50);
let (inbound_tx, _inbound_rx) = mpsc::channel(16);
let client = tokio::spawn(run_qp_scoped_client(
config,
wallet,
runtime,
inbound_tx,
Arc::new(NoopTestCallbacks),
));
let mut session_frames = Vec::new();
for _ in 0..4 {
let frame = tokio::time::timeout(Duration::from_secs(5), frames_rx.recv())
.await
.expect("frame within timeout")
.expect("channel open");
session_frames.push(frame);
}
for chunk in session_frames.chunks(2) {
let mut scopes: Vec<ScopeId> = chunk
.iter()
.map(|frame| match frame {
ClientOutbound::ScopedIndicativeQuoteUpdate { scope, .. } => *scope,
other => panic!("expected scoped frame, got {other:?}"),
})
.collect();
scopes.sort_by_key(|s| s.to_hex());
assert_eq!(scopes, vec![scope("MSFT"), scope("SPX")]);
}
client.abort();
server.abort();
}
#[tokio::test]
async fn legacy_server_gets_union_full_snapshot_fallback() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (frames_tx, mut frames_rx) = mpsc::channel(16);
let server = tokio::spawn(async move {
mock_qp_session(&listener, Vec::new(), frames_tx, 1).await;
});
let wallet = Arc::new(HypercallWallet::from_private_key(TEST_PRIVATE_KEY, 999).unwrap());
let (handle, runtime) = qp_scoped_channel();
handle.set_indicative_quotes(scope("MSFT"), vec![quote("MSFT-C-450")]);
handle.set_indicative_quotes(scope("SPX"), vec![quote("SP500-C-5000")]);
let config = QpClientConfig::new(format!("http://{addr}"));
let (inbound_tx, _inbound_rx) = mpsc::channel(16);
let client = tokio::spawn(run_qp_scoped_client(
config,
wallet,
runtime,
inbound_tx,
Arc::new(NoopTestCallbacks),
));
let frame = tokio::time::timeout(Duration::from_secs(5), frames_rx.recv())
.await
.expect("frame within timeout")
.expect("channel open");
match frame {
ClientOutbound::IndicativeQuoteUpdate { quotes } => {
let mut instruments: Vec<_> = quotes.iter().map(|q| q.instrument.as_str()).collect();
instruments.sort_unstable();
assert_eq!(instruments, vec!["MSFT-C-450", "SP500-C-5000"]);
}
other => panic!("legacy server must get a full snapshot, got {other:?}"),
}
client.abort();
server.abort();
}
#[tokio::test]
async fn connection_state_reports_negotiated_capabilities() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (frames_tx, mut frames_rx) = mpsc::channel(16);
let server = tokio::spawn(async move {
mock_qp_session(
&listener,
vec![CAP_SCOPED_INDICATIVE.to_string()],
frames_tx,
1,
)
.await;
});
let wallet = Arc::new(HypercallWallet::from_private_key(TEST_PRIVATE_KEY, 999).unwrap());
let (handle, runtime) = qp_scoped_channel();
let mut state_rx = handle.connection_state();
assert_eq!(*state_rx.borrow(), ConnectionState::Disconnected);
let config = QpClientConfig::new(format!("http://{addr}"));
let (inbound_tx, _inbound_rx) = mpsc::channel(16);
let client = tokio::spawn(run_qp_scoped_client(
config,
wallet,
runtime,
inbound_tx,
Arc::new(NoopTestCallbacks),
));
tokio::time::timeout(Duration::from_secs(5), state_rx.changed())
.await
.expect("state change within timeout")
.unwrap();
assert!(state_rx.borrow().scoped_indicative());
handle.set_indicative_quotes(scope("MSFT"), vec![quote("MSFT-C-450")]);
let frame = tokio::time::timeout(Duration::from_secs(5), frames_rx.recv())
.await
.expect("frame within timeout")
.expect("channel open");
assert!(matches!(
frame,
ClientOutbound::ScopedIndicativeQuoteUpdate { .. }
));
client.abort();
server.abort();
}
#[tokio::test]
async fn connection_state_is_stored_even_with_no_subscribers() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (frames_tx, mut frames_rx) = mpsc::channel(16);
let server = tokio::spawn(async move {
mock_qp_session(
&listener,
vec![CAP_SCOPED_INDICATIVE.to_string()],
frames_tx,
8,
)
.await;
});
let wallet = Arc::new(HypercallWallet::from_private_key(TEST_PRIVATE_KEY, 999).unwrap());
let (handle, runtime) = qp_scoped_channel();
handle.set_indicative_quotes(scope("MSFT"), vec![quote("MSFT-C-450")]);
let config = QpClientConfig::new(format!("http://{addr}"));
let (inbound_tx, _inbound_rx) = mpsc::channel(16);
let client = tokio::spawn(run_qp_scoped_client(
config,
wallet,
runtime,
inbound_tx,
Arc::new(NoopTestCallbacks),
));
let _frame = tokio::time::timeout(Duration::from_secs(5), frames_rx.recv())
.await
.expect("frame within timeout")
.expect("channel open");
let state_rx = handle.connection_state();
assert!(
state_rx.borrow().scoped_indicative(),
"late subscriber must read the stored Connected state"
);
client.abort();
server.abort();
}
#[tokio::test]
async fn watch_reads_disconnected_after_terminal_inbound_close() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let server = tokio::spawn(async move {
let (stream, _) = listener.accept().await.unwrap();
let mut ws = accept_async(stream).await.unwrap();
let _auth = ws.next().await.unwrap().unwrap();
let authenticated = QpServerMessage::Authenticated {
wallet: "0x0000000000000000000000000000000000000000".to_string(),
capabilities: vec![CAP_SCOPED_INDICATIVE.to_string()],
};
ws.send(Message::Text(
serde_json::to_string(&authenticated).unwrap(),
))
.await
.unwrap();
let rfq = r#"{"type":"rfq_request","rfq_id":"abc","legs":[],"taker_wallet":"0x123","request_timestamp":1,"response_deadline_ms":5000,"auto_execute":false}"#;
ws.send(Message::Text(rfq.to_string())).await.unwrap();
let _ = tokio::time::timeout(Duration::from_secs(5), ws.next()).await;
});
let wallet = Arc::new(HypercallWallet::from_private_key(TEST_PRIVATE_KEY, 999).unwrap());
let (handle, runtime) = qp_scoped_channel();
let state_rx = handle.connection_state();
let config = QpClientConfig::new(format!("http://{addr}"));
let (inbound_tx, inbound_rx) = mpsc::channel(1);
drop(inbound_rx);
let client = tokio::spawn(run_qp_scoped_client(
config,
wallet,
runtime,
inbound_tx,
Arc::new(NoopTestCallbacks),
));
tokio::time::timeout(Duration::from_secs(5), client)
.await
.expect("runtime exits on closed inbound channel")
.unwrap();
assert_eq!(
*state_rx.borrow(),
ConnectionState::Disconnected,
"watch must not report Connected after the runtime died"
);
server.abort();
}
#[tokio::test]
async fn stale_drain_callback_may_reenter_handle() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (frames_tx, mut frames_rx) = mpsc::channel(16);
let server = tokio::spawn(async move {
mock_qp_session(
&listener,
vec![CAP_SCOPED_INDICATIVE.to_string()],
frames_tx,
2,
)
.await;
});
let wallet = Arc::new(HypercallWallet::from_private_key(TEST_PRIVATE_KEY, 999).unwrap());
let (handle, runtime) = qp_scoped_channel();
let callbacks = Arc::new(ReentrantCallbacks {
handle: std::sync::Mutex::new(None),
});
*callbacks.handle.lock().unwrap() = Some(handle.clone());
handle.set_indicative_quotes(scope("MSFT"), vec![quote("OLD")]);
handle.set_indicative_quotes(scope("MSFT"), vec![quote("MSFT-C-450")]);
let config = QpClientConfig::new(format!("http://{addr}"));
let (inbound_tx, _inbound_rx) = mpsc::channel(16);
let client = tokio::spawn(run_qp_scoped_client(
config, wallet, runtime, inbound_tx, callbacks,
));
let frame = tokio::time::timeout(Duration::from_secs(5), frames_rx.recv())
.await
.expect("no deadlock: frame within timeout")
.expect("channel open");
assert!(matches!(
frame,
ClientOutbound::ScopedIndicativeQuoteUpdate { .. }
));
client.abort();
server.abort();
}
#[tokio::test]
async fn keepalive_republishes_quiet_scopes() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (frames_tx, mut frames_rx) = mpsc::channel(16);
let server = tokio::spawn(async move {
mock_qp_session(
&listener,
vec![CAP_SCOPED_INDICATIVE.to_string()],
frames_tx,
3,
)
.await;
});
let wallet = Arc::new(HypercallWallet::from_private_key(TEST_PRIVATE_KEY, 999).unwrap());
let (handle, runtime) = qp_scoped_channel();
handle.set_indicative_quotes(scope("MSFT"), vec![quote("MSFT-C-450")]);
let mut config = QpClientConfig::new(format!("http://{addr}"));
config.indicative_republish_interval = Duration::from_millis(100);
let (inbound_tx, _inbound_rx) = mpsc::channel(16);
let client = tokio::spawn(run_qp_scoped_client(
config,
wallet,
runtime,
inbound_tx,
Arc::new(NoopTestCallbacks),
));
for _ in 0..3 {
let frame = tokio::time::timeout(Duration::from_secs(5), frames_rx.recv())
.await
.expect("frame within timeout")
.expect("channel open");
match frame {
ClientOutbound::ScopedIndicativeQuoteUpdate { scope: s, quotes } => {
assert_eq!(s, scope("MSFT"));
assert_eq!(quotes[0].instrument, "MSFT-C-450");
}
other => panic!("expected scoped frame, got {other:?}"),
}
}
client.abort();
server.abort();
}
struct RecordingCallbacks {
dropped: Mutex<Vec<(ClientOutbound, RfqDropReason)>>,
stale: std::sync::atomic::AtomicU64,
}
impl QpClientCallbacks for RecordingCallbacks {
fn on_rfq_response_dropped(&self, message: &ClientOutbound, reason: RfqDropReason) {
self.dropped.lock().unwrap().push((message.clone(), reason));
}
fn on_stale_messages_drained(&self, count: u64) {
self.stale
.fetch_add(count, std::sync::atomic::Ordering::Relaxed);
}
}
fn rfq_decline(rfq_id: &str) -> ClientOutbound {
ClientOutbound::RfqResponse {
rfq_id: rfq_id.to_string(),
action: "decline".to_string(),
legs: None,
net_premium: None,
valid_for_ms: None,
signature: None,
nonce: None,
}
}
#[tokio::test]
async fn expired_rfq_response_is_reported_with_its_message() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (frames_tx, mut frames_rx) = mpsc::channel(16);
let server = tokio::spawn(async move {
mock_qp_session(
&listener,
vec![CAP_SCOPED_INDICATIVE.to_string()],
frames_tx,
1,
)
.await;
});
let wallet = Arc::new(HypercallWallet::from_private_key(TEST_PRIVATE_KEY, 999).unwrap());
let (handle, runtime) = qp_scoped_channel();
let already_expired = Instant::now()
.checked_sub(Duration::from_secs(1))
.expect("monotonic clock is at least 1s old");
handle
.send_rfq_response(rfq_decline("expired-1"), already_expired)
.unwrap();
handle
.send_rfq_response(
rfq_decline("live-1"),
Instant::now() + Duration::from_secs(30),
)
.unwrap();
let callbacks = Arc::new(RecordingCallbacks {
dropped: Mutex::new(Vec::new()),
stale: std::sync::atomic::AtomicU64::new(0),
});
let config = QpClientConfig::new(format!("http://{addr}"));
let (inbound_tx, _inbound_rx) = mpsc::channel(16);
let client = tokio::spawn(run_qp_scoped_client(
config,
wallet,
runtime,
inbound_tx,
Arc::clone(&callbacks) as Arc<dyn QpClientCallbacks>,
));
let sent = tokio::time::timeout(Duration::from_secs(5), frames_rx.recv())
.await
.expect("frame within timeout")
.expect("session forwarded a frame");
assert!(
matches!(&sent, ClientOutbound::RfqResponse { rfq_id, .. } if rfq_id == "live-1"),
"expected the live response on the wire, got {sent:?}"
);
let dropped = callbacks.dropped.lock().unwrap().clone();
assert_eq!(
dropped.len(),
1,
"exactly one drop expected, got {dropped:?}"
);
let (message, reason) = &dropped[0];
assert_eq!(*reason, RfqDropReason::Expired);
assert!(
matches!(message, ClientOutbound::RfqResponse { rfq_id, .. } if rfq_id == "expired-1"),
"dropped message must be the expired response, got {message:?}"
);
assert!(
callbacks.stale.load(std::sync::atomic::Ordering::Relaxed) >= 1,
"on_stale_messages_drained must still fire for an RFQ drop"
);
client.abort();
server.abort();
}